Commit cb0ff012d3cb9658b88ad4f752046286ba5ea442

Edward Thomson 2015-11-06T17:15:35

racy-git: do a single index->workdir diff When examining paths that are racily clean, do a single index->workdir diff over the entirety of the racily clean files, instead of a diff per file.

diff --git a/src/index.c b/src/index.c
index 0903770..9eb1289 100644
--- a/src/index.c
+++ b/src/index.c
@@ -18,6 +18,7 @@
 #include "ignore.h"
 #include "blob.h"
 #include "idxmap.h"
+#include "diff.h"
 
 #include "git2/odb.h"
 #include "git2/oid.h"
@@ -752,7 +753,9 @@ static int truncate_racily_clean(git_index *index)
 	int error;
 	git_index_entry *entry;
 	git_diff_options diff_opts = GIT_DIFF_OPTIONS_INIT;
-	git_diff *diff;
+	git_diff *diff = NULL;
+	git_vector paths = GIT_VECTOR_INIT;
+	git_diff_delta *delta;
 
 	/* Nothing to do if there's no repo to talk about */
 	if (!INDEX_OWNER(index))
@@ -765,21 +768,31 @@ static int truncate_racily_clean(git_index *index)
 	diff_opts.flags |= GIT_DIFF_INCLUDE_TYPECHANGE | GIT_DIFF_IGNORE_SUBMODULES | GIT_DIFF_DISABLE_PATHSPEC_MATCH;
 	git_vector_foreach(&index->entries, i, entry) {
 		if (!is_racy_timestamp(&index->stamp.mtime, entry))
-			continue;
+			git_vector_insert(&paths, (char *)entry->path);
+	}
 
-		/* TODO: use the (non-fnmatching) filelist iterator */
-		diff_opts.pathspec.count = 1;
-		diff_opts.pathspec.strings = (char **) &entry->path;
+	if (paths.length == 0)
+		goto done;
 
-		if ((error = git_diff_index_to_workdir(&diff, INDEX_OWNER(index), index, &diff_opts)) < 0)
-			return error;
+	diff_opts.pathspec.count = paths.length;
+	diff_opts.pathspec.strings = (char **)paths.contents;
 
-		if (git_diff_num_deltas(diff) > 0)
-			entry->file_size = 0;
+	if ((error = git_diff_index_to_workdir(&diff, INDEX_OWNER(index), index, &diff_opts)) < 0)
+		return error;
+
+	git_vector_foreach(&diff->deltas, i, delta) {
+		entry = (git_index_entry *)git_index_get_bypath(index, delta->old_file.path, 0);
 
-		git_diff_free(diff);
+		/* Ensure that we have a stage 0 for this file (ie, it's not a
+		 * conflict), otherwise smudging it is quite pointless.
+		 */
+		if (entry)
+			entry->file_size = 0;
 	}
 
+done:
+	git_diff_free(diff);
+	git_vector_free(&paths);
 	return 0;
 }