threads / patch / 28499

patchgit-remote rename should match whole string when renaming remote ref directory

Subject: [PATCH] git-remote rename should match whole string when renaming remote ref directory

## tl;dr

5 messages between Sep 26, 2011 and Sep 27, 2011. Diffs are folded; open one to read it.

replies: 4people: 3as markdown or json

Benny Halevy· Sep 26, 2011, 13:53 UTC · lore
From: Benny Halevy <bhalevy@tonian.com>
Otherwise, with two remotes: test, test-2
	git remote rename test test-
ends up with:
	.git/refs/remotes/test-
	.git/refs/remotes/test--2
Signed-off-by: Benny Halevy <bhalevy@tonian.com>
---

By the way, I also see the old names as empty directories in .git/refs/remotes not sure if that's a bug or a feature... :)

Benny
 builtin/remote.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
Show changes to builtin/remote.c +1 −1
diff --git a/builtin/remote.c b/builtin/remote.c
index f2a9c26..5443e71 100644
--- a/builtin/remote.c
+++ b/builtin/remote.c
@@ -571,7 +571,7 @@ static int read_remote_branches(const char *refname,
 	const char *symref;
 
 	strbuf_addf(&buf, "refs/remotes/%s", rename->old);
-	if (!prefixcmp(refname, buf.buf)) {
+	if (!strcmp(refname, buf.buf)) {
 		item = string_list_append(rename->remote_branches, xstrdup(refname));
 		symref = resolve_ref(refname, orig_sha1, 1, &flag);
 		if (flag & REF_ISSYMREF)
-- 
1.7.7.rc3.dirty
Junio C Hamano· Sep 26, 2011, 18:04 UTC · re: Benny Halevy · lore

Re: [PATCH] git-remote rename should match whole string when renaming remote ref directory

Benny Halevy <benny@tonian.com> writes:
Show 18 quoted lines
> From: Benny Halevy <bhalevy@tonian.com>
>
> Otherwise, with two remotes: test, test-2
> 	git remote rename test test-
> ends up with:
> 	.git/refs/remotes/test-
> 	.git/refs/remotes/test--2
> ...
> diff --git a/builtin/remote.c b/builtin/remote.c
> index f2a9c26..5443e71 100644
> --- a/builtin/remote.c
> +++ b/builtin/remote.c
> @@ -571,7 +571,7 @@ static int read_remote_branches(const char *refname,
>  	const char *symref;
>  
>  	strbuf_addf(&buf, "refs/remotes/%s", rename->old);
> -	if (!prefixcmp(refname, buf.buf)) {
> +	if (!strcmp(refname, buf.buf)) {

At this point of the code, refname has "refs/remotes/test/foo" and it is queued to later rename it to "refs/remotes/test-/foo" (the next invocation of this function will see "refs/remotes/test/bar" in refname). And the strbuf buf.buf has "refs/remotes/test"; your !strcmp(refname, buf.buf) would never trigger, I suspect.

Isn't 60e5eee (remote: "rename o foo" should not rename ref "origin/bar", 2011-09-01) the correct fix for this issue? It makes buf.buf properly terminated with a slash, to contain "refs/remotes/test/" so that prefixcmp properly matches it with "refs/remotes/test/foo" but not with refs under other hierarchies like "refs/remotes/test-2/anything".

Thanks.
Benny Halevy· Sep 27, 2011, 02:45 UTC · re: Junio C Hamano · lore

Re: [PATCH] git-remote rename should match whole string when renaming remote ref directory

On 2011-09-26 21:04, Junio C Hamano wrote:
Show 32 quoted lines
> Benny Halevy <benny@tonian.com> writes:
> 
>> From: Benny Halevy <bhalevy@tonian.com>
>>
>> Otherwise, with two remotes: test, test-2
>> 	git remote rename test test-
>> ends up with:
>> 	.git/refs/remotes/test-
>> 	.git/refs/remotes/test--2
>> ...
>> diff --git a/builtin/remote.c b/builtin/remote.c
>> index f2a9c26..5443e71 100644
>> --- a/builtin/remote.c
>> +++ b/builtin/remote.c
>> @@ -571,7 +571,7 @@ static int read_remote_branches(const char *refname,
>>  	const char *symref;
>>  
>>  	strbuf_addf(&buf, "refs/remotes/%s", rename->old);
>> -	if (!prefixcmp(refname, buf.buf)) {
>> +	if (!strcmp(refname, buf.buf)) {
> 
> At this point of the code, refname has "refs/remotes/test/foo" and it is
> queued to later rename it to "refs/remotes/test-/foo" (the next invocation
> of this function will see "refs/remotes/test/bar" in refname). And the
> strbuf buf.buf has "refs/remotes/test"; your !strcmp(refname, buf.buf)
> would never trigger, I suspect.
> 
> Isn't 60e5eee (remote: "rename o foo" should not rename ref "origin/bar",
> 2011-09-01) the correct fix for this issue?  It makes buf.buf properly
> terminated with a slash, to contain "refs/remotes/test/" so that prefixcmp
> properly matches it with "refs/remotes/test/foo" but not with refs under
> other hierarchies like "refs/remotes/test-2/anything".

OK, 60e5eee solves the problem too. I wasn't aware of it as I was looking at the master branch. FWIW, here's the test I used:

#!/bin/sh

git=git cwd=$(pwd)

function fail ()
{
	echo $0: $*
	exit 1
}
for i in main test test-2; do
	mkdir $i || fail $i exists;
	$git init $i || fail git init $i failed
	echo $i > $i/foo
	( cd $i; git add foo; git commit -m $i )
done
cd main || fail cd main failed
for i in test test-2; do
	$git remote add $i file://$cwd/$i || fail git remote add $i failed
done
$git remote update || fail git remote update fail
$git remote rename test test-
$git show test-2/master || fail FAILED
echo PASSED
> 
> Thanks.
Junio C Hamano· Sep 27, 2011, 06:07 UTC · re: Benny Halevy · lore

Re: [PATCH] git-remote rename should match whole string when renaming remote ref directory

Benny Halevy <bhalevy@tonian.com> writes:
Show 16 quoted lines
> On 2011-09-26 21:04, Junio C Hamano wrote:
>> Benny Halevy <benny@tonian.com> writes:
>> ...
>>> -	if (!prefixcmp(refname, buf.buf)) {
>>> +	if (!strcmp(refname, buf.buf)) {
>> 
>> At this point of the code, refname has "refs/remotes/test/foo" and it is
>> queued to later rename it to "refs/remotes/test-/foo" (the next invocation
>> of this function will see "refs/remotes/test/bar" in refname). And the
>> strbuf buf.buf has "refs/remotes/test"; your !strcmp(refname, buf.buf)
>> would never trigger, I suspect.
>> 
>> Isn't 60e5eee (remote: "rename o foo" should not rename ref "origin/bar",
>> 2011-09-01) the correct fix for this issue?...
>
> OK, 60e5eee solves the problem too.

Hmm, what do you mean by "too" here? Martin's patch fixes the issue, but does yours, too?

Show 10 quoted lines
> FWIW, here's the test I used:
> ...
> cd main || fail cd main failed
> for i in test test-2; do
> 	$git remote add $i file://$cwd/$i || fail git remote add $i failed
> done
> $git remote update || fail git remote update fail
> $git remote rename test test-
> $git show test-2/master || fail FAILED
> echo PASSED
Before your last "echo PASSED", add this line:
	$git show test-/master || fail FAILED
and see what happens with your patch.

It is unfortunately a rather common trap to fall into, so I wouldn't blame you too much. People tend to concentrate only on an aspect of the problem that originally motivated them, and forget about the other issues that are equally important, if not more. In this case, you were too thrilled to see that your updated code no longer renames "test-2" mistakenly to "test--2", and you forgot that the primary task of the resulting code was to rename "test" to "test-" correctly. The additional line I gave you above is to test that.

When testing your own code, make it a habit to _always_ test both sides of the coin. It is somewhat difficult until you get used to it [*1*], but it is a skill that is really worth acquiring.

Thanks.
[Footnote]

*1* ...and I do not claim that I myself never forget to fully enumerate other sides; even experienced people still overlook and embarrass themselves in public ;-)

Benny Halevy· Sep 27, 2011, 09:38 UTC · re: Junio C Hamano · lore

Re: [PATCH] git-remote rename should match whole string when renaming remote ref directory

On 2011-09-27 09:07, Junio C Hamano wrote:
Show 22 quoted lines
> Benny Halevy <bhalevy@tonian.com> writes:
> 
>> On 2011-09-26 21:04, Junio C Hamano wrote:
>>> Benny Halevy <benny@tonian.com> writes:
>>> ...
>>>> -	if (!prefixcmp(refname, buf.buf)) {
>>>> +	if (!strcmp(refname, buf.buf)) {
>>>
>>> At this point of the code, refname has "refs/remotes/test/foo" and it is
>>> queued to later rename it to "refs/remotes/test-/foo" (the next invocation
>>> of this function will see "refs/remotes/test/bar" in refname). And the
>>> strbuf buf.buf has "refs/remotes/test"; your !strcmp(refname, buf.buf)
>>> would never trigger, I suspect.
>>>
>>> Isn't 60e5eee (remote: "rename o foo" should not rename ref "origin/bar",
>>> 2011-09-01) the correct fix for this issue?...
>>
>> OK, 60e5eee solves the problem too.
> 
> Hmm, what do you mean by "too" here?  Martin's patch fixes the issue, but
> does yours, too?
> 
correct.
Show 16 quoted lines
>> FWIW, here's the test I used:
>> ...
>> cd main || fail cd main failed
>> for i in test test-2; do
>> 	$git remote add $i file://$cwd/$i || fail git remote add $i failed
>> done
>> $git remote update || fail git remote update fail
>> $git remote rename test test-
>> $git show test-2/master || fail FAILED
>> echo PASSED
> 
> Before your last "echo PASSED", add this line:
> 
> 	$git show test-/master || fail FAILED
> 
> and see what happens with your patch.

Yup, with my patch this test indeed fails, and with Martin's it passes.

Thanks!
Benny
Show 22 quoted lines
> 
> It is unfortunately a rather common trap to fall into, so I wouldn't blame
> you too much. People tend to concentrate only on an aspect of the problem
> that originally motivated them, and forget about the other issues that are
> equally important, if not more. In this case, you were too thrilled to see
> that your updated code no longer renames "test-2" mistakenly to "test--2",
> and you forgot that the primary task of the resulting code was to rename
> "test" to "test-" correctly. The additional line I gave you above is to
> test that.
> 
> When testing your own code, make it a habit to _always_ test both sides of
> the coin. It is somewhat difficult until you get used to it [*1*], but it
> is a skill that is really worth acquiring.
> 
> Thanks.
> 
> 
> [Footnote]
> 
> *1* ...and I do not claim that I myself never forget to fully enumerate
> other sides; even experienced people still overlook and embarrass
> themselves in public ;-)

← back to recent threads