]> git.ipfire.org Git - thirdparty/git.git/blame - builtin-update-ref.c
Merge branch 'maint'
[thirdparty/git.git] / builtin-update-ref.c
CommitLineData
66bf85a4
LT
1#include "cache.h"
2#include "refs.h"
854b4629 3#include "builtin.h"
66bf85a4 4
5b16b090 5static const char git_update_ref_usage[] =
ac5409e4 6"git-update-ref [-m <reason>] (-d <refname> <value> | <refname> <value> [<oldval>])";
152da3df 7
a633fca0 8int cmd_update_ref(int argc, const char **argv, const char *prefix)
66bf85a4 9{
5b16b090
SP
10 const char *refname=NULL, *value=NULL, *oldval=NULL, *msg=NULL;
11 struct ref_lock *lock;
12 unsigned char sha1[20], oldsha1[20];
ac5409e4 13 int i, delete;
66bf85a4 14
ac5409e4 15 delete = 0;
0b0fe4a6 16 setup_ident();
84a9b58c 17 git_config(git_default_config);
5b16b090
SP
18
19 for (i = 1; i < argc; i++) {
20 if (!strcmp("-m", argv[i])) {
21 if (i+1 >= argc)
22 usage(git_update_ref_usage);
23 msg = argv[++i];
24 if (!*msg)
25 die("Refusing to perform update with empty message.");
26 if (strchr(msg, '\n'))
27 die("Refusing to perform update with \\n in message.");
28 continue;
29 }
ac5409e4
JH
30 if (!strcmp("-d", argv[i])) {
31 delete = 1;
32 continue;
33 }
5b16b090
SP
34 if (!refname) {
35 refname = argv[i];
36 continue;
37 }
38 if (!value) {
39 value = argv[i];
40 continue;
41 }
42 if (!oldval) {
43 oldval = argv[i];
44 continue;
45 }
46 }
47 if (!refname || !value)
66bf85a4
LT
48 usage(git_update_ref_usage);
49
31fff305 50 if (get_sha1(value, sha1))
66bf85a4 51 die("%s: not a valid SHA1", value);
ac5409e4
JH
52
53 if (delete) {
54 if (oldval)
55 usage(git_update_ref_usage);
56 return delete_ref(refname, sha1);
57 }
58
a8e0d16d 59 hashclr(oldsha1);
ac5409e4 60 if (oldval && *oldval && get_sha1(oldval, oldsha1))
66bf85a4
LT
61 die("%s: not a valid old SHA1", oldval);
62
4431fcc4 63 lock = lock_any_ref_for_update(refname, oldval ? oldsha1 : NULL);
5b16b090
SP
64 if (!lock)
65 return 1;
66 if (write_ref_sha1(lock, sha1, msg) < 0)
67 return 1;
854b4629
LS
68
69 /* write_ref_sha1 always unlocks the ref, no need to do it explicitly */
66bf85a4
LT
70 return 0;
71}