revisions: refactor handle_revision_opt into parse_revision_opt.
[git/git-p4.git] / builtin-update-ref.c
blobd90d11d2e35c38baab32f05f58125aaf1baee6cc
1 #include "cache.h"
2 #include "refs.h"
3 #include "builtin.h"
4 #include "parse-options.h"
6 static 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
12 int cmd_update_ref(int argc, const char **argv, const char *prefix)
14 const char *refname, *oldval, *msg=NULL;
15 unsigned char sha1[20], oldsha1[20];
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(),
25 git_config(git_default_config, NULL);
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.");
30 if (delete) {
31 if (argc < 1 || argc > 2)
32 usage_with_options(git_update_ref_usage, options);
33 refname = argv[0];
34 oldval = argv[1];
35 } else {
36 const char *value;
37 if (argc < 2 || argc > 3)
38 usage_with_options(git_update_ref_usage, options);
39 refname = argv[0];
40 value = argv[1];
41 oldval = argv[2];
42 if (get_sha1(value, sha1))
43 die("%s: not a valid SHA1", value);
46 hashclr(oldsha1); /* all-zero hash in case oldval is the empty string */
47 if (oldval && *oldval && get_sha1(oldval, oldsha1))
48 die("%s: not a valid old SHA1", oldval);
50 if (delete)
51 return delete_ref(refname, oldval ? oldsha1 : NULL);
52 else
53 return update_ref(msg, refname, sha1, oldval ? oldsha1 : NULL,
54 no_deref ? REF_NODEREF : 0, DIE_ON_ERR);