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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
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;
}