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

[PATCH] replace: fix replacing object with itself

From
Manzur Mukhitdinov <manzurmm@gmail.com>
Date
Nov 9, 2014, 00:05 UTC
Message-ID
<1415491531-29913-1-git-send-email-manzurmm@gmail.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
Next: Jeff King
Message 1 of 3 in “replace: fix replacing object with itself”
  1. replace: fix replacing object with itselfManzur Mukhitdinov, Nov 9, 2014
  2. Jeff KingNov 9, 2014
  3. Junio C HamanoNov 9, 2014

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.