stash: use _an_ index not _the_ index Don't manipulate the repository's index during stash; instead, manipulate a temporary index and check it out. This allows us to use the checkout mechanism to update the workdir and the repository's index, and allows checkout to use its common mechanisms to write data and handle errors.
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 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
diff --git a/src/stash.c b/src/stash.c
index b85a5e7..cb542a3 100644
--- a/src/stash.c
+++ b/src/stash.c
@@ -12,6 +12,7 @@
#include "message.h"
#include "tree.h"
#include "reflog.h"
+#include "blob.h"
#include "git2/diff.h"
#include "git2/stash.h"
#include "git2/status.h"
@@ -103,19 +104,23 @@ cleanup:
return error;
}
-static int build_tree_from_index(git_tree **out, git_index *index)
+static int build_tree_from_index(
+ git_tree **out,
+ git_repository *repo,
+ git_index *index)
{
int error;
git_oid i_tree_oid;
- if ((error = git_index_write_tree(&i_tree_oid, index)) < 0)
+ if ((error = git_index_write_tree_to(&i_tree_oid, index, repo)) < 0)
return error;
- return git_tree_lookup(out, git_index_owner(index), &i_tree_oid);
+ return git_tree_lookup(out, repo, &i_tree_oid);
}
static int commit_index(
git_commit **i_commit,
+ git_repository *repo,
git_index *index,
const git_signature *stasher,
const char *message,
@@ -126,7 +131,7 @@ static int commit_index(
git_buf msg = GIT_BUF_INIT;
int error;
- if ((error = build_tree_from_index(&i_tree, index)) < 0)
+ if ((error = build_tree_from_index(&i_tree, repo, index)) < 0)
goto cleanup;
if ((error = git_buf_printf(&msg, "index on %s\n", message)) < 0)
@@ -159,7 +164,38 @@ struct stash_update_rules {
bool include_ignored;
};
+/*
+ * Similar to git_index_add_bypath but able to operate on any
+ * index without making assumptions about the repository's index
+ */
+static int stash_to_index(
+ git_repository *repo,
+ git_index *index,
+ const char *path)
+{
+ git_index *repo_index;
+ git_index_entry entry = {{0}};
+ struct stat st;
+ int error;
+
+ if (!git_repository_is_bare(repo) &&
+ (error = git_repository_index__weakptr(&repo_index, repo)) < 0)
+ return error;
+
+ if ((error = git_blob__create_from_paths(
+ &entry.id, &st, repo, NULL, path, 0, true)) < 0)
+ return error;
+
+ git_index_entry__init_from_stat(&entry, &st,
+ (repo_index != NULL || !repo_index->distrust_filemode));
+
+ entry.path = path;
+
+ return git_index_add(index, &entry);
+}
+
static int stash_update_index_from_diff(
+ git_repository *repo,
git_index *index,
const git_diff *diff,
struct stash_update_rules *data)
@@ -205,7 +241,7 @@ static int stash_update_index_from_diff(
}
if (add_path != NULL)
- error = git_index_add_bypath(index, add_path);
+ error = stash_to_index(repo, index, add_path);
}
return error;
@@ -213,17 +249,19 @@ static int stash_update_index_from_diff(
static int build_untracked_tree(
git_tree **tree_out,
- git_index *index,
+ git_repository *repo,
git_commit *i_commit,
uint32_t flags)
{
+ git_index *i_index = NULL;
git_tree *i_tree = NULL;
git_diff *diff = NULL;
git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
struct stash_update_rules data = {0};
int error;
- git_index_clear(index);
+ if ((error = git_index_new(&i_index)) < 0)
+ goto cleanup;
if (flags & GIT_STASH_INCLUDE_UNTRACKED) {
opts.flags |= GIT_DIFF_INCLUDE_UNTRACKED |
@@ -240,24 +278,24 @@ static int build_untracked_tree(
if ((error = git_commit_tree(&i_tree, i_commit)) < 0)
goto cleanup;
- if ((error = git_diff_tree_to_workdir(
- &diff, git_index_owner(index), i_tree, &opts)) < 0)
+ if ((error = git_diff_tree_to_workdir(&diff, repo, i_tree, &opts)) < 0)
goto cleanup;
- if ((error = stash_update_index_from_diff(index, diff, &data)) < 0)
+ if ((error = stash_update_index_from_diff(repo, i_index, diff, &data)) < 0)
goto cleanup;
- error = build_tree_from_index(tree_out, index);
+ error = build_tree_from_index(tree_out, repo, i_index);
cleanup:
git_diff_free(diff);
git_tree_free(i_tree);
+ git_index_free(i_index);
return error;
}
static int commit_untracked(
git_commit **u_commit,
- git_index *index,
+ git_repository *repo,
const git_signature *stasher,
const char *message,
git_commit *i_commit,
@@ -268,7 +306,7 @@ static int commit_untracked(
git_buf msg = GIT_BUF_INIT;
int error;
- if ((error = build_untracked_tree(&u_tree, index, i_commit, flags)) < 0)
+ if ((error = build_untracked_tree(&u_tree, repo, i_commit, flags)) < 0)
goto cleanup;
if ((error = git_buf_printf(&msg, "untracked files on %s\n", message)) < 0)
@@ -276,7 +314,7 @@ static int commit_untracked(
if ((error = git_commit_create(
&u_commit_oid,
- git_index_owner(index),
+ repo,
NULL,
stasher,
stasher,
@@ -287,7 +325,7 @@ static int commit_untracked(
NULL)) < 0)
goto cleanup;
- error = git_commit_lookup(u_commit, git_index_owner(index), &u_commit_oid);
+ error = git_commit_lookup(u_commit, repo, &u_commit_oid);
cleanup:
git_tree_free(u_tree);
@@ -316,10 +354,10 @@ static git_diff_delta *stash_delta_merge(
static int build_workdir_tree(
git_tree **tree_out,
- git_index *index,
+ git_repository *repo,
+ git_index *i_index,
git_commit *b_commit)
{
- git_repository *repo = git_index_owner(index);
git_tree *b_tree = NULL;
git_diff *diff = NULL, *idx_to_wd = NULL;
git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
@@ -331,17 +369,17 @@ static int build_workdir_tree(
if ((error = git_commit_tree(&b_tree, b_commit)) < 0)
goto cleanup;
- if ((error = git_diff_tree_to_index(&diff, repo, b_tree, index, &opts)) < 0 ||
- (error = git_diff_index_to_workdir(&idx_to_wd, repo, index, &opts)) < 0 ||
+ if ((error = git_diff_tree_to_index(&diff, repo, b_tree, i_index, &opts)) < 0 ||
+ (error = git_diff_index_to_workdir(&idx_to_wd, repo, i_index, &opts)) < 0 ||
(error = git_diff__merge(diff, idx_to_wd, stash_delta_merge)) < 0)
goto cleanup;
data.include_changed = true;
- if ((error = stash_update_index_from_diff(index, diff, &data)) < 0)
+ if ((error = stash_update_index_from_diff(repo, i_index, diff, &data)) < 0)
goto cleanup;
- error = build_tree_from_index(tree_out, index);
+ error = build_tree_from_index(tree_out, repo, i_index);
cleanup:
git_diff_free(idx_to_wd);
@@ -353,7 +391,7 @@ cleanup:
static int commit_worktree(
git_oid *w_commit_oid,
- git_index *index,
+ git_repository *repo,
const git_signature *stasher,
const char *message,
git_commit *i_commit,
@@ -362,7 +400,9 @@ static int commit_worktree(
{
int error = 0;
git_tree *w_tree = NULL, *i_tree = NULL;
+ git_index *i_index = NULL;
const git_commit *parents[] = { NULL, NULL, NULL };
+ int ignorecase;
parents[0] = b_commit;
parents[1] = i_commit;
@@ -371,15 +411,21 @@ static int commit_worktree(
if ((error = git_commit_tree(&i_tree, i_commit)) < 0)
goto cleanup;
- if ((error = git_index_read_tree(index, i_tree)) < 0)
+ if ((error = git_index_new(&i_index)) < 0 ||
+ (error = git_repository__cvar(&ignorecase, repo, GIT_CVAR_IGNORECASE)) < 0)
+ goto cleanup;
+
+ git_index__set_ignore_case(i_index, ignorecase);
+
+ if ((error = git_index_read_tree(i_index, i_tree)) < 0)
goto cleanup;
- if ((error = build_workdir_tree(&w_tree, index, b_commit)) < 0)
+ if ((error = build_workdir_tree(&w_tree, repo, i_index, b_commit)) < 0)
goto cleanup;
error = git_commit_create(
w_commit_oid,
- git_index_owner(index),
+ repo,
NULL,
stasher,
stasher,
@@ -392,6 +438,7 @@ static int commit_worktree(
cleanup:
git_tree_free(i_tree);
git_tree_free(w_tree);
+ git_index_free(i_index);
return error;
}
@@ -534,12 +581,12 @@ int git_stash_save(
goto cleanup;
if ((error = commit_index(
- &i_commit, index, stasher, git_buf_cstr(&msg), b_commit)) < 0)
+ &i_commit, repo, index, stasher, git_buf_cstr(&msg), b_commit)) < 0)
goto cleanup;
if ((flags & (GIT_STASH_INCLUDE_UNTRACKED | GIT_STASH_INCLUDE_IGNORED)) &&
(error = commit_untracked(
- &u_commit, index, stasher, git_buf_cstr(&msg),
+ &u_commit, repo, stasher, git_buf_cstr(&msg),
i_commit, flags)) < 0)
goto cleanup;
@@ -547,7 +594,7 @@ int git_stash_save(
goto cleanup;
if ((error = commit_worktree(
- out, index, stasher, git_buf_cstr(&msg),
+ out, repo, stasher, git_buf_cstr(&msg),
i_commit, b_commit, u_commit)) < 0)
goto cleanup;
@@ -738,6 +785,8 @@ static void normalize_apply_options(
memcpy(opts, &default_apply_opts, sizeof(git_stash_apply_options));
}
+ opts->checkout_options.checkout_strategy |= GIT_CHECKOUT_NO_REFRESH;
+
if (!opts->checkout_options.our_label)
opts->checkout_options.our_label = "Updated upstream";