# [PATCH] replace: fix replacing object with itself

3 messages from 2014-11-09 to 2014-11-09. Participants: Manzur Mukhitdinov, Jeff King, Junio C Hamano.
Thread: https://gitlist.dev/t/37910

## Manzur Mukhitdinov, 2014-11-09 00:05

Subject: [PATCH] replace: fix replacing object with itself
Message-ID: <1415491531-29913-1-git-send-email-manzurmm@gmail.com>
URL: https://gitlist.dev/e/1415491531-29913-1-git-send-email-manzurmm%40gmail.com

```
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(+)

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, 2014-11-09 05:58

Subject: Re: [PATCH] replace: fix replacing object with itself
Message-ID: <20141109055842.GB13445@peff.net>
URL: https://gitlist.dev/e/20141109055842.GB13445%40peff.net
In-Reply-To: <1415491531-29913-1-git-send-email-manzurmm@gmail.com>

```
On Sun, Nov 09, 2014 at 01:05:31AM +0100, Manzur Mukhitdinov wrote:

> 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.

> 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, 2014-11-09 17:35

Subject: Re: [PATCH] replace: fix replacing object with itself
Message-ID: <xmqqy4rkcm4n.fsf@gitster.dls.corp.google.com>
URL: https://gitlist.dev/e/xmqqy4rkcm4n.fsf%40gitster.dls.corp.google.com
In-Reply-To: <20141109055842.GB13445@peff.net>

```
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.

```
