Skip to content

Commit

Permalink
update-ref --stdin -z: deprecate interpreting the empty string as zeros
Browse files Browse the repository at this point in the history
In the original version of this command, for the single case of the
"update" command's <newvalue>, the empty string was interpreted as
being equivalent to 40 "0"s.  This shorthand is unnecessary (binary
input will usually be generated programmatically anyway), and it
complicates the parser and the documentation.

So gently deprecate this usage: remove its description from the
documentation and emit a warning if it is found.  But for reasons of
backwards compatibility, continue to accept it.

Helped-by: Brad King <brad.king@kitware.com>
Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
Michael Haggerty authored and Junio C Hamano committed Apr 7, 2014
1 parent 3afcc46 commit 1fbd504
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
18 changes: 12 additions & 6 deletions Documentation/git-update-ref.txt
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,25 @@ performs all modifications together. Specify commands of the form:
option SP <opt> LF

Quote fields containing whitespace as if they were strings in C source
code. Alternatively, use `-z` to specify commands without quoting:
code; i.e., surrounded by double-quotes and with backslash escapes.
Use 40 "0" characters or the empty string to specify a zero value. To
specify a missing value, omit the value and its preceding SP entirely.

Alternatively, use `-z` to specify in NUL-terminated format, without
quoting:

update SP <ref> NUL <newvalue> NUL [<oldvalue>] NUL
create SP <ref> NUL <newvalue> NUL
delete SP <ref> NUL [<oldvalue>] NUL
verify SP <ref> NUL [<oldvalue>] NUL
option SP <opt> NUL

Lines of any other format or a repeated <ref> produce an error.
Command meanings are:
In this format, use 40 "0" to specify a zero value, and use the empty
string to specify a missing value.

In either format, values can be specified in any form that Git
recognizes as an object name. Commands in any other format or a
repeated <ref> produce an error. Command meanings are:

update::
Set <ref> to <newvalue> after verifying <oldvalue>, if given.
Expand All @@ -102,9 +111,6 @@ option::
The only valid option is `no-deref` to avoid dereferencing
a symbolic ref.

Use 40 "0" or the empty string to specify a zero value, except that
with `-z` an empty <oldvalue> is considered missing.

If all <ref>s can be locked with matching <oldvalue>s
simultaneously, all modifications are performed. Otherwise, no
modifications are performed. Note that while each individual
Expand Down
2 changes: 2 additions & 0 deletions builtin/update-ref.c
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,8 @@ static int parse_next_sha1(struct strbuf *input, const char **next,
goto invalid;
} else if (flags & PARSE_SHA1_ALLOW_EMPTY) {
/* With -z, treat an empty value as all zeros: */
warning("%s %s: missing <newvalue>, treating as zero",
command, refname);
hashclr(sha1);
} else {
/*
Expand Down
5 changes: 3 additions & 2 deletions t/t1400-update-ref.sh
Original file line number Diff line number Diff line change
Expand Up @@ -730,10 +730,11 @@ test_expect_success 'stdin -z fails update with bad ref name' '
grep "fatal: invalid ref format: ~a" err
'

test_expect_success 'stdin -z treats empty new value as zeros' '
test_expect_success 'stdin -z emits warning with empty new value' '
git update-ref $a $m &&
printf $F "update $a" "" "" >stdin &&
git update-ref -z --stdin <stdin &&
git update-ref -z --stdin <stdin 2>err &&
grep "warning: update $a: missing <newvalue>, treating as zero" err &&
test_must_fail git rev-parse --verify -q $a
'

Expand Down

0 comments on commit 1fbd504

Please sign in to comment.