web-dev-qa-db-ja.com

Gitの追加、コミット、bashスクリプトからのプッシュ

時々コミットすることで、org-modeファイルをGitHubリポジトリと同期させておく簡単なcronジョブを作成しようとしています。何らかの理由で、常にファイルをコミットすることができますが、コマンドラインからスクリプトを実行すると、git Pushの出力が表示されますが、

Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 12 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 329 bytes | 329.00 KiB/s, done.
Total 3 (delta 2), reused 0 (delta 0)
remote: Resolving deltas: 100% (2/2), completed with 2 local objects.
To github.com:my/repo.git
   df98efb..0d4943b  master -> master

git statusはまだプッシュする必要があると私に言っています:

On branch master
Your branch is ahead of 'Origin/master' by 1 commit.
  (use "git Push" to publish your local commits)

nothing to commit, working tree clean

これが私のスクリプトです:

#!/bin/sh

message="auto-commit from $USER@$(hostname -s) on $(date)"
GIT=`which git`
REPO_DIR=~/org
cd ${REPO_DIR}
${GIT} add --all .
${GIT} commit -m "$message"
${GIT} Push [email protected]:my/repo.git master

そして、これが私のcrontabでどのように見えるかです:

* * * * * echo ["$(date)"] "$(echo "$(~/cron/scripts/org-commit.sh | tail -1)" | xargs)" >> ~/cron/log/org-commit.log 2>&1

~/cron/log/org-commit.logでは、次のようになります。

[Wed Jun 12 16:44:00 CDT 2019] nothing to commit, working tree clean
[Wed Jun 12 16:45:00 CDT 2019] 1 file changed, 1 insertion(+)

最初の行は変更なしで実行されたときで、2番目の行は1つの変更がありました。そのログ行はgit Push ..からのものであるため、git commitは実行されていないようです。


これを引き起こしている原因を見つけるにはどうすればよいですか?

1
Bruno Ely

あなたの問題はgit Pushは実際には非同期です。結果を変数に割り当ててから、結果をエコーすることで機能するはずです。次のようなものは、期待する応答を出力するはずです。

#!/bin/sh

message="auto-commit from $USER@$(hostname -s) on $(date)"
GIT=`which git`
REPO_DIR=~/org
cd ${REPO_DIR}
${GIT} add --all .
${GIT} commit -m "$message"

gitPush=$(${GIT} Push -vvv [email protected]:my/repo.git master 2>&1)
echo "$gitPush"


1
Doug