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