1#include "cache.h"
2#include "refs.h"
3
4static const char git_update_ref_usage[] =
5"git-update-ref <refname> <value> [<oldval>] [-m <reason>]";
6
7int main(int argc, char **argv)
8{
9 const char *refname=NULL, *value=NULL, *oldval=NULL, *msg=NULL;
10 struct ref_lock *lock;
11 unsigned char sha1[20], oldsha1[20];
12 int i;
13
14 setup_git_directory();
15 git_config(git_default_config);
16
17 for (i = 1; i < argc; i++) {
18 if (!strcmp("-m", argv[i])) {
19 if (i+1 >= argc)
20 usage(git_update_ref_usage);
21 msg = argv[++i];
22 if (!*msg)
23 die("Refusing to perform update with empty message.");
24 if (strchr(msg, '\n'))
25 die("Refusing to perform update with \\n in message.");
26 continue;
27 }
28 if (!refname) {
29 refname = argv[i];
30 continue;
31 }
32 if (!value) {
33 value = argv[i];
34 continue;
35 }
36 if (!oldval) {
37 oldval = argv[i];
38 continue;
39 }
40 }
41 if (!refname || !value)
42 usage(git_update_ref_usage);
43
44 if (get_sha1(value, sha1))
45 die("%s: not a valid SHA1", value);
46 memset(oldsha1, 0, 20);
47 if (oldval && get_sha1(oldval, oldsha1))
48 die("%s: not a valid old SHA1", oldval);
49
50 lock = lock_any_ref_for_update(refname, oldval ? oldsha1 : NULL, 0);
51 if (!lock)
52 return 1;
53 if (write_ref_sha1(lock, sha1, msg) < 0)
54 return 1;
55 return 0;
56}