使用 sed 或 awk 自动记录 gitconfig 别名
Automatic documentation of gitconfig aliases using sed or awk
我目前正在使用以下代码(一起破解)为使用 'git alias' 的 git 别名生成文档。这假定每个别名上方都有一条以### 开头的注释,并且别名的格式为 'alias_name = command'。这按原样工作,但有没有人有更好的方法? :)
当前代码($HOME/.bin/bin/gdoc
的内容):
grep --no-group-separator -A1 '###' "$HOME"/.gitconfig | awk 'END{if((NR%2))print p}!(NR%2){print[=10=]p}{p=[=10=]}' | sed -re 's/( =)(.*)(###)/:*/g' | awk -F* '{printf "3[1;31m%-30s3[0m %s\n", , }' | sort
别名示例:
### use difftool to view differences in file
dt = difftool
#
### add and commit a file, arg1=file, arg2=commit_message
ac = "!cd -- \"${GIT_PREFIX:-.}\" && git add \"\" && git commit -m \"\" #"
### remove any files that are in gitignore from tracking
ig = "!git rm --cached `git ls-files -i --exclude-from=.gitignore` #"
### print out available aliases
alias = "!$HOME/.bin/bin/gdoc #"
### add a file to gitignore
ignore = "!([ ! -e .gitignore ] && touch .gitignore) | echo >> .gitignore #"
### git rm files that have been deleted without using git
r = "!git ls-files -z --deleted | xargs -0 git rm #"
#
### Quote a sh command, converting it to a git alias string
quote-string = "!read -r l; printf \\"!; printf %s \"$l\" | sed 's/\([\\"]\)/\\\1/g'; printf \" #\\"\n\" #"
### Unquote a git alias command command, converting it to a sh command
quote-string-undo = "!read -r l; printf %s \"$l\" | sed 's/\\\([\\"]\)/\1/g'; printf \"\n\" #"
### debug git aliases - 'git debug <alias>'
debug = "!set -x; GIT_TRACE=2 GIT_CURL_VERBOSE=2 GIT_TRACE_PERFORMANCE=2 GIT_TRACE_PACK_ACCESS=2 GIT_TRACE_PACKET=2 GIT_TRACE_PACKFILE=2 GIT_TRACE_SETUP=2 GIT_TRACE_SHALLOW=2 git"
当前输出:
ac: add and commit a file, arg1=file, arg2=commit_message
alias: print out available aliases
debug: debug git aliases - 'git debug <alias>'
dt: use difftool to view differences in file
ig: remove any files that are in gitignore from tracking
ignore: add a file to gitignore
quote-string-undo: Unquote a git alias command command, converting it to a sh command
quote-string: Quote a sh command, converting it to a git alias string
r: git rm files that have been deleted without using git
像这样:
awk 'a{print ""c;a=0}/###/{="";c=[=10=];a=1}' "$HOME"/.gitconfig | sort
在同一个管道中使用 awk
、grep
和 sed
主要是它们未被有效使用的标志。通常它们可以被单个 awk
命令替换。
对于漂亮的缩进输出,您可以使用 column
:
awk 'a{print "%"c;a=0}/###/{="";c=[=11=];a=1}' "$HOME"/.gitconfig | sort | column -ts%
注意:我正在使用 awk
注入一个 %
分隔符,用于使用 column
缩进输出
输出:
ac add and commit a file, arg1=file, arg2=commit_message
alias print out available aliases
debug debug git aliases - 'git debug <alias>'
dt use difftool to view differences in file
ignore add a file to gitignore
ig remove any files that are in gitignore from tracking
quote-string Quote a sh command, converting it to a git alias string
quote-string-undo Unquote a git alias command command, converting it to a sh command
r git rm files that have been deleted without using git
如果你有 gawk
(GNU awk),你也可以只使用 awk,像这样:
#!/usr/bin/gawk -f
in_alias{
aliases[]=comment
in_alias=0
len=length()
if(len > maxlen){
maxlen = len
}
}
/###/{
=""
comment=[=13=]
in_alias=1
}
END{
n = asorti(aliases, sorted) # <-- Requires gawk
for(i = 1; i <= n; i ++){
pad = maxlen - length(sorted[i]) + 1
printf "%s%"pad"s%s\n",sorted[i]," ",aliases[sorted[i]]
}
}
将其保存到文件中,比方说,/usr/local/bin/git-aliases 并使其可执行:
chmod +x /usr/local/bin/git-aliases
git-aliases "$HOME"/.gitconfig
我目前正在使用以下代码(一起破解)为使用 'git alias' 的 git 别名生成文档。这假定每个别名上方都有一条以### 开头的注释,并且别名的格式为 'alias_name = command'。这按原样工作,但有没有人有更好的方法? :)
当前代码($HOME/.bin/bin/gdoc
的内容):
grep --no-group-separator -A1 '###' "$HOME"/.gitconfig | awk 'END{if((NR%2))print p}!(NR%2){print[=10=]p}{p=[=10=]}' | sed -re 's/( =)(.*)(###)/:*/g' | awk -F* '{printf "3[1;31m%-30s3[0m %s\n", , }' | sort
别名示例:
### use difftool to view differences in file
dt = difftool
#
### add and commit a file, arg1=file, arg2=commit_message
ac = "!cd -- \"${GIT_PREFIX:-.}\" && git add \"\" && git commit -m \"\" #"
### remove any files that are in gitignore from tracking
ig = "!git rm --cached `git ls-files -i --exclude-from=.gitignore` #"
### print out available aliases
alias = "!$HOME/.bin/bin/gdoc #"
### add a file to gitignore
ignore = "!([ ! -e .gitignore ] && touch .gitignore) | echo >> .gitignore #"
### git rm files that have been deleted without using git
r = "!git ls-files -z --deleted | xargs -0 git rm #"
#
### Quote a sh command, converting it to a git alias string
quote-string = "!read -r l; printf \\"!; printf %s \"$l\" | sed 's/\([\\"]\)/\\\1/g'; printf \" #\\"\n\" #"
### Unquote a git alias command command, converting it to a sh command
quote-string-undo = "!read -r l; printf %s \"$l\" | sed 's/\\\([\\"]\)/\1/g'; printf \"\n\" #"
### debug git aliases - 'git debug <alias>'
debug = "!set -x; GIT_TRACE=2 GIT_CURL_VERBOSE=2 GIT_TRACE_PERFORMANCE=2 GIT_TRACE_PACK_ACCESS=2 GIT_TRACE_PACKET=2 GIT_TRACE_PACKFILE=2 GIT_TRACE_SETUP=2 GIT_TRACE_SHALLOW=2 git"
当前输出:
ac: add and commit a file, arg1=file, arg2=commit_message
alias: print out available aliases
debug: debug git aliases - 'git debug <alias>'
dt: use difftool to view differences in file
ig: remove any files that are in gitignore from tracking
ignore: add a file to gitignore
quote-string-undo: Unquote a git alias command command, converting it to a sh command
quote-string: Quote a sh command, converting it to a git alias string
r: git rm files that have been deleted without using git
像这样:
awk 'a{print ""c;a=0}/###/{="";c=[=10=];a=1}' "$HOME"/.gitconfig | sort
在同一个管道中使用 awk
、grep
和 sed
主要是它们未被有效使用的标志。通常它们可以被单个 awk
命令替换。
对于漂亮的缩进输出,您可以使用 column
:
awk 'a{print "%"c;a=0}/###/{="";c=[=11=];a=1}' "$HOME"/.gitconfig | sort | column -ts%
注意:我正在使用 awk
注入一个 %
分隔符,用于使用 column
输出:
ac add and commit a file, arg1=file, arg2=commit_message
alias print out available aliases
debug debug git aliases - 'git debug <alias>'
dt use difftool to view differences in file
ignore add a file to gitignore
ig remove any files that are in gitignore from tracking
quote-string Quote a sh command, converting it to a git alias string
quote-string-undo Unquote a git alias command command, converting it to a sh command
r git rm files that have been deleted without using git
如果你有 gawk
(GNU awk),你也可以只使用 awk,像这样:
#!/usr/bin/gawk -f
in_alias{
aliases[]=comment
in_alias=0
len=length()
if(len > maxlen){
maxlen = len
}
}
/###/{
=""
comment=[=13=]
in_alias=1
}
END{
n = asorti(aliases, sorted) # <-- Requires gawk
for(i = 1; i <= n; i ++){
pad = maxlen - length(sorted[i]) + 1
printf "%s%"pad"s%s\n",sorted[i]," ",aliases[sorted[i]]
}
}
将其保存到文件中,比方说,/usr/local/bin/git-aliases 并使其可执行:
chmod +x /usr/local/bin/git-aliases
git-aliases "$HOME"/.gitconfig