私は Windows で git bash を使用しています。概念的には次のような git エイリアス コマンドを作成したいと考えています。
[alias]
assume_unchanged_below = "!git ls-files -z $dir | xargs -0 git update-index --assume-unchanged"
が実行されたbash$dir
シェルの現在の作業ディレクトリに展開さgit assume_unchanged_below
れます。
つまり、私が入っC:\somedir\somesubdir
て実行するgit assume_unchanged_below
場合、入力したかのようにしたいでしょうgit ls-files -z C:\somedir\somesubdir | xargs -0 git update-index --assume-unchanged
経由で現在のディレクトリを取得できるようgit rev-parse --show-prefix
です。また、無名関数を使用して引数を渡すことができることも知っていますが、これら 2 つの概念を組み合わせて目的の動作を実現することはできませんでした。
たとえば、これを行うと:
[alias]
assume_unchanged_below = "!f() { \
git ls-files -z $1 | xargs -0 git update-index --assume-unchanged; \
}; f"
次に、入力できます
git assume_unchanged_below `git rev-parse --show-prefix`
And it behaves as expected ($1
is correctly expanded to the current working directory).
However, I don't want to have to manually pass git rev-parse --show-prefix
to the alias, I want it inferred automatically so that I only need to type
git assume_unchanged_below
have it operate on the current working directory.
I've tried this
[alias]
assume_unchanged_below = "!f() { \
git ls-files -z $1 | xargs -0 git update-index --assume-unchanged; \
}; f `git rev-parse --show-prefix`"
but this does not work correctly; $1
in f()
is empty. Presumably this is because the new shell process that is executing f()
has the repository root directory as its current directory.
Is it even possible to do what I'm trying to do in a git alias, or do I need to make an alias in my .bashrc instead to accomplish this?
-------------------- Update with Answer --------------------
$GIT_PREFIX は、元のエイリアスがトリガーされた git シェルからその値を継承するため、$GIT_PREFIX を使用するという @vampire の提案は機能しました。
使いやすさのために装飾を加えた実際の例を次に示します。
[alias]
assumeallbelow = "!f() { \
if [ x"$1" != x ]; then \
echo "You cannot specify the directory because this command operates on all files in the current directory and below"; \
return; \
fi; \
echo "Marking all files under $GIT_PREFIX as assume-unchanged"; \
git ls-files -z $GIT_PREFIX | xargs -0 git update-index --assume-unchanged; \
}; f"