git-apply: do not barf when updating an originally empty file.
[gitweb.git] / rev-parse.c
index 0c951af0d49da459fc6c85c32707b016bf581a57..f90e999e607d238fbfe17e84f286570c04844100 100644 (file)
@@ -20,6 +20,7 @@ static char *def = NULL;
 #define REVERSED 1
 static int show_type = NORMAL;
 static int symbolic = 0;
+static int abbrev = 0;
 static int output_sq = 0;
 
 static int revs_count = 0;
@@ -38,20 +39,24 @@ static int is_rev_argument(const char *arg)
                "--header",
                "--max-age=",
                "--max-count=",
-               "--merge-order",
                "--min-age=",
                "--no-merges",
                "--objects",
+               "--objects-edge",
                "--parents",
                "--pretty",
-               "--show-breaks",
                "--sparse",
                "--topo-order",
+               "--date-order",
                "--unpacked",
                NULL
        };
        const char **p = rev_args;
 
+       /* accept -<digit>, like traditional "head" */
+       if ((*arg == '-') && isdigit(arg[1]))
+               return 1;
+
        for (;;) {
                const char *str = *p++;
                int len;
@@ -95,17 +100,22 @@ static void show_rev(int type, const unsigned char *sha1, const char *name)
                putchar('^');
        if (symbolic && name)
                show(name);
+       else if (abbrev)
+               show(find_unique_abbrev(sha1, abbrev));
        else
                show(sha1_to_hex(sha1));
 }
 
 /* Output a flag, only if filter allows it. */
-static void show_flag(char *arg)
+static int show_flag(char *arg)
 {
        if (!(filter & DO_FLAGS))
-               return;
-       if (filter & (is_rev_argument(arg) ? DO_REVS : DO_NOREV))
+               return 0;
+       if (filter & (is_rev_argument(arg) ? DO_REVS : DO_NOREV)) {
                show(arg);
+               return 1;
+       }
+       return 0;
 }
 
 static void show_default(void)
@@ -140,11 +150,14 @@ static void show_datestring(const char *flag, const char *datestr)
        show(buffer);
 }
 
-static void show_file(const char *arg)
+static int show_file(const char *arg)
 {
        show_default();
-       if ((filter & (DO_NONFLAGS|DO_NOREV)) == (DO_NONFLAGS|DO_NOREV))
+       if ((filter & (DO_NONFLAGS|DO_NOREV)) == (DO_NONFLAGS|DO_NOREV)) {
                show(arg);
+               return 1;
+       }
+       return 0;
 }
 
 int main(int argc, char **argv)
@@ -154,6 +167,7 @@ int main(int argc, char **argv)
        const char *prefix = setup_git_directory();
        
        for (i = 1; i < argc; i++) {
+               struct stat st;
                char *arg = argv[i];
                char *dotdot;
        
@@ -161,6 +175,21 @@ int main(int argc, char **argv)
                        show_file(arg);
                        continue;
                }
+               if (!strcmp(arg,"-n")) {
+                       if (++i >= argc)
+                               die("-n requires an argument");
+                       if ((filter & DO_FLAGS) && (filter & DO_REVS)) {
+                               show(arg);
+                               show(argv[i]);
+                       }
+                       continue;
+               }
+               if (!strncmp(arg,"-n",2)) {
+                       if ((filter & DO_FLAGS) && (filter & DO_REVS))
+                               show(arg);
+                       continue;
+               }
+
                if (*arg == '-') {
                        if (!strcmp(arg, "--")) {
                                as_is = 1;
@@ -195,6 +224,19 @@ int main(int argc, char **argv)
                                verify = 1;
                                continue;
                        }
+                       if (!strcmp(arg, "--short") ||
+                           !strncmp(arg, "--short=", 8)) {
+                               filter &= ~(DO_FLAGS|DO_NOREV);
+                               verify = 1;
+                               abbrev = DEFAULT_ABBREV;
+                               if (arg[7] == '=')
+                                       abbrev = strtoul(arg + 8, NULL, 10);
+                               if (abbrev < MINIMUM_ABBREV)
+                                       abbrev = MINIMUM_ABBREV;
+                               else if (40 <= abbrev)
+                                       abbrev = 40;
+                               continue;
+                       }
                        if (!strcmp(arg, "--sq")) {
                                output_sq = 1;
                                continue;
@@ -260,9 +302,8 @@ int main(int argc, char **argv)
                                show_datestring("--min-age=", arg+8);
                                continue;
                        }
-                       if (verify)
+                       if (show_flag(arg) && verify)
                                die("Needed a single revision");
-                       show_flag(arg);
                        continue;
                }
 
@@ -291,10 +332,13 @@ int main(int argc, char **argv)
                        show_rev(REVERSED, sha1, arg+1);
                        continue;
                }
+               as_is = 1;
+               if (!show_file(arg))
+                       continue;
                if (verify)
                        die("Needed a single revision");
-               as_is = 1;
-               show_file(arg);
+               if (lstat(arg, &st) < 0)
+                       die("'%s': %s", arg, strerror(errno));
        }
        show_default();
        if (verify && revs_count != 1)