我在Stack Overflow上找到了此命令,以计算每个用户对仓库的LOC贡献:
git log --no-merges --pretty=format:%an --numstat | awk '/./ && !author { author = $0; next } author { ins[author] += $1; del[author] += $2 } /^$/ { 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中正常运行。我已经基于StackOverflow上描述git别名的其他问题进行了多次尝试,但是我一直遇到不同的问题。
到目前为止,我已经在gitconfig中尝试了很多东西。这给了我最少的错误:
[alias]
count-lines = "!f() { git log --no-merges --pretty=format:%an --numstat | awk '/./ && !author { author = $0; next } author { ins[author] += $1; del[author] += $2 } /^$/ { 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计算机上,因此我需要在整个命令周围加上引号)
答案 0 :(得分:1)
好吧,经过一个半小时的调试和摆弄,我终于弄清楚了这个问题:我需要在printf语句中的换行符中转义反斜杠。
broken: count-lines = "!f() { git log --no-merges --pretty=format:%an --numstat | awk '/./ && !author { author = $0; next } author { ins[author] += $1; del[author] += $2 } /^$/ { 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 = $0; next } author { ins[author] += $1; del[author] += $2 } /^$/ { 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"