2005-09-25 22:43:05 +04:00
|
|
|
#include "cache.h"
|
|
|
|
#include "refs.h"
|
2006-06-14 00:22:00 +04:00
|
|
|
#include "builtin.h"
|
2007-10-08 01:14:43 +04:00
|
|
|
#include "parse-options.h"
|
2005-09-25 22:43:05 +04:00
|
|
|
|
2007-10-08 01:14:43 +04:00
|
|
|
static const char * const git_update_ref_usage[] = {
|
2012-08-20 16:32:49 +04:00
|
|
|
N_("git update-ref [options] -d <refname> [<oldval>]"),
|
|
|
|
N_("git update-ref [options] <refname> <newval> [<oldval>]"),
|
2007-10-08 01:14:43 +04:00
|
|
|
NULL
|
|
|
|
};
|
2005-09-26 03:28:51 +04:00
|
|
|
|
2006-07-29 09:44:25 +04:00
|
|
|
int cmd_update_ref(int argc, const char **argv, const char *prefix)
|
2005-09-25 22:43:05 +04:00
|
|
|
{
|
2011-08-25 19:40:50 +04:00
|
|
|
const char *refname, *oldval, *msg = NULL;
|
2006-05-17 13:55:19 +04:00
|
|
|
unsigned char sha1[20], oldsha1[20];
|
2008-10-26 05:33:58 +03:00
|
|
|
int delete = 0, no_deref = 0, flags = 0;
|
2007-10-08 01:14:43 +04:00
|
|
|
struct option options[] = {
|
2012-08-20 16:32:49 +04:00
|
|
|
OPT_STRING( 'm', NULL, &msg, N_("reason"), N_("reason of the update")),
|
2012-08-20 16:32:55 +04:00
|
|
|
OPT_BOOLEAN('d', NULL, &delete, N_("delete the reference")),
|
2007-10-08 01:14:43 +04:00
|
|
|
OPT_BOOLEAN( 0 , "no-deref", &no_deref,
|
2012-08-20 16:32:49 +04:00
|
|
|
N_("update <refname> not the one it points to")),
|
2007-10-08 01:14:43 +04:00
|
|
|
OPT_END(),
|
|
|
|
};
|
2005-09-25 22:43:05 +04:00
|
|
|
|
2008-05-14 21:46:53 +04:00
|
|
|
git_config(git_default_config, NULL);
|
2009-05-23 22:53:12 +04:00
|
|
|
argc = parse_options(argc, argv, prefix, options, git_update_ref_usage,
|
|
|
|
0);
|
2007-10-08 01:14:43 +04:00
|
|
|
if (msg && !*msg)
|
|
|
|
die("Refusing to perform update with empty message.");
|
2006-05-17 13:55:19 +04:00
|
|
|
|
2006-09-27 12:58:57 +04:00
|
|
|
if (delete) {
|
2008-06-03 03:34:53 +04:00
|
|
|
if (argc < 1 || argc > 2)
|
2008-06-03 03:34:48 +04:00
|
|
|
usage_with_options(git_update_ref_usage, options);
|
|
|
|
refname = argv[0];
|
|
|
|
oldval = argv[1];
|
|
|
|
} else {
|
|
|
|
const char *value;
|
|
|
|
if (argc < 2 || argc > 3)
|
2007-10-08 01:14:43 +04:00
|
|
|
usage_with_options(git_update_ref_usage, options);
|
2008-06-03 03:34:48 +04:00
|
|
|
refname = argv[0];
|
|
|
|
value = argv[1];
|
|
|
|
oldval = argv[2];
|
|
|
|
if (get_sha1(value, sha1))
|
|
|
|
die("%s: not a valid SHA1", value);
|
2006-09-27 12:58:57 +04:00
|
|
|
}
|
|
|
|
|
2008-06-03 03:34:48 +04:00
|
|
|
hashclr(oldsha1); /* all-zero hash in case oldval is the empty string */
|
2006-09-27 12:58:57 +04:00
|
|
|
if (oldval && *oldval && get_sha1(oldval, oldsha1))
|
2005-09-25 22:43:05 +04:00
|
|
|
die("%s: not a valid old SHA1", oldval);
|
|
|
|
|
2008-10-26 05:33:58 +03:00
|
|
|
if (no_deref)
|
|
|
|
flags = REF_NODEREF;
|
2008-06-03 03:34:48 +04:00
|
|
|
if (delete)
|
2008-10-26 05:33:58 +03:00
|
|
|
return delete_ref(refname, oldval ? oldsha1 : NULL, flags);
|
2008-06-03 03:34:48 +04:00
|
|
|
else
|
|
|
|
return update_ref(msg, refname, sha1, oldval ? oldsha1 : NULL,
|
2008-10-26 05:33:58 +03:00
|
|
|
flags, DIE_ON_ERR);
|
2005-09-25 22:43:05 +04:00
|
|
|
}
|