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