5 #include "parse-options.h"
7 static const char * const git_symbolic_ref_usage[] = {
8 N_("git symbolic-ref [<options>] <name> [<ref>]"),
9 N_("git symbolic-ref -d [-q] <name>"),
13 static int check_symref(const char *HEAD, int quiet, int shorten, int print)
16 const char *refname = resolve_ref_unsafe(HEAD, 0, NULL, &flag);
19 die("No such ref: %s", HEAD);
20 else if (!(flag & REF_ISSYMREF)) {
22 die("ref %s is not a symbolic ref", HEAD);
28 refname = shorten_unambiguous_ref(refname, 0);
34 int cmd_symbolic_ref(int argc, const char **argv, const char *prefix)
36 int quiet = 0, delete = 0, shorten = 0, ret = 0;
37 const char *msg = NULL;
38 struct option options[] = {
40 N_("suppress error message for non-symbolic (detached) refs")),
41 OPT_BOOL('d', "delete", &delete, N_("delete symbolic ref")),
42 OPT_BOOL(0, "short", &shorten, N_("shorten ref output")),
43 OPT_STRING('m', NULL, &msg, N_("reason"), N_("reason of the update")),
47 git_config(git_default_config, NULL);
48 argc = parse_options(argc, argv, prefix, options,
49 git_symbolic_ref_usage, 0);
51 die("Refusing to perform update with empty message");
55 usage_with_options(git_symbolic_ref_usage, options);
56 ret = check_symref(argv[0], 1, 0, 0);
58 die("Cannot delete %s, not a symbolic ref", argv[0]);
59 if (!strcmp(argv[0], "HEAD"))
60 die("deleting '%s' is not allowed", argv[0]);
61 return delete_ref(NULL, argv[0], NULL, REF_NO_DEREF);
66 ret = check_symref(argv[0], quiet, shorten, 1);
69 if (!strcmp(argv[0], "HEAD") &&
70 !starts_with(argv[1], "refs/"))
71 die("Refusing to point HEAD outside of refs/");
72 ret = !!create_symref(argv[0], argv[1], msg);
75 usage_with_options(git_symbolic_ref_usage, options);