diff: do not use null sha1 as a sentinel value
[git/git.git] / builtin / update-ref.c
CommitLineData
66bf85a4
LT
1#include "cache.h"
2#include "refs.h"
854b4629 3#include "builtin.h"
89942be6 4#include "parse-options.h"
66bf85a4 5
89942be6 6static const char * const git_update_ref_usage[] = {
1b1dd23f
SB
7 "git update-ref [options] -d <refname> [<oldval>]",
8 "git update-ref [options] <refname> <newval> [<oldval>]",
89942be6
PH
9 NULL
10};
152da3df 11
a633fca0 12int cmd_update_ref(int argc, const char **argv, const char *prefix)
66bf85a4 13{
f877fd82 14 const char *refname, *oldval, *msg = NULL;
5b16b090 15 unsigned char sha1[20], oldsha1[20];
569740bd 16 int delete = 0, no_deref = 0, flags = 0;
89942be6
PH
17 struct option options[] = {
18 OPT_STRING( 'm', NULL, &msg, "reason", "reason of the update"),
19 OPT_BOOLEAN('d', NULL, &delete, "deletes the reference"),
20 OPT_BOOLEAN( 0 , "no-deref", &no_deref,
21 "update <refname> not the one it points to"),
22 OPT_END(),
23 };
66bf85a4 24
ef90d6d4 25 git_config(git_default_config, NULL);
37782920
SB
26 argc = parse_options(argc, argv, prefix, options, git_update_ref_usage,
27 0);
89942be6
PH
28 if (msg && !*msg)
29 die("Refusing to perform update with empty message.");
5b16b090 30
ac5409e4 31 if (delete) {
3fe8dce6 32 if (argc < 1 || argc > 2)
973a70ea
KW
33 usage_with_options(git_update_ref_usage, options);
34 refname = argv[0];
35 oldval = argv[1];
36 } else {
37 const char *value;
38 if (argc < 2 || argc > 3)
89942be6 39 usage_with_options(git_update_ref_usage, options);
973a70ea
KW
40 refname = argv[0];
41 value = argv[1];
42 oldval = argv[2];
43 if (get_sha1(value, sha1))
44 die("%s: not a valid SHA1", value);
ac5409e4
JH
45 }
46
973a70ea 47 hashclr(oldsha1); /* all-zero hash in case oldval is the empty string */
ac5409e4 48 if (oldval && *oldval && get_sha1(oldval, oldsha1))
66bf85a4
LT
49 die("%s: not a valid old SHA1", oldval);
50
569740bd
MV
51 if (no_deref)
52 flags = REF_NODEREF;
973a70ea 53 if (delete)
569740bd 54 return delete_ref(refname, oldval ? oldsha1 : NULL, flags);
973a70ea
KW
55 else
56 return update_ref(msg, refname, sha1, oldval ? oldsha1 : NULL,
569740bd 57 flags, DIE_ON_ERR);
66bf85a4 58}