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