gitgit-branchgit-clean

How to print X month ago time before branch name?


Below code I am trying and the output should contain the branch name with x month ago. Any suggestion in this logic would be greatly appreciated.

Here the main aim of the code is to fetch list all branch name before 4 month ago.

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

Getting Output :

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

I need to add x month time stamp after the serial number in sort by date

Expected output :

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

Solution

  • 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