2006-08-03 23:24:38 +08:00
|
|
|
#include "builtin.h"
|
2005-10-01 05:26:57 +08:00
|
|
|
#include "cache.h"
|
2006-09-21 13:02:01 +08:00
|
|
|
#include "refs.h"
|
2007-10-08 05:18:23 +08:00
|
|
|
#include "parse-options.h"
|
2005-10-01 05:26:57 +08:00
|
|
|
|
2007-10-08 05:18:23 +08:00
|
|
|
static const char * const git_symbolic_ref_usage[] = {
|
2012-08-20 20:32:46 +08:00
|
|
|
N_("git symbolic-ref [options] name [ref]"),
|
2012-10-21 19:32:33 +08:00
|
|
|
N_("git symbolic-ref -d [-q] name"),
|
2007-10-08 05:18:23 +08:00
|
|
|
NULL
|
|
|
|
};
|
2005-10-01 05:26:57 +08:00
|
|
|
|
2012-10-21 19:32:33 +08:00
|
|
|
static int check_symref(const char *HEAD, int quiet, int shorten, int print)
|
2005-10-01 05:26:57 +08:00
|
|
|
{
|
|
|
|
unsigned char sha1[20];
|
2006-09-21 13:02:01 +08:00
|
|
|
int flag;
|
2012-02-28 06:10:38 +08:00
|
|
|
const char *refname = resolve_ref_unsafe(HEAD, sha1, 0, &flag);
|
2006-09-12 11:17:35 +08:00
|
|
|
|
2012-02-28 06:10:38 +08:00
|
|
|
if (!refname)
|
2005-10-01 05:26:57 +08:00
|
|
|
die("No such ref: %s", HEAD);
|
2007-01-16 05:56:05 +08:00
|
|
|
else if (!(flag & REF_ISSYMREF)) {
|
|
|
|
if (!quiet)
|
|
|
|
die("ref %s is not a symbolic ref", HEAD);
|
|
|
|
else
|
2012-10-21 19:32:33 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (print) {
|
|
|
|
if (shorten)
|
|
|
|
refname = shorten_unambiguous_ref(refname, 0);
|
|
|
|
puts(refname);
|
2007-01-16 05:56:05 +08:00
|
|
|
}
|
2012-10-21 19:32:33 +08:00
|
|
|
return 0;
|
2005-10-01 05:26:57 +08:00
|
|
|
}
|
|
|
|
|
2006-08-03 23:24:38 +08:00
|
|
|
int cmd_symbolic_ref(int argc, const char **argv, const char *prefix)
|
2005-10-01 05:26:57 +08:00
|
|
|
{
|
2012-10-21 19:32:33 +08:00
|
|
|
int quiet = 0, delete = 0, shorten = 0, ret = 0;
|
2007-01-27 06:26:10 +08:00
|
|
|
const char *msg = NULL;
|
2007-10-08 05:18:23 +08:00
|
|
|
struct option options[] = {
|
2010-11-09 03:54:48 +08:00
|
|
|
OPT__QUIET(&quiet,
|
2012-08-20 20:32:46 +08:00
|
|
|
N_("suppress error message for non-symbolic (detached) refs")),
|
2012-10-21 19:32:33 +08:00
|
|
|
OPT_BOOL('d', "delete", &delete, N_("delete symbolic ref")),
|
2012-08-20 20:32:46 +08:00
|
|
|
OPT_BOOL(0, "short", &shorten, N_("shorten ref output")),
|
|
|
|
OPT_STRING('m', NULL, &msg, N_("reason"), N_("reason of the update")),
|
2007-10-08 05:18:23 +08:00
|
|
|
OPT_END(),
|
|
|
|
};
|
2007-01-16 05:56: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_symbolic_ref_usage, 0);
|
2013-10-17 01:26:39 +08:00
|
|
|
if (msg && !*msg)
|
2007-10-08 05:18:23 +08:00
|
|
|
die("Refusing to perform update with empty message");
|
2012-10-21 19:32:33 +08:00
|
|
|
|
|
|
|
if (delete) {
|
|
|
|
if (argc != 1)
|
|
|
|
usage_with_options(git_symbolic_ref_usage, options);
|
|
|
|
ret = check_symref(argv[0], 1, 0, 0);
|
|
|
|
if (ret)
|
|
|
|
die("Cannot delete %s, not a symbolic ref", argv[0]);
|
|
|
|
return delete_ref(argv[0], NULL, REF_NODEREF);
|
|
|
|
}
|
|
|
|
|
2005-10-01 05:26:57 +08:00
|
|
|
switch (argc) {
|
2007-10-08 05:18:23 +08:00
|
|
|
case 1:
|
2012-10-21 19:32:33 +08:00
|
|
|
ret = check_symref(argv[0], quiet, shorten, 1);
|
2005-10-01 05:26:57 +08:00
|
|
|
break;
|
2007-10-08 05:18:23 +08:00
|
|
|
case 2:
|
2009-01-29 16:33:02 +08:00
|
|
|
if (!strcmp(argv[0], "HEAD") &&
|
2009-02-14 02:26:09 +08:00
|
|
|
prefixcmp(argv[1], "refs/"))
|
|
|
|
die("Refusing to point HEAD outside of refs/");
|
2007-10-08 05:18:23 +08:00
|
|
|
create_symref(argv[0], argv[1], msg);
|
2005-10-01 05:26:57 +08:00
|
|
|
break;
|
|
|
|
default:
|
2007-10-08 05:18:23 +08:00
|
|
|
usage_with_options(git_symbolic_ref_usage, options);
|
2005-10-01 05:26:57 +08:00
|
|
|
}
|
2012-10-21 19:32:33 +08:00
|
|
|
return ret;
|
2005-10-01 05:26:57 +08:00
|
|
|
}
|