deps/libgit2/src/libgit2/stash.c

filter: Statementstmt 71.3% 396 / 555 Branchbran 49.6% 142 / 286 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 63.9% 538 / 841 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« deps/libgit2/src/libgit2/attrcache.c ↑ Summary deps/libgit2/src/libgit2/commit_list.c »
1 /*
2 * Copyright (C) the libgit2 contributors. All rights reserved.
3 *
4 * This file is part of libgit2, distributed under the GNU GPL v2 with
5 * a Linking Exception. For full terms see the included COPYING file.
6 */
7  
8 #include "common.h"
9  
10 #include "repository.h"
11 #include "commit.h"
12 #include "tree.h"
13 #include "reflog.h"
14 #include "blob.h"
15 #include "git2/diff.h"
16 #include "git2/stash.h"
17 #include "git2/status.h"
18 #include "git2/checkout.h"
19 #include "git2/index.h"
20 #include "git2/transaction.h"
21 #include "git2/merge.h"
22 #include "index.h"
23 #include "signature.h"
24 #include "iterator.h"
25 #include "merge.h"
26 #include "diff.h"
27 #include "diff_generate.h"
28  
29 1 static int create_error(int error, const char *msg)
30 {
31 1 git_error_set(GIT_ERROR_STASH, "cannot stash changes - %s", msg);
32 1 return error;
33 }
34  
35 7 static int retrieve_head(git_reference **out, git_repository *repo)
36 {
37 7 int error = git_repository_head(out, repo);
38  
39 7 ▶ if (error == GIT_EUNBORNBRANCH)
Branch: 1 branch
BranchTrueFalseTotal
if (error == GIT_EUNBORNBRANCH) 0 7 7
40 0 return create_error(error, "you do not have the initial commit yet.");
41  
42 7 return error;
43 }
44  
45 7 static int append_abbreviated_oid(git_str *out, const git_oid *b_commit)
46 {
47 char *formatted_oid;
48  
49 7 formatted_oid = git_oid_allocfmt(b_commit);
50 7 ▶ GIT_ERROR_CHECK_ALLOC(formatted_oid);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ERROR_CHECK_ALLOC(formatted_oid); 0 7 7
51  
52 7 git_str_put(out, formatted_oid, 7);
53 7 git__free(formatted_oid);
54  
55 7 ▶ return git_str_oom(out) ? -1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
return git_str_oom(out) ? -1 : 0; 0 7 7
56 }
57  
58 7 static int append_commit_description(git_str *out, git_commit *commit)
59 {
60 7 const char *summary = git_commit_summary(commit);
61 7 ▶ GIT_ERROR_CHECK_ALLOC(summary);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ERROR_CHECK_ALLOC(summary); 0 7 7
62  
63 7 ▶ if (append_abbreviated_oid(out, git_commit_id(commit)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (append_abbreviated_oid(out, git_commit_id(commit)) < 0) 0 7 7
64 0 return -1;
65  
66 7 git_str_putc(out, ' ');
67 7 git_str_puts(out, summary);
68 7 git_str_putc(out, '\n');
69  
70 7 ▶ return git_str_oom(out) ? -1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
return git_str_oom(out) ? -1 : 0; 0 7 7
71 }
72  
73 7 static int retrieve_base_commit_and_message(
74 git_commit **b_commit,
75 git_str *stash_message,
76 git_repository *repo)
77 {
78 7 git_reference *head = NULL;
79 int error;
80  
81 7 ▶ if ((error = retrieve_head(&head, repo)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = retrieve_head(&head, repo)) < 0) 0 7 7
82 0 return error;
83  
84 7 ▶ if (strcmp("HEAD", git_reference_name(head)) == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (strcmp("HEAD", git_reference_name(head)) == 0) 0 7 7
85 0 error = git_str_puts(stash_message, "(no branch): ");
86 else
87 7 error = git_str_printf(
88 stash_message,
89 "%s: ",
90 7 git_reference_name(head) + strlen(GIT_REFS_HEADS_DIR));
91 7 ▶ if (error < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (error < 0) 0 7 7
92 0 goto cleanup;
93  
94 7 ▶ if ((error = git_commit_lookup(
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_commit_lookup( 0 7 7
95 b_commit, repo, git_reference_target(head))) < 0)
96 0 goto cleanup;
97  
98 7 ▶ if ((error = append_commit_description(stash_message, *b_commit)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = append_commit_description(stash_message, *b_commit)) < 0) 7 0 7
99 0 goto cleanup;
100  
101 7 cleanup:
102 7 git_reference_free(head);
103 7 return error;
104 }
105  
106 14 static int build_tree_from_index(
107 git_tree **out,
108 git_repository *repo,
109 git_index *index)
110 {
111 int error;
112 git_oid i_tree_oid;
113  
114 14 ▶ if ((error = git_index_write_tree_to(&i_tree_oid, index, repo)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_index_write_tree_to(&i_tree_oid, index, repo)) < 0) 0 14 14
115 0 return error;
116  
117 14 return git_tree_lookup(out, repo, &i_tree_oid);
118 }
119  
120 6 static int commit_index(
121 git_commit **i_commit,
122 git_repository *repo,
123 git_index *index,
124 const git_signature *stasher,
125 const char *message,
126 const git_commit *parent)
127 {
128 6 git_tree *i_tree = NULL;
129 git_oid i_commit_oid;
130 6 git_str msg = GIT_STR_INIT;
131 int error;
132  
133 6 ▶ if ((error = build_tree_from_index(&i_tree, repo, index)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = build_tree_from_index(&i_tree, repo, index)) < 0) 0 6 6
134 0 goto cleanup;
135  
136 6 ▶ if ((error = git_str_printf(&msg, "index on %s\n", message)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_str_printf(&msg, "index on %s\n", message)) < 0) 0 6 6
137 0 goto cleanup;
138  
139 6 ▶ if ((error = git_commit_create(
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_commit_create( 0 6 6
140 &i_commit_oid,
141 git_index_owner(index),
142 NULL,
143 stasher,
144 stasher,
145 NULL,
146 git_str_cstr(&msg),
147 i_tree,
148 1,
149 &parent)) < 0)
150 0 goto cleanup;
151  
152 6 error = git_commit_lookup(i_commit, git_index_owner(index), &i_commit_oid);
153  
154 6 cleanup:
155 6 git_tree_free(i_tree);
156 6 git_str_dispose(&msg);
157 6 return error;
158 }
159  
160 struct stash_update_rules {
161 bool include_changed;
162 bool include_untracked;
163 bool include_ignored;
164 };
165  
166 /*
167 * Similar to git_index_add_bypath but able to operate on any
168 * index without making assumptions about the repository's index
169 */
170 12 static int stash_to_index(
171 git_repository *repo,
172 git_index *index,
173 const char *path)
174 {
175 12 git_index *repo_index = NULL;
176 12 git_index_entry entry = {{0}};
177 struct stat st;
178 int error;
179  
180 24 if (!git_repository_is_bare(repo) &&
181 12 (error = git_repository_index__weakptr(&repo_index, repo)) < 0)
182 0 return error;
183  
184 12 ▶ if ((error = git_blob__create_from_paths(
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_blob__create_from_paths( 0 12 12
185 &entry.id, &st, repo, NULL, path, 0, true)) < 0)
186 0 return error;
187  
188 12 git_index_entry__init_from_stat(&entry, &st,
189 12 ▶ (repo_index == NULL || !repo_index->distrust_filemode));
Branch: 2 branches
BranchTrueFalseTotal
(repo_index == NULL || !repo_index->distrust_filemode)); 12 0 12
(repo_index == NULL || !repo_index->distrust_filemode)); 12 0 12
190  
191 12 entry.path = path;
192  
193 12 return git_index_add(index, &entry);
194 }
195  
196 8 static int stash_update_index_from_diff(
197 git_repository *repo,
198 git_index *index,
199 const git_diff *diff,
200 struct stash_update_rules *data)
201 {
202 8 int error = 0;
203 8 size_t d, max_d = git_diff_num_deltas(diff);
204  
205 44 ▶ for (d = 0; !error && d < max_d; ++d) {
Branch: 2 branches
BranchTrueFalseTotal
for (d = 0; !error && d < max_d; ++d) { 44 0 44
for (d = 0; !error && d < max_d; ++d) { 36 8 44
206 36 const char *add_path = NULL;
207 36 const git_diff_delta *delta = git_diff_get_delta(diff, d);
208  
209 36 switch (delta->status) {
210 1 case GIT_DELTA_IGNORED:
211 1 ▶ if (data->include_ignored)
Branch: 1 branch
BranchTrueFalseTotal
if (data->include_ignored) 1 0 1
212 1 add_path = delta->new_file.path;
213 1 break;
214  
215 30 case GIT_DELTA_UNTRACKED:
216 30 ▶ if (data->include_untracked &&
Branch: 1 branch
BranchTrueFalseTotal
if (data->include_untracked && 6 24 30
217 6 ▶ delta->new_file.mode != GIT_FILEMODE_TREE)
Branch: 1 branch
BranchTrueFalseTotal
delta->new_file.mode != GIT_FILEMODE_TREE) 6 0 6
218 6 add_path = delta->new_file.path;
219 30 break;
220  
221 5 case GIT_DELTA_ADDED:
222 case GIT_DELTA_MODIFIED:
223 5 ▶ if (data->include_changed)
Branch: 1 branch
BranchTrueFalseTotal
if (data->include_changed) 5 0 5
224 5 add_path = delta->new_file.path;
225 5 break;
226  
227 0 case GIT_DELTA_DELETED:
228 0 if (data->include_changed &&
229 0 !git_index_find(NULL, index, delta->old_file.path))
230 0 error = git_index_remove(index, delta->old_file.path, 0);
231 0 break;
232  
233 0 default:
234 /* Unimplemented */
235 0 git_error_set(
236 GIT_ERROR_INVALID,
237 "cannot update index. Unimplemented status (%d)",
238 0 delta->status);
239 0 return -1;
240 }
241  
242 36 ▶ if (add_path != NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (add_path != NULL) 12 24 36
243 12 error = stash_to_index(repo, index, add_path);
244 }
245  
246 8 return error;
247 }
248  
249 2 static int build_untracked_tree(
250 git_tree **tree_out,
251 git_repository *repo,
252 git_commit *i_commit,
253 uint32_t flags)
254 {
255 2 git_index *i_index = NULL;
256 2 git_tree *i_tree = NULL;
257 2 git_diff *diff = NULL;
258 2 git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
259 2 struct stash_update_rules data = {0};
260 int error;
261  
262 2 ▶ if ((error = git_index_new(&i_index)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_index_new(&i_index)) < 0) 0 2 2
263 0 goto cleanup;
264  
265 2 ▶ if (flags & GIT_STASH_INCLUDE_UNTRACKED) {
Branch: 1 branch
BranchTrueFalseTotal
if (flags & GIT_STASH_INCLUDE_UNTRACKED) { 1 1 2
266 1 opts.flags |= GIT_DIFF_INCLUDE_UNTRACKED |
267 GIT_DIFF_RECURSE_UNTRACKED_DIRS;
268 1 data.include_untracked = true;
269 }
270  
271 2 ▶ if (flags & GIT_STASH_INCLUDE_IGNORED) {
Branch: 1 branch
BranchTrueFalseTotal
if (flags & GIT_STASH_INCLUDE_IGNORED) { 1 1 2
272 1 opts.flags |= GIT_DIFF_INCLUDE_IGNORED |
273 GIT_DIFF_RECURSE_IGNORED_DIRS;
274 1 data.include_ignored = true;
275 }
276  
277 2 ▶ if ((error = git_commit_tree(&i_tree, i_commit)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_commit_tree(&i_tree, i_commit)) < 0) 0 2 2
278 0 goto cleanup;
279  
280 2 ▶ if ((error = git_diff_tree_to_workdir(&diff, repo, i_tree, &opts)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_diff_tree_to_workdir(&diff, repo, i_tree, &opts)) < 0) 0 2 2
281 0 goto cleanup;
282  
283 2 ▶ if ((error = stash_update_index_from_diff(repo, i_index, diff, &data)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = stash_update_index_from_diff(repo, i_index, diff, &data)) < 0) 0 2 2
284 0 goto cleanup;
285  
286 2 error = build_tree_from_index(tree_out, repo, i_index);
287  
288 2 cleanup:
289 2 git_diff_free(diff);
290 2 git_tree_free(i_tree);
291 2 git_index_free(i_index);
292 2 return error;
293 }
294  
295 2 static int commit_untracked(
296 git_commit **u_commit,
297 git_repository *repo,
298 const git_signature *stasher,
299 const char *message,
300 git_commit *i_commit,
301 uint32_t flags)
302 {
303 2 git_tree *u_tree = NULL;
304 git_oid u_commit_oid;
305 2 git_str msg = GIT_STR_INIT;
306 int error;
307  
308 2 ▶ if ((error = build_untracked_tree(&u_tree, repo, i_commit, flags)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = build_untracked_tree(&u_tree, repo, i_commit, flags)) < 0) 0 2 2
309 0 goto cleanup;
310  
311 2 ▶ if ((error = git_str_printf(&msg, "untracked files on %s\n", message)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_str_printf(&msg, "untracked files on %s\n", message)) < 0) 0 2 2
312 0 goto cleanup;
313  
314 2 ▶ if ((error = git_commit_create(
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_commit_create( 0 2 2
315 &u_commit_oid,
316 repo,
317 NULL,
318 stasher,
319 stasher,
320 NULL,
321 git_str_cstr(&msg),
322 u_tree,
323 0,
324 NULL)) < 0)
325 0 goto cleanup;
326  
327 2 error = git_commit_lookup(u_commit, repo, &u_commit_oid);
328  
329 2 cleanup:
330 2 git_tree_free(u_tree);
331 2 git_str_dispose(&msg);
332 2 return error;
333 }
334  
335 3 static git_diff_delta *stash_delta_merge(
336 const git_diff_delta *a,
337 const git_diff_delta *b,
338 git_pool *pool)
339 {
340 /* Special case for stash: if a file is deleted in the index, but exists
341 * in the working tree, we need to stash the workdir copy for the workdir.
342 */
343 3 ▶ if (a->status == GIT_DELTA_DELETED && b->status == GIT_DELTA_UNTRACKED) {
Branch: 2 branches
BranchTrueFalseTotal
if (a->status == GIT_DELTA_DELETED && b->status == GIT_DELTA_UNTRACKED) { 0 3 3
if (a->status == GIT_DELTA_DELETED && b->status == GIT_DELTA_UNTRACKED) { 0 0 0
344 0 git_diff_delta *dup = git_diff__delta_dup(b, pool);
345  
346 0 ▶ if (dup)
Branch: 1 branch
BranchTrueFalseTotal
if (dup) 0 0 0
347 0 dup->status = GIT_DELTA_MODIFIED;
348 0 return dup;
349 }
350  
351 3 return git_diff__merge_like_cgit(a, b, pool);
352 }
353  
354 6 static int build_workdir_tree(
355 git_tree **tree_out,
356 git_repository *repo,
357 git_index *i_index,
358 git_commit *b_commit)
359 {
360 6 git_tree *b_tree = NULL;
361 6 git_diff *diff = NULL, *idx_to_wd = NULL;
362 6 git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
363 6 struct stash_update_rules data = {0};
364 int error;
365  
366 6 opts.flags = GIT_DIFF_IGNORE_SUBMODULES | GIT_DIFF_INCLUDE_UNTRACKED;
367  
368 6 ▶ if ((error = git_commit_tree(&b_tree, b_commit)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_commit_tree(&b_tree, b_commit)) < 0) 0 6 6
369 0 goto cleanup;
370  
371 12 if ((error = git_diff_tree_to_index(&diff, repo, b_tree, i_index, &opts)) < 0 ||
372 12 ▶ (error = git_diff_index_to_workdir(&idx_to_wd, repo, i_index, &opts)) < 0 ||
Branch: 1 branch
BranchTrueFalseTotal
(error = git_diff_index_to_workdir(&idx_to_wd, repo, i_index, &opts)) < 0 || 0 6 6
373 6 (error = git_diff__merge(diff, idx_to_wd, stash_delta_merge)) < 0)
374 0 goto cleanup;
375  
376 6 data.include_changed = true;
377  
378 6 ▶ if ((error = stash_update_index_from_diff(repo, i_index, diff, &data)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = stash_update_index_from_diff(repo, i_index, diff, &data)) < 0) 0 6 6
379 0 goto cleanup;
380  
381 6 error = build_tree_from_index(tree_out, repo, i_index);
382  
383 6 cleanup:
384 6 git_diff_free(idx_to_wd);
385 6 git_diff_free(diff);
386 6 git_tree_free(b_tree);
387  
388 6 return error;
389 }
390  
391 6 static int commit_worktree(
392 git_oid *w_commit_oid,
393 git_repository *repo,
394 const git_signature *stasher,
395 const char *message,
396 git_commit *i_commit,
397 git_commit *b_commit,
398 git_commit *u_commit)
399 {
400 6 const git_commit *parents[] = { NULL, NULL, NULL };
401 6 git_index *i_index = NULL, *r_index = NULL;
402 6 git_tree *w_tree = NULL;
403 6 int error = 0, ignorecase;
404  
405 6 parents[0] = b_commit;
406 6 parents[1] = i_commit;
407 6 parents[2] = u_commit;
408  
409 12 if ((error = git_repository_index(&r_index, repo) < 0) ||
410 6 (error = git_index_new(&i_index)) < 0 ||
411 12 (error = git_index__fill(i_index, &r_index->entries) < 0) ||
412 6 (error = git_repository__configmap_lookup(&ignorecase, repo, GIT_CONFIGMAP_IGNORECASE)) < 0)
413 0 goto cleanup;
414  
415 6 git_index__set_ignore_case(i_index, ignorecase);
416  
417 6 ▶ if ((error = build_workdir_tree(&w_tree, repo, i_index, b_commit)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = build_workdir_tree(&w_tree, repo, i_index, b_commit)) < 0) 0 6 6
418 0 goto cleanup;
419  
420 6 ▶ error = git_commit_create(
Branch: 1 branch
BranchTrueFalseTotal
error = git_commit_create( 2 4 6
421 w_commit_oid,
422 repo,
423 NULL,
424 stasher,
425 stasher,
426 NULL,
427 message,
428 w_tree,
429 u_commit ? 3 : 2,
430 parents);
431  
432 6 cleanup:
433 6 git_tree_free(w_tree);
434 6 git_index_free(i_index);
435 6 git_index_free(r_index);
436 6 return error;
437 }
438  
439 6 static int prepare_worktree_commit_message(git_str *out, const char *user_message)
440 {
441 6 git_str buf = GIT_STR_INIT;
442 6 int error = 0;
443  
444 6 ▶ if (!user_message) {
Branch: 1 branch
BranchTrueFalseTotal
if (!user_message) { 0 6 6
445 0 git_str_printf(&buf, "WIP on %s", git_str_cstr(out));
446 } else {
447 const char *colon;
448  
449 6 ▶ if ((colon = strchr(git_str_cstr(out), ':')) == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if ((colon = strchr(git_str_cstr(out), ':')) == NULL) 0 6 6
450 0 goto cleanup;
451  
452 6 git_str_puts(&buf, "On ");
453 6 git_str_put(&buf, git_str_cstr(out), colon - out->ptr);
454 6 git_str_printf(&buf, ": %s\n", user_message);
455 }
456  
457 6 ▶ if (git_str_oom(&buf)) {
Branch: 1 branch
BranchTrueFalseTotal
if (git_str_oom(&buf)) { 0 6 6
458 0 error = -1;
459 0 goto cleanup;
460 }
461  
462 6 git_str_swap(out, &buf);
463  
464 6 cleanup:
465 6 git_str_dispose(&buf);
466 6 return error;
467 }
468  
469 6 static int update_reflog(
470 git_oid *w_commit_oid,
471 git_repository *repo,
472 const char *message)
473 {
474 git_reference *stash;
475 int error;
476  
477 6 ▶ if ((error = git_reference_ensure_log(repo, GIT_REFS_STASH_FILE)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_reference_ensure_log(repo, GIT_REFS_STASH_FILE)) < 0) 0 6 6
478 0 return error;
479  
480 6 error = git_reference_create(&stash, repo, GIT_REFS_STASH_FILE, w_commit_oid, 1, message);
481  
482 6 git_reference_free(stash);
483  
484 6 return error;
485 }
486  
487 6 static int is_dirty_cb(const char *path, unsigned int status, void *payload)
488 {
489 6 GIT_UNUSED(path);
490 6 GIT_UNUSED(status);
491 6 GIT_UNUSED(payload);
492  
493 6 return GIT_PASSTHROUGH;
494 }
495  
496 7 static int ensure_there_are_changes_to_stash(git_repository *repo, uint32_t flags)
497 {
498 int error;
499 7 git_status_options opts = GIT_STATUS_OPTIONS_INIT;
500  
501 7 opts.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
502 7 opts.flags = GIT_STATUS_OPT_EXCLUDE_SUBMODULES;
503  
504 7 ▶ if (flags & GIT_STASH_INCLUDE_UNTRACKED)
Branch: 1 branch
BranchTrueFalseTotal
if (flags & GIT_STASH_INCLUDE_UNTRACKED) 1 6 7
505 1 opts.flags |= GIT_STATUS_OPT_INCLUDE_UNTRACKED |
506 GIT_STATUS_OPT_RECURSE_UNTRACKED_DIRS;
507  
508 7 ▶ if (flags & GIT_STASH_INCLUDE_IGNORED)
Branch: 1 branch
BranchTrueFalseTotal
if (flags & GIT_STASH_INCLUDE_IGNORED) 1 6 7
509 1 opts.flags |= GIT_STATUS_OPT_INCLUDE_IGNORED |
510 GIT_STATUS_OPT_RECURSE_IGNORED_DIRS;
511  
512 7 error = git_status_foreach_ext(repo, &opts, is_dirty_cb, NULL);
513  
514 7 ▶ if (error == GIT_PASSTHROUGH)
Branch: 1 branch
BranchTrueFalseTotal
if (error == GIT_PASSTHROUGH) 6 1 7
515 6 return 0;
516  
517 1 ▶ if (!error)
Branch: 1 branch
BranchTrueFalseTotal
if (!error) 1 0 1
518 1 return create_error(GIT_ENOTFOUND, "there is nothing to stash.");
519  
520 0 return error;
521 }
522  
523 6 static int reset_index_and_workdir(git_repository *repo, git_commit *commit, uint32_t flags)
524 {
525 6 git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
526  
527 6 opts.checkout_strategy = GIT_CHECKOUT_FORCE;
528 6 ▶ if (flags & GIT_STASH_INCLUDE_UNTRACKED)
Branch: 1 branch
BranchTrueFalseTotal
if (flags & GIT_STASH_INCLUDE_UNTRACKED) 1 5 6
529 1 opts.checkout_strategy |= GIT_CHECKOUT_REMOVE_UNTRACKED;
530 6 ▶ if (flags & GIT_STASH_INCLUDE_IGNORED)
Branch: 1 branch
BranchTrueFalseTotal
if (flags & GIT_STASH_INCLUDE_IGNORED) 1 5 6
531 1 opts.checkout_strategy |= GIT_CHECKOUT_REMOVE_IGNORED;
532  
533 6 return git_checkout_tree(repo, (git_object *)commit, &opts);
534 }
535  
536 7 int git_stash_save(
537 git_oid *out,
538 git_repository *repo,
539 const git_signature *stasher,
540 const char *message,
541 uint32_t flags)
542 {
543 7 git_index *index = NULL;
544 7 git_commit *b_commit = NULL, *i_commit = NULL, *u_commit = NULL;
545 7 git_str msg = GIT_STR_INIT;
546 int error;
547  
548 7 ▶ GIT_ASSERT_ARG(out);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(out); 0 7 7
549 7 ▶ GIT_ASSERT_ARG(repo);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(repo); 0 7 7
550 7 ▶ GIT_ASSERT_ARG(stasher);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(stasher); 0 7 7
551  
552 7 ▶ if ((error = git_repository__ensure_not_bare(repo, "stash save")) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_repository__ensure_not_bare(repo, "stash save")) < 0) 0 7 7
553 0 return error;
554  
555 7 ▶ if ((error = retrieve_base_commit_and_message(&b_commit, &msg, repo)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = retrieve_base_commit_and_message(&b_commit, &msg, repo)) < 0) 0 7 7
556 0 goto cleanup;
557  
558 7 ▶ if ((error = ensure_there_are_changes_to_stash(repo, flags)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = ensure_there_are_changes_to_stash(repo, flags)) < 0) 1 6 7
559 1 goto cleanup;
560  
561 6 ▶ if ((error = git_repository_index(&index, repo)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_repository_index(&index, repo)) < 0) 0 6 6
562 0 goto cleanup;
563  
564 6 ▶ if ((error = commit_index(&i_commit, repo, index, stasher,
Branch: 1 branch
BranchTrueFalseTotal
if ((error = commit_index(&i_commit, repo, index, stasher, 0 6 6
565 git_str_cstr(&msg), b_commit)) < 0)
566 0 goto cleanup;
567  
568 8 if ((flags & (GIT_STASH_INCLUDE_UNTRACKED | GIT_STASH_INCLUDE_IGNORED)) &&
569 2 (error = commit_untracked(&u_commit, repo, stasher,
570 git_str_cstr(&msg), i_commit, flags)) < 0)
571 0 goto cleanup;
572  
573 6 ▶ if ((error = prepare_worktree_commit_message(&msg, message)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = prepare_worktree_commit_message(&msg, message)) < 0) 0 6 6
574 0 goto cleanup;
575  
576 6 ▶ if ((error = commit_worktree(out, repo, stasher, git_str_cstr(&msg),
Branch: 1 branch
BranchTrueFalseTotal
if ((error = commit_worktree(out, repo, stasher, git_str_cstr(&msg), 0 6 6
577 i_commit, b_commit, u_commit)) < 0)
578 0 goto cleanup;
579  
580 6 git_str_rtrim(&msg);
581  
582 6 ▶ if ((error = update_reflog(out, repo, git_str_cstr(&msg))) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = update_reflog(out, repo, git_str_cstr(&msg))) < 0) 0 6 6
583 0 goto cleanup;
584  
585 6 ▶ if ((error = reset_index_and_workdir(repo, (flags & GIT_STASH_KEEP_INDEX) ? i_commit : b_commit,
Branch: 2 branches
BranchTrueFalseTotal
if ((error = reset_index_and_workdir(repo, (flags & GIT_STASH_KEEP_INDEX) ? i_commit : b_commit, 1 5 6
if ((error = reset_index_and_workdir(repo, (flags & GIT_STASH_KEEP_INDEX) ? i_commit : b_commit, 6 0 6
586 flags)) < 0)
587 0 goto cleanup;
588  
589 6 cleanup:
590  
591 7 git_str_dispose(&msg);
592 7 git_commit_free(i_commit);
593 7 git_commit_free(b_commit);
594 7 git_commit_free(u_commit);
595 7 git_index_free(index);
596  
597 7 return error;
598 }
599  
600 2 static int retrieve_stash_commit(
601 git_commit **commit,
602 git_repository *repo,
603 size_t index)
604 {
605 2 git_reference *stash = NULL;
606 2 git_reflog *reflog = NULL;
607 int error;
608 size_t max;
609 const git_reflog_entry *entry;
610  
611 2 ▶ if ((error = git_reference_lookup(&stash, repo, GIT_REFS_STASH_FILE)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_reference_lookup(&stash, repo, GIT_REFS_STASH_FILE)) < 0) 0 2 2
612 0 goto cleanup;
613  
614 2 ▶ if ((error = git_reflog_read(&reflog, repo, GIT_REFS_STASH_FILE)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_reflog_read(&reflog, repo, GIT_REFS_STASH_FILE)) < 0) 0 2 2
615 0 goto cleanup;
616  
617 2 max = git_reflog_entrycount(reflog);
618 2 ▶ if (!max || index > max - 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (!max || index > max - 1) { 2 0 2
if (!max || index > max - 1) { 0 2 2
619 0 error = GIT_ENOTFOUND;
620 0 git_error_set(GIT_ERROR_STASH, "no stashed state at position %" PRIuZ, index);
621 0 goto cleanup;
622 }
623  
624 2 entry = git_reflog_entry_byindex(reflog, index);
625 2 ▶ if ((error = git_commit_lookup(commit, repo, git_reflog_entry_id_new(entry))) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_commit_lookup(commit, repo, git_reflog_entry_id_new(entry))) < 0) 2 0 2
626 0 goto cleanup;
627  
628 2 cleanup:
629 2 git_reference_free(stash);
630 2 git_reflog_free(reflog);
631 2 return error;
632 }
633  
634 2 static int retrieve_stash_trees(
635 git_tree **out_stash_tree,
636 git_tree **out_base_tree,
637 git_tree **out_index_tree,
638 git_tree **out_index_parent_tree,
639 git_tree **out_untracked_tree,
640 git_commit *stash_commit)
641 {
642 2 git_tree *stash_tree = NULL;
643 2 git_commit *base_commit = NULL;
644 2 git_tree *base_tree = NULL;
645 2 git_commit *index_commit = NULL;
646 2 git_tree *index_tree = NULL;
647 2 git_commit *index_parent_commit = NULL;
648 2 git_tree *index_parent_tree = NULL;
649 2 git_commit *untracked_commit = NULL;
650 2 git_tree *untracked_tree = NULL;
651 int error;
652  
653 2 ▶ if ((error = git_commit_tree(&stash_tree, stash_commit)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_commit_tree(&stash_tree, stash_commit)) < 0) 0 2 2
654 0 goto cleanup;
655  
656 2 ▶ if ((error = git_commit_parent(&base_commit, stash_commit, 0)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_commit_parent(&base_commit, stash_commit, 0)) < 0) 0 2 2
657 0 goto cleanup;
658 2 ▶ if ((error = git_commit_tree(&base_tree, base_commit)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_commit_tree(&base_tree, base_commit)) < 0) 0 2 2
659 0 goto cleanup;
660  
661 2 ▶ if ((error = git_commit_parent(&index_commit, stash_commit, 1)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_commit_parent(&index_commit, stash_commit, 1)) < 0) 0 2 2
662 0 goto cleanup;
663 2 ▶ if ((error = git_commit_tree(&index_tree, index_commit)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_commit_tree(&index_tree, index_commit)) < 0) 0 2 2
664 0 goto cleanup;
665  
666 2 ▶ if ((error = git_commit_parent(&index_parent_commit, index_commit, 0)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_commit_parent(&index_parent_commit, index_commit, 0)) < 0) 0 2 2
667 0 goto cleanup;
668 2 ▶ if ((error = git_commit_tree(&index_parent_tree, index_parent_commit)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_commit_tree(&index_parent_tree, index_parent_commit)) < 0) 0 2 2
669 0 goto cleanup;
670  
671 2 ▶ if (git_commit_parentcount(stash_commit) == 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (git_commit_parentcount(stash_commit) == 3) { 0 2 2
672 0 ▶ if ((error = git_commit_parent(&untracked_commit, stash_commit, 2)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_commit_parent(&untracked_commit, stash_commit, 2)) < 0) 0 0 0
673 0 goto cleanup;
674 0 ▶ if ((error = git_commit_tree(&untracked_tree, untracked_commit)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_commit_tree(&untracked_tree, untracked_commit)) < 0) 0 0 0
675 0 goto cleanup;
676 }
677  
678 2 *out_stash_tree = stash_tree;
679 2 *out_base_tree = base_tree;
680 2 *out_index_tree = index_tree;
681 2 *out_index_parent_tree = index_parent_tree;
682 2 *out_untracked_tree = untracked_tree;
683  
684 2 cleanup:
685 2 git_commit_free(untracked_commit);
686 2 git_commit_free(index_parent_commit);
687 2 git_commit_free(index_commit);
688 2 git_commit_free(base_commit);
689 2 ▶ if (error < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (error < 0) { 0 2 2
690 0 git_tree_free(stash_tree);
691 0 git_tree_free(base_tree);
692 0 git_tree_free(index_tree);
693 0 git_tree_free(index_parent_tree);
694 0 git_tree_free(untracked_tree);
695 }
696 2 return error;
697 }
698  
699 0 static int merge_indexes(
700 git_index **out,
701 git_repository *repo,
702 git_tree *ancestor_tree,
703 git_index *ours_index,
704 git_index *theirs_index)
705 {
706 0 git_iterator *ancestor = NULL, *ours = NULL, *theirs = NULL;
707 0 git_iterator_options iter_opts = GIT_ITERATOR_OPTIONS_INIT;
708 int error;
709  
710 0 iter_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE;
711  
712 0 if ((error = git_iterator_for_tree(&ancestor, ancestor_tree, &iter_opts)) < 0 ||
713 0 ▶ (error = git_iterator_for_index(&ours, repo, ours_index, &iter_opts)) < 0 ||
Branch: 1 branch
BranchTrueFalseTotal
(error = git_iterator_for_index(&ours, repo, ours_index, &iter_opts)) < 0 || 0 0 0
714 0 (error = git_iterator_for_index(&theirs, repo, theirs_index, &iter_opts)) < 0)
715 0 goto done;
716  
717 0 error = git_merge__iterators(out, repo, ancestor, ours, theirs, NULL);
718  
719 0 done:
720 0 git_iterator_free(ancestor);
721 0 git_iterator_free(ours);
722 0 git_iterator_free(theirs);
723 0 return error;
724 }
725  
726 4 static int merge_index_and_tree(
727 git_index **out,
728 git_repository *repo,
729 git_tree *ancestor_tree,
730 git_index *ours_index,
731 git_tree *theirs_tree)
732 {
733 4 git_iterator *ancestor = NULL, *ours = NULL, *theirs = NULL;
734 4 git_iterator_options iter_opts = GIT_ITERATOR_OPTIONS_INIT;
735 int error;
736  
737 4 iter_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE;
738  
739 8 if ((error = git_iterator_for_tree(&ancestor, ancestor_tree, &iter_opts)) < 0 ||
740 8 ▶ (error = git_iterator_for_index(&ours, repo, ours_index, &iter_opts)) < 0 ||
Branch: 1 branch
BranchTrueFalseTotal
(error = git_iterator_for_index(&ours, repo, ours_index, &iter_opts)) < 0 || 0 4 4
741 4 (error = git_iterator_for_tree(&theirs, theirs_tree, &iter_opts)) < 0)
742 0 goto done;
743  
744 4 error = git_merge__iterators(out, repo, ancestor, ours, theirs, NULL);
745  
746 4 done:
747 4 git_iterator_free(ancestor);
748 4 git_iterator_free(ours);
749 4 git_iterator_free(theirs);
750 4 return error;
751 }
752  
753 2 static void normalize_apply_options(
754 git_stash_apply_options *opts,
755 const git_stash_apply_options *given_apply_opts)
756 {
757 2 ▶ if (given_apply_opts != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (given_apply_opts != NULL) { 2 0 2
758 2 memcpy(opts, given_apply_opts, sizeof(git_stash_apply_options));
759 } else {
760 0 git_stash_apply_options default_apply_opts = GIT_STASH_APPLY_OPTIONS_INIT;
761 0 memcpy(opts, &default_apply_opts, sizeof(git_stash_apply_options));
762 }
763  
764 2 opts->checkout_options.checkout_strategy |= GIT_CHECKOUT_NO_REFRESH;
765  
766 2 ▶ if (!opts->checkout_options.our_label)
Branch: 1 branch
BranchTrueFalseTotal
if (!opts->checkout_options.our_label) 2 0 2
767 2 opts->checkout_options.our_label = "Updated upstream";
768  
769 2 ▶ if (!opts->checkout_options.their_label)
Branch: 1 branch
BranchTrueFalseTotal
if (!opts->checkout_options.their_label) 2 0 2
770 2 opts->checkout_options.their_label = "Stashed changes";
771 2 }
772  
773 0 int git_stash_apply_options_init(git_stash_apply_options *opts, unsigned int version)
774 {
775 0 ▶ GIT_INIT_STRUCTURE_FROM_TEMPLATE(
Branch: 1 branch
BranchTrueFalseTotal
GIT_INIT_STRUCTURE_FROM_TEMPLATE( 0 0 0
776 opts, version, git_stash_apply_options, GIT_STASH_APPLY_OPTIONS_INIT);
777 0 return 0;
778 }
779  
780 #ifndef GIT_DEPRECATE_HARD
781 0 int git_stash_apply_init_options(git_stash_apply_options *opts, unsigned int version)
782 {
783 0 return git_stash_apply_options_init(opts, version);
784 }
785 #endif
786  
787 #define NOTIFY_PROGRESS(opts, progress_type) \
788 do { \
789 if ((opts).progress_cb && \
790 (error = (opts).progress_cb((progress_type), (opts).progress_payload))) { \
791 error = (error < 0) ? error : -1; \
792 goto cleanup; \
793 } \
794 } while(false);
795  
796 2 static int ensure_clean_index(git_repository *repo, git_index *index)
797 {
798 2 git_tree *head_tree = NULL;
799 2 git_diff *index_diff = NULL;
800 2 int error = 0;
801  
802 4 if ((error = git_repository_head_tree(&head_tree, repo)) < 0 ||
803 2 (error = git_diff_tree_to_index(
804 &index_diff, repo, head_tree, index, NULL)) < 0)
805 0 goto done;
806  
807 2 ▶ if (git_diff_num_deltas(index_diff) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (git_diff_num_deltas(index_diff) > 0) { 2 0 2
808 0 git_error_set(GIT_ERROR_STASH, "%" PRIuZ " uncommitted changes exist in the index",
809 git_diff_num_deltas(index_diff));
810 0 error = GIT_EUNCOMMITTED;
811 }
812  
813 2 done:
814 2 git_diff_free(index_diff);
815 2 git_tree_free(head_tree);
816 2 return error;
817 }
818  
819 0 static int stage_new_file(const git_index_entry **entries, void *data)
820 {
821 0 git_index *index = data;
822  
823 0 ▶ if(entries[0] == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if(entries[0] == NULL) 0 0 0
824 0 return git_index_add(index, entries[1]);
825 else
826 0 return git_index_add(index, entries[0]);
827 }
828  
829 0 static int stage_new_files(
830 git_index **out,
831 git_tree *parent_tree,
832 git_tree *tree)
833 {
834 0 git_iterator *iterators[2] = { NULL, NULL };
835 0 git_iterator_options iterator_options = GIT_ITERATOR_OPTIONS_INIT;
836 0 git_index *index = NULL;
837 int error;
838  
839 0 if ((error = git_index_new(&index)) < 0 ||
840 0 (error = git_iterator_for_tree(
841 0 ▶ &iterators[0], parent_tree, &iterator_options)) < 0 ||
Branch: 1 branch
BranchTrueFalseTotal
&iterators[0], parent_tree, &iterator_options)) < 0 || 0 0 0
842 0 (error = git_iterator_for_tree(
843 &iterators[1], tree, &iterator_options)) < 0)
844 0 goto done;
845  
846 0 error = git_iterator_walk(iterators, 2, stage_new_file, index);
847  
848 0 done:
849 0 ▶ if (error < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (error < 0) 0 0 0
850 0 git_index_free(index);
851 else
852 0 *out = index;
853  
854 0 git_iterator_free(iterators[0]);
855 0 git_iterator_free(iterators[1]);
856  
857 0 return error;
858 }
859  
860 2 int git_stash_apply(
861 git_repository *repo,
862 size_t index,
863 const git_stash_apply_options *given_opts)
864 {
865 git_stash_apply_options opts;
866 unsigned int checkout_strategy;
867 2 git_commit *stash_commit = NULL;
868 2 git_tree *stash_tree = NULL;
869 2 git_tree *stash_parent_tree = NULL;
870 2 git_tree *index_tree = NULL;
871 2 git_tree *index_parent_tree = NULL;
872 2 git_tree *untracked_tree = NULL;
873 2 git_index *stash_adds = NULL;
874 2 git_index *repo_index = NULL;
875 2 git_index *unstashed_index = NULL;
876 2 git_index *modified_index = NULL;
877 2 git_index *untracked_index = NULL;
878 int error;
879  
880 2 ▶ GIT_ERROR_CHECK_VERSION(given_opts, GIT_STASH_APPLY_OPTIONS_VERSION, "git_stash_apply_options");
Branch: 1 branch
BranchTrueFalseTotal
GIT_ERROR_CHECK_VERSION(given_opts, GIT_STASH_APPLY_OPTIONS_VERSION, "git_stash_apply_options"); 0 2 2
881  
882 2 normalize_apply_options(&opts, given_opts);
883 2 checkout_strategy = opts.checkout_options.checkout_strategy;
884  
885 2 ▶ NOTIFY_PROGRESS(opts, GIT_STASH_APPLY_PROGRESS_LOADING_STASH);
Branch: 2 branches
BranchTrueFalseTotal
NOTIFY_PROGRESS(opts, GIT_STASH_APPLY_PROGRESS_LOADING_STASH); 2 0 2
NOTIFY_PROGRESS(opts, GIT_STASH_APPLY_PROGRESS_LOADING_STASH); 0 2 2
886  
887 /* Retrieve commit corresponding to the given stash */
888 2 ▶ if ((error = retrieve_stash_commit(&stash_commit, repo, index)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = retrieve_stash_commit(&stash_commit, repo, index)) < 0) 0 2 2
889 0 goto cleanup;
890  
891 /* Retrieve all trees in the stash */
892 2 ▶ if ((error = retrieve_stash_trees(
Branch: 1 branch
BranchTrueFalseTotal
if ((error = retrieve_stash_trees( 0 2 2
893 &stash_tree, &stash_parent_tree, &index_tree,
894 &index_parent_tree, &untracked_tree, stash_commit)) < 0)
895 0 goto cleanup;
896  
897 /* Load repo index */
898 2 ▶ if ((error = git_repository_index(&repo_index, repo)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_repository_index(&repo_index, repo)) < 0) 0 2 2
899 0 goto cleanup;
900  
901 2 ▶ NOTIFY_PROGRESS(opts, GIT_STASH_APPLY_PROGRESS_ANALYZE_INDEX);
Branch: 2 branches
BranchTrueFalseTotal
NOTIFY_PROGRESS(opts, GIT_STASH_APPLY_PROGRESS_ANALYZE_INDEX); 2 0 2
NOTIFY_PROGRESS(opts, GIT_STASH_APPLY_PROGRESS_ANALYZE_INDEX); 0 2 2
902  
903 2 ▶ if ((error = ensure_clean_index(repo, repo_index)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = ensure_clean_index(repo, repo_index)) < 0) 0 2 2
904 0 goto cleanup;
905  
906 /* Restore index if required */
907 4 if ((opts.flags & GIT_STASH_APPLY_REINSTATE_INDEX) &&
908 2 git_oid_cmp(git_tree_id(stash_parent_tree), git_tree_id(index_tree))) {
909  
910 2 ▶ if ((error = merge_index_and_tree(
Branch: 1 branch
BranchTrueFalseTotal
if ((error = merge_index_and_tree( 0 2 2
911 &unstashed_index, repo, index_parent_tree, repo_index, index_tree)) < 0)
912 0 goto cleanup;
913  
914 2 ▶ if (git_index_has_conflicts(unstashed_index)) {
Branch: 1 branch
BranchTrueFalseTotal
if (git_index_has_conflicts(unstashed_index)) { 0 2 2
915 0 error = GIT_ECONFLICT;
916 0 goto cleanup;
917 }
918  
919 /* Otherwise, stage any new files in the stash tree. (Note: their
920 * previously unstaged contents are staged, not the previously staged.)
921 */
922 0 ▶ } else if ((opts.flags & GIT_STASH_APPLY_REINSTATE_INDEX) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if ((opts.flags & GIT_STASH_APPLY_REINSTATE_INDEX) == 0) { 0 0 0
923 0 ▶ if ((error = stage_new_files(
Branch: 1 branch
BranchTrueFalseTotal
if ((error = stage_new_files( 0 0 0
924 0 ▶ &stash_adds, stash_parent_tree, stash_tree)) < 0 ||
Branch: 1 branch
BranchTrueFalseTotal
&stash_adds, stash_parent_tree, stash_tree)) < 0 || 0 0 0
925 0 (error = merge_indexes(
926 &unstashed_index, repo, stash_parent_tree, repo_index, stash_adds)) < 0)
927 0 goto cleanup;
928 }
929  
930 2 ▶ NOTIFY_PROGRESS(opts, GIT_STASH_APPLY_PROGRESS_ANALYZE_MODIFIED);
Branch: 2 branches
BranchTrueFalseTotal
NOTIFY_PROGRESS(opts, GIT_STASH_APPLY_PROGRESS_ANALYZE_MODIFIED); 2 0 2
NOTIFY_PROGRESS(opts, GIT_STASH_APPLY_PROGRESS_ANALYZE_MODIFIED); 0 2 2
931  
932 /* Restore modified files in workdir */
933 2 ▶ if ((error = merge_index_and_tree(
Branch: 1 branch
BranchTrueFalseTotal
if ((error = merge_index_and_tree( 0 2 2
934 &modified_index, repo, stash_parent_tree, repo_index, stash_tree)) < 0)
935 0 goto cleanup;
936  
937 /* If applicable, restore untracked / ignored files in workdir */
938 2 ▶ if (untracked_tree) {
Branch: 1 branch
BranchTrueFalseTotal
if (untracked_tree) { 0 2 2
939 0 ▶ NOTIFY_PROGRESS(opts, GIT_STASH_APPLY_PROGRESS_ANALYZE_UNTRACKED);
Branch: 2 branches
BranchTrueFalseTotal
NOTIFY_PROGRESS(opts, GIT_STASH_APPLY_PROGRESS_ANALYZE_UNTRACKED); 0 0 0
NOTIFY_PROGRESS(opts, GIT_STASH_APPLY_PROGRESS_ANALYZE_UNTRACKED); 0 0 0
940  
941 0 ▶ if ((error = merge_index_and_tree(&untracked_index, repo, NULL, repo_index, untracked_tree)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = merge_index_and_tree(&untracked_index, repo, NULL, repo_index, untracked_tree)) < 0) 0 0 0
942 0 goto cleanup;
943 }
944  
945 2 ▶ if (untracked_index) {
Branch: 1 branch
BranchTrueFalseTotal
if (untracked_index) { 0 2 2
946 0 opts.checkout_options.checkout_strategy |= GIT_CHECKOUT_DONT_UPDATE_INDEX;
947  
948 0 ▶ NOTIFY_PROGRESS(opts, GIT_STASH_APPLY_PROGRESS_CHECKOUT_UNTRACKED);
Branch: 2 branches
BranchTrueFalseTotal
NOTIFY_PROGRESS(opts, GIT_STASH_APPLY_PROGRESS_CHECKOUT_UNTRACKED); 0 0 0
NOTIFY_PROGRESS(opts, GIT_STASH_APPLY_PROGRESS_CHECKOUT_UNTRACKED); 0 0 0
949  
950 0 ▶ if ((error = git_checkout_index(repo, untracked_index, &opts.checkout_options)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_checkout_index(repo, untracked_index, &opts.checkout_options)) < 0) 0 0 0
951 0 goto cleanup;
952  
953 0 opts.checkout_options.checkout_strategy = checkout_strategy;
954 }
955  
956  
957 /* If there are conflicts in the modified index, then we need to actually
958 * check that out as the repo's index. Otherwise, we don't update the
959 * index.
960 */
961  
962 2 ▶ if (!git_index_has_conflicts(modified_index))
Branch: 1 branch
BranchTrueFalseTotal
if (!git_index_has_conflicts(modified_index)) 2 0 2
963 2 opts.checkout_options.checkout_strategy |= GIT_CHECKOUT_DONT_UPDATE_INDEX;
964  
965 /* Check out the modified index using the existing repo index as baseline,
966 * so that existing modifications in the index can be rewritten even when
967 * checking out safely.
968 */
969 2 opts.checkout_options.baseline_index = repo_index;
970  
971 2 ▶ NOTIFY_PROGRESS(opts, GIT_STASH_APPLY_PROGRESS_CHECKOUT_MODIFIED);
Branch: 2 branches
BranchTrueFalseTotal
NOTIFY_PROGRESS(opts, GIT_STASH_APPLY_PROGRESS_CHECKOUT_MODIFIED); 2 0 2
NOTIFY_PROGRESS(opts, GIT_STASH_APPLY_PROGRESS_CHECKOUT_MODIFIED); 0 2 2
972  
973 2 ▶ if ((error = git_checkout_index(repo, modified_index, &opts.checkout_options)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_checkout_index(repo, modified_index, &opts.checkout_options)) < 0) 0 2 2
974 0 goto cleanup;
975  
976 2 ▶ if (unstashed_index && !git_index_has_conflicts(modified_index)) {
Branch: 2 branches
BranchTrueFalseTotal
if (unstashed_index && !git_index_has_conflicts(modified_index)) { 2 0 2
if (unstashed_index && !git_index_has_conflicts(modified_index)) { 2 0 2
977 2 ▶ if ((error = git_index_read_index(repo_index, unstashed_index)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_index_read_index(repo_index, unstashed_index)) < 0) 0 2 2
978 0 goto cleanup;
979 }
980  
981 2 ▶ NOTIFY_PROGRESS(opts, GIT_STASH_APPLY_PROGRESS_DONE);
Branch: 2 branches
BranchTrueFalseTotal
NOTIFY_PROGRESS(opts, GIT_STASH_APPLY_PROGRESS_DONE); 2 0 2
NOTIFY_PROGRESS(opts, GIT_STASH_APPLY_PROGRESS_DONE); 0 2 2
982  
983 2 error = git_index_write(repo_index);
984  
985 2 cleanup:
986 2 git_index_free(untracked_index);
987 2 git_index_free(modified_index);
988 2 git_index_free(unstashed_index);
989 2 git_index_free(stash_adds);
990 2 git_index_free(repo_index);
991 2 git_tree_free(untracked_tree);
992 2 git_tree_free(index_parent_tree);
993 2 git_tree_free(index_tree);
994 2 git_tree_free(stash_parent_tree);
995 2 git_tree_free(stash_tree);
996 2 git_commit_free(stash_commit);
997 2 return error;
998 }
999  
1000 9 int git_stash_foreach(
1001 git_repository *repo,
1002 git_stash_cb callback,
1003 void *payload)
1004 {
1005 git_reference *stash;
1006 9 git_reflog *reflog = NULL;
1007 int error;
1008 size_t i, max;
1009 const git_reflog_entry *entry;
1010  
1011 9 error = git_reference_lookup(&stash, repo, GIT_REFS_STASH_FILE);
1012 9 ▶ if (error == GIT_ENOTFOUND) {
Branch: 1 branch
BranchTrueFalseTotal
if (error == GIT_ENOTFOUND) { 1 8 9
1013 1 git_error_clear();
1014 1 return 0;
1015 }
1016 8 ▶ if (error < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (error < 0) 0 8 8
1017 0 goto cleanup;
1018  
1019 8 ▶ if ((error = git_reflog_read(&reflog, repo, GIT_REFS_STASH_FILE)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_reflog_read(&reflog, repo, GIT_REFS_STASH_FILE)) < 0) 0 8 8
1020 0 goto cleanup;
1021  
1022 8 max = git_reflog_entrycount(reflog);
1023 16 ▶ for (i = 0; i < max; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < max; i++) { 12 4 16
1024 12 entry = git_reflog_entry_byindex(reflog, i);
1025  
1026 12 error = callback(i,
1027 git_reflog_entry_message(entry),
1028 git_reflog_entry_id_new(entry),
1029 payload);
1030  
1031 12 ▶ if (error) {
Branch: 1 branch
BranchTrueFalseTotal
if (error) { 4 8 12
1032 4 git_error_set_after_callback(error);
1033 4 break;
1034 }
1035 }
1036  
1037 4 cleanup:
1038 8 git_reference_free(stash);
1039 8 git_reflog_free(reflog);
1040 8 return error;
1041 }
1042  
1043 3 int git_stash_drop(
1044 git_repository *repo,
1045 size_t index)
1046 {
1047 git_transaction *tx;
1048 3 git_reference *stash = NULL;
1049 3 git_reflog *reflog = NULL;
1050 size_t max;
1051 int error;
1052  
1053 3 ▶ if ((error = git_transaction_new(&tx, repo)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_transaction_new(&tx, repo)) < 0) 0 3 3
1054 0 return error;
1055  
1056 3 ▶ if ((error = git_transaction_lock_ref(tx, GIT_REFS_STASH_FILE)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_transaction_lock_ref(tx, GIT_REFS_STASH_FILE)) < 0) 0 3 3
1057 0 goto cleanup;
1058  
1059 3 ▶ if ((error = git_reference_lookup(&stash, repo, GIT_REFS_STASH_FILE)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_reference_lookup(&stash, repo, GIT_REFS_STASH_FILE)) < 0) 0 3 3
1060 0 goto cleanup;
1061  
1062 3 ▶ if ((error = git_reflog_read(&reflog, repo, GIT_REFS_STASH_FILE)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_reflog_read(&reflog, repo, GIT_REFS_STASH_FILE)) < 0) 0 3 3
1063 0 goto cleanup;
1064  
1065 3 max = git_reflog_entrycount(reflog);
1066  
1067 3 ▶ if (!max || index > max - 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (!max || index > max - 1) { 3 0 3
if (!max || index > max - 1) { 0 3 3
1068 0 error = GIT_ENOTFOUND;
1069 0 git_error_set(GIT_ERROR_STASH, "no stashed state at position %" PRIuZ, index);
1070 0 goto cleanup;
1071 }
1072  
1073 3 ▶ if ((error = git_reflog_drop(reflog, index, true)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_reflog_drop(reflog, index, true)) < 0) 0 3 3
1074 0 goto cleanup;
1075  
1076 3 ▶ if ((error = git_transaction_set_reflog(tx, GIT_REFS_STASH_FILE, reflog)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_transaction_set_reflog(tx, GIT_REFS_STASH_FILE, reflog)) < 0) 0 3 3
1077 0 goto cleanup;
1078  
1079 3 ▶ if (max == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (max == 1) { 1 2 3
1080 1 ▶ if ((error = git_transaction_remove(tx, GIT_REFS_STASH_FILE)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_transaction_remove(tx, GIT_REFS_STASH_FILE)) < 0) 0 1 1
1081 0 goto cleanup;
1082 2 ▶ } else if (index == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (index == 0) { 2 0 2
1083 const git_reflog_entry *entry;
1084  
1085 2 entry = git_reflog_entry_byindex(reflog, 0);
1086 2 ▶ if ((error = git_transaction_set_target(tx, GIT_REFS_STASH_FILE, &entry->oid_cur, NULL, NULL)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_transaction_set_target(tx, GIT_REFS_STASH_FILE, &entry->oid_cur, NULL, NULL)) < 0) 0 2 2
1087 0 goto cleanup;
1088 }
1089  
1090 3 error = git_transaction_commit(tx);
1091  
1092 3 cleanup:
1093 3 git_reference_free(stash);
1094 3 git_transaction_free(tx);
1095 3 git_reflog_free(reflog);
1096 3 return error;
1097 }
1098  
1099 1 int git_stash_pop(
1100 git_repository *repo,
1101 size_t index,
1102 const git_stash_apply_options *options)
1103 {
1104 int error;
1105  
1106 1 ▶ if ((error = git_stash_apply(repo, index, options)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_stash_apply(repo, index, options)) < 0) 0 1 1
1107 0 return error;
1108  
1109 1 return git_stash_drop(repo, index);
1110 }
« deps/libgit2/src/libgit2/attrcache.c ↑ Summary deps/libgit2/src/libgit2/commit_list.c »