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)
15 unsigned char sha1[20];
17 const char *refname = resolve_ref_unsafe(HEAD, 0, sha1, &flag);
20 die("No such ref: %s", HEAD);
21 else if (!(flag & REF_ISSYMREF)) {
23 die("ref %s is not a symbolic ref", HEAD);
29 refname = shorten_unambiguous_ref(refname, 0);
35 int cmd_symbolic_ref(int argc, const char **argv, const char *prefix)
37 int quiet = 0, delete = 0, shorten = 0, ret = 0;
38 const char *msg = NULL;
39 struct option options[] = {
41 N_("suppress error message for non-symbolic (detached) refs")),
42 OPT_BOOL('d', "delete", &delete, N_("delete symbolic ref")),
43 OPT_BOOL(0, "short", &shorten, N_("shorten ref output")),
44 OPT_STRING('m', NULL, &msg, N_("reason"), N_("reason of the update")),
48 git_config(git_default_config, NULL);
49 argc = parse_options(argc, argv, prefix, options,
50 git_symbolic_ref_usage, 0);
52 die("Refusing to perform update with empty message");
56 usage_with_options(git_symbolic_ref_usage, options);
57 ret = check_symref(argv[0], 1, 0, 0);
59 die("Cannot delete %s, not a symbolic ref", argv[0]);
60 if (!strcmp(argv[0], "HEAD"))
61 die("deleting '%s' is not allowed", argv[0]);
62 return delete_ref(NULL, argv[0], NULL, REF_NODEREF);
67 ret = check_symref(argv[0], quiet, shorten, 1);
70 if (!strcmp(argv[0], "HEAD") &&
71 !starts_with(argv[1], "refs/"))
72 die("Refusing to point HEAD outside of refs/");
73 ret = !!create_symref(argv[0], argv[1], msg);
76 usage_with_options(git_symbolic_ref_usage, options);