t1404: add a bunch of tests of D/F conflicts
[git/debian.git] / builtin / symbolic-ref.c
blobdf75cb9d4a21a76df3451f925d674b475dc26753
1 #include "builtin.h"
2 #include "config.h"
3 #include "cache.h"
4 #include "refs.h"
5 #include "parse-options.h"
7 static const char * const git_symbolic_ref_usage[] = {
8 N_("git symbolic-ref [<options>] <name> [<ref>]"),
9 N_("git symbolic-ref -d [-q] <name>"),
10 NULL
13 static int check_symref(const char *HEAD, int quiet, int shorten, int print)
15 unsigned char sha1[20];
16 int flag;
17 const char *refname = resolve_ref_unsafe(HEAD, 0, sha1, &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 return 1;
27 if (print) {
28 if (shorten)
29 refname = shorten_unambiguous_ref(refname, 0);
30 puts(refname);
32 return 0;
35 int cmd_symbolic_ref(int argc, const char **argv, const char *prefix)
37 int quiet = 0, delete = 0, shorten = 0, ret = 0;
38 const char *msg = NULL;
39 struct option options[] = {
40 OPT__QUIET(&quiet,
41 N_("suppress error message for non-symbolic (detached) refs")),
42 OPT_BOOL('d', "delete", &delete, N_("delete symbolic ref")),
43 OPT_BOOL(0, "short", &shorten, N_("shorten ref output")),
44 OPT_STRING('m', NULL, &msg, N_("reason"), N_("reason of the update")),
45 OPT_END(),
48 git_config(git_default_config, NULL);
49 argc = parse_options(argc, argv, prefix, options,
50 git_symbolic_ref_usage, 0);
51 if (msg && !*msg)
52 die("Refusing to perform update with empty message");
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]);
60 if (!strcmp(argv[0], "HEAD"))
61 die("deleting '%s' is not allowed", argv[0]);
62 return delete_ref(NULL, argv[0], NULL, REF_NODEREF);
65 switch (argc) {
66 case 1:
67 ret = check_symref(argv[0], quiet, shorten, 1);
68 break;
69 case 2:
70 if (!strcmp(argv[0], "HEAD") &&
71 !starts_with(argv[1], "refs/"))
72 die("Refusing to point HEAD outside of refs/");
73 ret = !!create_symref(argv[0], argv[1], msg);
74 break;
75 default:
76 usage_with_options(git_symbolic_ref_usage, options);
78 return ret;