プログラミング

Git gulp expect cron Atomでリモートサーバー同士でファイル同期をする

リモートマシン上のテキストやファイルを、ホストPCにコピペするのが面倒だったので、
Git gulp expect cron Atomを使ってファイル同期できるようなシステムを考えてみました。

ファイルが更新されるたびに自動でgit pushする

/mnt/c/pg/expect/git_push

#!/usr/bin/expect

# C:\pg\expect\rsync_ssh

# log_file /var/log/expect.log

set USER "yuis-ice"
set PW "YOUROWN"
set Prompt "\[#$%>\]"

set timeout 5

puts [exec git add -A ;]
puts [exec git commit -m '.' ;]

spawn git push -u origin master

expect {
    -glob "Username" {
        send -- "${USER}\n"
        exp_continue
    }
    -glob "Password" {
        send -- "${PW}\n"
    }
}

expect {
    -glob "${Prompt}" {
        interact
        exit 0
    }
}

共有したいディレクトリで、まずはgit remote addまでを済ましましょう。

次に、gulpの設定です。

npm install -D gulp
npm init -y
npm install child_process

gulpfile.js

var gulp = require("gulp");
var ps = require('child_process').exec;

gulp.task('exec_file', function() {
  var command = "/mnt/c/pg/expect/git_push";
  ps(command , function (err, stdout, stderr) {
    console.log(stdout);
  });
});

gulp.task("watch", function() {
  var targets = [
    './**'
  ];
  gulp.watch(targets, ['exec_file']);
});

cronでgit pullを定期的に実行

最初にcloneしておきましょう。

clone github.com/repo.git

こういう感じのスクリプトをcronに登録しておきます。

cd repo.git
git pull

これでファイルの同期ができるようになりました。

Atomだとファイルの更新があればすぐにファイルを最新のものにしてくれますので、pullすると勝手にファイルをpullしたものに置き換えてくれますので便利です。

pullしたファイルを編集できるようにする

pullしたファイルを編集したりすると、こういうエラーがでるようになります。
解決策として…

error: Your local changes to the following files would be overwritten by merge:
        README.md
Please, commit your changes or stash them before you can merge.

読み取り専用、として利用するのが良いと思いますが、もし書き換えてそれをcommitしたいとかがあれば、書き換えてしまいましょう。
そうすると先程のエラーがでて、同期がストップします。

そして、commitをすればいいわけです。

もし書き換えてしまったのが間違いだった場合、次のコマンドでローカルのリポジトリの変更を破棄することができます。

git reset --hard

[get_title https://stackoverflow.com/questions/15745045/how-do-i-resolve-git-saying-commit-your-changes-or-stash-them-before-you-can-me]

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です