update-ref --stdin: improve error messages for invalid values

If an invalid value is passed to "update-ref --stdin" as <oldvalue> or <newvalue>, include the command and the name of the reference at the beginning of the error message. Update the tests accordingly. Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Michael Haggerty committed Apr 7, 2014 at 15:48 UTC 1746ef4e9d869fb0f54194bb225604eb61a77501
2 files changed +17 -15
builtin/update-ref.c
+13 -11
@@ -35,20 +35,22 @@ static struct ref_update *update_alloc(void)
35 return update;
36 }
37
38 -static void update_store_new_sha1(struct ref_update *update,
38 +static void update_store_new_sha1(const char *command,
39 + struct ref_update *update,
40 const char *newvalue)
41 {
42 if (*newvalue && get_sha1(newvalue, update->new_sha1))
42 - die("invalid new value for ref %s: %s",
43 - update->ref_name, newvalue);
43 + die("%s %s: invalid new value: %s",
44 + command, update->ref_name, newvalue);
45 }
46
46 -static void update_store_old_sha1(struct ref_update *update,
47 +static void update_store_old_sha1(const char *command,
48 + struct ref_update *update,
49 const char *oldvalue)
50 {
51 if (*oldvalue && get_sha1(oldvalue, update->old_sha1))
50 - die("invalid old value for ref %s: %s",
51 - update->ref_name, oldvalue);
52 + die("%s %s: invalid old value: %s",
53 + command, update->ref_name, oldvalue);
54
55 /* We have an old value if non-empty, or if empty without -z */
56 update->have_old = *oldvalue || line_termination;
@@ -165,12 +167,12 @@ static const char *parse_cmd_update(struct strbuf *input, const char *next)
167 die("update line missing <ref>");
168
169 if (!parse_next_arg(input, &next, &newvalue))
168 - update_store_new_sha1(update, newvalue.buf);
170 + update_store_new_sha1("update", update, newvalue.buf);
171 else
172 die("update %s missing <newvalue>", update->ref_name);
173
174 if (!parse_next_arg(input, &next, &oldvalue)) {
173 - update_store_old_sha1(update, oldvalue.buf);
175 + update_store_old_sha1("update", update, oldvalue.buf);
176 if (*next != line_termination)
177 die("update %s has extra input: %s", update->ref_name, next);
178 } else if (!line_termination)
@@ -191,7 +193,7 @@ static const char *parse_cmd_create(struct strbuf *input, const char *next)
193 die("create line missing <ref>");
194
195 if (!parse_next_arg(input, &next, &newvalue))
194 - update_store_new_sha1(update, newvalue.buf);
196 + update_store_new_sha1("create", update, newvalue.buf);
197 else
198 die("create %s missing <newvalue>", update->ref_name);
199
@@ -216,7 +218,7 @@ static const char *parse_cmd_delete(struct strbuf *input, const char *next)
218 die("delete line missing <ref>");
219
220 if (!parse_next_arg(input, &next, &oldvalue)) {
219 - update_store_old_sha1(update, oldvalue.buf);
221 + update_store_old_sha1("delete", update, oldvalue.buf);
222 if (update->have_old && is_null_sha1(update->old_sha1))
223 die("delete %s given zero old value", update->ref_name);
224 } else if (!line_termination)
@@ -240,7 +242,7 @@ static const char *parse_cmd_verify(struct strbuf *input, const char *next)
242 die("verify line missing <ref>");
243
244 if (!parse_next_arg(input, &next, &value)) {
243 - update_store_old_sha1(update, value.buf);
245 + update_store_old_sha1("verify", update, value.buf);
246 hashcpy(update->new_sha1, update->old_sha1);
247 } else if (!line_termination)
248 die("verify %s missing [<oldvalue>] NUL", update->ref_name);
t/t1400-update-ref.sh
+4 -4
@@ -518,14 +518,14 @@ test_expect_success 'stdin update ref fails with wrong old value' '
518 test_expect_success 'stdin update ref fails with bad old value' '
519 echo "update $c $m does-not-exist" >stdin &&
520 test_must_fail git update-ref --stdin <stdin 2>err &&
521 - grep "fatal: invalid old value for ref $c: does-not-exist" err &&
521 + grep "fatal: update $c: invalid old value: does-not-exist" err &&
522 test_must_fail git rev-parse --verify -q $c
523 '
524
525 test_expect_success 'stdin create ref fails with bad new value' '
526 echo "create $c does-not-exist" >stdin &&
527 test_must_fail git update-ref --stdin <stdin 2>err &&
528 - grep "fatal: invalid new value for ref $c: does-not-exist" err &&
528 + grep "fatal: create $c: invalid new value: does-not-exist" err &&
529 test_must_fail git rev-parse --verify -q $c
530 '
531
@@ -840,14 +840,14 @@ test_expect_success 'stdin -z update ref fails with wrong old value' '
840 test_expect_success 'stdin -z update ref fails with bad old value' '
841 printf $F "update $c" "$m" "does-not-exist" >stdin &&
842 test_must_fail git update-ref -z --stdin <stdin 2>err &&
843 - grep "fatal: invalid old value for ref $c: does-not-exist" err &&
843 + grep "fatal: update $c: invalid old value: does-not-exist" err &&
844 test_must_fail git rev-parse --verify -q $c
845 '
846
847 test_expect_success 'stdin -z create ref fails with bad new value' '
848 printf $F "create $c" "does-not-exist" >stdin &&
849 test_must_fail git update-ref -z --stdin <stdin 2>err &&
850 - grep "fatal: invalid new value for ref $c: does-not-exist" err &&
850 + grep "fatal: create $c: invalid new value: does-not-exist" err &&
851 test_must_fail git rev-parse --verify -q $c
852 '
853