0

So I have a build shell script that calculates a build number for a mobile application based on the count of commits to the HEAD branch (BUILD_NUMBER=git rev-list HEAD --count)

Is there a way of getting the total number of commits across two branches? Either in a single git command or in multiple lines in the script? Not well versed in either git or zsh :(

To be clear, what I am seeking is a way to get the SUM of commits in two branches (count of commits in branch1 + count of commits in branch2) Can this be done in a single command? Is there a way to do it in multiple steps within the build shell script?

Jonas P
  • 67
  • 8

1 Answers1

1

Instead of HEAD, just give the branch name

However, it's possible the number will be the same for many branches with different commits, say

main:     aaaaaaa -> bbbbbbb
feature1: aaaaaaa -> bbbbbbb -> ccccccc
feature2: aaaaaaa -> bbbbbbb -> ddddddd

Also note you may want to git pull and use origin/branchname to avoid having to update the local copy of each branch individually


You can add numbers in most modern shells with $(()) syntax

% echo "$((10 + 11))"
21

All together

% echo "$(( $(git rev-list --count origin/feature1) + $(git rev-list --count origin/feature2) ))"
500

However, assuming the final result is the combination of two branches, it would be best to try and merge to ensure you get the true commit count, perhaps in a temporary branch

ti7
  • 16,375
  • 6
  • 40
  • 68
  • is there a way to sum the count of commits on two separate but similarly named branches? – Jonas P Aug 23 '23 at 19:56
  • sure - you could just use like `$(())` - let me add a little – ti7 Aug 23 '23 at 20:01
  • 1
    thank you so much. I dont much experience with the syntax and had tried a bunch of different things so this was exactly what I needed. this needs to be done for a stupid reason based on our client's pipeline but I agree the merge would be the best strategy. – Jonas P Aug 23 '23 at 20:10