Git status upstream and downstream

I can run these commands and get the expected result

$ git branch --track test
$ git checkout test
$ touch hello.txt
$ git add hello.txt
$ git commit -m hello.txt
$ git status
On branch test
Your branch is ahead of 'master' by 1 commit.

      

However, here's my problem -

$ git checkout master
$ git status
On branch master
Your branch is up-to-date with 'origin/master'.

      

I want to know if I am ahead origin/master

, but I also want to know for test

. I'm looking for a command to give me something like this:

On branch master
Your branch is up-to-date with 'origin/master'.
Your branch is behind 'test' by 1 commit, and can be fast-forwarded.

      

+3


source to share


1 answer


There is no such thing, and the tracking is only kept in the other direction: "X-tracks Y", not "Y tracked ...", which is more difficult since the "..." part can expand to more than one element. (Also, I believe it is more typical of Y to have the remote tracking branch in the first place, in which case you can never be on Y, although a command that tries to find "which tracks Y" can certainly take arguments. so you can say "tell me about any of my branches that track origin/master

).

However, it is certainly possible to build. The algorithm looks like this, in Python-esque pseudocode:

table = {}
for branch in local_branches:
    try:
        remote, tracked = get_what_branch_tracks(branch)
    except ValueError:
        continue # local branch "branch" not tracking anything
    try:
        br2 = analyze(branch, remote, tracked)
    except ValueError:
        warn('upstream for %s is gone' % branch)
        continue
    # at this point br2 is, e.g., origin/master or a local branch that "branch" tracks
    table.setdefault(br2, []).append(branch)

# now table[] is a table of branches that are tracked, with
# each table[key] being the branches that track branch "key"

      

So now, for any interesting branch (s) that is in table

, you simply count the revisions that are in different pairs of branches, just like git status

what is true in the shell:



# to compute "git status" for branch X that tracks Y:
nahead=$(git rev-list --count Y..X) # we're ahead $nahead commits
nbehind=$(git rev-list --count X..Y) # and behind $nbehind

      

If you are lagging behind but not forward, you can speed up fast forward.

The details for get_what_branch_tracks

is just git config --get

s, branch.branch.remote

and branch.branch.merge

, and the details for are analyze

more complex: if remote

there is .

, then what is in tracked

, but if it is an actual remote, then everything in tracked

must be passed through the appropriate lines fetch

to find the corresponding remote tracking branch ...

+3


source







All Articles