builtin-check-ref-format.con commit Merge branch 'np/maint-sideband-favor-status' (e36e6c0)
   1/*
   2 * GIT - The information manager from hell
   3 */
   4
   5#include "cache.h"
   6#include "refs.h"
   7#include "builtin.h"
   8#include "strbuf.h"
   9
  10static const char builtin_check_ref_format_usage[] =
  11"git check-ref-format [--print] <refname>\n"
  12"   or: git check-ref-format --branch <branchname-shorthand>";
  13
  14/*
  15 * Replace each run of adjacent slashes in src with a single slash,
  16 * and write the result to dst.
  17 *
  18 * This function is similar to normalize_path_copy(), but stripped down
  19 * to meet check_ref_format's simpler needs.
  20 */
  21static void collapse_slashes(char *dst, const char *src)
  22{
  23        char ch;
  24        char prev = '\0';
  25
  26        while ((ch = *src++) != '\0') {
  27                if (prev == '/' && ch == prev)
  28                        continue;
  29
  30                *dst++ = ch;
  31                prev = ch;
  32        }
  33        *dst = '\0';
  34}
  35
  36int cmd_check_ref_format(int argc, const char **argv, const char *prefix)
  37{
  38        if (argc == 3 && !strcmp(argv[1], "--branch")) {
  39                struct strbuf sb = STRBUF_INIT;
  40
  41                if (strbuf_check_branch_ref(&sb, argv[2]))
  42                        die("'%s' is not a valid branch name", argv[2]);
  43                printf("%s\n", sb.buf + 11);
  44                exit(0);
  45        }
  46        if (argc == 3 && !strcmp(argv[1], "--print")) {
  47                char *refname = xmalloc(strlen(argv[2]) + 1);
  48
  49                if (check_ref_format(argv[2]))
  50                        exit(1);
  51                collapse_slashes(refname, argv[2]);
  52                printf("%s\n", refname);
  53                exit(0);
  54        }
  55        if (argc != 2)
  56                usage(builtin_check_ref_format_usage);
  57        return !!check_ref_format(argv[1]);
  58}