]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/symbolic-ref.c
config: don't include config.h by default
[thirdparty/git.git] / builtin / symbolic-ref.c
CommitLineData
640ce105 1#include "builtin.h"
b2141fc1 2#include "config.h"
8098a178 3#include "cache.h"
8da19775 4#include "refs.h"
78558614 5#include "parse-options.h"
8098a178 6
78558614 7static const char * const git_symbolic_ref_usage[] = {
9c9b4f2f
AH
8 N_("git symbolic-ref [<options>] <name> [<ref>]"),
9 N_("git symbolic-ref -d [-q] <name>"),
78558614
PH
10 NULL
11};
8098a178 12
9ab55daa 13static int check_symref(const char *HEAD, int quiet, int shorten, int print)
8098a178
JH
14{
15 unsigned char sha1[20];
8da19775 16 int flag;
7695d118 17 const char *refname = resolve_ref_unsafe(HEAD, 0, sha1, &flag);
ed378ec7 18
42b00599 19 if (!refname)
8098a178 20 die("No such ref: %s", HEAD);
a0f4280f
JH
21 else if (!(flag & REF_ISSYMREF)) {
22 if (!quiet)
23 die("ref %s is not a symbolic ref", HEAD);
24 else
9ab55daa
JH
25 return 1;
26 }
27 if (print) {
28 if (shorten)
29 refname = shorten_unambiguous_ref(refname, 0);
30 puts(refname);
a0f4280f 31 }
9ab55daa 32 return 0;
8098a178
JH
33}
34
640ce105 35int cmd_symbolic_ref(int argc, const char **argv, const char *prefix)
8098a178 36{
9ab55daa 37 int quiet = 0, delete = 0, shorten = 0, ret = 0;
8b5157e4 38 const char *msg = NULL;
78558614 39 struct option options[] = {
8c839683 40 OPT__QUIET(&quiet,
b10bf3fa 41 N_("suppress error message for non-symbolic (detached) refs")),
9ab55daa 42 OPT_BOOL('d', "delete", &delete, N_("delete symbolic ref")),
b10bf3fa
NTND
43 OPT_BOOL(0, "short", &shorten, N_("shorten ref output")),
44 OPT_STRING('m', NULL, &msg, N_("reason"), N_("reason of the update")),
78558614
PH
45 OPT_END(),
46 };
a0f4280f 47
ef90d6d4 48 git_config(git_default_config, NULL);
37782920
SB
49 argc = parse_options(argc, argv, prefix, options,
50 git_symbolic_ref_usage, 0);
c01499ef 51 if (msg && !*msg)
78558614 52 die("Refusing to perform update with empty message");
9ab55daa
JH
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]);
12cfa792
JH
60 if (!strcmp(argv[0], "HEAD"))
61 die("deleting '%s' is not allowed", argv[0]);
755b49ae 62 return delete_ref(NULL, argv[0], NULL, REF_NODEREF);
9ab55daa
JH
63 }
64
8098a178 65 switch (argc) {
78558614 66 case 1:
9ab55daa 67 ret = check_symref(argv[0], quiet, shorten, 1);
8098a178 68 break;
78558614 69 case 2:
afe5d3d5 70 if (!strcmp(argv[0], "HEAD") &&
59556548 71 !starts_with(argv[1], "refs/"))
e9cc02f0 72 die("Refusing to point HEAD outside of refs/");
3e4068ed 73 ret = !!create_symref(argv[0], argv[1], msg);
8098a178
JH
74 break;
75 default:
78558614 76 usage_with_options(git_symbolic_ref_usage, options);
8098a178 77 }
9ab55daa 78 return ret;
8098a178 79}