]> asedeno.scripts.mit.edu Git - git.git/blobdiff - diff-lib.c
Merge branch 'master' of git://repo.or.cz/git-gui
[git.git] / diff-lib.c
index ae8364b42a0c67e2461fc20e731417b3203c472f..6abb981534bf032d50e28eb3a14033c1b6546762 100644 (file)
@@ -30,22 +30,28 @@ static int read_directory(const char *path, struct path_list *list)
        return 0;
 }
 
+static int get_mode(const char *path, int *mode)
+{
+       struct stat st;
+
+       if (!path || !strcmp(path, "/dev/null"))
+               *mode = 0;
+       else if (!strcmp(path, "-"))
+               *mode = ntohl(create_ce_mode(0666));
+       else if (stat(path, &st))
+               return error("Could not access '%s'", path);
+       else
+               *mode = st.st_mode;
+       return 0;
+}
+
 static int queue_diff(struct diff_options *o,
                const char *name1, const char *name2)
 {
-       struct stat st;
        int mode1 = 0, mode2 = 0;
 
-       if (name1) {
-               if (stat(name1, &st))
-                       return error("Could not access '%s'", name1);
-               mode1 = st.st_mode;
-       }
-       if (name2) {
-               if (stat(name2, &st))
-                       return error("Could not access '%s'", name2);
-               mode2 = st.st_mode;
-       }
+       if (get_mode(name1, &mode1) || get_mode(name2, &mode2))
+               return -1;
 
        if (mode1 && mode2 && S_ISDIR(mode1) != S_ISDIR(mode2))
                return error("file/directory conflict: %s, %s", name1, name2);
@@ -200,6 +206,78 @@ static int handle_diff_files_args(struct rev_info *revs,
        return 0;
 }
 
+static int is_outside_repo(const char *path, int nongit, const char *prefix)
+{
+       int i;
+       if (nongit || !strcmp(path, "-") || path[0] == '/')
+               return 1;
+       if (prefixcmp(path, "../"))
+               return 0;
+       if (!prefix)
+               return 1;
+       for (i = strlen(prefix); !prefixcmp(path, "../"); ) {
+               while (i > 0 && prefix[i - 1] != '/')
+                       i--;
+               if (--i < 0)
+                       return 1;
+               path += 3;
+       }
+       return 0;
+}
+
+int setup_diff_no_index(struct rev_info *revs,
+               int argc, const char ** argv, int nongit, const char *prefix)
+{
+       int i;
+       for (i = 1; i < argc; i++)
+               if (argv[i][0] != '-' || argv[i][1] == '\0')
+                       break;
+               else if (!strcmp(argv[i], "--")) {
+                       i++;
+                       break;
+               } else if (i < argc - 3 && !strcmp(argv[i], "--no-index")) {
+                       i = argc - 3;
+                       break;
+               }
+       if (argc != i + 2 || (!is_outside_repo(argv[i + 1], nongit, prefix) &&
+                               !is_outside_repo(argv[i], nongit, prefix)))
+               return -1;
+
+       diff_setup(&revs->diffopt);
+       for (i = 1; i < argc - 2; )
+               if (!strcmp(argv[i], "--no-index"))
+                       i++;
+               else {
+                       int j = diff_opt_parse(&revs->diffopt,
+                                       argv + i, argc - i);
+                       if (!j)
+                               die("invalid diff option/value: %s", argv[i]);
+                       i += j;
+               }
+
+       if (prefix) {
+               int len = strlen(prefix);
+
+               revs->diffopt.paths = xcalloc(2, sizeof(char*));
+               for (i = 0; i < 2; i++) {
+                       const char *p = argv[argc - 2 + i];
+                       /*
+                        * stdin should be spelled as '-'; if you have
+                        * path that is '-', spell it as ./-.
+                        */
+                       p = (strcmp(p, "-")
+                            ? xstrdup(prefix_filename(prefix, len, p))
+                            : p);
+                       revs->diffopt.paths[i] = p;
+               }
+       }
+       else
+               revs->diffopt.paths = argv + argc - 2;
+       revs->diffopt.nr_paths = 2;
+       revs->max_count = -2;
+       return 0;
+}
+
 int run_diff_files_cmd(struct rev_info *revs, int argc, const char **argv)
 {
        int silent_on_removed;
@@ -210,11 +288,16 @@ int run_diff_files_cmd(struct rev_info *revs, int argc, const char **argv)
        if (revs->max_count == -2) {
                if (revs->diffopt.nr_paths != 2)
                        return error("need two files/directories with --no-index");
-               queue_diff(&revs->diffopt, revs->diffopt.paths[0],
-                               revs->diffopt.paths[1]);
+               if (queue_diff(&revs->diffopt, revs->diffopt.paths[0],
+                               revs->diffopt.paths[1]))
+                       return -1;
                diffcore_std(&revs->diffopt);
                diff_flush(&revs->diffopt);
-               return 0;
+               /*
+                * The return code for --no-index imitates diff(1):
+                * 0 = no changes, 1 = changes, else error
+                */
+               return revs->diffopt.found_changes;
        }
 
        if (read_cache() < 0) {
@@ -341,6 +424,9 @@ int run_diff_files(struct rev_info *revs, int silent_on_removed)
                    S_ISREG(newmode) && S_ISREG(oldmode) &&
                    ((newmode ^ oldmode) == 0111))
                        newmode = oldmode;
+               else if (!has_symlinks &&
+                   S_ISREG(newmode) && S_ISLNK(oldmode))
+                       newmode = oldmode;
                diff_change(&revs->diffopt, oldmode, newmode,
                            ce->sha1, (changed ? null_sha1 : ce->sha1),
                            ce->name, NULL);