0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

pre-pushのwhile文が動かない

Posted at

はじめに

前回の記事に続き、git hooksにおいて、mainブランチへの直push禁止設定を行う際に
上手く動作し無かったので記録します。

前回記事はこちら

実行環境

Visual Studio 2022
git 2.43.0.windows.1

問題

以下スクリプトを実装したが、ループ内の処理が行われなかった。

#!/bin/bash

while read local_ ref local_sha1 remote_ref remote_ sha1
do
...
done

エラーが発生していたわけではなかったが、
do...done内にechoを記述しても何も表示されないため、
そもそも要素が何も取得できていない疑いが出てきた

解決方法

while文で受け取っている標準入力について
以下の説明をしている記事があった

これはそれぞれローカルのブランチ名/ローカルのコミットID
/リモートのブランチ名/リモートのコミットIDを表していて、
gitが pre-push フックに対して標準入力経由で
これらの値を渡すようになっている。

この標準入力はgit pushコマンドにおけるpush設定から取得される。

ではなぜ取得できていない??

Apologies if this is stating the obvious, but if there's mothine to push you won t get any lines on stdin.

当たり前のことを言うようで申し訳ないのですが、プッシュするものが何もなければ、標準入力に何も表示されません。

そもそもpushするものがないままpushし続けていました

ダミーファイルをcommitした後、pushを行い、正常に動作していることを確認

git add test.txt
git commit -m ”pre-push 動作確認用”
git push origin main

おわりに

当たり前のことを言うようで申し訳ないような問題ですが、
こんなことが割とよくあるので、悩めば悩むほど、今後同じことで悩むことは無い、
という事にしておきます。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?