如何在 gitconfig 中正确地为这个 git 行计数命令设置别名?
How to correctly alias this git line-counting command in gitconfig?
我在 Stack Overflow 上找到这个命令来计算每个用户对 repo 的 LOC 贡献:
git log --no-merges --pretty=format:%an --numstat | awk '/./ && !author { author = [=11=]; next } author { ins[author] += ; del[author] += } /^$/ { author = ""; next } END { for (a in ins) { printf "%10d %10d %10d %s\n", ins[a] - del[a], ins[a], del[a], a } }' | sort -rn
但是,当我尝试在我的 .gitconfig 中将其作为命令的别名时,我遇到了各种错误。我不知道如何正确转义它,以便在我调用 "git count-lines" 时它在我的 bash 中正常运行。我根据 Whosebug 上描述 git 别名的其他问题进行了多次尝试,但我不断遇到不同的问题。
到目前为止,我已经在 gitconfig 中尝试了很多东西。这个给我的错误最少:
[alias]
count-lines = "!f() { git log --no-merges --pretty=format:%an --numstat | awk '/./ && !author { author = [=12=]; next } author { ins[author] += ; del[author] += } /^$/ { author = \"\"; next } END { for (a in ins) { printf \"%10d %10d %10d %s\n\", ins[a] - del[a], ins[a], del[a], a } }' | sort -rn; }; f"
这是上面给出的错误:
awk: cmd. line:1: /./ && ... printf "%10d %10d %10d %s
awk: cmd. line:1: ^ unterminated string
awk: cmd. line:1: /./ && ... printf "%10d %10d %10d %s
awk: cmd. line:1: ^ syntax error
(据我了解,我使用的是 windows 7 计算机,因此我需要在整个命令周围使用引号)
好的,经过一个半小时的调试和摆弄,我终于找到了问题所在:我需要转义 printf 语句中换行符中的反斜杠。
broken: count-lines = "!f() { git log --no-merges --pretty=format:%an --numstat | awk '/./ && !author { author = [=10=]; next } author { ins[author] += ; del[author] += } /^$/ { author = \"\"; next } END { for (a in ins) { printf \"%10d %10d %10d %s\n\", ins[a] - del[a], ins[a], del[a], a } }' | sort -rn; }; f"
fixed: count-lines = "!f() { git log --no-merges --pretty=format:%an --numstat | awk '/./ && !author { author = [=10=]; next } author { ins[author] += ; del[author] += } /^$/ { author = \"\"; next } END { for (a in ins) { printf \"%10d %10d %10d %s\n\", ins[a] - del[a], ins[a], del[a], a } }' | sort -rn; }; f"
编辑:根据要求添加最终脚本(以下是我的 ~/.gitconfig
文件的全部内容):
[user]
name = Sam Smith
email = sam.smith@gmail.com
[alias]
count-lines = "!f() { git log --no-merges --pretty=format:%an --numstat | awk '/./ && !author { author = [=11=]; next } author { ins[author] += ; del[author] += } /^$/ { author = \"\"; next } END { for (a in ins) { printf \"%10d %10d %10d %s\n\", ins[a] - del[a], ins[a], del[a], a } }' | sort -rn; }; f"
我在 Stack Overflow 上找到这个命令来计算每个用户对 repo 的 LOC 贡献:
git log --no-merges --pretty=format:%an --numstat | awk '/./ && !author { author = [=11=]; next } author { ins[author] += ; del[author] += } /^$/ { author = ""; next } END { for (a in ins) { printf "%10d %10d %10d %s\n", ins[a] - del[a], ins[a], del[a], a } }' | sort -rn
但是,当我尝试在我的 .gitconfig 中将其作为命令的别名时,我遇到了各种错误。我不知道如何正确转义它,以便在我调用 "git count-lines" 时它在我的 bash 中正常运行。我根据 Whosebug 上描述 git 别名的其他问题进行了多次尝试,但我不断遇到不同的问题。
到目前为止,我已经在 gitconfig 中尝试了很多东西。这个给我的错误最少:
[alias]
count-lines = "!f() { git log --no-merges --pretty=format:%an --numstat | awk '/./ && !author { author = [=12=]; next } author { ins[author] += ; del[author] += } /^$/ { author = \"\"; next } END { for (a in ins) { printf \"%10d %10d %10d %s\n\", ins[a] - del[a], ins[a], del[a], a } }' | sort -rn; }; f"
这是上面给出的错误:
awk: cmd. line:1: /./ && ... printf "%10d %10d %10d %s
awk: cmd. line:1: ^ unterminated string
awk: cmd. line:1: /./ && ... printf "%10d %10d %10d %s
awk: cmd. line:1: ^ syntax error
(据我了解,我使用的是 windows 7 计算机,因此我需要在整个命令周围使用引号)
好的,经过一个半小时的调试和摆弄,我终于找到了问题所在:我需要转义 printf 语句中换行符中的反斜杠。
broken: count-lines = "!f() { git log --no-merges --pretty=format:%an --numstat | awk '/./ && !author { author = [=10=]; next } author { ins[author] += ; del[author] += } /^$/ { author = \"\"; next } END { for (a in ins) { printf \"%10d %10d %10d %s\n\", ins[a] - del[a], ins[a], del[a], a } }' | sort -rn; }; f"
fixed: count-lines = "!f() { git log --no-merges --pretty=format:%an --numstat | awk '/./ && !author { author = [=10=]; next } author { ins[author] += ; del[author] += } /^$/ { author = \"\"; next } END { for (a in ins) { printf \"%10d %10d %10d %s\n\", ins[a] - del[a], ins[a], del[a], a } }' | sort -rn; }; f"
编辑:根据要求添加最终脚本(以下是我的 ~/.gitconfig
文件的全部内容):
[user]
name = Sam Smith
email = sam.smith@gmail.com
[alias]
count-lines = "!f() { git log --no-merges --pretty=format:%an --numstat | awk '/./ && !author { author = [=11=]; next } author { ins[author] += ; del[author] += } /^$/ { author = \"\"; next } END { for (a in ins) { printf \"%10d %10d %10d %s\n\", ins[a] - del[a], ins[a], del[a], a } }' | sort -rn; }; f"