summaryrefslogtreecommitdiff
path: root/diff-lib.c
diff options
context:
space:
mode:
Diffstat (limited to 'diff-lib.c')
-rw-r--r--diff-lib.c223
1 files changed, 188 insertions, 35 deletions
diff --git a/diff-lib.c b/diff-lib.c
index 25fd2de..683f11e 100644
--- a/diff-lib.c
+++ b/diff-lib.c
@@ -1,18 +1,26 @@
/*
* Copyright (C) 2005 Junio C Hamano
*/
-#include "cache.h"
-#include "quote.h"
+#include "git-compat-util.h"
#include "commit.h"
#include "diff.h"
#include "diffcore.h"
+#include "gettext.h"
+#include "hash.h"
+#include "hex.h"
+#include "object-name.h"
+#include "read-cache.h"
#include "revision.h"
#include "cache-tree.h"
#include "unpack-trees.h"
#include "refs.h"
+#include "repository.h"
#include "submodule.h"
+#include "symlinks.h"
+#include "trace.h"
#include "dir.h"
#include "fsmonitor.h"
+#include "commit-reach.h"
/*
* diff-files
@@ -29,11 +37,18 @@
*/
static int check_removed(const struct cache_entry *ce, struct stat *st)
{
- if (lstat(ce->name, st) < 0) {
+ int stat_err;
+
+ if (!(ce->ce_flags & CE_FSMONITOR_VALID))
+ stat_err = lstat(ce->name, st);
+ else
+ stat_err = fake_lstat(ce, st);
+ if (stat_err < 0) {
if (!is_missing_file_error(errno))
return -1;
return 1;
}
+
if (has_symlink_leading_path(ce->name, ce_namelen(ce)))
return 1;
if (S_ISDIR(st->st_mode)) {
@@ -86,7 +101,7 @@ static int match_stat_with_submodule(struct diff_options *diffopt,
return changed;
}
-int run_diff_files(struct rev_info *revs, unsigned int option)
+void run_diff_files(struct rev_info *revs, unsigned int option)
{
int entries, i;
int diff_unmerged_stage = revs->max_count;
@@ -97,6 +112,8 @@ int run_diff_files(struct rev_info *revs, unsigned int option)
diff_set_mnemonic_prefix(&revs->diffopt, "i/", "w/");
+ refresh_fsmonitor(istate);
+
if (diff_unmerged_stage < 0)
diff_unmerged_stage = 2;
entries = istate->cache_nr;
@@ -110,7 +127,20 @@ int run_diff_files(struct rev_info *revs, unsigned int option)
if (diff_can_quit_early(&revs->diffopt))
break;
- if (!ce_path_match(istate, ce, &revs->prune_data, NULL))
+ /*
+ * NEEDSWORK:
+ * Here we filter with pathspec but the result is further
+ * filtered out when --relative is in effect. To end-users,
+ * a pathspec element that matched only to paths outside the
+ * current directory is like not matching anything at all;
+ * the handling of ps_matched[] here may become problematic
+ * if/when we add the "--error-unmatch" option to "git diff".
+ */
+ if (!ce_path_match(istate, ce, &revs->prune_data, revs->ps_matched))
+ continue;
+
+ if (revs->diffopt.prefix &&
+ strncmp(ce->name, revs->diffopt.prefix, revs->diffopt.prefix_length))
continue;
if (ce_stage(ce)) {
@@ -177,9 +207,7 @@ int run_diff_files(struct rev_info *revs, unsigned int option)
i--;
if (revs->combine_merges && num_compare_stages == 2) {
- show_combined_diff(dpath, 2,
- revs->dense_combined_merges,
- revs);
+ show_combined_diff(dpath, 2, revs);
free(dpath);
continue;
}
@@ -199,8 +227,17 @@ int run_diff_files(struct rev_info *revs, unsigned int option)
if (ce_uptodate(ce) || ce_skip_worktree(ce))
continue;
- /* If CE_VALID is set, don't look at workdir for file removal */
- if (ce->ce_flags & CE_VALID) {
+ /*
+ * When CE_VALID is set (via "update-index --assume-unchanged"
+ * or via adding paths while core.ignorestat is set to true),
+ * the user has promised that the working tree file for that
+ * path will not be modified. When CE_FSMONITOR_VALID is true,
+ * the fsmonitor knows that the path hasn't been modified since
+ * we refreshed the cached stat information. In either case,
+ * we do not have to stat to see if the path has been removed
+ * or modified.
+ */
+ if (ce->ce_flags & (CE_VALID | CE_FSMONITOR_VALID)) {
changed = 0;
newmode = ce->ce_mode;
} else {
@@ -219,8 +256,9 @@ int run_diff_files(struct rev_info *revs, unsigned int option)
continue;
} else if (revs->diffopt.ita_invisible_in_index &&
ce_intent_to_add(ce)) {
- diff_addremove(&revs->diffopt, '+', ce->ce_mode,
- &null_oid, 0, ce->name, 0);
+ newmode = ce_mode_from_stat(ce, st.st_mode);
+ diff_addremove(&revs->diffopt, '+', newmode,
+ null_oid(), 0, ce->name, 0);
continue;
}
@@ -237,7 +275,7 @@ int run_diff_files(struct rev_info *revs, unsigned int option)
}
oldmode = ce->ce_mode;
old_oid = &ce->oid;
- new_oid = changed ? &null_oid : &ce->oid;
+ new_oid = changed ? null_oid() : &ce->oid;
diff_change(&revs->diffopt, oldmode, newmode,
old_oid, new_oid,
!is_null_oid(old_oid),
@@ -248,7 +286,6 @@ int run_diff_files(struct rev_info *revs, unsigned int option)
diffcore_std(&revs->diffopt);
diff_flush(&revs->diffopt);
trace_performance_since(start, "diff-files");
- return 0;
}
/*
@@ -267,7 +304,8 @@ static void diff_index_show_file(struct rev_info *revs,
oid, oid_valid, ce->name, dirty_submodule);
}
-static int get_stat_data(const struct cache_entry *ce,
+static int get_stat_data(const struct index_state *istate,
+ const struct cache_entry *ce,
const struct object_id **oidp,
unsigned int *modep,
int cached, int match_missing,
@@ -294,7 +332,7 @@ static int get_stat_data(const struct cache_entry *ce,
0, dirty_submodule);
if (changed) {
mode = ce_mode_from_stat(ce, st.st_mode);
- oid = &null_oid;
+ oid = null_oid();
}
}
@@ -310,12 +348,18 @@ static void show_new_file(struct rev_info *revs,
const struct object_id *oid;
unsigned int mode;
unsigned dirty_submodule = 0;
+ struct index_state *istate = revs->diffopt.repo->index;
+
+ if (new_file && S_ISSPARSEDIR(new_file->ce_mode)) {
+ diff_tree_oid(NULL, &new_file->oid, new_file->name, &revs->diffopt);
+ return;
+ }
/*
* New file in the index: it might actually be different in
* the working tree.
*/
- if (get_stat_data(new_file, &oid, &mode, cached, match_missing,
+ if (get_stat_data(istate, new_file, &oid, &mode, cached, match_missing,
&dirty_submodule, &revs->diffopt) < 0)
return;
@@ -331,8 +375,23 @@ static int show_modified(struct rev_info *revs,
unsigned int mode, oldmode;
const struct object_id *oid;
unsigned dirty_submodule = 0;
+ struct index_state *istate = revs->diffopt.repo->index;
- if (get_stat_data(new_entry, &oid, &mode, cached, match_missing,
+ assert(S_ISSPARSEDIR(old_entry->ce_mode) ==
+ S_ISSPARSEDIR(new_entry->ce_mode));
+
+ /*
+ * If both are sparse directory entries, then expand the
+ * modifications to the file level. If only one was a sparse
+ * directory, then they appear as an add and delete instead of
+ * a modification.
+ */
+ if (S_ISSPARSEDIR(new_entry->ce_mode)) {
+ diff_tree_oid(&old_entry->oid, &new_entry->oid, new_entry->name, &revs->diffopt);
+ return 0;
+ }
+
+ if (get_stat_data(istate, new_entry, &oid, &mode, cached, match_missing,
&dirty_submodule, &revs->diffopt) < 0) {
if (report_missing)
diff_index_show_file(revs, "-", old_entry,
@@ -360,7 +419,7 @@ static int show_modified(struct rev_info *revs,
p->parent[1].status = DIFF_STATUS_MODIFIED;
p->parent[1].mode = old_entry->ce_mode;
oidcpy(&p->parent[1].oid, &old_entry->oid);
- show_combined_diff(p, 2, revs->dense_combined_merges, revs);
+ show_combined_diff(p, 2, revs);
free(p);
return 0;
}
@@ -404,14 +463,8 @@ static void do_oneway_diff(struct unpack_trees_options *o,
/* if the entry is not checked out, don't examine work tree */
cached = o->index_only ||
(idx && ((idx->ce_flags & CE_VALID) || ce_skip_worktree(idx)));
- /*
- * Backward compatibility wart - "diff-index -m" does
- * not mean "do not ignore merges", but "match_missing".
- *
- * But with the revision flag parsing, that's found in
- * "!revs->ignore_merges".
- */
- match_missing = !revs->ignore_merges;
+
+ match_missing = revs->match_missing;
if (cached && idx && ce_stage(idx)) {
struct diff_filepair *pair;
@@ -434,6 +487,11 @@ static void do_oneway_diff(struct unpack_trees_options *o,
* Something removed from the tree?
*/
if (!idx) {
+ if (S_ISSPARSEDIR(tree->ce_mode)) {
+ diff_tree_oid(&tree->oid, NULL, tree->name, &revs->diffopt);
+ return;
+ }
+
diff_index_show_file(revs, "-", tree, &tree->oid, 1,
tree->ce_mode, 0);
return;
@@ -513,20 +571,80 @@ static int diff_cache(struct rev_info *revs,
opts.pathspec = &revs->diffopt.pathspec;
opts.pathspec->recursive = 1;
- init_tree_desc(&t, tree->buffer, tree->size);
+ init_tree_desc(&t, &tree->object.oid, tree->buffer, tree->size);
return unpack_trees(1, &t, &opts);
}
-int run_diff_index(struct rev_info *revs, int cached)
+void diff_get_merge_base(const struct rev_info *revs, struct object_id *mb)
+{
+ int i;
+ struct commit *mb_child[2] = {0};
+ struct commit_list *merge_bases = NULL;
+
+ for (i = 0; i < revs->pending.nr; i++) {
+ struct object *obj = revs->pending.objects[i].item;
+ if (obj->flags)
+ die(_("--merge-base does not work with ranges"));
+ }
+
+ /*
+ * This check must go after the for loop above because A...B
+ * ranges produce three pending commits, resulting in a
+ * misleading error message.
+ */
+ if (revs->pending.nr < 1 || revs->pending.nr > 2)
+ BUG("unexpected revs->pending.nr: %d", revs->pending.nr);
+
+ for (i = 0; i < revs->pending.nr; i++)
+ mb_child[i] = lookup_commit_reference(the_repository, &revs->pending.objects[i].item->oid);
+ if (revs->pending.nr == 1) {
+ struct object_id oid;
+
+ if (repo_get_oid(the_repository, "HEAD", &oid))
+ die(_("unable to get HEAD"));
+
+ mb_child[1] = lookup_commit_reference(the_repository, &oid);
+ }
+
+ if (repo_get_merge_bases(the_repository, mb_child[0], mb_child[1], &merge_bases) < 0)
+ exit(128);
+ if (!merge_bases)
+ die(_("no merge base found"));
+ if (merge_bases->next)
+ die(_("multiple merge bases found"));
+
+ oidcpy(mb, &merge_bases->item->object.oid);
+
+ free_commit_list(merge_bases);
+}
+
+void run_diff_index(struct rev_info *revs, unsigned int option)
{
struct object_array_entry *ent;
+ int cached = !!(option & DIFF_INDEX_CACHED);
+ int merge_base = !!(option & DIFF_INDEX_MERGE_BASE);
+ struct object_id oid;
+ const char *name;
+ char merge_base_hex[GIT_MAX_HEXSZ + 1];
+ struct index_state *istate = revs->diffopt.repo->index;
if (revs->pending.nr != 1)
BUG("run_diff_index must be passed exactly one tree");
trace_performance_enter();
ent = revs->pending.objects;
- if (diff_cache(revs, &ent->item->oid, ent->name, cached))
+
+ refresh_fsmonitor(istate);
+
+ if (merge_base) {
+ diff_get_merge_base(revs, &oid);
+ name = oid_to_hex_r(merge_base_hex, &oid);
+ } else {
+ oidcpy(&oid, &ent->item->oid);
+ name = ent->name;
+ }
+
+ if (diff_cache(revs, &oid, name, cached))
exit(128);
diff_set_mnemonic_prefix(&revs->diffopt, "c/", cached ? "i/" : "w/");
@@ -534,7 +652,6 @@ int run_diff_index(struct rev_info *revs, int cached)
diffcore_std(&revs->diffopt);
diff_flush(&revs->diffopt);
trace_performance_leave("diff-index");
- return 0;
}
int do_diff_cache(const struct object_id *tree_oid, struct diff_options *opt)
@@ -543,10 +660,12 @@ int do_diff_cache(const struct object_id *tree_oid, struct diff_options *opt)
repo_init_revisions(opt->repo, &revs, NULL);
copy_pathspec(&revs.prune_data, &opt->pathspec);
+ diff_setup_done(&revs.diffopt);
revs.diffopt = *opt;
if (diff_cache(&revs, tree_oid, NULL, 1))
exit(128);
+ release_revisions(&revs);
return 0;
}
@@ -556,6 +675,7 @@ int index_differs_from(struct repository *r,
{
struct rev_info rev;
struct setup_revision_opt opt;
+ unsigned has_changes;
repo_init_revisions(r, &rev, NULL);
memset(&opt, 0, sizeof(opt));
@@ -563,10 +683,43 @@ int index_differs_from(struct repository *r,
setup_revisions(0, NULL, &rev, &opt);
rev.diffopt.flags.quick = 1;
rev.diffopt.flags.exit_with_status = 1;
- if (flags)
+ if (flags) {
diff_flags_or(&rev.diffopt.flags, flags);
+ /*
+ * Now that flags are merged, honor override_submodule_config
+ * and ignore_submodules from passed flags.
+ */
+ if (flags->override_submodule_config)
+ rev.diffopt.flags.ignore_submodules = flags->ignore_submodules;
+ }
rev.diffopt.ita_invisible_in_index = ita_invisible_in_index;
- run_diff_index(&rev, 1);
- object_array_clear(&rev.pending);
- return (rev.diffopt.flags.has_changes != 0);
+ run_diff_index(&rev, DIFF_INDEX_CACHED);
+ has_changes = rev.diffopt.flags.has_changes;
+ release_revisions(&rev);
+ return (has_changes != 0);
+}
+
+static struct strbuf *idiff_prefix_cb(struct diff_options *opt UNUSED, void *data)
+{
+ return data;
+}
+
+void show_interdiff(const struct object_id *oid1, const struct object_id *oid2,
+ int indent, struct diff_options *diffopt)
+{
+ struct diff_options opts;
+ struct strbuf prefix = STRBUF_INIT;
+
+ memcpy(&opts, diffopt, sizeof(opts));
+ opts.output_format = DIFF_FORMAT_PATCH;
+ opts.output_prefix = idiff_prefix_cb;
+ strbuf_addchars(&prefix, ' ', indent);
+ opts.output_prefix_data = &prefix;
+ diff_setup_done(&opts);
+
+ diff_tree_oid(oid1, oid2, "", &opts);
+ diffcore_std(&opts);
+ diff_flush(&opts);
+
+ strbuf_release(&prefix);
}