]> git.ipfire.org Git - thirdparty/git.git/blame - builtin-update-ref.c
config.c: Escape backslashes in section names properly
[thirdparty/git.git] / builtin-update-ref.c
CommitLineData
66bf85a4
LT
1#include "cache.h"
2#include "refs.h"
854b4629 3#include "builtin.h"
89942be6 4#include "parse-options.h"
66bf85a4 5
89942be6
PH
6static const char * const git_update_ref_usage[] = {
7 "git-update-ref [options] -d <refname> <oldval>",
8 "git-update-ref [options] <refname> <newval> [<oldval>]",
9 NULL
10};
152da3df 11
a633fca0 12int cmd_update_ref(int argc, const char **argv, const char *prefix)
66bf85a4 13{
89942be6 14 const char *refname, *value, *oldval, *msg=NULL;
5b16b090 15 unsigned char sha1[20], oldsha1[20];
89942be6
PH
16 int delete = 0, no_deref = 0;
17 struct option options[] = {
18 OPT_STRING( 'm', NULL, &msg, "reason", "reason of the update"),
19 OPT_BOOLEAN('d', NULL, &delete, "deletes the reference"),
20 OPT_BOOLEAN( 0 , "no-deref", &no_deref,
21 "update <refname> not the one it points to"),
22 OPT_END(),
23 };
66bf85a4 24
84a9b58c 25 git_config(git_default_config);
89942be6
PH
26 argc = parse_options(argc, argv, options, git_update_ref_usage, 0);
27 if (msg && !*msg)
28 die("Refusing to perform update with empty message.");
5b16b090 29
89942be6
PH
30 if (argc < 2 || argc > 3)
31 usage_with_options(git_update_ref_usage, options);
32 refname = argv[0];
33 value = argv[1];
34 oldval = argv[2];
66bf85a4 35
31fff305 36 if (get_sha1(value, sha1))
66bf85a4 37 die("%s: not a valid SHA1", value);
ac5409e4
JH
38
39 if (delete) {
40 if (oldval)
89942be6 41 usage_with_options(git_update_ref_usage, options);
ac5409e4
JH
42 return delete_ref(refname, sha1);
43 }
44
a8e0d16d 45 hashclr(oldsha1);
ac5409e4 46 if (oldval && *oldval && get_sha1(oldval, oldsha1))
66bf85a4
LT
47 die("%s: not a valid old SHA1", oldval);
48
3d9f037c 49 return update_ref(msg, refname, sha1, oldval ? oldsha1 : NULL,
89942be6 50 no_deref ? REF_NODEREF : 0, DIE_ON_ERR);
66bf85a4 51}