notes.c: use designated initializers for clarity
[git/debian.git] / builtin / symbolic-ref.c
blob10198a74faee55d0dfcfccb36b75c2e5bc5da8f5
1 #include "builtin.h"
2 #include "config.h"
3 #include "cache.h"
4 #include "gettext.h"
5 #include "refs.h"
6 #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>"),
12 NULL
15 static int check_symref(const char *HEAD, int quiet, int shorten, int recurse, int print)
17 int resolve_flags, flag;
18 const char *refname;
20 resolve_flags = (recurse ? 0 : RESOLVE_REF_NO_RECURSE);
21 refname = resolve_ref_unsafe(HEAD, resolve_flags, NULL, &flag);
23 if (!refname)
24 die("No such ref: %s", HEAD);
25 else if (!(flag & REF_ISSYMREF)) {
26 if (!quiet)
27 die("ref %s is not a symbolic ref", HEAD);
28 else
29 return 1;
31 if (print) {
32 char *to_free = NULL;
33 if (shorten)
34 refname = to_free = shorten_unambiguous_ref(refname, 0);
35 puts(refname);
36 free(to_free);
38 return 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[] = {
46 OPT__QUIET(&quiet,
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")),
52 OPT_END(),
55 git_config(git_default_config, NULL);
56 argc = parse_options(argc, argv, prefix, options,
57 git_symbolic_ref_usage, 0);
58 if (msg && !*msg)
59 die("Refusing to perform update with empty message");
61 if (delete) {
62 if (argc != 1)
63 usage_with_options(git_symbolic_ref_usage, options);
64 ret = check_symref(argv[0], 1, 0, 0, 0);
65 if (ret)
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);
72 switch (argc) {
73 case 1:
74 ret = check_symref(argv[0], quiet, shorten, recurse, 1);
75 break;
76 case 2:
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);
83 break;
84 default:
85 usage_with_options(git_symbolic_ref_usage, options);
87 return ret;