summaryrefslogtreecommitdiff
path: root/src/index.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/index.c')
-rw-r--r--src/index.c81
1 files changed, 79 insertions, 2 deletions
diff --git a/src/index.c b/src/index.c
index 7c0c31052..d673f4921 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;
}