3

so I have this snippet that I want to use to filter out branches that doesn't have a certain prefix and that hasn't received any commits in over 3 months so that I can remove them from our remote later on.

 for k in $(git branch -r | awk -Forigin !'/\/Prefix1\/|\/prefix2\//'); do
  if [ "$(git log -1 --before="3 month" $k)" ]; then
    echo "$(git log -1 --pretty=format:"%ci, %cr, " $k) $k";
  fi;
done

The problem is currently that when I run this I see branches that have received commits 3 weeks ago, 5 months ago, 2 months ago, 1 month ago etc etc and I can't figure out why.

But if I only run: git log --before="4 month" --pretty=format:"%ci, %cr, " It works as intended.

Can anyone give me any guidance?

Ka_booom
  • 33
  • 5

1 Answers1

5

The -1 in git log -1 [filters] $k will :

  • unroll the history of git log [filters] $k
  • limit this history to its first line

So if a branch has a 3 month old commit in its history (I would guess : any of your branches does), git log -1 --before="3 month" $k will always show 1 line -- the first commit in its history that is more than 3 month old.

Your leading if [ ... ] condition will always be true.


To fix that, you can limit the range of commits to select only the leading commit of each branch :

git log --before="3month" $k^..$k

[edit]

Another option (as suggested by user phd in a comment to this question) is to list each branch together with the date of its topmost commit, and keep only lines where the date fall below 4 months ago:

# in this example I use 'committerdate', you may also use 'authordate' if
# you think it is a better fit for your needs
git branch -r --format="%(committerdate:format:%F)  %(refname:short)" \
    --sort=-committerdate | <... process the list ...>

note the --format option for git branch is documented in git help for-each-ref (here: --format flag and here: FIELD NAMES section),
if you scroll at the bottom of the FIELD NAMES section, you will find a mention of how to format date fields:

As a special case for the date-type fields, you may specify a format for the date by adding : followed by date format name (see the values the --date option to git-rev-list[1] takes).

LeGEC
  • 46,477
  • 5
  • 57
  • 104
  • 1
    edited my answer several times, that last suggestion should work. – LeGEC May 04 '20 at 08:41
  • Thank you for the pedagogical answer. This seems to have done the trick! <3 – Ka_booom May 04 '20 at 08:49
  • @Ka_booom Please consider accepting the answer then (and eventually up-voting it), cf. [What should I do when someone answers my question?](https://stackoverflow.com/help/someone-answers). – Keldorn May 04 '20 at 10:02
  • I'm here again with a follow up question: – Ka_booom Mar 25 '21 at 14:56
  • Hi, the best way to do that is to ask a new question ([{Ask Question}](https://stackoverflow.com/questions/ask) button at top of the page), and paste a link to this current post in your new question. – LeGEC Mar 25 '21 at 15:03
  • @Ka_boom : your comment ends after the `:`, I don't see a question there – LeGEC Mar 25 '21 at 15:39