]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/symbolic-ref.c
commit,status: describe --porcelain just like push
[thirdparty/git.git] / builtin / symbolic-ref.c
CommitLineData
640ce105 1#include "builtin.h"
8098a178 2#include "cache.h"
8da19775 3#include "refs.h"
78558614 4#include "parse-options.h"
8098a178 5
78558614 6static const char * const git_symbolic_ref_usage[] = {
1b1dd23f 7 "git symbolic-ref [options] name [ref]",
78558614
PH
8 NULL
9};
8098a178 10
a0f4280f 11static void check_symref(const char *HEAD, int quiet)
8098a178
JH
12{
13 unsigned char sha1[20];
8da19775
JH
14 int flag;
15 const char *refs_heads_master = resolve_ref(HEAD, sha1, 0, &flag);
ed378ec7
LT
16
17 if (!refs_heads_master)
8098a178 18 die("No such ref: %s", HEAD);
a0f4280f
JH
19 else if (!(flag & REF_ISSYMREF)) {
20 if (!quiet)
21 die("ref %s is not a symbolic ref", HEAD);
22 else
23 exit(1);
24 }
ed378ec7 25 puts(refs_heads_master);
8098a178
JH
26}
27
640ce105 28int cmd_symbolic_ref(int argc, const char **argv, const char *prefix)
8098a178 29{
a0f4280f 30 int quiet = 0;
8b5157e4 31 const char *msg = NULL;
78558614 32 struct option options[] = {
8c839683
JN
33 OPT__QUIET(&quiet,
34 "suppress error message for non-symbolic (detached) refs"),
78558614
PH
35 OPT_STRING('m', NULL, &msg, "reason", "reason of the update"),
36 OPT_END(),
37 };
a0f4280f 38
ef90d6d4 39 git_config(git_default_config, NULL);
37782920
SB
40 argc = parse_options(argc, argv, prefix, options,
41 git_symbolic_ref_usage, 0);
78558614
PH
42 if (msg &&!*msg)
43 die("Refusing to perform update with empty message");
8098a178 44 switch (argc) {
78558614
PH
45 case 1:
46 check_symref(argv[0], quiet);
8098a178 47 break;
78558614 48 case 2:
afe5d3d5 49 if (!strcmp(argv[0], "HEAD") &&
e9cc02f0
JK
50 prefixcmp(argv[1], "refs/"))
51 die("Refusing to point HEAD outside of refs/");
78558614 52 create_symref(argv[0], argv[1], msg);
8098a178
JH
53 break;
54 default:
78558614 55 usage_with_options(git_symbolic_ref_usage, options);
8098a178
JH
56 }
57 return 0;
58}