简体   繁体   English

如何处理git别名的组合

[英]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):我在 .gitconfig 中定义了三个 git 别名(使用一个外部 bash 脚本定义了一个名为 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?如何定义“diffln”,以便我可以告诉它使用哪个版本的 diff 别名?

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.感谢@KamilCuk的解决方案。 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):完整的工作命令是这样的(如果没有提供参数,则默认使用 git diff 选项):

    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 $ git diffln diffc但它只是给了我默认的 git 选项,就好像我只是在调用$ git

Add a something after it.在它后面添加一个东西。 Typically: bash -c "blabla" _ or bash -c "balbla" -- .通常: bash -c "blabla" _bash -c "balbla" --

The first argument after bash -c "" <here> is $0 - the second argument is $1 . bash -c "" <here>之后的第一个参数是$0 -第二个参数是$1

Also remember about quoting - put $anything inside double quotes to prevent word splitting and filename expansions.还要记住引用 - 将$anything放在双引号内以防止分词和文件名扩展。 But I am not sure how git handled that.但我不确定 git 是如何处理的。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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