1

下面的代码我正在尝试,输出应该包含 x 个月前的分支名称。此逻辑中的任何建议将不胜感激。

这里代码的主要目的是获取 4 个月前的所有分支名称列表。

current_timestamp=$(date +%s)
four_month_ago=$(( $current_timestamp - 4*30*24*60*60 ))

for x in `git branch -r`; do
    branch_timestamp=$(git show -s --format=%at $x)
    if [[ "$branch_timestamp" < "$four_month_ago" ]]; then
        list_branch+=("${x/origin\//}")
    fi
done

i=0
for x in ${list_branch[*]}; do
    printf "    %3d - %s\n" $i $x
    i=$(( i + 1 ))
done

获取输出:

0 - fix-code
1 - bug-read
2 - feature/memcp-fix

我需要在按日期排序的序列号后添加 x 月时间戳

预期输出:

0 - 5 month ago - fix-code
1 - 7 month ago - bug-read
2 - 10 month ago - feature/memcp-fix
4

2 回答 2

1
current_timestamp=$(date +%s)
four_month_ago=$(( $current_timestamp - 4*30*24*60*60 ))

for x in `git branch -r|sed 's/origin\///'|sed -e '/ HEAD /d'`; do
    branch_timestamp=$(git show -s --format=%at origin/$x)
    if [[ "$branch_timestamp" < "$four_month_ago" ]]; then
        num=$(( ($current_timestamp - $branch_timestamp) / (30*24*60*60)))
        list_branch+=("$num month ago - ${x}")
    fi
done

i=0
for x in "${list_branch[@]}"; do
    printf "    %3d - %s\n" $i "$x"
    i=$(( i + 1 ))
done

于 2021-11-13T12:19:57.207 回答
0

git log(和git show)有一个--date=<format>选项:

git log --date=relative --format="%ad %s" -1 <branch or commit>
于 2021-11-15T14:00:37.100 回答