Refactor git_tcp_connect() functions a little.
[git/raj.git] / update-ref.c
bloba1e6bb90fe598dfd030c98f37a5d48c3a30590eb
1 #include "cache.h"
2 #include "refs.h"
4 static const char git_update_ref_usage[] =
5 "git-update-ref <refname> <value> [<oldval>] [-m <reason>]";
7 int main(int argc, char **argv)
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;
14 setup_git_directory();
15 git_config(git_default_config);
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;
28 if (!refname) {
29 refname = argv[i];
30 continue;
32 if (!value) {
33 value = argv[i];
34 continue;
36 if (!oldval) {
37 oldval = argv[i];
38 continue;
41 if (!refname || !value)
42 usage(git_update_ref_usage);
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);
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;