threads / patch / 37910

patchreplace: fix replacing object with itself

Subject: [PATCH] replace: fix replacing object with itself

## tl;dr

3 messages between Nov 9, 2014 and Nov 9, 2014. Diffs are folded; open one to read it.

replies: 2people: 3as markdown or json

Manzur Mukhitdinov· Nov 9, 2014, 00:05 UTC · lore
When object is replaced with itself git shows unhelpful messages like(git log):
    "fatal: replace depth too high for object <SHA1>"

Prevents user from replacing object with itself(with test for checking this case).

Signed-off-by: Manzur Mukhitdinov <manzurmm@gmail.com>
---
 builtin/replace.c  | 3 +++
 t/t6050-replace.sh | 6 ++++++
 2 files changed, 9 insertions(+)
Show changes to 2 files +9 −0

builtin/replace.c, t/t6050-replace.sh

diff --git a/builtin/replace.c b/builtin/replace.c
index 294b61b..b7e05ad 100644
--- a/builtin/replace.c
+++ b/builtin/replace.c
@@ -186,6 +186,9 @@ static int replace_object(const char *object_ref, const char *replace_ref, int f
 	if (get_sha1(replace_ref, repl))
 		die("Failed to resolve '%s' as a valid ref.", replace_ref);
 
+	if (!hashcmp(object, repl))
+		return error("new object is the same as the old one: '%s'", sha1_to_hex(object));
+
 	return replace_object_sha1(object_ref, object, replace_ref, repl, force);
 }
 
diff --git a/t/t6050-replace.sh b/t/t6050-replace.sh
index 4d5a25e..f2b0307 100755
--- a/t/t6050-replace.sh
+++ b/t/t6050-replace.sh
@@ -440,4 +440,10 @@ test_expect_success GPG '--graft on a commit with a mergetag' '
 	git replace -d $HASH10
 '
 
+test_expect_success 'replacing object with itself must fail' '
+    test_must_fail git replace $HASH1 $HASH1 &&
+    HASH8=$(git rev-parse --verify HEAD) &&
+    test_must_fail git replace HEAD $HASH8
+'
+
 test_done
-- 
1.9.1
Jeff King· Nov 9, 2014, 05:58 UTC · re: Manzur Mukhitdinov · lore

Re: [PATCH] replace: fix replacing object with itself

On Sun, Nov 09, 2014 at 01:05:31AM +0100, Manzur Mukhitdinov wrote:
Show 5 quoted lines
> When object is replaced with itself git shows unhelpful messages like(git log):
>     "fatal: replace depth too high for object <SHA1>"
> 
> Prevents user from replacing object with itself(with test for checking
> this case).

I thought we already did this in the last round of git-replace patches, but it looks like we only did it for the newly added --edit and --graft cases, not "git replace X X". I think this is probably a good step. I've also considered that this should be another way of deleting the replacement, but I think we decided that was too magical.

Show 12 quoted lines
> diff --git a/builtin/replace.c b/builtin/replace.c
> index 294b61b..b7e05ad 100644
> --- a/builtin/replace.c
> +++ b/builtin/replace.c
> @@ -186,6 +186,9 @@ static int replace_object(const char *object_ref, const char *replace_ref, int f
>  	if (get_sha1(replace_ref, repl))
>  		die("Failed to resolve '%s' as a valid ref.", replace_ref);
>  
> +	if (!hashcmp(object, repl))
> +		return error("new object is the same as the old one: '%s'", sha1_to_hex(object));
> +
>  	return replace_object_sha1(object_ref, object, replace_ref, repl, force);

I think all of the callers of replace_object_sha1 do this same check now. Can we just move the check into that function instead of adding another instance of it?

-Peff
Junio C Hamano· Nov 9, 2014, 17:35 UTC · re: Jeff King · lore

Re: [PATCH] replace: fix replacing object with itself

Jeff King <peff@peff.net> writes:
> I think all of the callers of replace_object_sha1 do this same check
> now. Can we just move the check into that function instead of adding
> another instance of it?
Good thinking.  Thanks.

← back to recent threads