Alexander Mills
Alexander Mills

Reputation: 100000

Using git merge / git merge-base, reliably determine if branch is a feature branch

Say we are following the gitflow branching model where feature branches are created off of the integration branch.

What would be a fairly reliable way to check if a certain branch was created from the integration branch?

As opposed to branched from master or something?

I am not sure how to go about this.

Upvotes: 1

Views: 47

Answers (2)

Majus Misiak
Majus Misiak

Reputation: 674

Assuming that you know the names of all integration and release branches, you can find all merge bases and compare (or sort) them by timestamps.

For example, if main branches are named master and develop, you can find merge bases between feature branch and each of main branches:

branch_name="feature/feature-1"
is_feature_branch=false

git checkout $branch_name

mb_develop=$( git merge-base HEAD develop )
mb_master=$( git merge-base HEAD master)

ts_develop=$( git show -s --format=%ct $mb_develop)
ts_master=$( git show -s --format=%ct $mb_master)

if (( $ts_develop > $ts_master ))
then
    is_feature_branch=true
fi

Admittedly, this approach may be problematic, if there are many more main branches (e.g. develop, master, release/1.0, release/1.1 etc.) or you do not know the name of every main branch in repository.

Upvotes: 1

VonC
VonC

Reputation: 1323973

Even though it is not always easy to find a "branch parent", in most common case it is possible.

So one way would be to apply one of the recipe of "Find the parent branch of a Git branch".

git show-branch -a \
| grep '\*' \
| grep -v `git rev-parse --abbrev-ref HEAD` \
| head -n1 \
| sed 's/.*\[\(.*\)\].*/\1/' \
| sed 's/[\^~].*//'

Upvotes: 0

Related Questions