如何处理.gitconfig中的长行?

时间:2017-03-19 14:44:20

标签: git

在我的.gitconfig我有这个:

[format]
    pretty = "%C(bold blue)%h %C(bold green)%<|(20)% \
ar%C(reset) %C(white)% s %C(dim white) -% an%C(reset) \
%C(bold yellow)% d \
%C(bold red)% N"     

请注意,最后三行不能缩进,否则我会在格式字符串中得到不需要的缩进。

有没有更优雅的方式来写这个(当然这不起作用):

[format]
    pretty = "%C(bold blue)%h %C(bold green)%<|(20)%" +
             "ar%C(reset) %C(white)% s %C(dim white) -% an%C(reset)" +
             "%C(bold yellow)% d " +
             "%C(bold red)% N"

1 个答案:

答案 0 :(得分:3)

你可以在bash脚本中编写它,你可以使用字符串来实现你想要的。

例如:

~/.githelper

#!/bin/bash

HASH="%C(yellow)%h%Creset"
RELATIVE_TIME="%Cgreen(%ar)%Creset"
AUTHOR="%C(bold blue)<%an>%Creset"
REFS="%C(red)%d%Creset"
SUBJECT="%s"

FORMAT="$HASH}$RELATIVE_TIME}$AUTHOR}$REFS $SUBJECT"

show_git_head() {
  pretty_git_log -1
  git show -p --pretty="tformat:"
}

pretty_git_log() {
  git log --graph --abbrev-commit --date=relative --  pretty="tformat:${FORMAT}" $* |
  # Repalce (2 years ago) with (2 years)
  sed -Ee 's/(^[^<]*) ago)/\1)/' |
  # Replace (2 years, 5 months) with (2 years)
  sed -Ee 's/(^[^<]*), [[:digit:]]+ .*months?)/\1)/' |
  # Line columns up based on } delimiter
  column -s '}' -t |
  # Page only if we need to
  less -FXRS
}

比你的别名将使用这样的脚本,例如:

l = "!source ~/.githelper && pretty_git_log"