index: use a diff to perform update_all We currently iterate over all the entries and re-add them to the index. While this provides correctness, it is wasteful as we try to re-insert files which have not changed. Instead, take a diff between the index and the worktree and only re-add those which we already know have changed.
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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
diff --git a/src/index.c b/src/index.c
index 7c0c310..d673f49 100644
--- a/src/index.c
+++ b/src/index.c
@@ -2758,18 +2758,95 @@ int git_index_remove_all(
return error;
}
+struct foreach_diff_data {
+ git_index *index;
+ const git_pathspec *pathspec;
+ git_index_matched_path_cb cb;
+ void *payload;
+};
+
+static int add_each_file(const git_diff_delta *delta, float progress, void *payload)
+{
+ struct foreach_diff_data *data = payload;
+ const char *match, *path;
+ int error = 0;
+
+ GIT_UNUSED(progress);
+
+ /* We only want those we already have in the index */
+ if (delta->status != GIT_DELTA_MODIFIED &&
+ delta->status != GIT_DELTA_TYPECHANGE &&
+ delta->status != GIT_DELTA_DELETED)
+ return 0;
+
+ path = delta->old_file.path;
+
+ /* We only want those which match the pathspecs */
+ if (!git_pathspec__match(
+ &data->pathspec->pathspec, path, false, (bool)data->index->ignore_case,
+ &match, NULL))
+ return 0;
+
+ if (data->cb)
+ error = data->cb(path, match, data->payload);
+
+ if (error > 0) /* skip this entry */
+ return 0;
+ if (error < 0) /* actual error */
+ return error;
+
+ if (delta->status == GIT_DELTA_DELETED)
+ error = git_index_remove_bypath(data->index, path);
+ else
+ error = git_index_add_bypath(data->index, path);
+
+ return error;
+}
+
int git_index_update_all(
git_index *index,
const git_strarray *pathspec,
git_index_matched_path_cb cb,
void *payload)
{
- int error = index_apply_to_all(
- index, INDEX_ACTION_UPDATE, pathspec, cb, payload);
+ int error;
+ git_repository *repo;
+ git_diff *diff;
+ git_pathspec ps;
+ struct foreach_diff_data data = {
+ index,
+ NULL,
+ cb,
+ payload,
+ };
+
+ repo = INDEX_OWNER(index);
+
+ if (!repo) {
+ return create_index_error(-1,
+ "cannot run update; the index is not backed up by a repository.");
+ }
+
+ /*
+ * We do the matching ourselves intead of passing the list to
+ * diff because we want to tell the callback which one
+ * matched, which we do not know if we ask diff to filter for us.
+ */
+ if ((error = git_pathspec__init(&ps, pathspec)) < 0)
+ return error;
+
+ if ((error = git_diff_index_to_workdir(&diff, repo, index, NULL)) < 0)
+ goto cleanup;
+
+ data.pathspec = &ps;
+ error = git_diff_foreach(diff, add_each_file, NULL, NULL, &data);
+ git_diff_free(diff);
if (error) /* make sure error is set if callback stopped iteration */
giterr_set_after_callback(error);
+cleanup:
+ git_pathspec__clear(&ps);
return error;
}