【问题标题】:How to split git log output into an array of commits in bash?如何将 git log 输出拆分为 bash 中的提交数组?
【发布时间】:2022-10-04 19:19:53
【问题描述】:

我不擅长 bash,我想做的是从 git log --name-status branch_one...branch_two 获取信息,然后通过提交拆分输出,例如:

git_command_output="
commit one
Author: Johny
Date: 01/01/1999

feat: add something

M src/some_file.txt

commit two
Author: Alex
Date: 02/01/1999

fix: bug

M src/some_file.txt"

从上面我想创建和array 的字符串,其中每个字符串都是关于一次提交的信息。

例如 array 看起来像这样:

echo "${array[0]}" 

# Author: Johny
# Date: 01/01/1999

# feat: add something

# M src/my_file.txt

echo "${array[1]}" 

# Author: Alex
# Date: 02/01/1999

# fix: bug

# M src/foreign_file.txt

我试过的(没有用):

array[0]=$(echo "$git_command_output" | awk '{split($0, array, "commit"); print array[0]}')

IFS='commit' read -r -a array <<< "$git_command_output"

有没有一种简洁的方法,最好使用awk

【问题讨论】:

    标签: regex bash awk split script


    【解决方案1】:

    您可以将此readarrayawk 一起使用:

    IFS= readarray -d '' -t arr < <(awk -v RS='
    M [^
    ]+
    ' '{ORS = RT "
    【解决方案2】:

    您可以将许多git 命令(例如log)的输出过滤到影响特定文件的那些,只需在命令末尾添加这些文件的路径即可。尝试:

    git log --name-status branch_one...branch_two -- src/my_file.txt
    

    【讨论】:

    • 抱歉,我编辑了问题以通过修改文件过滤来删除部分,但这真的很有帮助。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2018-01-02
    • 1970-01-01
    • 2022-06-23
    • 1970-01-01
    • 2010-11-29
    • 2021-11-22
    • 2022-08-24
    相关资源
    最近更新 更多