git-merge-tree: generalize the "traverse <n> trees in sync" functionality
[gitweb.git] / diff-index.c
index bc41d54d903e90cc8c6bc56725c40e9a67916fd5..f8a102ec1665753e078798eaaac7aca48b845995 100644 (file)
@@ -1,27 +1,22 @@
 #include "cache.h"
+#include "tree.h"
 #include "diff.h"
 
 static int cached_only = 0;
-static int diff_output_format = DIFF_FORMAT_RAW;
-static int diff_line_termination = '\n';
 static int match_nonexisting = 0;
-static int detect_rename = 0;
-static int find_copies_harder = 0;
-static int diff_setup_opt = 0;
-static int diff_score_opt = 0;
-static const char *pickaxe = NULL;
-static int pickaxe_opts = 0;
-static int diff_break_opt = -1;
-static const char *orderfile = NULL;
-static const char *diff_filter = NULL;
+static struct diff_options diff_options;
 
 /* A file entry went away or appeared */
-static void show_file(const char *prefix, struct cache_entry *ce, unsigned char *sha1, unsigned int mode)
+static void show_file(const char *prefix,
+                     struct cache_entry *ce,
+                     unsigned char *sha1, unsigned int mode)
 {
-       diff_addremove(prefix[0], ntohl(mode), sha1, ce->name, NULL);
+       diff_addremove(&diff_options, prefix[0], ntohl(mode),
+                      sha1, ce->name, NULL);
 }
 
