git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCHv4] bash completion: Support "divergence from upstream" messages in __git_ps1

From
Junio C Hamano <gitster@pobox.com>
Date
Jun 16, 2010, 19:05 UTC
Message-ID
<7v7hlyg5nh.fsf@alter.siamese.dyndns.org>
In-Reply-To
<4C17F5B3.4070907@pileofstuff.org>
Andrew Sayers <andrew-git@pileofstuff.org> writes:
Show 10 quoted lines
> Add a notification in the command prompt specifying whether (and optionally how
> far) your branch has diverged from its upstream.  This is especially helpful in
> small teams that very frequently (forget to) push to each other.
>
> Support git-svn upstream detection as a special case, as migrators from
> centralised version control systems are especially likely to forget to push.
>
> Support for other types of upstream than SVN should be easy to add if anyone is
> so inclined.
> ---
Sign-off?
Show 21 quoted lines
> diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
> index 57245a8..dabcdaa 100755
> --- a/contrib/completion/git-completion.bash
> +++ b/contrib/completion/git-completion.bash
> @@ -42,6 +42,23 @@
>  #       set GIT_PS1_SHOWUNTRACKEDFILES to a nonempty value. If there're
>  #       untracked files, then a '%' will be shown next to the branch name.
>  #
> +#       If you would like to see the difference between HEAD and its
> +#       upstream, set GIT_PS1_SHOWUPSTREAM to a nonempty value.  A "<"
> +#       indicates you are behind, ">" indicates you are ahead, and
> +#       "<>" indicates you have diverged.  You can further control
> +#       behaviour by setting GIT_PS1_SHOWUPSTREAM to a space-separated
> +#       list of values:
> +#           git           compare HEAD to @{upstream}
> +#           svn           compare HEAD to your SVN upstream
> +#           verbose       show number of commits ahead/behind (+/-) upstream
> +#           legacy        don't use the '--count' option available in recent
> +#                         versions of git-rev-list
> +#       By default, __git_ps1 will compare HEAD to your SVN upstream
> +#       if it can find one, or @{upstream} otherwise.
This feels somewhat weird.

I can sort-of read from the above that I can set the variable to a random string, e.g. "garbage", if I only want a simple show-upstream feature without frills (i.e. I don't want it to be verbose, I don't want it to restrict the comparison only to "git" upstream nor "svn" upstream, and I don't think I would ever use ancient git that lack "rev-list --count"). But the description does not assure me that the random string I happened to choose (in this case "garbage") is a safe one. Perhaps list (and implement) "default" as a safe, otherwise-no-op value?

How much overhead are we shaving if you specify "git" (without "svn") or "svn" (without "git") to the variable? I suspect that the bulk of the time is spent by reading from "git config" to look for svn-remote.*.url, which you seem to unconditionally do even when "git" was asked for anyway.

> +#       You can
> +#       override the value of GIT_PS1_SHOWUPSTREAM on a per-repository
> +#       basis by setting the bash.showUpstream config variable.
That's totally backwards from it should be, isn't it?

Usually configuration variables are used to give you the default, and you use environment variables to override them.

Show 17 quoted lines
> +# stores the divergence from upstream in $p
> +# used by GIT_PS1_SHOWUPSTREAM
> +__git_ps1_show_upstream ()
> +{
> +	local key value
> +	local svn_remote=() svn_url_pattern count n
> +	local upstream=git legacy verbose
> +
> +	# get some config options from git-config
> +	while read key value; do
> +		case "$key" in
> +			bash.showupstream)
> +				GIT_PS1_SHOWUPSTREAM="$value"
> +				if [[ -z "${GIT_PS1_SHOWUPSTREAM}" ]]; then
> +					p=""
> +					return
> +				fi
This is the "backwards" part.
Show 6 quoted lines
> +				;;
> +			svn-remote.*.url)
> +				svn_remote[ $((${#svn_remote[@]} + 1)) ]="$value"
> +				svn_url_pattern+="\\|$value"
> +				upstream=svn # default upstream is SVN if available
> +				;;

I expected that (1) when on a branch that is a fork of a svn upstream, you would use the svn magic; (2) otherwise when on a branch that is a fork of a git upstream, you would use "@{upstream}". That way, the users do not even have to say "git" or "svn" in GIT_PS1_SHOWUPSTREAM at all, no?

But that does not seem to be what is happening here. Your loop seems to force "upstream=svn" if I have one branch that is a fork from svn upstream, even if my current branch does not have anything to do with that branch nor svn upstream. Is that what was intended?

Oh, also, all of your case arms are one-indent too deep. Please write them like this:

	case foo in
        arm1)
        	stmt1
                ;;
	esac
> +		esac
> +	done < <(git config -z --get-regexp '^(svn-remote\..*\.url|bash\.showupstream)$' 2>/dev/null | tr '\0\n' '\n ')
If you "tr" to trash "\0" anyway, do you need to run "config -z"?
> +	# parse configuration values
> +	for option in ${GIT_PS1_SHOWUPSTREAM}; do

Is this safe under "set -u"? See 25a31f8 (bash-completion: Support running when set -u is enabled, 2009-01-15).

Previous: Andrew SayersNext: Thomas Rast
Message 16 of 22 in “bash completion: Support "divergence from upstream" warnings in __git_ps1”
  1. 0/2 bash completion: Support "divergence from upstream" warnings in __git_ps1Thomas Rast, Jun 12, 2010
  2. 1/2 rev-list: introduce --count optionThomas Rast, Jun 12, 2010
  3. 2/2 bash completion: Support "divergence from upstream" warnings in __git_ps1Thomas Rast, Jun 12, 2010
  4. Junio C HamanoJun 14, 2010
  5. Thomas RastJun 14, 2010
  6. SZEDER GáborJun 14, 2010
  7. vger doesn't like UTF-8 from send-emailThomas Rast, Jun 12, 2010
  8. send-email: ask about and declare 8bit mailsThomas Rast, Jun 12, 2010
  9. Junio C HamanoJun 12, 2010
  10. Thomas RastJun 13, 2010
  11. Michael WittenJun 13, 2010
  12. Erik Faye-LundJun 14, 2010
  13. bash completion: Support "divergence from upstream" messages in __git_ps1Andrew Sayers, Jun 12, 2010
  14. Thomas RastJun 14, 2010
  15. [PATCHv4] bash completion: Support "divergence from upstream" messages in __git_ps1Andrew Sayers, Jun 15, 2010
  16. Junio C HamanoJun 16, 2010
  17. Thomas RastJun 16, 2010
  18. 0/2 bash completion: Support "divergence from upstream" messages in __git_ps1Andrew Sayers, Jun 17, 2010
  19. Junio C HamanoJun 18, 2010
  20. Andrew SayersJun 18, 2010
  21. 1/2 bash completion: Support "divergence from upstream" messages in __git_ps1Andrew Sayers, Jun 17, 2010
  22. 2/2 bash-completion: Fix __git_ps1 to work with "set -u"Andrew Sayers, Jun 17, 2010

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.