summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--diff-lib.c151
-rw-r--r--unpack-trees.h1
-rw-r--r--wt-status.c10
3 files changed, 138 insertions, 24 deletions
diff --git a/diff-lib.c b/diff-lib.c
index 4a05b02cd0..045f3cc58e 100644
--- a/diff-lib.c
+++ b/diff-lib.c
@@ -9,6 +9,7 @@
#include "revision.h"
#include "cache-tree.h"
#include "path-list.h"
+#include "unpack-trees.h"
/*
* diff-files
@@ -435,6 +436,8 @@ int run_diff_files(struct rev_info *revs, unsigned int option)
continue;
}
+ if (ce_uptodate(ce))
+ continue;
if (lstat(ce->name, &st) < 0) {
if (errno != ENOENT && errno != ENOTDIR) {
perror(ce->name);
@@ -665,20 +668,133 @@ static void mark_merge_entries(void)
}
}
-int run_diff_index(struct rev_info *revs, int cached)
+/*
+ * This gets a mix of an existing index and a tree, one pathname entry
+ * at a time. The index entry may be a single stage-0 one, but it could
+ * also be multiple unmerged entries (in which case idx_pos/idx_nr will
+ * give you the position and number of entries in the index).
+ */
+static void do_oneway_diff(struct unpack_trees_options *o,
+ struct cache_entry *idx,
+ struct cache_entry *tree,
+ int idx_pos, int idx_nr)
{
- int ret;
- struct object *ent;
- struct tree *tree;
- const char *tree_name;
- int match_missing = 0;
+ struct rev_info *revs = o->unpack_data;
+ int match_missing, cached;
/*
* Backward compatibility wart - "diff-index -m" does
- * not mean "do not ignore merges", but totally different.
+ * not mean "do not ignore merges", but "match_missing".
+ *
+ * But with the revision flag parsing, that's found in
+ * "!revs->ignore_merges".
+ */
+ cached = o->index_only;
+ match_missing = !revs->ignore_merges;
+
+ if (cached && idx && ce_stage(idx)) {
+ if (tree)
+ diff_unmerge(&revs->diffopt, idx->name, idx->ce_mode, idx->sha1);
+ return;
+ }
+
+ /*
+ * Something added to the tree?
+ */
+ if (!tree) {
+ show_new_file(revs, idx, cached, match_missing);
+ return;
+ }
+
+ /*
+ * Something removed from the tree?
*/
- if (!revs->ignore_merges)
- match_missing = 1;
+ if (!idx) {
+ diff_index_show_file(revs, "-", tree, tree->sha1, tree->ce_mode);
+ return;
+ }
+
+ /* Show difference between old and new */
+ show_modified(revs, tree, idx, 1, cached, match_missing);
+}
+
+/*
+ * Count how many index entries go with the first one
+ */
+static inline int count_skip(const struct cache_entry *src, int pos)
+{
+ int skip = 1;
+
+ /* We can only have multiple entries if the first one is not stage-0 */
+ if (ce_stage(src)) {
+ struct cache_entry **p = active_cache + pos;
+ int namelen = ce_namelen(src);
+
+ for (;;) {
+ const struct cache_entry *ce;
+ pos++;
+ if (pos >= active_nr)
+ break;
+ ce = *++p;
+ if (ce_namelen(ce) != namelen)
+ break;
+ if (memcmp(ce->name, src->name, namelen))
+ break;
+ skip++;
+ }
+ }
+ return skip;
+}
+
+/*
+ * The unpack_trees() interface is designed for merging, so
+ * the different source entries are designed primarily for
+ * the source trees, with the old index being really mainly
+ * used for being replaced by the result.
+ *
+ * For diffing, the index is more important, and we only have a
+ * single tree.
+ *
+ * We're supposed to return how many index entries we want to skip.
+ *
+ * This wrapper makes it all more readable, and takes care of all
+ * the fairly complex unpack_trees() semantic requirements, including
+ * the skipping, the path matching, the type conflict cases etc.
+ */
+static int oneway_diff(struct cache_entry **src,
+ struct unpack_trees_options *o,
+ int index_pos)
+{
+ int skip = 0;
+ struct cache_entry *idx = src[0];
+ struct cache_entry *tree = src[1];
+ struct rev_info *revs = o->unpack_data;
+
+ if (index_pos >= 0)
+ skip = count_skip(idx, index_pos);
+
+ /*
+ * Unpack-trees generates a DF/conflict entry if
+ * there was a directory in the index and a tree
+ * in the tree. From a diff standpoint, that's a
+ * delete of the tree and a create of the file.
+ */
+ if (tree == o->df_conflict_entry)
+ tree = NULL;
+
+ if (ce_path_match(idx ? idx : tree, revs->prune_data))
+ do_oneway_diff(o, idx, tree, index_pos, skip);
+
+ return skip;
+}
+
+int run_diff_index(struct rev_info *revs, int cached)
+{
+ struct object *ent;
+ struct tree *tree;
+ const char *tree_name;
+ struct unpack_trees_options opts;
+ struct tree_desc t;
mark_merge_entries();
@@ -687,13 +803,20 @@ int run_diff_index(struct rev_info *revs, int cached)
tree = parse_tree_indirect(ent->sha1);
if (!tree)
return error("bad tree object %s", tree_name);
- if (read_tree(tree, 1, revs->prune_data))
- return error("unable to read tree object %s", tree_name);
- ret = diff_cache(revs, active_cache, active_nr, revs->prune_data,
- cached, match_missing);
+
+ memset(&opts, 0, sizeof(opts));
+ opts.head_idx = 1;
+ opts.index_only = cached;
+ opts.merge = 1;
+ opts.fn = oneway_diff;
+ opts.unpack_data = revs;
+
+ init_tree_desc(&t, tree->buffer, tree->size);
+ unpack_trees(1, &t, &opts);
+
diffcore_std(&revs->diffopt);
diff_flush(&revs->diffopt);
- return ret;
+ return 0;
}
int do_diff_cache(const unsigned char *tree_sha1, struct diff_options *opt)
diff --git a/unpack-trees.h b/unpack-trees.h
index 5517faafad..197a0044aa 100644
--- a/unpack-trees.h
+++ b/unpack-trees.h
@@ -25,6 +25,7 @@ struct unpack_trees_options {
int merge_size;
struct cache_entry *df_conflict_entry;
+ void *unpack_data;
};
extern int unpack_trees(unsigned n, struct tree_desc *t,
diff --git a/wt-status.c b/wt-status.c
index c0c247243b..27b946d552 100644
--- a/wt-status.c
+++ b/wt-status.c
@@ -217,19 +217,12 @@ static void wt_status_print_changed_cb(struct diff_queue_struct *q,
wt_status_print_trailer(s);
}
-static void wt_read_cache(struct wt_status *s)
-{
- discard_cache();
- read_cache_from(s->index_file);
-}
-
static void wt_status_print_initial(struct wt_status *s)
{
int i;
struct strbuf buf;
strbuf_init(&buf, 0);
- wt_read_cache(s);
if (active_nr) {
s->commitable = 1;
wt_status_print_cached_header(s);
@@ -256,7 +249,6 @@ static void wt_status_print_updated(struct wt_status *s)
rev.diffopt.detect_rename = 1;
rev.diffopt.rename_limit = 100;
rev.diffopt.break_opt = 0;
- wt_read_cache(s);
run_diff_index(&rev, 1);
}
@@ -268,7 +260,6 @@ static void wt_status_print_changed(struct wt_status *s)
rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
rev.diffopt.format_callback = wt_status_print_changed_cb;
rev.diffopt.format_callback_data = s;
- wt_read_cache(s);
run_diff_files(&rev, 0);
}
@@ -335,7 +326,6 @@ static void wt_status_print_verbose(struct wt_status *s)
setup_revisions(0, NULL, &rev, s->reference);
rev.diffopt.output_format |= DIFF_FORMAT_PATCH;
rev.diffopt.detect_rename = 1;
- wt_read_cache(s);
run_diff_index(&rev, 1);
fflush(stdout);