zsh-users
 help / color / mirror / code / Atom feed
* Tip of the day: vcs_info git: ahead/behind the upstream branch indication
@ 2016-08-23 22:29 Daniel Shahaf
  2016-08-26 19:23 ` Daniel Hahler
  0 siblings, 1 reply; 2+ messages in thread
From: Daniel Shahaf @ 2016-08-23 22:29 UTC (permalink / raw)
  To: zsh-users

The following vcs_info hook adds an indication of whether the currently
checked out branch is ahead or behind its upstream branch:

    # Add up/down arrows after branch name, if there are changes to pull/to push
    zstyle ':vcs_info:git+post-backend:*' hooks git-post-backend-updown
    +vi-git-post-backend-updown() {
      git rev-parse @{upstream} >/dev/null 2>&1 || return
      local -a x; x=( $(git rev-list --left-right --count HEAD...@{upstream} ) )
      hook_com[branch]+="%f" # end coloring
      (( x[2] )) && hook_com[branch]+="↓"
      (( x[1] )) && hook_com[branch]+="↑"
      return 0
    }

For example:

    ### Behind upstream (after 'git fetch')
     (git)-[master↓]-                                                               
    % 

    ### Ahead of upstream (before 'git push')
     (git)-[master↑]-                                                               
    % 

    ### Diverged (each of upstream and local branch has commits the other doesn't have; a rebase/force-push situation)
     (git)-[master↓↑]-                                                               
    % 


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: Tip of the day: vcs_info git: ahead/behind the upstream branch indication
  2016-08-23 22:29 Tip of the day: vcs_info git: ahead/behind the upstream branch indication Daniel Shahaf
@ 2016-08-26 19:23 ` Daniel Hahler
  0 siblings, 0 replies; 2+ messages in thread
From: Daniel Hahler @ 2016-08-26 19:23 UTC (permalink / raw)
  To: zsh-users


[-- Attachment #1.1: Type: text/plain, Size: 1562 bytes --]

On 24.08.2016 00:29, Daniel Shahaf wrote:

> The following vcs_info hook adds an indication of whether the currently
> checked out branch is ahead or behind its upstream branch:

Thanks for sharing!

I am using something more advanced, which also has numbers and notifies you in case
the revisions inbetween contain fixup/squash commits:

From https://github.com/blueyed/oh-my-zsh/blob/5228d6e/themes/blueyed.zsh-theme#L766-L789,
where it is buried in my +vi-git-st hook, which also abbreviates branch names etc:

    # Gets the commit difference counts between local and remote.
    ahead_and_behind_cmd="$_git_cmd rev-list --count --left-right HEAD...@{upstream}"
    # Get ahead and behind counts.
    ahead_and_behind="$(${(z)ahead_and_behind_cmd} 2> /dev/null)"

    ahead="$ahead_and_behind[(w)1]"
    if (( $ahead )); then
        ahead="${normtext}+${ahead}"
        # Display a warning if there are fixup/squash commits that are usually
        # meant to be interactively rebased.
        if $_git_cmd log --pretty=format:%s @{upstream}.. | \grep -Eq '^(fixup|squash)!'; then
            ahead+="${hitext}(f!)"
        fi
        gitstatus+=($ahead)
    fi

    behind="$ahead_and_behind[(w)2]"
    if (( $behind )); then
        # Display hint for fixup/squash commits, but in normal text.
        if $_git_cmd log --pretty=format:%s ..@{upstream} | \grep -Eq '^(fixup|squash)!'; then
            behind+="${dimmedtext}(f)"
        fi
        gitstatus+=( "${alerttext}-${behind}" )
    fi


Cheers,
Daniel.


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 163 bytes --]

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2016-08-26 19:31 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-08-23 22:29 Tip of the day: vcs_info git: ahead/behind the upstream branch indication Daniel Shahaf
2016-08-26 19:23 ` Daniel Hahler

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).