【问题标题】:How to escape { in git alias如何在 git 别名中转义 {
【发布时间】:2018-11-11 18:51:22
【问题描述】:

我正在尝试写日志别名

git config --global --replace-all alias.lol6 "! f() { echo "\$1"; git --no-pager log --oneline  --graph -15 \${@}; }; f"

并像这样使用它:

git lol5 '@{-1}'
git log '@{-1}'  #works

但是传递给 git 的 commit-ish 是 @-1

fatal: ambiguous argument '@-1': unknown revision or path not in the working tree

我阅读了this post,但不明白如何使用它

谢谢 波阿斯

更多信息,尝试@vonc 建议

git config --global --replace-all alias.lol6 '! f() { echo "$1"; git --no-pager log --oneline  --graph -15 ${@}; }; f'

得到相同的结果,打开 GIT_TRACE

git lol6 @{-1}
20:20:37.454153 git.c:576               trace: exec: git-lol6 '@{-1}'
20:20:37.454153 run-command.c:640       trace: run_command: git-lol6 '@{-1}'
20:20:37.463150 run-command.c:640       trace: run_command: ' f() { echo "$1"; git --no-pager log --oneline  --graph -15 ${@}; }; f' '@{-1}'
@-1
22:20:37.607150 git.c:344               trace: built-in: git log --oneline --graph -15 @-1
fatal: ambiguous argument '@-1': unknown revision or path not in the working tree.

Git 在第一个参数周围添加 ''

但是!!!,如果我尝试过

git lol6 HEAD
20:30:35.621257 run-command.c:640       trace: run_command: ' f() { echo "$1"; git --no-pager log --oneline  --graph -15 ${@}; }; f' HEAD

Git 不会在 HEAD 周围添加 ''

【问题讨论】:

    标签: bash git escaping git-alias


    【解决方案1】:

    我更喜欢使用字符串引号 ' 而不是双引号:在你的情况下少转义。

    查看这个示例,在 git bash 会话中完成。

    vonc@voncavn7:/mnt/d/git/git$ 
    git config --global --replace-all alias.lol6 \
      '! f() { echo "$1"; git --no-pager log --oneline  --graph -15 ${@}; }; f'
    

    首先,我检查我是否可以打印一个虚假的参数:

    vonc@voncavn7:/mnt/d/git/git$ git config --global --replace-all alias.lol6 '! f() { echo "$1"; git --no-pager log --oneline  --graph -15 ${@}; }; f'
    vonc@voncavn7:/mnt/d/git/git$ git lol6 e
    e
    fatal: ambiguous argument 'e': unknown revision or path not in the working tree.
    Use '--' to separate paths from revisions, like this:
    'git <command> [<revision>...] -- [<file>...]'
    

    然后我尝试,调用别名时不带任何引号:git lol6 @{-1}

    vonc@voncavn7:/mnt/d/git/git$ git lol6 @{-1}
    @{-1}
    * 29533fb168 (tmp2) RelNotes: the eleventh batch
    *   dc8fb4dd7e Merge branch 'rb/quick-install-doc'
    |\
    | * 65289e9dcd install-doc-quick: allow specifying what ref to install
    

    如果上面的别名/命令不起作用,请始终使用simplified PATH 仔细检查:

    set G=c:\path\to\latest\git
    set PATH=%G%\bin;%G%\usr\bin;%G%\mingw64\bin
    set PATH=%PATH%;C:\windows\system32;C:\windows\System32\Wbem;C:\windows\System32\WindowsPowerShell\v1.0\
    

    OP Boaz Nahum 确认in the comments:

    使用“简化的 PATH”——它可以工作。
    现在是什么意思?为什么它有效?

    它之所以有效,是因为不知道您的原始 PATH、C:\Windows\System32 和放置在 Git 之前 的其他文件夹中的内容,并掩盖了一些 Git 可执行文件。

    我怎样才能使标准的 Git 安装也能正常工作?

    只需将上面的c:\path\to\latest\git替换成你自己的Git当前安装路径即可。

    然后设置你的PATH:

    set PATH=%G%\bin;%G%\usr\bin;%G%\mingw64\bin
    

    接下来,从原始 %PATH% 添加所有其他文件夹,并始终以 C:\windows\system32;C:\windows\System32\Wbem;C:\windows\System32\WindowsPowerShell\v1.0\ 结尾。
    这样,你:

    • 从最具体的开始(Git:git/bin、git/usr/bin、git/ming64/bin),确保 Git 按预期工作
    • 继续使用更通用的应用程序路径(原始 PATH 中的路径)
    • 以最通用的路径结束:操作系统 Windows 路径(C:\windows\system32\... 路径)

    通过从最具体到最一般,您可以降低已安装软件的“意外行为”率。

    【讨论】:

    • 感谢@vonc,尝试了您的建议,但没有任何运气,添加'set -x',很明显@-1 已传递给git。可能是windows下的git自带的bash实现相关的,会在linux上试试
    • 你的操作系统和你的版本是什么?
    • 你在我打字的时候问过 :) Windows 10, git 2.17.0.windows.1
    • 我会尝试与 git 无关的简单 bash 脚本
    • @BoazNahum 另外,使用相同的命令和 git bash 重试,但使用简化的 PATH:stackoverflow.com/a/50245596/6309
    猜你喜欢
    • 1970-01-01
    • 2016-10-29
    • 2021-12-12
    • 2013-12-05
    • 1970-01-01
    • 2013-10-11
    • 1970-01-01
    • 2011-07-02
    • 2015-02-16
    相关资源
    最近更新 更多