1

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
rowoc
  • 239
  • 2
  • 12
  • Please describe which date you want to prefix the branches with. From where do you intend to grab that date ... or how do you intend to calculate that date? – Lasse V. Karlsen Nov 15 '21 at 14:00
  • The "authordate" that need to be printed before the branch name. @LasseV.Karlsen – rowoc Nov 15 '21 at 14:35
  • And is it the authordate of the last commit on the branch you're talking about then? The authordate of the commit the branch references? – Lasse V. Karlsen Nov 15 '21 at 14:52
  • Yes the branch creation date . Basically I have to list the 4 month old branches with serial number | branch date | branch name and then delete all the branch listed . Please help me it would be greatly appreciated. @LasseV.Karlsen – rowoc Nov 15 '21 at 14:57
  • You say "yes" and then you say "branch creation date", which is not the same thing. If I create a branch right now you could argue that the branch creation date is today, November 15th. However, if I work on the branch for 1 month, the actual commit the branch refers to may have an authordate of 15th of December. Can you please clarify which of those two you want? I ask because obtaining the creation date according to my example (15th of November) might not be easy depending on merges and stuff. – Lasse V. Karlsen Nov 15 '21 at 15:01
  • Let it be "committerdate" as of now . Thanks in advance for the concern and hoping for a solution @LasseV.Karlsen – rowoc Nov 15 '21 at 15:06
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/239243/discussion-between-rowoc-and-lasse-v-karlsen). – rowoc Nov 15 '21 at 15:06

2 Answers2

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

banyudu
  • 1,046
  • 9
  • 19
0

git log (and git show) have a --date=<format> option :

git log --date=relative --format="%ad %s" -1 <branch or commit>
LeGEC
  • 46,477
  • 5
  • 57
  • 104