Merge pull request #1390 from ethomson/reuc_clear clear REUC on checkout
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
diff --git a/include/git2/index.h b/include/git2/index.h
index da95ee9..3d4bd15 100644
--- a/include/git2/index.h
+++ b/include/git2/index.h
@@ -564,6 +564,14 @@ GIT_EXTERN(int) git_index_reuc_add(git_index *index, const char *path,
*/
GIT_EXTERN(int) git_index_reuc_remove(git_index *index, size_t n);
+/**
+ * Remove all resolve undo entries from the index
+ *
+ * @param index an existing index object
+ * @return 0 or an error code
+ */
+GIT_EXTERN(void) git_index_reuc_clear(git_index *index);
+
/**@}*/
/** @} */
diff --git a/src/checkout.c b/src/checkout.c
index 040ead2..19ac913 100644
--- a/src/checkout.c
+++ b/src/checkout.c
@@ -1143,6 +1143,9 @@ static int checkout_data_init(
if ((error = git_repository_index(&data->index, data->repo)) < 0 ||
(error = git_index_read(data->index)) < 0)
goto cleanup;
+
+ /* clear the REUC when doing a tree or commit checkout */
+ git_index_reuc_clear(data->index);
}
}
diff --git a/src/index.c b/src/index.c
index 5964908..eb3376c 100644
--- a/src/index.c
+++ b/src/index.c
@@ -297,18 +297,8 @@ int git_index_new(git_index **out)
static void index_free(git_index *index)
{
- git_index_entry *e;
- git_index_reuc_entry *reuc;
- size_t i;
-
git_index_clear(index);
- git_vector_foreach(&index->entries, i, e) {
- index_entry_free(e);
- }
git_vector_free(&index->entries);
- git_vector_foreach(&index->reuc, i, reuc) {
- index_entry_reuc_free(reuc);
- }
git_vector_free(&index->reuc);
git__free(index->index_file_path);
@@ -335,16 +325,10 @@ void git_index_clear(git_index *index)
git__free(e->path);
git__free(e);
}
-
- for (i = 0; i < index->reuc.length; ++i) {
- git_index_reuc_entry *e;
- e = git_vector_get(&index->reuc, i);
- git__free(e->path);
- git__free(e);
- }
-
git_vector_clear(&index->entries);
- git_vector_clear(&index->reuc);
+
+ git_index_reuc_clear(index);
+
git_futils_filestamp_set(&index->stamp, NULL);
git_tree_cache_free(index->tree);
@@ -1151,6 +1135,21 @@ int git_index_reuc_remove(git_index *index, size_t position)
return error;
}
+void git_index_reuc_clear(git_index *index)
+{
+ size_t i;
+ git_index_reuc_entry *reuc;
+
+ assert(index);
+
+ git_vector_foreach(&index->reuc, i, reuc) {
+ git__free(reuc->path);
+ git__free(reuc);
+ }
+
+ git_vector_clear(&index->reuc);
+}
+
static int index_error_invalid(const char *message)
{
giterr_set(GITERR_INDEX, "Invalid data in index - %s", message);
diff --git a/tests-clar/index/reuc.c b/tests-clar/index/reuc.c
index 80c295e..4d5955a 100644
--- a/tests-clar/index/reuc.c
+++ b/tests-clar/index/reuc.c
@@ -1,6 +1,7 @@
#include "clar_libgit2.h"
#include "index.h"
#include "git2/repository.h"
+#include "../reset/reset_helpers.h"
static git_repository *repo;
static git_index *repo_index;
@@ -286,3 +287,87 @@ void test_index_reuc__write(void)
cl_assert_equal_s("two.txt", reuc->path);
}
+static int reuc_entry_exists(void)
+{
+ return (git_index_reuc_get_bypath(repo_index, "newfile.txt") != NULL);
+}
+
+void test_index_reuc__cleaned_on_reset_hard(void)
+{
+ git_object *target;
+
+ retrieve_target_from_oid(&target, repo, "3a34580a35add43a4cf361e8e9a30060a905c876");
+
+ test_index_reuc__add();
+ cl_git_pass(git_reset(repo, target, GIT_RESET_HARD));
+ cl_assert(reuc_entry_exists() == false);
+
+ git_object_free(target);
+}
+
+void test_index_reuc__cleaned_on_reset_mixed(void)
+{
+ git_object *target;
+
+ retrieve_target_from_oid(&target, repo, "3a34580a35add43a4cf361e8e9a30060a905c876");
+
+ test_index_reuc__add();
+ cl_git_pass(git_reset(repo, target, GIT_RESET_MIXED));
+ cl_assert(reuc_entry_exists() == false);
+
+ git_object_free(target);
+}
+
+void test_index_reuc__retained_on_reset_soft(void)
+{
+ git_object *target;
+
+ retrieve_target_from_oid(&target, repo, "3a34580a35add43a4cf361e8e9a30060a905c876");
+
+ git_reset(repo, target, GIT_RESET_HARD);
+
+ test_index_reuc__add();
+ cl_git_pass(git_reset(repo, target, GIT_RESET_SOFT));
+ cl_assert(reuc_entry_exists() == true);
+
+ git_object_free(target);
+}
+
+void test_index_reuc__cleaned_on_checkout_tree(void)
+{
+ git_oid oid;
+ git_object *obj;
+ git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT;
+
+ opts.checkout_strategy = GIT_CHECKOUT_SAFE | GIT_CHECKOUT_UPDATE_ONLY;
+
+ test_index_reuc__add();
+ git_reference_name_to_id(&oid, repo, "refs/heads/master");
+ git_object_lookup(&obj, repo, &oid, GIT_OBJ_ANY);
+ git_checkout_tree(repo, obj, &opts);
+ cl_assert(reuc_entry_exists() == false);
+
+ git_object_free(obj);
+}
+
+void test_index_reuc__cleaned_on_checkout_head(void)
+{
+ git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT;
+
+ opts.checkout_strategy = GIT_CHECKOUT_SAFE | GIT_CHECKOUT_UPDATE_ONLY;
+
+ test_index_reuc__add();
+ git_checkout_head(repo, &opts);
+ cl_assert(reuc_entry_exists() == false);
+}
+
+void test_index_reuc__retained_on_checkout_index(void)
+{
+ git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT;
+
+ opts.checkout_strategy = GIT_CHECKOUT_SAFE | GIT_CHECKOUT_UPDATE_ONLY;
+
+ test_index_reuc__add();
+ git_checkout_index(repo, repo_index, &opts);
+ cl_assert(reuc_entry_exists() == true);
+}