Let\'s say I have the following local repository with a commit tree like this:
master --> a
\\
\\
develop c --> d
JoeChrysler's command-line magic can be simplified. Here's Joe's logic - for brevity I've introduced a parameter named cur_branch
in place of the command substitution `git rev-parse --abbrev-ref HEAD`
into both versions; that can be initialized like so:
cur_branch=$(git rev-parse --abbrev-ref HEAD)
Then, here's Joe's pipeline:
git show-branch -a |
grep '\*' | # we want only lines that contain an asterisk
grep -v "$cur_branch" | # but also don't contain the current branch
head -n1 | # and only the first such line
sed 's/.*\[\(.*\)\].*/\1/' | # really, just the part of the line between []
sed 's/[\^~].*//' # and with any relative refs (^, ~n) removed
We can accomplish the same thing as all five of those individual command filters in a relatively simple awk
command:
git show-branch -a |
awk -F'[]^~[]' '/\*/ && !/'"$cur_branch"'/ {print $2;exit}'
That breaks down like this:
-F'[]^~[]'
split the line into fields at ]
, ^
, ~
, and [
characters.
/\*/
Find lines that contain an asterisk
&& !/'"$cur_branch"'/
...but not the current branch name
{ print $2;
When you find such a line, print its second field (that is, the part between the first and second occurrences of our field separator characters). For simple branch names, that will be just what's between the brackets; for refs with relative jumps, it will be just the name without the modifier. So our set of field separators handles the intent of both sed
commands.
exit }
Then exit immediately. This means it only ever processes the first matching line, so we don't need to pipe the output through head -n 1
.