-static int get_stat_data(struct cache_entry *ce, unsigned char **sha1p, unsigned int *modep)
+static int get_stat_data(struct cache_entry *ce,
+                        unsigned char ** sha1p, unsigned int *modep)
 {
        unsigned char *sha1 = ce->sha1;
        unsigned int mode = ce->ce_mode;
@@ -41,6 +36,10 @@ static int get_stat_data(struct cache_entry *ce, unsigned char **sha1p, unsigned
                changed = ce_match_stat(ce, &st);
                if (changed) {
                        mode = create_ce_mode(st.st_mode);
+                       if (!trust_executable_bit &&
+                           S_ISREG(mode) && S_ISREG(ce->ce_mode) &&
+                           ((mode ^ ce->ce_mode) == 0111))
+                               mode = ce->ce_mode;
                        sha1 = no_sha1;
                }
        }
@@ -55,7 +54,9 @@ static void show_new_file(struct cache_entry *new)
        unsigned char *sha1;
        unsigned int mode;
 
-       /* New file in the index: it might actually be different in the working copy */
+       /* New file in the index: it might actually be different in
+        * the working copy.
+        */
        if (get_stat_data(new, &sha1, &mode) < 0)
                return;
 
@@ -77,13 +78,13 @@ static int show_modified(struct cache_entry *old,
 
        oldmode = old->ce_mode;
        if (mode == oldmode && !memcmp(sha1, old->sha1, 20) &&
-           !find_copies_harder)
+           !diff_options.find_copies_harder)
                return 0;
 
        mode = ntohl(mode);
        oldmode = ntohl(oldmode);
 
-       diff_change(oldmode, mode,
+       diff_change(&diff_options, oldmode, mode,
                    old->sha1, sha1, old->name, NULL);
        return 0;
 }
@@ -116,7 +117,7 @@ static int diff_cache(struct cache_entry **ac, int entries, const char **pathspe
                        /* We come here with ce pointing at stage 1
                         * (original tree) and ac[1] pointing at stage
                         * 3 (unmerged).  show-modified with
-                        * report-mising set to false does not say the
+                        * report-missing set to false does not say the
                         * file is deleted but reports true if work
                         * tree does not have it, in which case we
                         * fall through to report the unmerged state.
@@ -127,7 +128,7 @@ static int diff_cache(struct cache_entry **ac, int entries, const char **pathspe
                                break;
                        /* fallthru */
                case 3:
-                       diff_unmerge(ce->name);
+                       diff_unmerge(&diff_options, ce->name);
                        break;
 
                default:
@@ -168,20 +169,22 @@ static const char diff_cache_usage[] =
 "[<common diff options>] <tree-ish> [<path>...]"
 COMMON_DIFF_OPTIONS_HELP;
 
-int main(int argc, char **argv)
+int main(int argc, const char **argv)
 {
        const char *tree_name = NULL;
        unsigned char sha1[20];
        const char *prefix = setup_git_directory();
        const char **pathspec = NULL;
-       void *tree;
-       unsigned long size;
+       struct tree *tree;
        int ret;
        int allow_options = 1;
        int i;
 
+       git_config(git_diff_config);
+       diff_setup(&diff_options);
        for (i = 1; i < argc; i++) {
                const char *arg = argv[i];
+               int diff_opt_cnt;
 
                if (!allow_options || *arg != '-') {
                        if (tree_name)
@@ -198,60 +201,23 @@ int main(int argc, char **argv)
                        /* We accept the -r flag just to look like git-diff-tree */
                        continue;
                }
-               /* We accept the -u flag as a synonym for "-p" */
-               if (!strcmp(arg, "-p") || !strcmp(arg, "-u")) {
-                       diff_output_format = DIFF_FORMAT_PATCH;
-                       continue;
-               }
-               if (!strncmp(arg, "-B", 2)) {
-                       if ((diff_break_opt = diff_scoreopt_parse(arg)) == -1)
-                               usage(diff_cache_usage);
-                       continue;
-               }
-               if (!strncmp(arg, "-M", 2)) {
-                       detect_rename = DIFF_DETECT_RENAME;
-                       if ((diff_score_opt = diff_scoreopt_parse(arg)) == -1)
-                               usage(diff_cache_usage);
-                       continue;
-               }
-               if (!strncmp(arg, "-C", 2)) {
-                       detect_rename = DIFF_DETECT_COPY;
-                       if ((diff_score_opt = diff_scoreopt_parse(arg)) == -1)
-                               usage(diff_cache_usage);
-                       continue;
-               }
-               if (!strcmp(arg, "--find-copies-harder")) {
-                       find_copies_harder = 1;
-                       continue;
-               }
-               if (!strcmp(arg, "-z")) {
-                       diff_line_termination = 0;
-                       continue;
-               }
-               if (!strcmp(arg, "--name-only")) {
-                       diff_output_format = DIFF_FORMAT_NAME;
-                       continue;
-               }
-               if (!strcmp(arg, "-R")) {
-                       diff_setup_opt |= DIFF_SETUP_REVERSE;
-                       continue;
-               }
-               if (!strncmp(arg, "-S", 2)) {
-                       pickaxe = arg + 2;
-                       continue;
-               }
-               if (!strncmp(arg, "--diff-filter=", 14)) {
-                       diff_filter = arg + 14;
-                       continue;
-               }
-               if (!strncmp(arg, "-O", 2)) {
-                       orderfile = arg + 2;
-                       continue;
-               }
-               if (!strcmp(arg, "--pickaxe-all")) {
-                       pickaxe_opts = DIFF_PICKAXE_ALL;
+               if (!strcmp(arg, "--cc"))
+                       /*
+                        * I _think_ "diff-index --cached HEAD" with an
+                        * unmerged index could show something else
+                        * later, but pretend --cc is the same as -p for
+                        * now.  "git diff" uses --cc by default.
+                        */
+                       argv[i] = arg = "-p";
+               diff_opt_cnt = diff_opt_parse(&diff_options, argv + i,
+                                             argc - i);
+               if (diff_opt_cnt < 0)
+                       usage(diff_cache_usage);
+               else if (diff_opt_cnt) {
+                       i += diff_opt_cnt - 1;
                        continue;
                }
+
                if (!strcmp(arg, "-m")) {
                        match_nonexisting = 1;
                        continue;
@@ -265,7 +231,7 @@ int main(int argc, char **argv)
 
        pathspec = get_pathspec(prefix, argv + i);
 
-       if (find_copies_harder && detect_rename != DIFF_DETECT_COPY)
+       if (diff_setup_done(&diff_options) < 0)
                usage(diff_cache_usage);
 
        if (!tree_name || get_sha1(tree_name, sha1))
@@ -273,24 +239,17 @@ int main(int argc, char **argv)
 
        read_cache();
 
-       /* The rest is for paths restriction. */
-       diff_setup(diff_setup_opt);
-
        mark_merge_entries();
 
-       tree = read_object_with_reference(sha1, "tree", &size, NULL);
+       tree = parse_tree_indirect(sha1);
        if (!tree)
                die("bad tree object %s", tree_name);
-       if (read_tree(tree, size, 1, pathspec))
+       if (read_tree(tree, 1, pathspec))
                die("unable to read tree object %s", tree_name);
 
        ret = diff_cache(active_cache, active_nr, pathspec);
 
-       diffcore_std(pathspec,
-                    detect_rename, diff_score_opt,
-                    pickaxe, pickaxe_opts,
-                    diff_break_opt,
-                    orderfile, diff_filter);
-       diff_flush(diff_output_format, diff_line_termination);
+       diffcore_std(&diff_options);
+       diff_flush(&diff_options);
        return ret;
 }