5 static const char git_update_ref_usage
[] =
6 "git-update-ref [-m <reason>] (-d <refname> <value> | <refname> <value> [<oldval>])";
8 int cmd_update_ref(int argc
, const char **argv
, const char *prefix
)
10 const char *refname
=NULL
, *value
=NULL
, *oldval
=NULL
, *msg
=NULL
;
11 struct ref_lock
*lock
;
12 unsigned char sha1
[20], oldsha1
[20];
16 git_config(git_default_config
);
18 for (i
= 1; i
< argc
; i
++) {
19 if (!strcmp("-m", argv
[i
])) {
21 usage(git_update_ref_usage
);
24 die("Refusing to perform update with empty message.");
25 if (strchr(msg
, '\n'))
26 die("Refusing to perform update with \\n in message.");
29 if (!strcmp("-d", argv
[i
])) {
46 if (!refname
|| !value
)
47 usage(git_update_ref_usage
);
49 if (get_sha1(value
, sha1
))
50 die("%s: not a valid SHA1", value
);
54 usage(git_update_ref_usage
);
55 return delete_ref(refname
, sha1
);
59 if (oldval
&& *oldval
&& get_sha1(oldval
, oldsha1
))
60 die("%s: not a valid old SHA1", oldval
);
62 lock
= lock_any_ref_for_update(refname
, oldval
? oldsha1
: NULL
);
65 if (write_ref_sha1(lock
, sha1
, msg
) < 0)
68 /* write_ref_sha1 always unlocks the ref, no need to do it explicitly */