builtin / symbolic-ref.con commit Merge branch 'mh/maint-parse-dirstat-fix' (8736c90)
   1#include "builtin.h"
   2#include "cache.h"
   3#include "refs.h"
   4#include "parse-options.h"
   5
   6static const char * const git_symbolic_ref_usage[] = {
   7        N_("git symbolic-ref [options] name [ref]"),
   8        NULL
   9};
  10
  11static int shorten;
  12
  13static void check_symref(const char *HEAD, int quiet)
  14{
  15        unsigned char sha1[20];
  16        int flag;
  17        const char *refname = resolve_ref_unsafe(HEAD, sha1, 0, &flag);
  18
  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);
  26        }
  27        if (shorten)
  28                refname = shorten_unambiguous_ref(refname, 0);
  29        puts(refname);
  30}
  31
  32int cmd_symbolic_ref(int argc, const char **argv, const char *prefix)
  33{
  34        int quiet = 0;
  35        const char *msg = NULL;
  36        struct option options[] = {
  37                OPT__QUIET(&quiet,
  38                        N_("suppress error message for non-symbolic (detached) refs")),
  39                OPT_BOOL(0, "short", &shorten, N_("shorten ref output")),
  40                OPT_STRING('m', NULL, &msg, N_("reason"), N_("reason of the update")),
  41                OPT_END(),
  42        };
  43
  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);
  61        }
  62        return 0;
  63}