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