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