rev-parse.con commit cvsexportcommit: add some examples to the documentation (1506fc3)
   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                "--merge-order",
  43                "--min-age=",
  44                "--no-merges",
  45                "--objects",
  46                "--parents",
  47                "--pretty",
  48                "--show-breaks",
  49                "--sparse",
  50                "--topo-order",
  51                "--unpacked",
  52                NULL
  53        };
  54        const char **p = rev_args;
  55
  56        for (;;) {
  57                const char *str = *p++;
  58                int len;
  59                if (!str)
  60                        return 0;
  61                len = strlen(str);
  62                if (!strcmp(arg, str) ||
  63                    (str[len-1] == '=' && !strncmp(arg, str, len)))
  64                        return 1;
  65        }
  66}
  67
  68/* Output argument as a string, either SQ or normal */
  69static void show(const char *arg)
  70{
  71        if (output_sq) {
  72                int sq = '\'', ch;
  73
  74                putchar(sq);
  75                while ((ch = *arg++)) {
  76                        if (ch == sq)
  77                                fputs("'\\'", stdout);
  78                        putchar(ch);
  79                }
  80                putchar(sq);
  81                putchar(' ');
  82        }
  83        else
  84                puts(arg);
  85}
  86
  87/* Output a revision, only if filter allows it */
  88static void show_rev(int type, const unsigned char *sha1, const char *name)
  89{
  90        if (!(filter & DO_REVS))
  91                return;
  92        def = NULL;
  93        revs_count++;
  94
  95        if (type != show_type)
  96                putchar('^');
  97        if (symbolic && name)
  98                show(name);
  99        else if (abbrev)
 100                show(find_unique_abbrev(sha1, abbrev));
 101        else
 102                show(sha1_to_hex(sha1));
 103}
 104
 105/* Output a flag, only if filter allows it. */
 106static void show_flag(char *arg)
 107{
 108        if (!(filter & DO_FLAGS))
 109                return;
 110        if (filter & (is_rev_argument(arg) ? DO_REVS : DO_NOREV))
 111                show(arg);
 112}
 113
 114static void show_default(void)
 115{
 116        char *s = def;
 117
 118        if (s) {
 119                unsigned char sha1[20];
 120
 121                def = NULL;
 122                if (!get_sha1(s, sha1)) {
 123                        show_rev(NORMAL, sha1, s);
 124                        return;
 125                }
 126        }
 127}
 128
 129static int show_reference(const char *refname, const unsigned char *sha1)
 130{
 131        show_rev(NORMAL, sha1, refname);
 132        return 0;
 133}
 134
 135static void show_datestring(const char *flag, const char *datestr)
 136{
 137        static char buffer[100];
 138
 139        /* date handling requires both flags and revs */
 140        if ((filter & (DO_FLAGS | DO_REVS)) != (DO_FLAGS | DO_REVS))
 141                return;
 142        snprintf(buffer, sizeof(buffer), "%s%lu", flag, approxidate(datestr));
 143        show(buffer);
 144}
 145
 146static void show_file(const char *arg)
 147{
 148        show_default();
 149        if ((filter & (DO_NONFLAGS|DO_NOREV)) == (DO_NONFLAGS|DO_NOREV))
 150                show(arg);
 151}
 152
 153int main(int argc, char **argv)
 154{
 155        int i, as_is = 0, verify = 0;
 156        unsigned char sha1[20];
 157        const char *prefix = setup_git_directory();
 158        
 159        for (i = 1; i < argc; i++) {
 160                struct stat st;
 161                char *arg = argv[i];
 162                char *dotdot;
 163        
 164                if (as_is) {
 165                        show_file(arg);
 166                        continue;
 167                }
 168                if (*arg == '-') {
 169                        if (!strcmp(arg, "--")) {
 170                                as_is = 1;
 171                                /* Pass on the "--" if we show anything but files.. */
 172                                if (filter & (DO_FLAGS | DO_REVS))
 173                                        show_file(arg);
 174                                continue;
 175                        }
 176                        if (!strcmp(arg, "--default")) {
 177                                def = argv[i+1];
 178                                i++;
 179                                continue;
 180                        }
 181                        if (!strcmp(arg, "--revs-only")) {
 182                                filter &= ~DO_NOREV;
 183                                continue;
 184                        }
 185                        if (!strcmp(arg, "--no-revs")) {
 186                                filter &= ~DO_REVS;
 187                                continue;
 188                        }
 189                        if (!strcmp(arg, "--flags")) {
 190                                filter &= ~DO_NONFLAGS;
 191                                continue;
 192                        }
 193                        if (!strcmp(arg, "--no-flags")) {
 194                                filter &= ~DO_FLAGS;
 195                                continue;
 196                        }
 197                        if (!strcmp(arg, "--verify")) {
 198                                filter &= ~(DO_FLAGS|DO_NOREV);
 199                                verify = 1;
 200                                continue;
 201                        }
 202                        if (!strcmp(arg, "--short") ||
 203                            !strncmp(arg, "--short=", 9)) {
 204                                filter &= ~(DO_FLAGS|DO_NOREV);
 205                                verify = 1;
 206                                abbrev = DEFAULT_ABBREV;
 207                                if (arg[8] == '=')
 208                                        abbrev = strtoul(arg + 9, NULL, 10);
 209                                if (abbrev < MINIMUM_ABBREV)
 210                                        abbrev = MINIMUM_ABBREV;
 211                                else if (40 <= abbrev)
 212                                        abbrev = 40;
 213                                continue;
 214                        }
 215                        if (!strcmp(arg, "--sq")) {
 216                                output_sq = 1;
 217                                continue;
 218                        }
 219                        if (!strcmp(arg, "--not")) {
 220                                show_type ^= REVERSED;
 221                                continue;
 222                        }
 223                        if (!strcmp(arg, "--symbolic")) {
 224                                symbolic = 1;
 225                                continue;
 226                        }
 227                        if (!strcmp(arg, "--all")) {
 228                                for_each_ref(show_reference);
 229                                continue;
 230                        }
 231                        if (!strcmp(arg, "--show-prefix")) {
 232                                if (prefix)
 233                                        puts(prefix);
 234                                continue;
 235                        }
 236                        if (!strcmp(arg, "--show-cdup")) {
 237                                const char *pfx = prefix;
 238                                while (pfx) {
 239                                        pfx = strchr(pfx, '/');
 240                                        if (pfx) {
 241                                                pfx++;
 242                                                printf("../");
 243                                        }
 244                                }
 245                                putchar('\n');
 246                                continue;
 247                        }
 248                        if (!strcmp(arg, "--git-dir")) {
 249                                const char *gitdir = getenv(GIT_DIR_ENVIRONMENT);
 250                                static char cwd[PATH_MAX];
 251                                if (gitdir) {
 252                                        puts(gitdir);
 253                                        continue;
 254                                }
 255                                if (!prefix) {
 256                                        puts(".git");
 257                                        continue;
 258                                }
 259                                if (!getcwd(cwd, PATH_MAX))
 260                                        die("unable to get current working directory");
 261                                printf("%s/.git\n", cwd);
 262                                continue;
 263                        }
 264                        if (!strncmp(arg, "--since=", 8)) {
 265                                show_datestring("--max-age=", arg+8);
 266                                continue;
 267                        }
 268                        if (!strncmp(arg, "--after=", 8)) {
 269                                show_datestring("--max-age=", arg+8);
 270                                continue;
 271                        }
 272                        if (!strncmp(arg, "--before=", 9)) {
 273                                show_datestring("--min-age=", arg+9);
 274                                continue;
 275                        }
 276                        if (!strncmp(arg, "--until=", 8)) {
 277                                show_datestring("--min-age=", arg+8);
 278                                continue;
 279                        }
 280                        if (verify)
 281                                die("Needed a single revision");
 282                        show_flag(arg);
 283                        continue;
 284                }
 285
 286                /* Not a flag argument */
 287                dotdot = strstr(arg, "..");
 288                if (dotdot) {
 289                        unsigned char end[20];
 290                        char *n = dotdot+2;
 291                        *dotdot = 0;
 292                        if (!get_sha1(arg, sha1)) {
 293                                if (!*n)
 294                                        n = "HEAD";
 295                                if (!get_sha1(n, end)) {
 296                                        show_rev(NORMAL, end, n);
 297                                        show_rev(REVERSED, sha1, arg);
 298                                        continue;
 299                                }
 300                        }
 301                        *dotdot = '.';
 302                }
 303                if (!get_sha1(arg, sha1)) {
 304                        show_rev(NORMAL, sha1, arg);
 305                        continue;
 306                }
 307                if (*arg == '^' && !get_sha1(arg+1, sha1)) {
 308                        show_rev(REVERSED, sha1, arg+1);
 309                        continue;
 310                }
 311                if (verify)
 312                        die("Needed a single revision");
 313                if ((filter & DO_REVS) &&
 314                    (filter & DO_NONFLAGS) && /* !def && */
 315                    lstat(arg, &st) < 0)
 316                        die("'%s': %s", arg, strerror(errno));
 317                as_is = 1;
 318                show_file(arg);
 319        }
 320        show_default();
 321        if (verify && revs_count != 1)
 322                die("Needed a single revision");
 323        return 0;
 324}