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
= refs_resolve_ref_unsafe(get_main_ref_store(the_repository
),
22 HEAD
, resolve_flags
, NULL
, &flag
);
25 die("No such ref: %s", HEAD
);
26 else if (!(flag
& REF_ISSYMREF
)) {
28 die("ref %s is not a symbolic ref", HEAD
);
35 refname
= to_free
= refs_shorten_unambiguous_ref(get_main_ref_store(the_repository
),
44 int cmd_symbolic_ref(int argc
, const char **argv
, const char *prefix
)
46 int quiet
= 0, delete = 0, shorten
= 0, recurse
= 1, ret
= 0;
47 const char *msg
= NULL
;
48 struct option options
[] = {
50 N_("suppress error message for non-symbolic (detached) refs")),
51 OPT_BOOL('d', "delete", &delete, N_("delete symbolic ref")),
52 OPT_BOOL(0, "short", &shorten
, N_("shorten ref output")),
53 OPT_BOOL(0, "recurse", &recurse
, N_("recursively dereference (default)")),
54 OPT_STRING('m', NULL
, &msg
, N_("reason"), N_("reason of the update")),
58 git_config(git_default_config
, NULL
);
59 argc
= parse_options(argc
, argv
, prefix
, options
,
60 git_symbolic_ref_usage
, 0);
62 die("Refusing to perform update with empty message");
66 usage_with_options(git_symbolic_ref_usage
, options
);
67 ret
= check_symref(argv
[0], 1, 0, 0, 0);
69 die("Cannot delete %s, not a symbolic ref", argv
[0]);
70 if (!strcmp(argv
[0], "HEAD"))
71 die("deleting '%s' is not allowed", argv
[0]);
72 return refs_delete_ref(get_main_ref_store(the_repository
),
73 NULL
, argv
[0], NULL
, REF_NO_DEREF
);
78 ret
= check_symref(argv
[0], quiet
, shorten
, recurse
, 1);
81 if (!strcmp(argv
[0], "HEAD") &&
82 !starts_with(argv
[1], "refs/"))
83 die("Refusing to point HEAD outside of refs/");
84 if (check_refname_format(argv
[1], REFNAME_ALLOW_ONELEVEL
) < 0)
85 die("Refusing to set '%s' to invalid ref '%s'", argv
[0], argv
[1]);
86 ret
= !!refs_update_symref(get_main_ref_store(the_repository
),
87 argv
[0], argv
[1], msg
);
90 usage_with_options(git_symbolic_ref_usage
, options
);