blob: 378dc1b7a6bb4d56d301a34a4d44dae2f9a37e44 [file] [log] [blame]
Linus Torvalds66bf85a2005-09-25 11:43:05 -07001#include "cache.h"
2#include "refs.h"
Lukas Sandström854b4622006-06-13 22:22:00 +02003#include "builtin.h"
Pierre Habouzit89942be2007-10-07 23:14:43 +02004#include "parse-options.h"
Linus Torvalds66bf85a2005-09-25 11:43:05 -07005
Pierre Habouzit89942be2007-10-07 23:14:43 +02006static const char * const git_update_ref_usage[] = {
Stephan Beyer1b1dd232008-07-13 15:36:15 +02007 "git update-ref [options] -d <refname> [<oldval>]",
8 "git update-ref [options] <refname> <newval> [<oldval>]",
Pierre Habouzit89942be2007-10-07 23:14:43 +02009 NULL
10};
Junio C Hamano152da3d2005-09-25 16:28:51 -070011
Linus Torvaldsa633fca2006-07-28 22:44:25 -070012int cmd_update_ref(int argc, const char **argv, const char *prefix)
Linus Torvalds66bf85a2005-09-25 11:43:05 -070013{
Karl Hasselström973a70e2008-06-03 01:34:48 +020014 const char *refname, *oldval, *msg=NULL;
Shawn Pearce5b16b092006-05-17 05:55:19 -040015 unsigned char sha1[20], oldsha1[20];
Miklos Vajna569740b2008-10-26 03:33:58 +010016 int delete = 0, no_deref = 0, flags = 0;
Pierre Habouzit89942be2007-10-07 23:14:43 +020017 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 };
Linus Torvalds66bf85a2005-09-25 11:43:05 -070024
Johannes Schindelinef90d6d2008-05-14 18:46:53 +010025 git_config(git_default_config, NULL);
Pierre Habouzit89942be2007-10-07 23:14:43 +020026 argc = parse_options(argc, argv, options, git_update_ref_usage, 0);
27 if (msg && !*msg)
28 die("Refusing to perform update with empty message.");
Shawn Pearce5b16b092006-05-17 05:55:19 -040029
Junio C Hamanoac5409e2006-09-27 01:58:57 -070030 if (delete) {
Karl Hasselström3fe8dce2008-06-03 01:34:53 +020031 if (argc < 1 || argc > 2)
Pierre Habouzit89942be2007-10-07 23:14:43 +020032 usage_with_options(git_update_ref_usage, options);
Karl Hasselström973a70e2008-06-03 01:34:48 +020033 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);
Junio C Hamanoac5409e2006-09-27 01:58:57 -070044 }
45
Karl Hasselström973a70e2008-06-03 01:34:48 +020046 hashclr(oldsha1); /* all-zero hash in case oldval is the empty string */
Junio C Hamanoac5409e2006-09-27 01:58:57 -070047 if (oldval && *oldval && get_sha1(oldval, oldsha1))
Linus Torvalds66bf85a2005-09-25 11:43:05 -070048 die("%s: not a valid old SHA1", oldval);
49
Miklos Vajna569740b2008-10-26 03:33:58 +010050 if (no_deref)
51 flags = REF_NODEREF;
Karl Hasselström973a70e2008-06-03 01:34:48 +020052 if (delete)
Miklos Vajna569740b2008-10-26 03:33:58 +010053 return delete_ref(refname, oldval ? oldsha1 : NULL, flags);
Karl Hasselström973a70e2008-06-03 01:34:48 +020054 else
55 return update_ref(msg, refname, sha1, oldval ? oldsha1 : NULL,
Miklos Vajna569740b2008-10-26 03:33:58 +010056 flags, DIE_ON_ERR);
Linus Torvalds66bf85a2005-09-25 11:43:05 -070057}