index: make add_all to act on a diff Instead of going through each entry we have and re-adding, which may not even be correct for certain crlf options and has bad performance, use the function which performs a diff against the worktree and try to add and remove files from that list.
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 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
diff --git a/src/index.c b/src/index.c
index d80ff05..3d0f4d1 100644
--- a/src/index.c
+++ b/src/index.c
@@ -24,6 +24,10 @@
#include "git2/config.h"
#include "git2/sys/index.h"
+static int index_apply_to_wd_diff(git_index *index, int action, const git_strarray *paths,
+ unsigned int flags,
+ git_index_matched_path_cb cb, void *payload);
+
#define entry_size(type,len) ((offsetof(type, path) + (len) + 8) & ~7)
#define short_entry_size(len) entry_size(struct entry_short, len)
#define long_entry_size(len) entry_size(struct entry_long, len)
@@ -2554,6 +2558,13 @@ git_repository *git_index_owner(const git_index *index)
return INDEX_OWNER(index);
}
+enum {
+ INDEX_ACTION_NONE = 0,
+ INDEX_ACTION_UPDATE = 1,
+ INDEX_ACTION_REMOVE = 2,
+ INDEX_ACTION_ADDALL = 3,
+};
+
int git_index_add_all(
git_index *index,
const git_strarray *paths,
@@ -2564,29 +2575,15 @@ int git_index_add_all(
int error;
git_repository *repo;
git_iterator *wditer = NULL;
- const git_index_entry *wd = NULL;
- git_index_entry *entry;
git_pathspec ps;
- const char *match;
- size_t existing;
bool no_fnmatch = (flags & GIT_INDEX_ADD_DISABLE_PATHSPEC_MATCH) != 0;
- int ignorecase;
- git_oid blobid;
assert(index);
- if (INDEX_OWNER(index) == NULL)
- return create_index_error(-1,
- "Could not add paths to index. "
- "Index is not backed up by an existing repository.");
-
repo = INDEX_OWNER(index);
if ((error = git_repository__ensure_not_bare(repo, "index add all")) < 0)
return error;
- if (git_repository__cvar(&ignorecase, repo, GIT_CVAR_IGNORECASE) < 0)
- return -1;
-
if ((error = git_pathspec__init(&ps, paths)) < 0)
return error;
@@ -2597,63 +2594,10 @@ int git_index_add_all(
repo, &ps.pathspec, no_fnmatch)) < 0)
goto cleanup;
- if ((error = git_iterator_for_workdir(
- &wditer, repo, NULL, NULL, 0, ps.prefix, ps.prefix)) < 0)
- goto cleanup;
-
- while (!(error = git_iterator_advance(&wd, wditer))) {
+ error = index_apply_to_wd_diff(index, INDEX_ACTION_ADDALL, paths, flags, cb, payload);
- /* check if path actually matches */
- if (!git_pathspec__match(
- &ps.pathspec, wd->path, no_fnmatch, (bool)ignorecase, &match, NULL))
- continue;
-
- /* skip ignored items that are not already in the index */
- if ((flags & GIT_INDEX_ADD_FORCE) == 0 &&
- git_iterator_current_is_ignored(wditer) &&
- index_find(&existing, index, wd->path, 0, 0, true) < 0)
- continue;
-
- /* issue notification callback if requested */
- if (cb && (error = cb(wd->path, match, payload)) != 0) {
- if (error > 0) /* return > 0 means skip this one */
- continue;
- if (error < 0) { /* return < 0 means abort */
- giterr_set_after_callback(error);
- break;
- }
- }
-
- /* TODO: Should we check if the file on disk is already an exact
- * match to the file in the index and skip this work if it is?
- */
-
- /* write the blob to disk and get the oid */
- if ((error = git_blob_create_fromworkdir(&blobid, repo, wd->path)) < 0)
- break;
-
- /* make the new entry to insert */
- if ((error = index_entry_dup(&entry, INDEX_OWNER(index), wd)) < 0)
- break;
-
- entry->id = blobid;
-
- /* add working directory item to index */
- if ((error = index_insert(index, &entry, 1, false)) < 0)
- break;
-
- git_tree_cache_invalidate_path(index->tree, wd->path);
-
- /* add implies conflict resolved, move conflict entries to REUC */
- if ((error = index_conflict_to_reuc(index, wd->path)) < 0) {
- if (error != GIT_ENOTFOUND)
- break;
- giterr_clear();
- }
- }
-
- if (error == GIT_ITEROVER)
- error = 0;
+ if (error)
+ giterr_set_after_callback(error);
cleanup:
git_iterator_free(wditer);
@@ -2662,15 +2606,10 @@ cleanup:
return error;
}
-enum {
- INDEX_ACTION_NONE = 0,
- INDEX_ACTION_UPDATE = 1,
- INDEX_ACTION_REMOVE = 2,
-};
-
struct foreach_diff_data {
git_index *index;
const git_pathspec *pathspec;
+ unsigned int flags;
git_index_matched_path_cb cb;
void *payload;
};
@@ -2702,27 +2641,30 @@ static int apply_each_file(const git_diff_delta *delta, float progress, void *pa
if (delta->status == GIT_DELTA_DELETED)
error = git_index_remove_bypath(data->index, path);
else
- error = git_index_add_bypath(data->index, path);
+ error = git_index_add_bypath(data->index, delta->new_file.path);
return error;
}
static int index_apply_to_wd_diff(git_index *index, int action, const git_strarray *paths,
+ unsigned int flags,
git_index_matched_path_cb cb, void *payload)
{
int error;
git_diff *diff;
git_pathspec ps;
git_repository *repo;
+ git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
struct foreach_diff_data data = {
index,
NULL,
+ flags,
cb,
payload,
};
assert(index);
- assert(action == INDEX_ACTION_UPDATE);
+ assert(action == INDEX_ACTION_UPDATE || action == INDEX_ACTION_ADDALL);
repo = INDEX_OWNER(index);
@@ -2739,7 +2681,13 @@ static int index_apply_to_wd_diff(git_index *index, int action, const git_strarr
if ((error = git_pathspec__init(&ps, paths)) < 0)
return error;
- if ((error = git_diff_index_to_workdir(&diff, repo, index, NULL)) < 0)
+ if (action == INDEX_ACTION_ADDALL) {
+ opts.flags = GIT_DIFF_INCLUDE_UNTRACKED | GIT_DIFF_RECURSE_IGNORED_DIRS;
+ if (flags == GIT_INDEX_ADD_FORCE)
+ opts.flags |= GIT_DIFF_INCLUDE_IGNORED;
+ }
+
+ if ((error = git_diff_index_to_workdir(&diff, repo, index, &opts)) < 0)
goto cleanup;
data.pathspec = &ps;
@@ -2850,7 +2798,7 @@ int git_index_update_all(
git_index_matched_path_cb cb,
void *payload)
{
- int error = index_apply_to_wd_diff(index, INDEX_ACTION_UPDATE, pathspec, cb, payload);
+ int error = index_apply_to_wd_diff(index, INDEX_ACTION_UPDATE, pathspec, 0, cb, payload);
if (error) /* make sure error is set if callback stopped iteration */
giterr_set_after_callback(error);