【问题标题】:How to escape curly braces @{u} of upstream in git alias (git-bash on windows)? [UPDATED]如何在git别名中转义上游的花括号@{u}(Windows上的git-bash)? [更新]
【发布时间】:2018-08-01 05:03:53
【问题描述】:

我在 git config 中有以下别名以进行漂亮的格式化

$ git config alias.lp '!git log --pretty=format:"%h - %an (%ar): %s"'

它用于其他别名,这里是正确工作的例子:

$ git config alias.la '!f(){ git lp -20 --author="${1-Baur}"; }; f'
$ git config alias.lm '!f(){ git lp --grep "${1-strange}"; }; f'
$ git config alias.lf '!git lp --follow'

但我找不到如何实现别名以查看上游提交的方法。

这行得通:

$ git log --pretty=format:"%h - %an (%ar): %s" HEAD..@{u}

这不起作用:

$ git lp HEAD..@{u}

带有错误信息:

fatal: ambiguous argument 'HEAD..@u': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'

如您所见,HEAD..@{u} 变成了HEAD..@u,大括号丢失了。

启用$ GIT_TRACE=1后:

$ git lp HEAD..@{u}
21:17:32.678084 git.c:560               trace: exec: 'git-lp' 'HEAD..@{u}'
21:17:32.679097 run-command.c:626       trace: run_command: 'git-lp' 'HEAD..@{u}'
21:17:32.683098 run-command.c:626       trace: run_command: 'git log --pretty=format:"%h - %an (%ar): %s"' 'HEAD..@{u}'
21:17:32.726084 git.c:328               trace: built-in: git 'log' '--pretty=format:%h - %an (%ar): %s' 'HEAD..@u'
fatal: ambiguous argument 'HEAD..@u': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'

我应该如何在 Windows 上的 git bash 中转义 git lp HEAD..@{u} 中的花括号?

[更新]
感谢 @phd 指出在 linux 上没有问题!
我检查了这个转义问题只在 Windows 上的 git bash 上。
原生ubuntu和windows上的ubuntu都没有这个问题。
但我必须使用windows版本。所以我还在寻找解决方案。

$ git --version
git version 2.14.1.windows.1

【问题讨论】:

    标签: bash git escaping


    【解决方案1】:

    为我工作:

    $ git --version 
    git version 2.11.0
    
    $ git config alias.lp '!git log --pretty=format:"%h - %an (%ar): %s"'
    
    $ git config alias.lp
    !git log --pretty=format:"%h - %an (%ar): %s"
    
    $ git lp HEAD~..@{u}
    fc4c763 - Oleg Broytman (5 weeks ago): Build, Tests(tox): Python 3.7
    
    $ GIT_TRACE=1 git lp @~..@{u}
    15:57:15.464236 git.c:600               trace: exec: 'git-lp' '@~..@{u}'
    15:57:15.464326 run-command.c:350       trace: run_command: 'git-lp' '@~..@{u}'
    15:57:15.465775 run-command.c:350       trace: run_command: 'git log --pretty=format:"%h - %an (%ar): %s"' '@~..@{u}'
    15:57:15.466182 run-command.c:209       trace: exec: '/bin/sh' '-c' 'git log --pretty=format:"%h - %an (%ar): %s" "$@"' 'git log --pretty=format:"%h - %an (%ar): %s"' '@~..@{u}'
    15:57:15.469076 git.c:371               trace: built-in: git 'log' '--pretty=format:%h - %an (%ar): %s' '@~..@{u}'
    15:57:15.471285 run-command.c:350       trace: run_command: 'less'
    15:57:15.471744 run-command.c:209       trace: exec: 'less'
    

    【讨论】:

    • 感谢您的回答!我已经更新了这个问题。原来这个问题只在 git for windows 上出现。
    【解决方案2】:

    最后我在 windows 上找到了正确的 git-bash 转义

    $ git lp \'HEAD..@{u}\'
    $ git config alias.lu '!git lp \'\'HEAD..@{u}\'\' #'
    $ git lu
    

    对于 gitconfig

    [alias]
      lu1 = "!git lp \\'HEAD..@{u}\\'"
    

    【讨论】:

    • 在更新到git version 2.18.0.windows.1 之后,我发现他们解决了这个问题,所以我们不再需要转义:$ git lp HEAD..@{u}$ git config alias.lu '!git lp HEAD..@{u}'$ git lu 对于 gitconfig ` lu = !git lp HEAD ..@{u}`
    猜你喜欢
    • 2011-12-10
    • 2013-03-23
    • 2016-09-03
    • 1970-01-01
    • 2021-05-28
    • 2018-11-11
    • 1970-01-01
    • 2012-05-16
    • 1970-01-01
    相关资源
    最近更新 更多