]>
Commit | Line | Data |
---|---|---|
66bf85a4 LT |
1 | #include "cache.h" |
2 | #include "refs.h" | |
854b4629 | 3 | #include "builtin.h" |
66bf85a4 | 4 | |
5b16b090 | 5 | static const char git_update_ref_usage[] = |
ac5409e4 | 6 | "git-update-ref [-m <reason>] (-d <refname> <value> | <refname> <value> [<oldval>])"; |
152da3df | 7 | |
a633fca0 | 8 | int 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; |
84a9b58c | 16 | git_config(git_default_config); |
5b16b090 SP |
17 | |
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."); | |
7f9acb2a JH |
25 | if (strchr(msg, '\n')) |
26 | die("Refusing to perform update with \\n in message."); | |
5b16b090 SP |
27 | continue; |
28 | } | |
ac5409e4 JH |
29 | if (!strcmp("-d", argv[i])) { |
30 | delete = 1; | |
31 | continue; | |
32 | } | |
5b16b090 SP |
33 | if (!refname) { |
34 | refname = argv[i]; | |
35 | continue; | |
36 | } | |
37 | if (!value) { | |
38 | value = argv[i]; | |
39 | continue; | |
40 | } | |
41 | if (!oldval) { | |
42 | oldval = argv[i]; | |
43 | continue; | |
44 | } | |
45 | } | |
46 | if (!refname || !value) | |
66bf85a4 LT |
47 | usage(git_update_ref_usage); |
48 | ||
31fff305 | 49 | if (get_sha1(value, sha1)) |
66bf85a4 | 50 | die("%s: not a valid SHA1", value); |
ac5409e4 JH |
51 | |
52 | if (delete) { | |
53 | if (oldval) | |
54 | usage(git_update_ref_usage); | |
55 | return delete_ref(refname, sha1); | |
56 | } | |
57 | ||
a8e0d16d | 58 | hashclr(oldsha1); |
ac5409e4 | 59 | if (oldval && *oldval && get_sha1(oldval, oldsha1)) |
66bf85a4 LT |
60 | die("%s: not a valid old SHA1", oldval); |
61 | ||
4431fcc4 | 62 | lock = lock_any_ref_for_update(refname, oldval ? oldsha1 : NULL); |
5b16b090 | 63 | if (!lock) |
a2f9fe92 | 64 | die("%s: cannot lock the ref", refname); |
5b16b090 | 65 | if (write_ref_sha1(lock, sha1, msg) < 0) |
a2f9fe92 | 66 | die("%s: cannot update the ref", refname); |
66bf85a4 LT |
67 | return 0; |
68 | } |