add 'git credential' plumbing command
[git/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
42b00599
JK
11static int shorten;
12
a0f4280f 13static void check_symref(const char *HEAD, int quiet)
8098a178
JH
14{
15 unsigned char sha1[20];
8da19775 16 int flag;
42b00599 17 const char *refname = resolve_ref_unsafe(HEAD, sha1, 0, &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
25 exit(1);
26 }
42b00599
JK
27 if (shorten)
28 refname = shorten_unambiguous_ref(refname, 0);
29 puts(refname);
8098a178
JH
30}
31
640ce105 32int cmd_symbolic_ref(int argc, const char **argv, const char *prefix)
8098a178 33{
a0f4280f 34 int quiet = 0;
8b5157e4 35 const char *msg = NULL;
78558614 36 struct option options[] = {
8c839683
JN
37 OPT__QUIET(&quiet,
38 "suppress error message for non-symbolic (detached) refs"),
42b00599 39 OPT_BOOL(0, "short", &shorten, "shorten ref output"),
78558614
PH
40 OPT_STRING('m', NULL, &msg, "reason", "reason of the update"),
41 OPT_END(),
42 };
a0f4280f 43
ef90d6d4 44 git_config(git_default_config, NULL);
37782920
SB
45 argc = parse_options(argc, argv, prefix, options,
46 git_symbolic_ref_usage, 0);
78558614
PH
47 if (msg &&!*msg)
48 die("Refusing to perform update with empty message");
8098a178 49 switch (argc) {
78558614
PH
50 case 1:
51 check_symref(argv[0], quiet);
8098a178 52 break;
78558614 53 case 2:
afe5d3d5 54 if (!strcmp(argv[0], "HEAD") &&
e9cc02f0
JK
55 prefixcmp(argv[1], "refs/"))
56 die("Refusing to point HEAD outside of refs/");
78558614 57 create_symref(argv[0], argv[1], msg);
8098a178
JH
58 break;
59 default:
78558614 60 usage_with_options(git_symbolic_ref_usage, options);
8098a178
JH
61 }
62 return 0;
63}