Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding branch-status script #419

Merged
merged 1 commit into from
Aug 15, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions bin/git-branch-status
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#!/bin/bash
#
# Shows relationship between branch and upstream.
#
# Configure with parent branch for local tracking, e.g.:
# git config branch.$branch.parent master
#
# Configure with description about what branch is for, e.g.:
# git config branch.$branch.description "Dependence changes"
#

git for-each-ref --format="%(refname:short) %(upstream:short)" refs/heads | \
while read local remote
do
if [ -n "$remote" ]; then
git rev-list --left-right ${local}...${remote} -- 2>/dev/null >/tmp/git_upstream_status_delta || continue
LEFT_AHEAD=$(grep -c '^<' /tmp/git_upstream_status_delta)
RIGHT_AHEAD=$(grep -c '^>' /tmp/git_upstream_status_delta)
if [ "$LEFT_AHEAD" != 0 -a "$RIGHT_AHEAD" != 0 ]; then
OFFSET="disjoint $LEFT_AHEAD/$RIGHT_AHEAD"
elif [ "$LEFT_AHEAD" != 0 ]; then
OFFSET="ahead $LEFT_AHEAD"
elif [ "$RIGHT_AHEAD" != 0 ]; then
OFFSET="behind $RIGHT_AHEAD"
else
OFFSET="insync"
fi
else
OFFSET="local"
fi

parent=$(git config branch.$local.parent)
if [[ -z $parent && $local != master ]]; then
parent=master
fi

if [ -n "$parent" ]; then
LEFT_PARENT=$(git rev-list --left-only --count $local...$parent)
RIGHT_PARENT=$(git rev-list --right-only --count $local...$parent)
if [ "$LEFT_PARENT" != 0 -a "$RIGHT_PARENT" != 0 ]; then
POFF="; disjoint $parent $LEFT_PARENT/$RIGHT_PARENT"
elif [ "$LEFT_PARENT" != 0 ]; then
POFF="; ahead $parent $LEFT_PARENT"
elif [ "$RIGHT_PARENT" != 0 ]; then
POFF="; behind $parent $RIGHT_PARENT"
else
POFF="; insync $parent"
fi
else
POFF=
fi

description=$(git config branch.$local.description)
if [[ -z $description && $local != master ]]; then
description="git config branch.$local.description XXX"
fi

printf "%-15s%-30s%s\n" $local "$OFFSET$POFF" "$description"
done