简体   繁体   中英

How to deal with combinations of git aliases

Problem

I have three git aliases defined in .gitconfig (with an external bash script defining a function called diff-lines):

    [alias]
        diffc = diff --cached
        diffnw = diff -w --ignore-cr-at-eol --ignore-all-space
        diffln =!bash -c 'source $HOME/.bash_functions/diff-lines && git diff | diff-lines'

How can I define 'diffln' such that I can tell it which version of my diff aliases to use?

I'm looking for something to prevent me from having to define each version similar to:

    diffcln =!bash -c 'source $HOME/.bash_functions/diff-lines && git diffc | diff-lines'
    diffnwln =!bash -c 'source $HOME/.bash_functions/diff-lines && git diffnw | diff-lines'
    diffcnwln =!bash -c 'source $HOME/.bash_functions/diff-lines && git diffnw --cached | diff-lines'
    etc...

Solution

Thanks to @KamilCuk for the solution. I needed to include something/anything after the alias, and also enclose the whole alias in double quotes.

The full working command is this (with a default option to use git diff if no argument is provided):

    diffln ="!bash -c 'source $HOME/.bash_functions/diff-lines && if ((!$#)); then set -- diff; fi && git $@ | diff-lines' _"

Previous Attempts

I have tried this:
diffln =!bash -c 'source $HOME/.bash_functions/diff-lines && git $@ | diff-lines'
and called via:
$ git diffln diffc but it just gave me the default git options as if I was only calling $ git

Add a something after it. Typically: bash -c "blabla" _ or bash -c "balbla" -- .

The first argument after bash -c "" <here> is $0 - the second argument is $1 .

Also remember about quoting - put $anything inside double quotes to prevent word splitting and filename expansions. But I am not sure how git handled that.

diffln =!bash -c 'source "$HOME/.bash_functions/diff-lines" && git "$@" | diff-lines' _

if you want to have a "default" argument, you can:

diffln =!bash -c 'source "$HOME/.bash_functions/diff-lines" && if ((!$#)); then set -- diff; fi && git "$@" | diff-lines' _

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