t4012: Break up pipe into serial redirections
[gitweb.git] / diff-no-index.c
index b44473e3c1fd5c2bdc4dce3af7aabc86fd37eb08..beec49b5ac98c6d3ea895adcf66265f3a2fc7875 100644 (file)
@@ -68,6 +68,7 @@ static int queue_diff(struct diff_options *o,
                struct string_list p1 = STRING_LIST_INIT_DUP;
                struct string_list p2 = STRING_LIST_INIT_DUP;
                int i1, i2, ret = 0;
+               size_t len1 = 0, len2 = 0;
 
                if (name1 && read_directory(name1, &p1))
                        return -1;
@@ -80,18 +81,23 @@ static int queue_diff(struct diff_options *o,
                        strbuf_addstr(&buffer1, name1);
                        if (buffer1.len && buffer1.buf[buffer1.len - 1] != '/')
                                strbuf_addch(&buffer1, '/');
+                       len1 = buffer1.len;
                }
 
                if (name2) {
                        strbuf_addstr(&buffer2, name2);
                        if (buffer2.len && buffer2.buf[buffer2.len - 1] != '/')
                                strbuf_addch(&buffer2, '/');
+                       len2 = buffer2.len;
                }
 
                for (i1 = i2 = 0; !ret && (i1 < p1.nr || i2 < p2.nr); ) {
                        const char *n1, *n2;
                        int comp;
 
+                       strbuf_setlen(&buffer1, len1);
+                       strbuf_setlen(&buffer2, len2);
+
                        if (i1 == p1.nr)
                                comp = 1;
                        else if (i2 == p2.nr)
@@ -117,8 +123,8 @@ static int queue_diff(struct diff_options *o,
                }
                string_list_clear(&p1, 0);
                string_list_clear(&p2, 0);
-               strbuf_reset(&buffer1);
-               strbuf_reset(&buffer2);
+               strbuf_release(&buffer1);
+               strbuf_release(&buffer2);
 
                return ret;
        } else {
@@ -145,23 +151,6 @@ static int queue_diff(struct diff_options *o,
        }
 }
 
-static int path_outside_repo(const char *path)
-{
-       const char *work_tree;
-       size_t len;
-
-       if (!is_absolute_path(path))
-               return 0;
-       work_tree = get_git_work_tree();
-       if (!work_tree)
-               return 1;
-       len = strlen(work_tree);
-       if (strncmp(path, work_tree, len) ||
-           (path[len] != '\0' && path[len] != '/'))
-               return 1;
-       return 0;
-}
-
 void diff_no_index(struct rev_info *revs,
                   int argc, const char **argv,
                   int nongit, const char *prefix)
@@ -191,8 +180,8 @@ void diff_no_index(struct rev_info *revs,
                 * a colourful "diff" replacement.
                 */
                if ((argc != i + 2) ||
-                   (!path_outside_repo(argv[i]) &&
-                    !path_outside_repo(argv[i+1])))
+                   (path_inside_repo(prefix, argv[i]) &&
+                    path_inside_repo(prefix, argv[i+1])))
                        return;
        }
        if (argc != i + 2)
@@ -218,13 +207,6 @@ void diff_no_index(struct rev_info *revs,
                }
        }
 
-       /*
-        * If the user asked for our exit code then don't start a
-        * pager or we would end up reporting its exit code instead.
-        */
-       if (!DIFF_OPT_TST(&revs->diffopt, EXIT_WITH_STATUS))
-               setup_pager();
-
        if (prefix) {
                int len = strlen(prefix);
                const char *paths[3];
@@ -249,13 +231,15 @@ void diff_no_index(struct rev_info *revs,
        if (!revs->diffopt.output_format)
                revs->diffopt.output_format = DIFF_FORMAT_PATCH;
 
-       DIFF_OPT_SET(&revs->diffopt, EXIT_WITH_STATUS);
        DIFF_OPT_SET(&revs->diffopt, NO_INDEX);
 
        revs->max_count = -2;
        if (diff_setup_done(&revs->diffopt) < 0)
                die("diff_setup_done failed");
 
+       setup_diff_pager(&revs->diffopt);
+       DIFF_OPT_SET(&revs->diffopt, EXIT_WITH_STATUS);
+
        if (queue_diff(&revs->diffopt, revs->diffopt.pathspec.raw[0],
                       revs->diffopt.pathspec.raw[1]))
                exit(1);
@@ -267,5 +251,5 @@ void diff_no_index(struct rev_info *revs,
         * The return code for --no-index imitates diff(1):
         * 0 = no changes, 1 = changes, else error
         */
-       exit(revs->diffopt.found_changes);
+       exit(diff_result_code(&revs->diffopt, 0));
 }