]> git.ipfire.org Git - thirdparty/git.git/blob - builtin/symbolic-ref.c
clone: allow "--bare" with "-o"
[thirdparty/git.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 int flag;
16 const char *refname = resolve_ref_unsafe(HEAD, 0, NULL, &flag);
17
18 if (!refname)
19 die("No such ref: %s", HEAD);
20 else if (!(flag & REF_ISSYMREF)) {
21 if (!quiet)
22 die("ref %s is not a symbolic ref", HEAD);
23 else
24 return 1;
25 }
26 if (print) {
27 char *to_free = NULL;
28 if (shorten)
29 refname = to_free = shorten_unambiguous_ref(refname, 0);
30 puts(refname);
31 free(to_free);
32 }
33 return 0;
34 }
35
36 int cmd_symbolic_ref(int argc, const char **argv, const char *prefix)
37 {
38 int quiet = 0, delete = 0, shorten = 0, ret = 0;
39 const char *msg = NULL;
40 struct option options[] = {
41 OPT__QUIET(&quiet,
42 N_("suppress error message for non-symbolic (detached) refs")),
43 OPT_BOOL('d', "delete", &delete, N_("delete symbolic ref")),
44 OPT_BOOL(0, "short", &shorten, N_("shorten ref output")),
45 OPT_STRING('m', NULL, &msg, N_("reason"), N_("reason of the update")),
46 OPT_END(),
47 };
48
49 git_config(git_default_config, NULL);
50 argc = parse_options(argc, argv, prefix, options,
51 git_symbolic_ref_usage, 0);
52 if (msg && !*msg)
53 die("Refusing to perform update with empty message");
54
55 if (delete) {
56 if (argc != 1)
57 usage_with_options(git_symbolic_ref_usage, options);
58 ret = check_symref(argv[0], 1, 0, 0);
59 if (ret)
60 die("Cannot delete %s, not a symbolic ref", argv[0]);
61 if (!strcmp(argv[0], "HEAD"))
62 die("deleting '%s' is not allowed", argv[0]);
63 return delete_ref(NULL, argv[0], NULL, REF_NO_DEREF);
64 }
65
66 switch (argc) {
67 case 1:
68 ret = check_symref(argv[0], quiet, shorten, 1);
69 break;
70 case 2:
71 if (!strcmp(argv[0], "HEAD") &&
72 !starts_with(argv[1], "refs/"))
73 die("Refusing to point HEAD outside of refs/");
74 ret = !!create_symref(argv[0], argv[1], msg);
75 break;
76 default:
77 usage_with_options(git_symbolic_ref_usage, options);
78 }
79 return ret;
80 }