使用sed或awk自动记录gitconfig别名的文档

3
我目前使用以下代码(拼凑而成)来使用'git alias'生成我的git别名文档。这需要假定在每个别名上面有一个以###开头的注释,并且别名格式为“别名名称=命令”。此代码可用,但是有没有更好的方法呢?:)
当前代码($HOME/.bin/bin/gdoc的内容):
grep --no-group-separator -A1 '###' "$HOME"/.gitconfig | awk 'END{if((NR%2))print p}!(NR%2){print$0p}{p=$0}' | sed -re 's/( =)(.*)(###)/:*/g' | awk -F* '{printf "\033[1;31m%-30s\033[0m %s\n", $1, $2}' | sort

示例别名:

### use difftool to view differences in file
dt = difftool

# https://dev59.com/4HA75IYBdhLWcg3wYYBQ#39523506
### add and commit a file, arg1=file, arg2=commit_message
ac = "!cd -- \"${GIT_PREFIX:-.}\" && git add \"$1\" && git commit -m \"$2\" #"

### 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 $1 >> .gitignore #"

### git rm files that have been deleted without using git
r = "!git ls-files -z --deleted | xargs -0 git rm #"

# https://dev59.com/dVoT5IYBdhLWcg3w4SpQ#39616600
### 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
1个回答

2

就像这样:

awk 'a{print $1""c;a=0}/###/{$1="";c=$0;a=1}' "$HOME"/.gitconfig | sort

在同一个管道中使用 awkgrepsed,通常意味着它们的使用效率不高。通常可以用单个 awk 命令替换它们。

要获得漂亮的缩进输出,您可以使用 column

awk 'a{print $1"%"c;a=0}/###/{$1="";c=$0;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[$1]=comment
    in_alias=0
    len=length($1)
    if(len > maxlen){
        maxlen = len
    }
}
/###/{
    $1=""
    comment=$0
    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

谢谢!你介意解释一下awk命令是如何工作的吗?我看到模式搜索了###,但我不太明白'a'和'c'是如何使用的。 - undefined
c 表示 _注释_,一个变量用于存储别名的注释文本。a 是一个标志位,可以是 01,表示我们是否在 别名 定义内部。这仅在文件被“正确”格式化时有效,即别名命令以 ### 开头,后跟下一行中的别名定义。 - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接