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