Merge branch 'maint'
[git/debian.git] / builtin / symbolic-ref.c
blob801d62ece5d2ec84d027089c29fc58e279126ebb
1 #include "builtin.h"
2 #include "cache.h"
3 #include "refs.h"
4 #include "parse-options.h"
6 static const char * const git_symbolic_ref_usage[] = {
7 "git symbolic-ref [options] name [ref]",
8 NULL
9 };
11 static int shorten;
13 static void check_symref(const char *HEAD, int quiet)
15 unsigned char sha1[20];
16 int flag;
17 const char *refname = resolve_ref_unsafe(HEAD, sha1, 0, &flag);
19 if (!refname)
20 die("No such ref: %s", HEAD);
21 else if (!(flag & REF_ISSYMREF)) {
22 if (!quiet)
23 die("ref %s is not a symbolic ref", HEAD);
24 else
25 exit(1);
27 if (shorten)
28 refname = shorten_unambiguous_ref(refname, 0);
29 puts(refname);
32 int cmd_symbolic_ref(int argc, const char **argv, const char *prefix)
34 int quiet = 0;
35 const char *msg = NULL;
36 struct option options[] = {
37 OPT__QUIET(&quiet,
38 "suppress error message for non-symbolic (detached) refs"),
39 OPT_BOOL(0, "short", &shorten, "shorten ref output"),
40 OPT_STRING('m', NULL, &msg, "reason", "reason of the update"),
41 OPT_END(),
44 git_config(git_default_config, NULL);
45 argc = parse_options(argc, argv, prefix, options,
46 git_symbolic_ref_usage, 0);
47 if (msg &&!*msg)
48 die("Refusing to perform update with empty message");
49 switch (argc) {
50 case 1:
51 check_symref(argv[0], quiet);
52 break;
53 case 2:
54 if (!strcmp(argv[0], "HEAD") &&
55 prefixcmp(argv[1], "refs/"))
56 die("Refusing to point HEAD outside of refs/");
57 create_symref(argv[0], argv[1], msg);
58 break;
59 default:
60 usage_with_options(git_symbolic_ref_usage, options);
62 return 0;