-
-
Save intel352/9761288 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env zsh | |
branch=`git rev-parse --abbrev-ref HEAD` | |
git show-branch | ack '\*' | ack -v "$branch" | head -n1 | sed 's/.*\[\(.*\)\].*/\1/' | sed 's/[\^~].*//' | |
# How it works: | |
# 1| Display a textual history of all commits. | |
# 2| Ancestors of the current commit are indicated | |
# by a star. Filter out everything else. | |
# 3| Ignore all the commits in the current branch. | |
# 4| The first result will be the nearest ancestor branch. | |
# Ignore the other results. | |
# 5| Branch names are displayed [in brackets]. Ignore | |
# everything outside the brackets, and the brackets. | |
# 6| Sometimes the branch name will include a ~2 or ^1 to | |
# indicate how many commits are between the referenced | |
# commit and the branch tip. We don't care. Ignore them. |
vbc=$(git rev-parse --abbrev-ref HEAD)
vbc_col=$(( $(git show-branch | grep '^[^\[]*\*' | head -1 | cut -d* -f1 | wc -c) - 1 ))
swimming_lane_start_row=$(( $(git show-branch | grep -n "^[\-]*$" | cut -d: -f1) + 1 ))
git show-branch | tail -n +$swimming_lane_start_row | grep -v "^[^\[]*\[$vbc" | grep "^.\{$vbc_col\}[^ ]" | head -n1 | sed 's/.*\[\(.*\)\].*/\1/' | sed 's/[\^~].*//'
Achieves the same ends, but uses a much safer approach that doesn't misbehave in a number of scenarios:
- Parent branch's last commit is a merge, making the column show
-
not*
- Commit message contains branch name
- Commit message contains
*
Obviously you can combine this into a single line if you wish.
Doesn't latest git support showing the parent branch in a native way?
Doesn't latest git support showing the parent branch in a native way?
found the following to work for me (limited testing)
git branch --create-reflog | head -n 1
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@jpbochi: It's a nice idea to make commands like the
cb
andpb
aliases. I've gone a bit further, and:git
andawk
(the command(s) should work withmawk
, as well asgawk
):git show-closest-parent-branch
command return a non-zero exit code when the branch name could not be computed;Sample runs: