builtin/rebase: fix leaking `commit.gpgsign` value
[git/gitster.git] / builtin / symbolic-ref.c
blob81abdd170fe73e2a5aa836cdf3069e0eb426c802
1 #include "builtin.h"
2 #include "config.h"
3 #include "gettext.h"
4 #include "refs.h"
5 #include "parse-options.h"
6 #include "strbuf.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 = refs_resolve_ref_unsafe(get_main_ref_store(the_repository),
22 HEAD, resolve_flags, NULL, &flag);
24 if (!refname)
25 die("No such ref: %s", HEAD);
26 else if (!(flag & REF_ISSYMREF)) {
27 if (!quiet)
28 die("ref %s is not a symbolic ref", HEAD);
29 else
30 return 1;
32 if (print) {
33 char *to_free = NULL;
34 if (shorten)
35 refname = to_free = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository),
36 refname,
37 0);
38 puts(refname);
39 free(to_free);
41 return 0;
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[] = {
49 OPT__QUIET(&quiet,
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")),
55 OPT_END(),
58 git_config(git_default_config, NULL);
59 argc = parse_options(argc, argv, prefix, options,
60 git_symbolic_ref_usage, 0);
61 if (msg && !*msg)
62 die("Refusing to perform update with empty message");
64 if (delete) {
65 if (argc != 1)
66 usage_with_options(git_symbolic_ref_usage, options);
67 ret = check_symref(argv[0], 1, 0, 0, 0);
68 if (ret)
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);
76 switch (argc) {
77 case 1:
78 ret = check_symref(argv[0], quiet, shorten, recurse, 1);
79 break;
80 case 2:
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);
88 break;
89 default:
90 usage_with_options(git_symbolic_ref_usage, options);
92 return ret;