5 #include "parse-options.h"
8 static const char * const git_symbolic_ref_usage
[] = {
9 N_("git symbolic-ref [-m <reason>] <name> <ref>"),
10 N_("git symbolic-ref [-q] [--short] [--no-recurse] <name>"),
11 N_("git symbolic-ref --delete [-q] <name>"),
15 static int check_symref(const char *HEAD
, int quiet
, int shorten
, int recurse
, int print
)
17 int resolve_flags
, flag
;
20 resolve_flags
= (recurse
? 0 : RESOLVE_REF_NO_RECURSE
);
21 refname
= resolve_ref_unsafe(HEAD
, resolve_flags
, NULL
, &flag
);
24 die("No such ref: %s", HEAD
);
25 else if (!(flag
& REF_ISSYMREF
)) {
27 die("ref %s is not a symbolic ref", HEAD
);
34 refname
= to_free
= shorten_unambiguous_ref(refname
, 0);
41 int cmd_symbolic_ref(int argc
, const char **argv
, const char *prefix
)
43 int quiet
= 0, delete = 0, shorten
= 0, recurse
= 1, ret
= 0;
44 const char *msg
= NULL
;
45 struct option options
[] = {
47 N_("suppress error message for non-symbolic (detached) refs")),
48 OPT_BOOL('d', "delete", &delete, N_("delete symbolic ref")),
49 OPT_BOOL(0, "short", &shorten
, N_("shorten ref output")),
50 OPT_BOOL(0, "recurse", &recurse
, N_("recursively dereference (default)")),
51 OPT_STRING('m', NULL
, &msg
, N_("reason"), N_("reason of the update")),
55 git_config(git_default_config
, NULL
);
56 argc
= parse_options(argc
, argv
, prefix
, options
,
57 git_symbolic_ref_usage
, 0);
59 die("Refusing to perform update with empty message");
63 usage_with_options(git_symbolic_ref_usage
, options
);
64 ret
= check_symref(argv
[0], 1, 0, 0, 0);
66 die("Cannot delete %s, not a symbolic ref", argv
[0]);
67 if (!strcmp(argv
[0], "HEAD"))
68 die("deleting '%s' is not allowed", argv
[0]);
69 return delete_ref(NULL
, argv
[0], NULL
, REF_NO_DEREF
);
74 ret
= check_symref(argv
[0], quiet
, shorten
, recurse
, 1);
77 if (!strcmp(argv
[0], "HEAD") &&
78 !starts_with(argv
[1], "refs/"))
79 die("Refusing to point HEAD outside of refs/");
80 if (check_refname_format(argv
[1], REFNAME_ALLOW_ONELEVEL
) < 0)
81 die("Refusing to set '%s' to invalid ref '%s'", argv
[0], argv
[1]);
82 ret
= !!create_symref(argv
[0], argv
[1], msg
);
85 usage_with_options(git_symbolic_ref_usage
, options
);