5 static const char git_update_ref_usage[] =
6 "git-update-ref [-m <reason>] (-d <refname> <value> | [--no-deref] <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];
13 int i, delete, ref_flags;
17 git_config(git_default_config);
19 for (i = 1; i < argc; i++) {
20 if (!strcmp("-m", argv[i])) {
22 usage(git_update_ref_usage);
25 die("Refusing to perform update with empty message.");
28 if (!strcmp("-d", argv[i])) {
32 if (!strcmp("--no-deref", argv[i])) {
33 ref_flags |= REF_NODEREF;
49 if (!refname || !value)
50 usage(git_update_ref_usage);
52 if (get_sha1(value, sha1))
53 die("%s: not a valid SHA1", value);
57 usage(git_update_ref_usage);
58 return delete_ref(refname, sha1);
62 if (oldval && *oldval && get_sha1(oldval, oldsha1))
63 die("%s: not a valid old SHA1", oldval);
65 lock = lock_any_ref_for_update(refname, oldval ? oldsha1 : NULL, ref_flags);
67 die("%s: cannot lock the ref", refname);
68 if (write_ref_sha1(lock, sha1, msg) < 0)
69 die("%s: cannot update the ref", refname);