Git 提交:解析为必要的格式

Git commit: Parse to necessary format

我正在使用 Git bash 获取 git 分支上的提交历史记录(第一次使用该工具)。当我使用 git log 命令时,我得到以下格式的提交

1erdf146: 2020-06-15 (myself)  #Ticket Subject: Ticket I used to commit #Ticket ID: https://myjiralink.com/r-12345 #Ticket Summary: My Summary

有没有办法提取#Ticket Subject 和#Ticket summary 以及提交日期?我必须为每次提交都这样做。

我不确定你的问题实际上是关于 git 而不是关于字符串解析。

使用 git log 您可以非常自由地自定义输出格式。例如,您只能输出提交日期和提交主题:

git log --format=format:"%cs %s"

有关详细信息,请参阅 the format section in the git log man page

现在要提取票证 subject/summary 您需要 parse/process 提交消息,例如使用 grep (1):

# output the commit dates
git log --format=format:"%cs"

# output only the Ticket Subject part of the commit message
git log --format=format:"%s" | grep -Po '(?<=#Ticket Subject: ).*?(?= *#Ticket|$)'

# output only the Ticket Summary part of the commit message
git log --format=format:"%s" | grep -Po '(?<=#Ticket Summary: ).*?(?= *#Ticket|$)'

# output only the Ticket ID part of the commit message
git log --format=format:"%s" | grep -Po '(?<=#Ticket ID: ).*?(?= *#Ticket|$)'

您可能还想查看 sed (1) and awk (1) 以了解有关处理输出的更多选项。