git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
* [PATCH] completion: resolve svn remote upstream refs
@ 2012-08-02  8:20 Yves Blusseau
  0 siblings, 0 replies; 3+ messages in thread
From: Yves Blusseau @ 2012-08-02  8:20 UTC (permalink / raw
  To: git

This fix is used to return the svn reference of the remote svn upstream
branch when the git repository is a clone of a svn repository that was
created with the --stdlayout and --prefix options of git svn command.

* completion/git-prompt.sh: add function to resolve svn branches
   into git remote refs using paths declared in svn-remote.*.branches
   configurations

Signed-off-by: Yves Blusseau <blusseau@zetam.org>
---
  contrib/completion/git-prompt.sh | 48 
+++++++++++++++++++++++++++++++++++++++-
  1 file changed, 47 insertions(+), 1 deletion(-)

diff --git a/contrib/completion/git-prompt.sh 
b/contrib/completion/git-prompt.sh
index 29b1ec9..dafcecc 100644
--- a/contrib/completion/git-prompt.sh
+++ b/contrib/completion/git-prompt.sh
@@ -73,17 +73,57 @@ __gitdir ()
  	fi
  }
  +# resolve svn refs
+# The function accepts 2 arguments:
+# 1: An array containing the translation ref(s) (ie 
branches/*:refs/remotes/svn/*)
+# 2: the ref to be translated (ie. branches/prod)
+# returns the remote refs or original ref if it could not be translated
+__git_resolve_svn_refs ()
+{
+	local idx ref_globs left_part_ref_glob right_part_ref_glob left right 
value
+
+	local all_ref_globs=("${!1}")
+	local ref="$2"
+
+	for (( idx=0 ; idx < ${#all_ref_globs[@]}; idx++ ));do
+		ref_globs="${all_ref_globs[$idx]}"
+		# get the left part ref glob (before the :refs/)
+		left_part_ref_glob="${ref_globs%%:refs/*}"
+		case $ref in
+			# check if the ref match the glob
+			$left_part_ref_glob)
+				# extract the value that match the pattern
+				left=${left_part_ref_glob%%\**}
+				right=${left_part_ref_glob##*\*}
+				value=${ref#$left}
+				value=${value%$right}
+				# get the right part ref glob (after the :)
+				right_part_ref_glob="${ref_globs/${left_part_ref_glob}:/}"
+				# replace the pattern with the value found above
+				left=${right_part_ref_glob%%\**}
+				right=${right_part_ref_glob##*\*}
+				ref=${left}${value}${right}
+				# remove leading refs/remotes/
+				ref=${ref/refs\/remotes\//}
+				break
+				;;
+		esac
+	done
+	echo "$ref"
+}
+
  # 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 branches_refs_globs=()
  	local upstream=git legacy="" verbose=""
   	svn_remote=()
  	# get some config options from git-config
-	local output="$(git config -z --get-regexp 
'^(svn-remote\..*\.url|bash\.showupstream)$' 2>/dev/null | tr '\0\n' '\n ')"
+	local output="$(git config -z --get-regexp 
'^(svn-remote\..*\.(branches|url)|bash\.showupstream)$' 2>/dev/null | tr 
'\0\n' '\n ')"
  	while read -r key value; do
  		case "$key" in
  		bash.showupstream)
@@ -93,6 +133,9 @@ __git_ps1_show_upstream ()
  				return
  			fi
  			;;
+		svn-remote.*.branches)
+			branches_refs_globs[${#branches_refs_globs[*]}]="$value"
+			;;
  		svn-remote.*.url)
  			svn_remote[ $((${#svn_remote[@]} + 1)) ]="$value"
  			svn_url_pattern+="\\|$value"
@@ -132,6 +175,9 @@ __git_ps1_show_upstream ()
  			else
  				upstream=${svn_upstream#/}
  			fi
+			if [[ ${#branches_refs_globs[@]} -gt 0 ]]; then
+				upstream=$(__git_resolve_svn_refs branches_refs_globs[@] "$upstream")
+			fi
  		elif [[ "svn+git" = "$upstream" ]]; then
  			upstream="@{upstream}"
  		fi
-- 
1.7.12.rc1

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

* [PATCH] completion: resolve svn remote upstream refs
@ 2012-08-03  9:54 Yves Blusseau
  2012-08-03 20:40 ` Junio C Hamano
  0 siblings, 1 reply; 3+ messages in thread
From: Yves Blusseau @ 2012-08-03  9:54 UTC (permalink / raw
  To: git

Updated version of my previous patch

This fix is used to return the svn reference of the remote svn upstream
branch when the git repository is a clone of a svn repository that was
created with the --stdlayout and --prefix options of git svn command.

* completion/git-prompt.sh: add function to resolve svn branches into
   git remote refs using paths declared in svn-remote.*.fetch and
   svn-remote.*.branches configurations

Signed-off-by: Yves Blusseau <blusseau@zetam.org>
---
  contrib/completion/git-prompt.sh | 56 
+++++++++++++++++++++++++++++++++++++++-
  1 file changed, 55 insertions(+), 1 deletion(-)

diff --git a/contrib/completion/git-prompt.sh 
b/contrib/completion/git-prompt.sh
index 29b1ec9..f9a3421 100644
--- a/contrib/completion/git-prompt.sh
+++ b/contrib/completion/git-prompt.sh
@@ -73,17 +73,65 @@ __gitdir ()
  	fi
  }
  +# resolve svn refs
+# The function accepts 2 arguments:
+# 1: An array containing the translation ref(s) (ie 
branches/*:refs/remotes/svn/*)
+# 2: the ref to be translated (ie. branches/prod)
+# returns the remote refs or original ref if it could not be translated
+__git_resolve_svn_refs ()
+{
+	local idx ref_globs left_part_ref_glob right_part_ref_glob left right 
value
+
+	local all_ref_globs=("${!1}")
+	local ref="$2"
+
+	for (( idx=0 ; idx < ${#all_ref_globs[@]}; idx++ ));do
+		ref_globs="${all_ref_globs[$idx]}"
+		# get the left part ref glob (before the :refs/)
+		left_part_ref_glob="${ref_globs%%:refs/*}"
+
+		# If the ref match the left part we can resolve the ref directly
+		if [[ "$ref" == "$left_part_ref_glob" ]];then
+			# resolve the ref into the right part (after the :)
+			ref="${ref_globs/${left_part_ref_glob}:/}"
+			break
+		else
+			case $ref in
+				# check if the ref match the glob
+				$left_part_ref_glob)
+					# extract the value that match the pattern
+					left=${left_part_ref_glob%%\**}
+					right=${left_part_ref_glob##*\*}
+					value=${ref#$left}
+					value=${value%$right}
+					# get the right part ref glob (after the :)
+					right_part_ref_glob="${ref_globs/${left_part_ref_glob}:/}"
+					# replace the pattern with the value found above
+					left=${right_part_ref_glob%%\**}
+					right=${right_part_ref_glob##*\*}
+					ref=${left}${value}${right}
+					break
+					;;
+			esac
+		fi
+	done
+	# remove leading refs/remotes/
+	ref=${ref/refs\/remotes\//}
+	echo "$ref"
+}
+
  # 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 svn_refs_globs=()
  	local upstream=git legacy="" verbose=""
   	svn_remote=()
  	# get some config options from git-config
-	local output="$(git config -z --get-regexp 
'^(svn-remote\..*\.url|bash\.showupstream)$' 2>/dev/null | tr '\0\n' '\n ')"
+	local output="$(git config -z --get-regexp 
'^(svn-remote\..*\.(fetch|branches|url)|bash\.showupstream)$' 
2>/dev/null | tr '\0\n' '\n ')"
  	while read -r key value; do
  		case "$key" in
  		bash.showupstream)
@@ -93,6 +141,9 @@ __git_ps1_show_upstream ()
  				return
  			fi
  			;;
+		svn-remote.*.fetch|svn-remote.*.branches)
+			svn_refs_globs[${#svn_refs_globs[*]}]="$value"
+			;;
  		svn-remote.*.url)
  			svn_remote[ $((${#svn_remote[@]} + 1)) ]="$value"
  			svn_url_pattern+="\\|$value"
@@ -132,6 +183,9 @@ __git_ps1_show_upstream ()
  			else
  				upstream=${svn_upstream#/}
  			fi
+			if [[ ${#svn_refs_globs[@]} -gt 0 ]]; then
+				upstream=$(__git_resolve_svn_refs svn_refs_globs[@] "$upstream")
+			fi
  		elif [[ "svn+git" = "$upstream" ]]; then
  			upstream="@{upstream}"
  		fi
-- 
1.7.12.rc1.17.g6cf3663

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

* Re: [PATCH] completion: resolve svn remote upstream refs
  2012-08-03  9:54 [PATCH] completion: resolve svn remote upstream refs Yves Blusseau
@ 2012-08-03 20:40 ` Junio C Hamano
  0 siblings, 0 replies; 3+ messages in thread
From: Junio C Hamano @ 2012-08-03 20:40 UTC (permalink / raw
  To: Yves Blusseau
  Cc: git, Felipe Contreras, SZEDER Gábor, Eric Wong,
	Michael G. Schwern

Administrivia: 

 - "Content-Type: text/plain; charset=ISO-8859-1; format=flowed" in
   the header tells us that the patch is corrupted and will not
   apply cleanly, but we still can discuss the contents of the
   patch.

 - "git shortlog --since=6.months -n --no-merges contrib/completion"
   tells us who the area experts who may be able to help you move
   this patch forward.  Also people who are involved in git-svn may
   be able to help with the configurations you are reading from.  By
   Cc'ing them, your patch may have a better chance of getting
   polished more quickly (I added Cc to relevant people).

Yves Blusseau <blusseau@zetam.org> writes:

> Updated version of my previous patch

Everything before the "---" line we see below will be for people who
read "git log" history 6 months down the road, when they have to
understand what you did and why (perhaps because they found bugs or
they want to further enhance what your patch did).

Because your previous patch will not appear in that "git log"
output, the above is not an appropriate thing to say here.

> This fix is used to return the svn reference of the remote svn upstream
> branch when the git repository is a clone of a svn repository that was
> created with the --stdlayout and --prefix options of git svn command.

Before using the word "fix", please describe what the current
(i.e. without the "fix") behaviour visible to the users, why it is
bad, and what behaviour you want to give your users instead.  And
then describe what change you made to give that desired behaviour.

> * completion/git-prompt.sh: add function to resolve svn branches into
>   git remote refs using paths declared in svn-remote.*.fetch and
>   svn-remote.*.branches configurations
>
> Signed-off-by: Yves Blusseau <blusseau@zetam.org>
> ---

Here, immediately after "---", is where you would say "Updated
version of my patch.  The difference from the previous round are
1. fixed foo, 2. fixed bar, 3. reworded baz...." if you want to.

>  contrib/completion/git-prompt.sh | 56
> +++++++++++++++++++++++++++++++++++++++-

Line-wrapped (comes from "format=flawed" we saw in the Content-Type
header).

>  1 file changed, 55 insertions(+), 1 deletion(-)
>
> diff --git a/contrib/completion/git-prompt.sh
> b/contrib/completion/git-prompt.sh
> index 29b1ec9..f9a3421 100644
> --- a/contrib/completion/git-prompt.sh
> +++ b/contrib/completion/git-prompt.sh
> @@ -73,17 +73,65 @@ __gitdir ()
>  	fi
>  }
>  +# resolve svn refs
> +# The function accepts 2 arguments:
> +# 1: An array containing the translation ref(s) (ie
> branches/*:refs/remotes/svn/*)
> +# 2: the ref to be translated (ie. branches/prod)
> +# returns the remote refs or original ref if it could not be translated

We see "translation ref(s)", which is an unheard-of term, and it is
unclear what is translated and into what other thing.  Is this the
moral equivalent of the ref mapping that maps refs at the remote
repository to remote tracking refs we have at our local repository?

If the first item of above were written like this:

    # 1: An array containing the mapping from branches in the subversion
    #  repository to remote tracking branches in this repository (e.g
    #  "branches/*:refs/remotes/svn/*").

I wouldn't have had to ask the above question, but it is unclear if
that is what you meant, hence this question.

> +__git_resolve_svn_refs ()
> +{
> +	local idx ref_globs left_part_ref_glob right_part_ref_glob
> left right value
> +
> +	local all_ref_globs=("${!1}")
> +	local ref="$2"

Bash/Zsh reusability czars might want to comment on the way
all_ref_globs is declared and initialized.

> +	for (( idx=0 ; idx < ${#all_ref_globs[@]}; idx++ ));do
> +		ref_globs="${all_ref_globs[$idx]}"
> +		# get the left part ref glob (before the :refs/)
> +		left_part_ref_glob="${ref_globs%%:refs/*}"

Is doubling of %% significant here?  I am assuming there isn't, as
there would be no colon in $ref_globs other than the boundary
between LHS and RHS.

> +		# If the ref match the left part we can resolve the ref directly
> +		if [[ "$ref" == "$left_part_ref_glob" ]];then

The coding standard used in this file is kept deliberately lax
compared to the remainder of the codebase, but I think the lack of
SP between ";" and "then" goes a bit too far.

> +			# resolve the ref into the right part (after the :)
> +			ref="${ref_globs/${left_part_ref_glob}:/}"
> +			break
> +		else
> +			case $ref in
> +				# check if the ref match the glob
> +				$left_part_ref_glob)

case/esac and case arms should align at the same column (otherwise
the body will be indented unreadably too deep to the right, like
this code).

>  # 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 svn_refs_globs=()
>  	local upstream=git legacy="" verbose=""
>   	svn_remote=()

Doesn't this have the same problem as the one fixed by 471dcfd
(contrib/completion: "local var=()" is misinterpreted as func-decl
by zsh, 2011-09-01)?

Thanks.

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

end of thread, other threads:[~2012-08-03 20:40 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-08-03  9:54 [PATCH] completion: resolve svn remote upstream refs Yves Blusseau
2012-08-03 20:40 ` Junio C Hamano
  -- strict thread matches above, loose matches on Subject: below --
2012-08-02  8:20 Yves Blusseau

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

	https://80x24.org/mirrors/git.git

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 read-only IMAP folder(s) and NNTP newsgroup(s).