7419d18b3bff8b3fc1160095c69f6b273ae27148
   1/*
   2 * rev-parse.c
   3 *
   4 * Copyright (C) Linus Torvalds, 2005
   5 */
   6#include "cache.h"
   7
   8/*
   9 * Some arguments are relevant "revision" arguments,
  10 * others are about output format or other details.
  11 * This sorts it all out.
  12 */
  13static int is_rev_argument(const char *arg)
  14{
  15        static const char *rev_args[] = {
  16                "--max-count=",
  17                "--max-age=",
  18                "--min-age=",
  19                "--merge-order",
  20                NULL
  21        };
  22        const char **p = rev_args;
  23
  24        for (;;) {
  25                const char *str = *p++;
  26                int len;
  27                if (!str)
  28                        return 0;
  29                len = strlen(str);
  30                if (!strncmp(arg, str, len))
  31                        return 1;
  32        }
  33}
  34
  35int main(int argc, char **argv)
  36{
  37        int i, as_is = 0, revs_only = 0, no_revs = 0;
  38        char *def = NULL;
  39        unsigned char sha1[20];
  40
  41        for (i = 1; i < argc; i++) {
  42                char *arg = argv[i];
  43                char *dotdot;
  44        
  45                if (as_is) {
  46                        printf("%s\n", arg);
  47                        continue;
  48                }
  49                if (*arg == '-') {
  50                        if (!strcmp(arg, "--")) {
  51                                if (def) {
  52                                        printf("%s\n", def);
  53                                        def = NULL;
  54                                }
  55                                if (revs_only)
  56                                        break;
  57                                as_is = 1;
  58                        }
  59                        if (!strcmp(arg, "--default")) {
  60                                if (def)
  61                                        printf("%s\n", def);
  62                                def = argv[i+1];
  63                                i++;
  64                                continue;
  65                        }
  66                        if (!strcmp(arg, "--revs-only")) {
  67                                revs_only = 1;
  68                                continue;
  69                        }
  70                        if (!strcmp(arg, "--no-revs")) {
  71                                no_revs = 1;
  72                                continue;
  73                        }
  74                        if (revs_only | no_revs) {
  75                                if (is_rev_argument(arg) != revs_only)
  76                                        continue;
  77                        }
  78                        printf("%s\n", arg);
  79                        continue;
  80                }
  81                dotdot = strstr(arg, "..");
  82                if (dotdot) {
  83                        unsigned char end[20];
  84                        char *n = dotdot+2;
  85                        *dotdot = 0;
  86                        if (!get_sha1(arg, sha1)) {
  87                                if (!*n)
  88                                        n = "HEAD";
  89                                if (!get_sha1(n, end)) {
  90                                        if (no_revs)
  91                                                continue;
  92                                        def = NULL;
  93                                        printf("%s\n", sha1_to_hex(end));
  94                                        printf("^%s\n", sha1_to_hex(sha1));
  95                                        continue;
  96                                }
  97                        }
  98                        *dotdot = '.';
  99                }
 100                if (!get_sha1(arg, sha1)) {
 101                        if (no_revs)
 102                                continue;
 103                        def = NULL;
 104                        printf("%s\n", sha1_to_hex(sha1));
 105                        continue;
 106                }
 107                if (*arg == '^' && !get_sha1(arg+1, sha1)) {
 108                        if (no_revs)
 109                                continue;
 110                        def = NULL;
 111                        printf("^%s\n", sha1_to_hex(sha1));
 112                        continue;
 113                }
 114                if (def) {
 115                        printf("%s\n", def);
 116                        def = NULL;
 117                }
 118                if (revs_only)
 119                        continue;
 120                printf("%s\n", arg);
 121        }
 122        if (def)
 123                printf("%s\n", def);
 124        return 0;
 125}