replace: parse revision argument for -d

'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> Signed-off-by: Jeff King <peff@peff.net>

Michael J Gruber committed Nov 13, 2012 at 11:34 UTC 9dfc36841b3f1a2669f0513676cc4d72ef1220a1
2 files changed +20 -6
builtin/replace.c
+9 -6
@@ -46,24 +46,27 @@ typedef int (*each_replace_name_fn)(const char *name, const char *ref,
46
47 static int for_each_replace_name(const char **argv, each_replace_name_fn fn)
48 {
49 - const char **p;
49 + const char **p, *full_hex;
50 char ref[PATH_MAX];
51 int had_error = 0;
52 unsigned char sha1[20];
53
54 for (p = argv; *p; p++) {
55 - if (snprintf(ref, sizeof(ref), "refs/replace/%s", *p)
56 - >= sizeof(ref)) {
57 - error("replace ref name too long: %.*s...", 50, *p);
55 + if (get_sha1(*p, sha1)) {
56 + error("Failed to resolve '%s' as a valid ref.", *p);
57 had_error = 1;
58 continue;
59 }
60 + full_hex = sha1_to_hex(sha1);
61 + snprintf(ref, sizeof(ref), "refs/replace/%s", full_hex);
62 + /* read_ref() may reuse the buffer */
63 + full_hex = ref + strlen("refs/replace/");
64 if (read_ref(ref, sha1)) {
62 - error("replace ref '%s' not found.", *p);
65 + error("replace ref '%s' not found.", full_hex);
66 had_error = 1;
67 continue;
68 }
66 - if (fn(*p, ref, sha1))
69 + if (fn(full_hex, ref, sha1))
70 had_error = 1;
71 }
72 return had_error;
t/t6050-replace.sh
+11
@@ -140,6 +140,17 @@ test_expect_success '"git replace" replacing' '
140 test "$HASH2" = "$(git replace)"
141 '
142
143 +test_expect_success '"git replace" resolves sha1' '
144 + SHORTHASH2=$(git rev-parse --short=8 $HASH2) &&
145 + git replace -d $SHORTHASH2 &&
146 + git replace $SHORTHASH2 $R &&
147 + git show $HASH2 | grep "O Thor" &&
148 + test_must_fail git replace $HASH2 $R &&
149 + git replace -f $HASH2 $R &&
150 + test_must_fail git replace -f &&
151 + test "$HASH2" = "$(git replace)"
152 +'
153 +
154 # This creates a side branch where the bug in H2
155 # does not appear because P2 is created by applying
156 # H2 and squashing H5 into it.