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

[PATCHv4] replace: parse revision argument for -d

From
Michael J Gruber <git@drmicha.warpmail.net>
Date
Nov 13, 2012, 10:34 UTC
Message-ID
<848bc8f48885acd63b4f6d29eea0543e73d86451.1352802239.git.git@drmicha.warpmail.net>
In-Reply-To
<50A22026.60506@drmicha.warpmail.net>

'git replace' parses the revision arguments when it creates replacements (so that a sha1 can be abbreviated, e.g.) but not when deleting replacements.

Make it parse the argument to 'replace -d' in the same way.
Signed-off-by: Michael J Gruber <git@drmicha.warpmail.net>
---
Notes:
    v4 names the aux variable more concisely and does away with a superfluous
    assignment.
 builtin/replace.c  | 15 +++++++++------
 t/t6050-replace.sh | 11 +++++++++++
 2 files changed, 20 insertions(+), 6 deletions(-)
diff --git a/builtin/replace.c b/builtin/replace.c
index e3aaf70..398ccd5 100644
--- a/builtin/replace.c
+++ b/builtin/replace.c
@@ -46,24 +46,27 @@ typedef int (*each_replace_name_fn)(const char *name, const char *ref,
 
 static int for_each_replace_name(const char **argv, each_replace_name_fn fn)
 {
-	const char **p;
+	const char **p, *full_hex;
 	char ref[PATH_MAX];
 	int had_error = 0;
 	unsigned char sha1[20];
 
 	for (p = argv; *p; p++) {
-		if (snprintf(ref, sizeof(ref), "refs/replace/%s", *p)
-					>= sizeof(ref)) {
-			error("replace ref name too long: %.*s...", 50, *p);
+		if (get_sha1(*p, sha1)) {
+			error("Failed to resolve '%s' as a valid ref.", *p);
 			had_error = 1;
 			continue;
 		}
+		full_hex = sha1_to_hex(sha1);
+		snprintf(ref, sizeof(ref), "refs/replace/%s", full_hex);
+		/* read_ref() may reuse the buffer */
+		full_hex = ref + strlen("refs/replace/");
 		if (read_ref(ref, sha1)) {
-			error("replace ref '%s' not found.", *p);
+			error("replace ref '%s' not found.", full_hex);
 			had_error = 1;
 			continue;
 		}
-		if (fn(*p, ref, sha1))
+		if (fn(full_hex, ref, sha1))
 			had_error = 1;
 	}
 	return had_error;
diff --git a/t/t6050-replace.sh b/t/t6050-replace.sh
index 5c87f28..decdc33 100755
--- a/t/t6050-replace.sh
+++ b/t/t6050-replace.sh
@@ -140,6 +140,17 @@ test_expect_success '"git replace" replacing' '
      test "$HASH2" = "$(git replace)"
 '
 
+test_expect_success '"git replace" resolves sha1' '
+     SHORTHASH2=$(git rev-parse --short=8 $HASH2) &&
+     git replace -d $SHORTHASH2 &&
+     git replace $SHORTHASH2 $R &&
+     git show $HASH2 | grep "O Thor" &&
+     test_must_fail git replace $HASH2 $R &&
+     git replace -f $HASH2 $R &&
+     test_must_fail git replace -f &&
+     test "$HASH2" = "$(git replace)"
+'
+
 # This creates a side branch where the bug in H2
 # does not appear because P2 is created by applying
 # H2 and squashing H5 into it.
-- 
1.8.0.311.gdd08018
Previous: Michael J GruberNext: Jeff King
Message 13 of 22 in “replace: parse revision argument for -d”
  1. replace: parse revision argument for -dMichael J Gruber, Oct 26, 2012
  2. Christian CouderOct 26, 2012
  3. Jeff KingOct 29, 2012
  4. Michael J GruberOct 29, 2012
  5. Jeff KingOct 29, 2012
  6. Michael J GruberOct 29, 2012
  7. [PATCHv2] replace: parse revision argument for -dMichael J Gruber, Oct 29, 2012
  8. Jeff KingNov 9, 2012
  9. Michael J GruberNov 12, 2012
  10. [PATCHv3] replace: parse revision argument for -dMichael J Gruber, Nov 12, 2012
  11. Jeff KingNov 12, 2012
  12. Michael J GruberNov 13, 2012
  13. [PATCHv4] replace: parse revision argument for -dMichael J Gruber, Nov 13, 2012
  14. Jeff KingNov 13, 2012
  15. Notes in format-patch (was: Re: [PATCHv3] replace: parse revision argument for -d)Michael J Gruber, Nov 13, 2012
  16. Jeff KingNov 13, 2012
  17. Junio C HamanoNov 13, 2012
  18. Junio C HamanoNov 13, 2012
  19. Junio C HamanoNov 13, 2012
  20. Junio C HamanoNov 13, 2012
  21. Michael J GruberNov 14, 2012
  22. Junio C HamanoNov 14, 2012

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.