symbolic-ref, update-ref: do not refuse reflog message with LF
[git/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[] =
68db31cc 6"git-update-ref [-m <reason>] (-d <refname> <value> | [--no-deref] <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];
68db31cc 13 int i, delete, ref_flags;
66bf85a4 14
ac5409e4 15 delete = 0;
68db31cc 16 ref_flags = 0;
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.");
5b16b090
SP
26 continue;
27 }
ac5409e4
JH
28 if (!strcmp("-d", argv[i])) {
29 delete = 1;
30 continue;
31 }
68db31cc
SV
32 if (!strcmp("--no-deref", argv[i])) {
33 ref_flags |= REF_NODEREF;
34 continue;
35 }
5b16b090
SP
36 if (!refname) {
37 refname = argv[i];
38 continue;
39 }
40 if (!value) {
41 value = argv[i];
42 continue;
43 }
44 if (!oldval) {
45 oldval = argv[i];
46 continue;
47 }
48 }
49 if (!refname || !value)
66bf85a4
LT
50 usage(git_update_ref_usage);
51
31fff305 52 if (get_sha1(value, sha1))
66bf85a4 53 die("%s: not a valid SHA1", value);
ac5409e4
JH
54
55 if (delete) {
56 if (oldval)
57 usage(git_update_ref_usage);
58 return delete_ref(refname, sha1);
59 }
60
a8e0d16d 61 hashclr(oldsha1);
ac5409e4 62 if (oldval && *oldval && get_sha1(oldval, oldsha1))
66bf85a4
LT
63 die("%s: not a valid old SHA1", oldval);
64
68db31cc 65 lock = lock_any_ref_for_update(refname, oldval ? oldsha1 : NULL, ref_flags);
5b16b090 66 if (!lock)
a2f9fe92 67 die("%s: cannot lock the ref", refname);
5b16b090 68 if (write_ref_sha1(lock, sha1, msg) < 0)
a2f9fe92 69 die("%s: cannot update the ref", refname);
66bf85a4
LT
70 return 0;
71}