Skip to content

Instantly share code, notes, and snippets.

@Mark-Booth
Forked from lth2h/git-branch-status
Last active October 21, 2024 13:04

Revisions

  1. Mark-Booth revised this gist Jan 13, 2016. No changes.
  2. Mark-Booth revised this gist Jan 13, 2016. 1 changed file with 13 additions and 7 deletions.
    20 changes: 13 additions & 7 deletions git-branch-status
    Original file line number Diff line number Diff line change
    @@ -14,25 +14,31 @@
    # $ git branch-status
    # master (ahead 2) | (behind 0) origin/master

    usage="$(basename "$0") [-hamv] -- Summarise status of branch(es)
    OPTIONS='hamvb:s:lru:'
    usage="$(basename "$0") ["$OPTIONS"] -- Summarise status of branch(es)
    Where:
    -h shows this help text
    -a shows all branches, not just the current one
    -a shows all local branches, not just the current one
    -m shows branch(es) with respect to origin/master
    -v verbose, show output even if counts are zero
    -b BRANCH shows branch(es) with respect to a given branch
    -s FROM/TO shows branch(es) with respect to a branch substitution
    Note: Any branches which don't match FROM will be ignored
    -l shows only when the left side is ahead
    -r shows only when the left side is behind (right is ahead)
    -l shows only when the left/local side is ahead (push needed)
    -r shows only when the right/remote side is ahead (pull needed)
    -u UPSTREAM selects an upstream other than origin for -b and -m options
    For example
    $(basename "$0") -am
    $(basename "$0") -s 8.34/8.36"
    $(basename "$0") -am # Show all branches which are ahead
    # of or behind their origin/master.
    $(basename "$0") -b 8.38-hotfix # Show each repo whose checked out
    # branch is behind the hotfix branch.
    $(basename "$0") -s 8.34/8.36 # Show all 8.34 branches which are
    # ahead of or behind their 8.38 remote.
    "

    while getopts 'hamvb:s:lru:' option; do
    while getopts $OPTIONS option; do
    case "$option" in
    h) echo "$usage"
    exit
  3. Mark-Booth revised this gist Jan 10, 2014. 1 changed file with 59 additions and 19 deletions.
    78 changes: 59 additions & 19 deletions git-branch-status
    Original file line number Diff line number Diff line change
    @@ -14,48 +14,88 @@
    # $ git branch-status
    # master (ahead 2) | (behind 0) origin/master

    usage="$(basename "$0") [-hav] -- summarise status of branch(es)
    usage="$(basename "$0") [-hamv] -- Summarise status of branch(es)
    where:
    -h show this help text
    -a show all branches, not just the current one
    -m shows branch(es) with respect to origin/master
    -v verbose, show output even if counts are zero"
    Where:
    -h shows this help text
    -a shows all branches, not just the current one
    -m shows branch(es) with respect to origin/master
    -v verbose, show output even if counts are zero
    -b BRANCH shows branch(es) with respect to a given branch
    -s FROM/TO shows branch(es) with respect to a branch substitution
    Note: Any branches which don't match FROM will be ignored
    -l shows only when the left side is ahead
    -r shows only when the left side is behind (right is ahead)
    -u UPSTREAM selects an upstream other than origin for -b and -m options
    while getopts 'hamv' option; do
    For example
    $(basename "$0") -am
    $(basename "$0") -s 8.34/8.36"

    while getopts 'hamvb:s:lru:' option; do
    case "$option" in
    h) echo "$usage"
    exit
    ;;
    a) filter=refs/heads
    ;;
    m) originmaster=true
    m) upstreambranch=master
    ;;
    v) verbose=true
    ;;
    ?) printf "illegal option: '%s'\n" "$OPTARG" >&2
    b) upstreambranch=$OPTARG
    ;;
    s) remotesubstitute=$OPTARG
    ;;
    l) aheadonly=true
    ;;
    r) behindonly=true
    ;;
    u) upstream=$OPTARG
    ;;
    :) echo "Option -$OPTARG requires an argument." >&2
    echo "$usage" >&2
    exit 1
    ;;
    ?) printf "Invalid option: -$OPTARG" >&2
    echo "$usage" >&2
    exit 1
    ;;
    esac
    done
    shift $((OPTIND - 1))

    if [ -z $filter ] ; then
    filter=$(git symbolic-ref -q HEAD)
    fi
    [ $filter ] || filter=$(git symbolic-ref -q HEAD)
    [ $upstream ] || upstream=origin

    git for-each-ref --format="%(refname:short) %(upstream:short)" $filter | \
    while read local remote
    do
    if [ $originmaster ] ; then
    remote=origin/master
    if [ $remotesubstitute ] ; then
    remote=$(echo $remote | sed "s/${remotesubstitute}/")
    elif [ $upstreambranch ] ; then
    remote=$upstream/$upstreambranch
    fi
    [ -z "$remote" ] && continue
    DELTAS=$(git rev-list --left-right ${local}...${remote})
    [ "$remote" ] || continue
    [ "$(git ls-remote . $remote)" ] || continue
    DELTAS=$(git rev-list --left-right ${local}...${remote} --)
    LEFT_AHEAD=$(echo "$DELTAS" | grep -c '^<')
    RIGHT_AHEAD=$(echo "$DELTAS" | grep -c '^>')
    if [ $verbose ] || [ $LEFT_AHEAD -gt 0 ] || [ $RIGHT_AHEAD -gt 0 ] ; then
    echo "$local (ahead $LEFT_AHEAD) | (behind $RIGHT_AHEAD) $remote"
    if [ -z $aheadonly ] && [ -z $behindonly ] ; then
    if [ $LEFT_AHEAD -gt 0 ] || [ $RIGHT_AHEAD -gt 0 ] || [ $verbose ] ; then
    echo "$local (ahead $LEFT_AHEAD) | (behind $RIGHT_AHEAD) $remote"
    fi
    elif [ $aheadonly ] ; then
    if [ $LEFT_AHEAD -gt 0 ] || [ $verbose ] ; then
    echo "$local (ahead $LEFT_AHEAD) | $remote"
    fi
    elif [ $behindonly ] ; then
    if [ $RIGHT_AHEAD -gt 0 ] || [ $verbose ] ; then
    echo "$local | (behind $RIGHT_AHEAD) $remote"
    fi
    else
    printf "Specifying both -l and -r makes no sense" >&2
    echo "$usage" >&2
    exit 1
    fi
    done
    done
  4. Mark-Booth revised this gist Mar 1, 2013. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion git-branch-status
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,8 @@
    #!/bin/bash
    # forked from https://gist.github.com/jehiah/1288596 @ e357c1e by lth2h
    # hosted at https://gist.github.com/Mark-Booth/5058384
    # forked from https://gist.github.com/lth2h/4177524 @ ae184f1 by mark.booth
    # forked from https://gist.github.com/jehiah/1288596 @ e357c1e by lth2h
    # ideas from https://github.com/kortina/bakpak/blob/master/bin/git-branches-vs-origin-master

    # this prints out some branch status
    # (similar to the '... ahead' info you get from git status)
  5. Mark-Booth revised this gist Mar 1, 2013. 1 changed file with 7 additions and 3 deletions.
    10 changes: 7 additions & 3 deletions git-branch-status
    Original file line number Diff line number Diff line change
    @@ -17,15 +17,18 @@ usage="$(basename "$0") [-hav] -- summarise status of branch(es)
    where:
    -h show this help text
    -a show all branches, not just the current one
    -m shows branch(es) with respect to origin/master
    -v verbose, show output even if counts are zero"

    while getopts 'hav' option; do
    while getopts 'hamv' option; do
    case "$option" in
    h) echo "$usage"
    exit
    ;;
    a) filter=refs/heads
    ;;
    m) originmaster=true
    ;;
    v) verbose=true
    ;;
    ?) printf "illegal option: '%s'\n" "$OPTARG" >&2
    @@ -40,11 +43,12 @@ if [ -z $filter ] ; then
    filter=$(git symbolic-ref -q HEAD)
    fi

    #echo $# : $* : $filter : $quiet

    git for-each-ref --format="%(refname:short) %(upstream:short)" $filter | \
    while read local remote
    do
    if [ $originmaster ] ; then
    remote=origin/master
    fi
    [ -z "$remote" ] && continue
    DELTAS=$(git rev-list --left-right ${local}...${remote})
    LEFT_AHEAD=$(echo "$DELTAS" | grep -c '^<')
  6. Mark-Booth revised this gist Feb 28, 2013. 1 changed file with 42 additions and 5 deletions.
    47 changes: 42 additions & 5 deletions git-branch-status
    Original file line number Diff line number Diff line change
    @@ -1,18 +1,55 @@
    #!/bin/bash
    # forked from http://github.com/jehiah
    # this prints out some branch status (similar to the '... ahead' info you get from git status)
    # forked from https://gist.github.com/jehiah/1288596 @ e357c1e by lth2h
    # forked from https://gist.github.com/lth2h/4177524 @ ae184f1 by mark.booth

    # this prints out some branch status
    # (similar to the '... ahead' info you get from git status)

    # example:
    # $ git branch-status
    # $ git branch-status -a
    # dns_check (ahead 1) | (behind 112) origin/master
    # master (ahead 2) | (behind 0) origin/master
    # $ git branch-status
    # master (ahead 2) | (behind 0) origin/master

    usage="$(basename "$0") [-hav] -- summarise status of branch(es)
    where:
    -h show this help text
    -a show all branches, not just the current one
    -v verbose, show output even if counts are zero"

    while getopts 'hav' option; do
    case "$option" in
    h) echo "$usage"
    exit
    ;;
    a) filter=refs/heads
    ;;
    v) verbose=true
    ;;
    ?) printf "illegal option: '%s'\n" "$OPTARG" >&2
    echo "$usage" >&2
    exit 1
    ;;
    esac
    done
    shift $((OPTIND - 1))

    if [ -z $filter ] ; then
    filter=$(git symbolic-ref -q HEAD)
    fi

    #echo $# : $* : $filter : $quiet

    git for-each-ref --format="%(refname:short) %(upstream:short)" refs/heads | \
    git for-each-ref --format="%(refname:short) %(upstream:short)" $filter | \
    while read local remote
    do
    [ -z "$remote" ] && continue
    DELTAS=$(git rev-list --left-right ${local}...${remote})
    LEFT_AHEAD=$(echo "$DELTAS" | grep -c '^<')
    RIGHT_AHEAD=$(echo "$DELTAS" | grep -c '^>')
    echo "$local (ahead $LEFT_AHEAD) | (behind $RIGHT_AHEAD) $remote"
    if [ $verbose ] || [ $LEFT_AHEAD -gt 0 ] || [ $RIGHT_AHEAD -gt 0 ] ; then
    echo "$local (ahead $LEFT_AHEAD) | (behind $RIGHT_AHEAD) $remote"
    fi
    done
  7. @lth2h lth2h revised this gist Nov 30, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion git-branch-status
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    #!/bin/bash
    # forked from by http://github.com/jehiah
    # forked from http://github.com/jehiah
    # this prints out some branch status (similar to the '... ahead' info you get from git status)

    # example:
  8. @lth2h lth2h revised this gist Nov 30, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion git-branch-status
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    #!/bin/bash
    # by http://github.com/jehiah
    # forked from by http://github.com/jehiah
    # this prints out some branch status (similar to the '... ahead' info you get from git status)

    # example:
  9. @lth2h lth2h revised this gist Nov 30, 2012. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions git-branch-status
    Original file line number Diff line number Diff line change
    @@ -11,8 +11,8 @@ git for-each-ref --format="%(refname:short) %(upstream:short)" refs/heads | \
    while read local remote
    do
    [ -z "$remote" ] && continue
    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)
    DELTAS=$(git rev-list --left-right ${local}...${remote})
    LEFT_AHEAD=$(echo "$DELTAS" | grep -c '^<')
    RIGHT_AHEAD=$(echo "$DELTAS" | grep -c '^>')
    echo "$local (ahead $LEFT_AHEAD) | (behind $RIGHT_AHEAD) $remote"
    done
    done
  10. @jehiah jehiah created this gist Oct 14, 2011.
    18 changes: 18 additions & 0 deletions git-branch-status
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    #!/bin/bash
    # by http://github.com/jehiah
    # this prints out some branch status (similar to the '... ahead' info you get from git status)

    # example:
    # $ git branch-status
    # dns_check (ahead 1) | (behind 112) origin/master
    # master (ahead 2) | (behind 0) origin/master

    git for-each-ref --format="%(refname:short) %(upstream:short)" refs/heads | \
    while read local remote
    do
    [ -z "$remote" ] && continue
    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)
    echo "$local (ahead $LEFT_AHEAD) | (behind $RIGHT_AHEAD) $remote"
    done