Merge branch 'nd/worktree-kill-parse-ref'
[git] / builtin / symbolic-ref.c
1 #include "builtin.h"
2 #include "config.h"
3 #include "cache.h"
4 #include "refs.h"
5 #include "parse-options.h"
6
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>"),
10         NULL
11 };
12
13 static int check_symref(const char *HEAD, int quiet, int shorten, int print)
14 {
15         unsigned char sha1[20];
16         int flag;
17         const char *refname = resolve_ref_unsafe(HEAD, 0, sha1, &flag);
18
19         if (!refname)
20                 die("No such ref: %s", HEAD);
21         else if (!(flag & REF_ISSYMREF)) {
22                 if (!quiet)
23                         die("ref %s is not a symbolic ref", HEAD);
24                 else
25                         return 1;
26         }
27         if (print) {
28                 if (shorten)
29                         refname = shorten_unambiguous_ref(refname, 0);
30                 puts(refname);
31         }
32         return 0;
33 }
34
35 int cmd_symbolic_ref(int argc, const char **argv, const char *prefix)
36 {
37         int quiet = 0, delete = 0, shorten = 0, ret = 0;
38         const char *msg = NULL;
39         struct option options[] = {
40                 OPT__QUIET(&quiet,
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")),
45                 OPT_END(),
46         };
47
48         git_config(git_default_config, NULL);
49         argc = parse_options(argc, argv, prefix, options,
50                              git_symbolic_ref_usage, 0);
51         if (msg && !*msg)
52                 die("Refusing to perform update with empty message");
53
54         if (delete) {
55                 if (argc != 1)
56                         usage_with_options(git_symbolic_ref_usage, options);
57                 ret = check_symref(argv[0], 1, 0, 0);
58                 if (ret)
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);
63         }
64
65         switch (argc) {
66         case 1:
67                 ret = check_symref(argv[0], quiet, shorten, 1);
68                 break;
69         case 2:
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);
74                 break;
75         default:
76                 usage_with_options(git_symbolic_ref_usage, options);
77         }
78         return ret;
79 }