简体   繁体   中英

Git alias: how to pass current directory to shell command?

I'm using git bash under Windows, and I would like to make a git alias command that's conceptually something like this:

[alias]
    assume_unchanged_below = "!git ls-files -z $dir | xargs -0 git update-index --assume-unchanged"

Where $dir expands out to the current working directory in the bash shell in which git assume_unchanged_below was executed.

Ie if I'm in C:\\somedir\\somesubdir and execute git assume_unchanged_below I would want it to be as if I had typed git ls-files -z C:\\somedir\\somesubdir | xargs -0 git update-index --assume-unchanged git ls-files -z C:\\somedir\\somesubdir | xargs -0 git update-index --assume-unchanged

It seems like it's possible to get the current directory via git rev-parse --show-prefix . I also know it's possible to pass arguments using an anonymous function, but I haven't been able to combine these two concepts to get the behavior I want.

For example, if I do this:

[alias]
    assume_unchanged_below = "!f() { \
       git ls-files -z $1 | xargs -0 git update-index --assume-unchanged; \
    }; f"

Then I can type

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 --------------------

@vampire's suggestion of using $GIT_PREFIX worked, because $GIT_PREFIX inherits its value from the git shell from which the original alias was triggered.

Here's a the working example, with embellishments for usability:

[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" 
git config alias.test '!git ls-files $GIT_PREFIX'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM