2

We are using gitflow within my organization, particularly the concept of release branches. We also support multiple simultaneous release branches. A sample repo may look like so:

master -> a
           \
            \    c <-release/v1.0.0    e <-release/v1.1.0    g <-release/v1.2.0
             \  /                     /                     /
    develop-> b ---------------------d---------------------f 

What I'd like to do is given a currently checked out release branch, find the predecessor release branch. For example:

-release/v1.2.0 is checked out, return release/v1.1.0
-release/v1.1.0 is checked out, return release/v1.0.0
-release/v1.0.0 is checked out, return nothing, there is no currently active previous release

We are using this to determine what files have changed between releases for deployment to a legacy system.

Assumptions: Release branches will always follow the format release/v.x.y.z, where x,y and z are version numbers. Release branches are always organized sequentially based on these version numbers. v1.2.1 > v1.2.0 and v1.2.0 > 1.1.0

I considered using some combination of git branch --list -a *release/v* to get a list of all release branches and some perl commands to sort through the results, but was wondering if there was some git commands along the vein of git rev-parse or git rev-list that could do it.

Notes: I am using z/OS USS ports of unix tools, git, bash, grep etc.

patrickw
  • 53
  • 5
  • "Notes: I am using z/OS USS ports of unix tools, git, bash, grep etc.": I see...: https://forum.rocketsoftware.com/tags/git, https://forum.rocketsoftware.com/t/using-git-for-z-os-with-github/654: https://forum.rocketsoftware.com/t/git-2-14-4-for-z-os-is-now-available/1217. I suppose you are on Git 2.14.4 – VonC May 03 '19 at 15:25
  • Yes, we are in the process of upgrading our git version to 2.14.4. I tried out your command below and it does work though. – patrickw May 06 '19 at 20:54

2 Answers2

2

Check first if (with the latest Git 2.21) the sort order for tags can be used for branches:

git branch --sort=version:refname

That way, you can get the right order before git grepping.

The OP patrickw mentions in the comments using:

git branch --list -r *release/v* --sort=-version:refname
VonC
  • 1,042,979
  • 435
  • 3,649
  • 4,283
  • I ultimately used a modified version of this command along with some bash to get the job done, thanks! `git branch --list -r *release/v* --sort=-version:refname` – patrickw Jul 24 '19 at 18:33
  • @patrickw Great! I have included your comment in the answer for more visibility. – VonC Jul 24 '19 at 21:04
1

There is no git-specific command, but you don't need to involve Perl, standard bash sort should do the work:

git branch --list -a *release/v* | sort -V | grep -B1 <current_version> | head -1

(note the -V option ->man)

Also note that you can't even get the name of the current branch without some text processing. See answers to this question here.

Kombajn zbożowy
  • 4,673
  • 1
  • 21
  • 49
  • I can get the current branch using a `git rev-parse`, no problem. Using the above example, my version of grep on uss does not recognize 1 as an option and I get nothing back to stdout. Am I supposed to redirect the output of this command to a file? – patrickw May 03 '19 at 14:38
  • "1" is a parameter to -B option that makes grep display that many lines of context before the match (->[man](http://man7.org/linux/man-pages/man1/grep.1.html)). If your grep doesn't support it, well, google for alternatives... – Kombajn zbożowy May 03 '19 at 20:27
  • I have this severely IBM hampered version of grep installed on the system that does not recognize context lines, and I don't see any alternative commands that do. I don't think the above would work in this case. https://www.ibm.com/support/knowledgecenter/en/SSLTBW_2.1.0/com.ibm.zos.v2r1.bpxa500/grep.htm – patrickw May 06 '19 at 20:46