rev-parse.con commit Merge branch 'jc/diff' (e9b5b75)
   1/*
   2 * rev-parse.c
   3 *
   4 * Copyright (C) Linus Torvalds, 2005
   5 */
   6#include "cache.h"
   7#include "commit.h"
   8#include "refs.h"
   9#include "quote.h"
  10
  11#define DO_REVS         1
  12#define DO_NOREV        2
  13#define DO_FLAGS        4
  14#define DO_NONFLAGS     8
  15static int filter = ~0;
  16
  17static char *def = NULL;
  18
  19#define NORMAL 0
  20#define REVERSED 1
  21static int show_type = NORMAL;
  22static int symbolic = 0;
  23static int abbrev = 0;
  24static int output_sq = 0;
  25
  26static int revs_count = 0;
  27
  28/*
  29 * Some arguments are relevant "revision" arguments,
  30 * others are about output format or other details.
  31 * This sorts it all out.
  32 */
  33static int is_rev_argument(const char *arg)
  34{
  35        static const char *rev_args[] = {
  36                "--all",
  37                "--bisect",
  38                "--dense",
  39                "--header",
  40                "--max-age=",
  41                "--max-count=",
  42                "--min-age=",
  43                "--no-merges",
  44                "--objects",
  45                "--objects-edge",
  46                "--parents",
  47                "--pretty",
  48                "--sparse",
  49                "--topo-order",
  50                "--date-order",
  51                "--unpacked",
  52                NULL
  53        };
  54        const char **p = rev_args;
  55
  56        /* accept -<digit>, like traditional "head" */
  57        if ((*arg == '-') && isdigit(arg[1]))
  58                return 1;
  59
  60        for (;;) {
  61                const char *str = *p++;
  62                int len;
  63                if (!str)
  64                        return 0;
  65                len = strlen(str);
  66                if (!strcmp(arg, str) ||
  67                    (str[len-1] == '=' && !strncmp(arg, str, len)))
  68                        return 1;
  69        }
  70}
  71
  72/* Output argument as a string, either SQ or normal */
  73static void show(const char *arg)
  74{
  75        if (output_sq) {
  76                int sq = '\'', ch;
  77
  78                putchar(sq);
  79                while ((ch = *arg++)) {
  80                        if (ch == sq)
  81                                fputs("'\\'", stdout);
  82                        putchar(ch);
  83                }
  84                putchar(sq);
  85                putchar(' ');
  86        }
  87        else
  88                puts(arg);
  89}
  90
  91/* Output a revision, only if filter allows it */
  92static void show_rev(int type, const unsigned char *sha1, const char *name)
  93{
  94        if (!(filter & DO_REVS))
  95                return;
  96        def = NULL;
  97        revs_count++;
  98
  99        if (type != show_type)
 100                putchar('^');
 101        if (symbolic && name)
 102                show(name);
 103        else if (abbrev)
 104                show(find_unique_abbrev(sha1, abbrev));
 105        else
 106                show(sha1_to_hex(sha1));
 107}
 108
 109/* Output a flag, only if filter allows it. */
 110static int show_flag(char *arg)
 111{
 112        if (!(filter & DO_FLAGS))
 113                return 0;
 114        if (filter & (is_rev_argument(arg) ? DO_REVS : DO_NOREV)) {
 115                show(arg);
 116                return 1;
 117        }
 118        return 0;
 119}
 120
 121static void show_default(void)
 122{
 123        char *s = def;
 124
 125        if (s) {
 126                unsigned char sha1[20];
 127
 128                def = NULL;
 129                if (!get_sha1(s, sha1)) {
 130                        show_rev(NORMAL, sha1, s);
 131                        return;
 132                }
 133        }
 134}
 135
 136static int show_reference(const char *refname, const unsigned char *sha1)
 137{
 138        show_rev(NORMAL, sha1, refname);
 139        return 0;
 140}
 141
 142static void show_datestring(const char *flag, const char *datestr)
 143{
 144        static char buffer[100];
 145
 146        /* date handling requires both flags and revs */
 147        if ((filter & (DO_FLAGS | DO_REVS)) != (DO_FLAGS | DO_REVS))
 148                return;
 149        snprintf(buffer, sizeof(buffer), "%s%lu", flag, approxidate(datestr));
 150        show(buffer);
 151}
 152
 153static int show_file(const char *arg)
 154{
 155        show_default();
 156        if ((filter & (DO_NONFLAGS|DO_NOREV)) == (DO_NONFLAGS|DO_NOREV)) {
 157                show(arg);
 158                return 1;
 159        }
 160        return 0;
 161}
 162
 163static void die_badfile(const char *arg)
 164{
 165        if (errno != ENOENT)
 166                die("'%s': %s", arg, strerror(errno));
 167        die("'%s' is ambiguous - revision name or file/directory name?\n"
 168            "Please put '--' before the list of filenames.", arg);
 169}
 170
 171int main(int argc, char **argv)
 172{
 173        int i, as_is = 0, verify = 0;
 174        unsigned char sha1[20];
 175        const char *prefix = setup_git_directory();
 176        
 177        git_config(git_default_config);
 178
 179        for (i = 1; i < argc; i++) {
 180                struct stat st;
 181                char *arg = argv[i];
 182                char *dotdot;
 183
 184                if (as_is) {
 185                        if (show_file(arg) && as_is < 2)
 186                                if (lstat(arg, &st) < 0)
 187                                        die_badfile(arg);
 188                        continue;
 189                }
 190                if (!strcmp(arg,"-n")) {
 191                        if (++i >= argc)
 192                                die("-n requires an argument");
 193                        if ((filter & DO_FLAGS) && (filter & DO_REVS)) {
 194                                show(arg);
 195                                show(argv[i]);
 196                        }
 197                        continue;
 198                }
 199                if (!strncmp(arg,"-n",2)) {
 200                        if ((filter & DO_FLAGS) && (filter & DO_REVS))
 201                                show(arg);
 202                        continue;
 203                }
 204
 205                if (*arg == '-') {
 206                        if (!strcmp(arg, "--")) {
 207                                as_is = 2;
 208                                /* Pass on the "--" if we show anything but files.. */
 209                                if (filter & (DO_FLAGS | DO_REVS))
 210                                        show_file(arg);
 211                                continue;
 212                        }
 213                        if (!strcmp(arg, "--default")) {
 214                                def = argv[i+1];
 215                                i++;
 216                                continue;
 217                        }
 218                        if (!strcmp(arg, "--revs-only")) {
 219                                filter &= ~DO_NOREV;
 220                                continue;
 221                        }
 222                        if (!strcmp(arg, "--no-revs")) {
 223                                filter &= ~DO_REVS;
 224                                continue;
 225                        }
 226                        if (!strcmp(arg, "--flags")) {
 227                                filter &= ~DO_NONFLAGS;
 228                                continue;
 229                        }
 230                        if (!strcmp(arg, "--no-flags")) {
 231                                filter &= ~DO_FLAGS;
 232                                continue;
 233                        }
 234                        if (!strcmp(arg, "--verify")) {
 235                                filter &= ~(DO_FLAGS|DO_NOREV);
 236                                verify = 1;
 237                                continue;
 238                        }
 239                        if (!strcmp(arg, "--short") ||
 240                            !strncmp(arg, "--short=", 8)) {
 241                                filter &= ~(DO_FLAGS|DO_NOREV);
 242                                verify = 1;
 243                                abbrev = DEFAULT_ABBREV;
 244                                if (arg[7] == '=')
 245                                        abbrev = strtoul(arg + 8, NULL, 10);
 246                                if (abbrev < MINIMUM_ABBREV)
 247                                        abbrev = MINIMUM_ABBREV;
 248                                else if (40 <= abbrev)
 249                                        abbrev = 40;
 250                                continue;
 251                        }
 252                        if (!strcmp(arg, "--sq")) {
 253                                output_sq = 1;
 254                                continue;
 255                        }
 256                        if (!strcmp(arg, "--not")) {
 257                                show_type ^= REVERSED;
 258                                continue;
 259                        }
 260                        if (!strcmp(arg, "--symbolic")) {
 261                                symbolic = 1;
 262                                continue;
 263                        }
 264                        if (!strcmp(arg, "--all")) {
 265                                for_each_ref(show_reference);
 266                                continue;
 267                        }
 268                        if (!strcmp(arg, "--show-prefix")) {
 269                                if (prefix)
 270                                        puts(prefix);
 271                                continue;
 272                        }
 273                        if (!strcmp(arg, "--show-cdup")) {
 274                                const char *pfx = prefix;
 275                                while (pfx) {
 276                                        pfx = strchr(pfx, '/');
 277                                        if (pfx) {
 278                                                pfx++;
 279                                                printf("../");
 280                                        }
 281                                }
 282                                putchar('\n');
 283                                continue;
 284                        }
 285                        if (!strcmp(arg, "--git-dir")) {
 286                                const char *gitdir = getenv(GIT_DIR_ENVIRONMENT);
 287                                static char cwd[PATH_MAX];
 288                                if (gitdir) {
 289                                        puts(gitdir);
 290                                        continue;
 291                                }
 292                                if (!prefix) {
 293                                        puts(".git");
 294                                        continue;
 295                                }
 296                                if (!getcwd(cwd, PATH_MAX))
 297                                        die("unable to get current working directory");
 298                                printf("%s/.git\n", cwd);
 299                                continue;
 300                        }
 301                        if (!strncmp(arg, "--since=", 8)) {
 302                                show_datestring("--max-age=", arg+8);
 303                                continue;
 304                        }
 305                        if (!strncmp(arg, "--after=", 8)) {
 306                                show_datestring("--max-age=", arg+8);
 307                                continue;
 308                        }
 309                        if (!strncmp(arg, "--before=", 9)) {
 310                                show_datestring("--min-age=", arg+9);
 311                                continue;
 312                        }
 313                        if (!strncmp(arg, "--until=", 8)) {
 314                                show_datestring("--min-age=", arg+8);
 315                                continue;
 316                        }
 317                        if (show_flag(arg) && verify)
 318                                die("Needed a single revision");
 319                        continue;
 320                }
 321
 322                /* Not a flag argument */
 323                dotdot = strstr(arg, "..");
 324                if (dotdot) {
 325                        unsigned char end[20];
 326                        char *next = dotdot + 2;
 327                        char *this = arg;
 328                        *dotdot = 0;
 329                        if (!*next)
 330                                next = "HEAD";
 331                        if (dotdot == arg)
 332                                this = "HEAD";
 333                        if (!get_sha1(this, sha1) && !get_sha1(next, end)) {
 334                                show_rev(NORMAL, end, next);
 335                                show_rev(REVERSED, sha1, this);
 336                                continue;
 337                        }
 338                        *dotdot = '.';
 339                }
 340                if (!get_sha1(arg, sha1)) {
 341                        show_rev(NORMAL, sha1, arg);
 342                        continue;
 343                }
 344                if (*arg == '^' && !get_sha1(arg+1, sha1)) {
 345                        show_rev(REVERSED, sha1, arg+1);
 346                        continue;
 347                }
 348                as_is = 1;
 349                if (!show_file(arg))
 350                        continue;
 351                if (verify)
 352                        die("Needed a single revision");
 353                if (lstat(arg, &st) < 0)
 354                        die_badfile(arg);
 355        }
 356        show_default();
 357        if (verify && revs_count != 1)
 358                die("Needed a single revision");
 359        return 0;
 360}