Merge branch 'gp/maint-diffdoc'
[git/git-p4.git] / builtin-update-ref.c
blobfe1f74c9f3fa4a44cfd56508171e65a40e27342c
1 #include "cache.h"
2 #include "refs.h"
3 #include "builtin.h"
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 unsigned char sha1[20], oldsha1[20];
12 int i, delete, ref_flags;
14 delete = 0;
15 ref_flags = 0;
16 git_config(git_default_config);
18 for (i = 1; i < argc; i++) {
19 if (!strcmp("-m", argv[i])) {
20 if (i+1 >= argc)
21 usage(git_update_ref_usage);
22 msg = argv[++i];
23 if (!*msg)
24 die("Refusing to perform update with empty message.");
25 continue;
27 if (!strcmp("-d", argv[i])) {
28 delete = 1;
29 continue;
31 if (!strcmp("--no-deref", argv[i])) {
32 ref_flags |= REF_NODEREF;
33 continue;
35 if (!refname) {
36 refname = argv[i];
37 continue;
39 if (!value) {
40 value = argv[i];
41 continue;
43 if (!oldval) {
44 oldval = argv[i];
45 continue;
48 if (!refname || !value)
49 usage(git_update_ref_usage);
51 if (get_sha1(value, sha1))
52 die("%s: not a valid SHA1", value);
54 if (delete) {
55 if (oldval)
56 usage(git_update_ref_usage);
57 return delete_ref(refname, sha1);
60 hashclr(oldsha1);
61 if (oldval && *oldval && get_sha1(oldval, oldsha1))
62 die("%s: not a valid old SHA1", oldval);
64 return update_ref(msg, refname, sha1, oldval ? oldsha1 : NULL,
65 ref_flags, DIE_ON_ERR);