line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
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
|
50
|
|
|
|
|
if (error == GIT_EUNBORNBRANCH) |
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
|
50
|
|
|
|
|
GIT_ERROR_CHECK_ALLOC(formatted_oid); |
51
|
|
|
|
|
|
|
|
52
|
7
|
|
|
|
|
|
git_str_put(out, formatted_oid, 7); |
53
|
7
|
|
|
|
|
|
git__free(formatted_oid); |
54
|
|
|
|
|
|
|
|
55
|
7
|
50
|
|
|
|
|
return git_str_oom(out) ? -1 : 0; |
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
|
50
|
|
|
|
|
GIT_ERROR_CHECK_ALLOC(summary); |
62
|
|
|
|
|
|
|
|
63
|
7
|
50
|
|
|
|
|
if (append_abbreviated_oid(out, git_commit_id(commit)) < 0) |
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
|
50
|
|
|
|
|
return git_str_oom(out) ? -1 : 0; |
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
|
50
|
|
|
|
|
if ((error = retrieve_head(&head, repo)) < 0) |
82
|
0
|
|
|
|
|
|
return error; |
83
|
|
|
|
|
|
|
|
84
|
7
|
50
|
|
|
|
|
if (strcmp("HEAD", git_reference_name(head)) == 0) |
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
|
50
|
|
|
|
|
if (error < 0) |
92
|
0
|
|
|
|
|
|
goto cleanup; |
93
|
|
|
|
|
|
|
|
94
|
7
|
50
|
|
|
|
|
if ((error = git_commit_lookup( |
95
|
|
|
|
|
|
|
b_commit, repo, git_reference_target(head))) < 0) |
96
|
0
|
|
|
|
|
|
goto cleanup; |
97
|
|
|
|
|
|
|
|
98
|
7
|
50
|
|
|
|
|
if ((error = append_commit_description(stash_message, *b_commit)) < 0) |
99
|
0
|
|
|
|
|
|
goto cleanup; |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
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
|
50
|
|
|
|
|
if ((error = git_index_write_tree_to(&i_tree_oid, index, repo)) < 0) |
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
|
50
|
|
|
|
|
if ((error = build_tree_from_index(&i_tree, repo, index)) < 0) |
134
|
0
|
|
|
|
|
|
goto cleanup; |
135
|
|
|
|
|
|
|
|
136
|
6
|
50
|
|
|
|
|
if ((error = git_str_printf(&msg, "index on %s\n", message)) < 0) |
137
|
0
|
|
|
|
|
|
goto cleanup; |
138
|
|
|
|
|
|
|
|
139
|
6
|
50
|
|
|
|
|
if ((error = git_commit_create( |
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
|
|
|
|
|
|
|
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
|
12
|
50
|
|
|
|
|
if (!git_repository_is_bare(repo) && |
|
|
50
|
|
|
|
|
|
181
|
|
|
|
|
|
|
(error = git_repository_index__weakptr(&repo_index, repo)) < 0) |
182
|
0
|
|
|
|
|
|
return error; |
183
|
|
|
|
|
|
|
|
184
|
12
|
50
|
|
|
|
|
if ((error = git_blob__create_from_paths( |
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
|
50
|
|
|
|
|
(repo_index == NULL || !repo_index->distrust_filemode)); |
|
|
50
|
|
|
|
|
|
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
|
50
|
|
|
|
|
for (d = 0; !error && d < max_d; ++d) { |
|
|
100
|
|
|
|
|
|
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
|
|
|
|
|
|
|
case GIT_DELTA_IGNORED: |
211
|
1
|
50
|
|
|
|
|
if (data->include_ignored) |
212
|
1
|
|
|
|
|
|
add_path = delta->new_file.path; |
213
|
1
|
|
|
|
|
|
break; |
214
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
case GIT_DELTA_UNTRACKED: |
216
|
30
|
100
|
|
|
|
|
if (data->include_untracked && |
|
|
50
|
|
|
|
|
|
217
|
6
|
|
|
|
|
|
delta->new_file.mode != GIT_FILEMODE_TREE) |
218
|
6
|
|
|
|
|
|
add_path = delta->new_file.path; |
219
|
30
|
|
|
|
|
|
break; |
220
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
case GIT_DELTA_ADDED: |
222
|
|
|
|
|
|
|
case GIT_DELTA_MODIFIED: |
223
|
5
|
50
|
|
|
|
|
if (data->include_changed) |
224
|
5
|
|
|
|
|
|
add_path = delta->new_file.path; |
225
|
5
|
|
|
|
|
|
break; |
226
|
|
|
|
|
|
|
|
227
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
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
|
100
|
|
|
|
|
if (add_path != NULL) |
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
|
50
|
|
|
|
|
if ((error = git_index_new(&i_index)) < 0) |
263
|
0
|
|
|
|
|
|
goto cleanup; |
264
|
|
|
|
|
|
|
|
265
|
2
|
100
|
|
|
|
|
if (flags & GIT_STASH_INCLUDE_UNTRACKED) { |
266
|
1
|
|
|
|
|
|
opts.flags |= GIT_DIFF_INCLUDE_UNTRACKED | |
267
|
|
|
|
|
|
|
GIT_DIFF_RECURSE_UNTRACKED_DIRS; |
268
|
1
|
|
|
|
|
|
data.include_untracked = true; |
269
|
|
|
|
|
|
|
} |
270
|
|
|
|
|
|
|
|
271
|
2
|
100
|
|
|
|
|
if (flags & GIT_STASH_INCLUDE_IGNORED) { |
272
|
1
|
|
|
|
|
|
opts.flags |= GIT_DIFF_INCLUDE_IGNORED | |
273
|
|
|
|
|
|
|
GIT_DIFF_RECURSE_IGNORED_DIRS; |
274
|
1
|
|
|
|
|
|
data.include_ignored = true; |
275
|
|
|
|
|
|
|
} |
276
|
|
|
|
|
|
|
|
277
|
2
|
50
|
|
|
|
|
if ((error = git_commit_tree(&i_tree, i_commit)) < 0) |
278
|
0
|
|
|
|
|
|
goto cleanup; |
279
|
|
|
|
|
|
|
|
280
|
2
|
50
|
|
|
|
|
if ((error = git_diff_tree_to_workdir(&diff, repo, i_tree, &opts)) < 0) |
281
|
0
|
|
|
|
|
|
goto cleanup; |
282
|
|
|
|
|
|
|
|
283
|
2
|
50
|
|
|
|
|
if ((error = stash_update_index_from_diff(repo, i_index, diff, &data)) < 0) |
284
|
0
|
|
|
|
|
|
goto cleanup; |
285
|
|
|
|
|
|
|
|
286
|
2
|
|
|
|
|
|
error = build_tree_from_index(tree_out, repo, i_index); |
287
|
|
|
|
|
|
|
|
288
|
|
|
|
|
|
|
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
|
50
|
|
|
|
|
if ((error = build_untracked_tree(&u_tree, repo, i_commit, flags)) < 0) |
309
|
0
|
|
|
|
|
|
goto cleanup; |
310
|
|
|
|
|
|
|
|
311
|
2
|
50
|
|
|
|
|
if ((error = git_str_printf(&msg, "untracked files on %s\n", message)) < 0) |
312
|
0
|
|
|
|
|
|
goto cleanup; |
313
|
|
|
|
|
|
|
|
314
|
2
|
50
|
|
|
|
|
if ((error = git_commit_create( |
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
|
|
|
|
|
|
|
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
|
50
|
|
|
|
|
if (a->status == GIT_DELTA_DELETED && b->status == GIT_DELTA_UNTRACKED) { |
|
|
0
|
|
|
|
|
|
344
|
0
|
|
|
|
|
|
git_diff_delta *dup = git_diff__delta_dup(b, pool); |
345
|
|
|
|
|
|
|
|
346
|
0
|
0
|
|
|
|
|
if (dup) |
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
|
50
|
|
|
|
|
if ((error = git_commit_tree(&b_tree, b_commit)) < 0) |
369
|
0
|
|
|
|
|
|
goto cleanup; |
370
|
|
|
|
|
|
|
|
371
|
6
|
50
|
|
|
|
|
if ((error = git_diff_tree_to_index(&diff, repo, b_tree, i_index, &opts)) < 0 || |
|
|
50
|
|
|
|
|
|
372
|
6
|
50
|
|
|
|
|
(error = git_diff_index_to_workdir(&idx_to_wd, repo, i_index, &opts)) < 0 || |
373
|
6
|
|
|
|
|
|
(error = git_diff__merge(diff, idx_to_wd, stash_delta_merge)) < 0) |
374
|
|
|
|
|
|
|
goto cleanup; |
375
|
|
|
|
|
|
|
|
376
|
6
|
|
|
|
|
|
data.include_changed = true; |
377
|
|
|
|
|
|
|
|
378
|
6
|
50
|
|
|
|
|
if ((error = stash_update_index_from_diff(repo, i_index, diff, &data)) < 0) |
379
|
0
|
|
|
|
|
|
goto cleanup; |
380
|
|
|
|
|
|
|
|
381
|
6
|
|
|
|
|
|
error = build_tree_from_index(tree_out, repo, i_index); |
382
|
|
|
|
|
|
|
|
383
|
|
|
|
|
|
|
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
|
6
|
50
|
|
|
|
|
if ((error = git_repository_index(&r_index, repo) < 0) || |
|
|
50
|
|
|
|
|
|
410
|
6
|
50
|
|
|
|
|
(error = git_index_new(&i_index)) < 0 || |
411
|
12
|
50
|
|
|
|
|
(error = git_index__fill(i_index, &r_index->entries) < 0) || |
412
|
|
|
|
|
|
|
(error = git_repository__configmap_lookup(&ignorecase, repo, GIT_CONFIGMAP_IGNORECASE)) < 0) |
413
|
|
|
|
|
|
|
goto cleanup; |
414
|
|
|
|
|
|
|
|
415
|
6
|
|
|
|
|
|
git_index__set_ignore_case(i_index, ignorecase); |
416
|
|
|
|
|
|
|
|
417
|
6
|
50
|
|
|
|
|
if ((error = build_workdir_tree(&w_tree, repo, i_index, b_commit)) < 0) |
418
|
0
|
|
|
|
|
|
goto cleanup; |
419
|
|
|
|
|
|
|
|
420
|
6
|
100
|
|
|
|
|
error = git_commit_create( |
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
|
|
|
|
|
|
|
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
|
50
|
|
|
|
|
if (!user_message) { |
445
|
0
|
|
|
|
|
|
git_str_printf(&buf, "WIP on %s", git_str_cstr(out)); |
446
|
|
|
|
|
|
|
} else { |
447
|
|
|
|
|
|
|
const char *colon; |
448
|
|
|
|
|
|
|
|
449
|
6
|
50
|
|
|
|
|
if ((colon = strchr(git_str_cstr(out), ':')) == NULL) |
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
|
50
|
|
|
|
|
if (git_str_oom(&buf)) { |
458
|
0
|
|
|
|
|
|
error = -1; |
459
|
0
|
|
|
|
|
|
goto cleanup; |
460
|
|
|
|
|
|
|
} |
461
|
|
|
|
|
|
|
|
462
|
6
|
|
|
|
|
|
git_str_swap(out, &buf); |
463
|
|
|
|
|
|
|
|
464
|
|
|
|
|
|
|
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
|
50
|
|
|
|
|
if ((error = git_reference_ensure_log(repo, GIT_REFS_STASH_FILE)) < 0) |
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
|
100
|
|
|
|
|
if (flags & GIT_STASH_INCLUDE_UNTRACKED) |
505
|
1
|
|
|
|
|
|
opts.flags |= GIT_STATUS_OPT_INCLUDE_UNTRACKED | |
506
|
|
|
|
|
|
|
GIT_STATUS_OPT_RECURSE_UNTRACKED_DIRS; |
507
|
|
|
|
|
|
|
|
508
|
7
|
100
|
|
|
|
|
if (flags & GIT_STASH_INCLUDE_IGNORED) |
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
|
100
|
|
|
|
|
if (error == GIT_PASSTHROUGH) |
515
|
6
|
|
|
|
|
|
return 0; |
516
|
|
|
|
|
|
|
|
517
|
1
|
50
|
|
|
|
|
if (!error) |
518
|
1
|
|
|
|
|
|
return create_error(GIT_ENOTFOUND, "there is nothing to stash."); |
519
|
|
|
|
|
|
|
|
520
|
7
|
|
|
|
|
|
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
|
100
|
|
|
|
|
if (flags & GIT_STASH_INCLUDE_UNTRACKED) |
529
|
1
|
|
|
|
|
|
opts.checkout_strategy |= GIT_CHECKOUT_REMOVE_UNTRACKED; |
530
|
6
|
100
|
|
|
|
|
if (flags & GIT_STASH_INCLUDE_IGNORED) |
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
|
50
|
|
|
|
|
GIT_ASSERT_ARG(out); |
549
|
7
|
50
|
|
|
|
|
GIT_ASSERT_ARG(repo); |
550
|
7
|
50
|
|
|
|
|
GIT_ASSERT_ARG(stasher); |
551
|
|
|
|
|
|
|
|
552
|
7
|
50
|
|
|
|
|
if ((error = git_repository__ensure_not_bare(repo, "stash save")) < 0) |
553
|
0
|
|
|
|
|
|
return error; |
554
|
|
|
|
|
|
|
|
555
|
7
|
50
|
|
|
|
|
if ((error = retrieve_base_commit_and_message(&b_commit, &msg, repo)) < 0) |
556
|
0
|
|
|
|
|
|
goto cleanup; |
557
|
|
|
|
|
|
|
|
558
|
7
|
100
|
|
|
|
|
if ((error = ensure_there_are_changes_to_stash(repo, flags)) < 0) |
559
|
1
|
|
|
|
|
|
goto cleanup; |
560
|
|
|
|
|
|
|
|
561
|
6
|
50
|
|
|
|
|
if ((error = git_repository_index(&index, repo)) < 0) |
562
|
0
|
|
|
|
|
|
goto cleanup; |
563
|
|
|
|
|
|
|
|
564
|
6
|
50
|
|
|
|
|
if ((error = commit_index(&i_commit, repo, index, stasher, |
565
|
|
|
|
|
|
|
git_str_cstr(&msg), b_commit)) < 0) |
566
|
0
|
|
|
|
|
|
goto cleanup; |
567
|
|
|
|
|
|
|
|
568
|
8
|
100
|
|
|
|
|
if ((flags & (GIT_STASH_INCLUDE_UNTRACKED | GIT_STASH_INCLUDE_IGNORED)) && |
|
|
50
|
|
|
|
|
|
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
|
50
|
|
|
|
|
if ((error = prepare_worktree_commit_message(&msg, message)) < 0) |
574
|
0
|
|
|
|
|
|
goto cleanup; |
575
|
|
|
|
|
|
|
|
576
|
6
|
50
|
|
|
|
|
if ((error = commit_worktree(out, repo, stasher, git_str_cstr(&msg), |
577
|
|
|
|
|
|
|
i_commit, b_commit, u_commit)) < 0) |
578
|
0
|
|
|
|
|
|
goto cleanup; |
579
|
|
|
|
|
|
|
|
580
|
6
|
|
|
|
|
|
git_str_rtrim(&msg); |
581
|
|
|
|
|
|
|
|
582
|
6
|
50
|
|
|
|
|
if ((error = update_reflog(out, repo, git_str_cstr(&msg))) < 0) |
583
|
0
|
|
|
|
|
|
goto cleanup; |
584
|
|
|
|
|
|
|
|
585
|
6
|
100
|
|
|
|
|
if ((error = reset_index_and_workdir(repo, (flags & GIT_STASH_KEEP_INDEX) ? i_commit : b_commit, |
|
|
50
|
|
|
|
|
|
586
|
|
|
|
|
|
|
flags)) < 0) |
587
|
0
|
|
|
|
|
|
goto cleanup; |
588
|
|
|
|
|
|
|
|
589
|
|
|
|
|
|
|
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
|
50
|
|
|
|
|
if ((error = git_reference_lookup(&stash, repo, GIT_REFS_STASH_FILE)) < 0) |
612
|
0
|
|
|
|
|
|
goto cleanup; |
613
|
|
|
|
|
|
|
|
614
|
2
|
50
|
|
|
|
|
if ((error = git_reflog_read(&reflog, repo, GIT_REFS_STASH_FILE)) < 0) |
615
|
0
|
|
|
|
|
|
goto cleanup; |
616
|
|
|
|
|
|
|
|
617
|
2
|
|
|
|
|
|
max = git_reflog_entrycount(reflog); |
618
|
2
|
50
|
|
|
|
|
if (!max || index > max - 1) { |
|
|
50
|
|
|
|
|
|
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
|
50
|
|
|
|
|
if ((error = git_commit_lookup(commit, repo, git_reflog_entry_id_new(entry))) < 0) |
626
|
0
|
|
|
|
|
|
goto cleanup; |
627
|
|
|
|
|
|
|
|
628
|
|
|
|
|
|
|
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
|
50
|
|
|
|
|
if ((error = git_commit_tree(&stash_tree, stash_commit)) < 0) |
654
|
0
|
|
|
|
|
|
goto cleanup; |
655
|
|
|
|
|
|
|
|
656
|
2
|
50
|
|
|
|
|
if ((error = git_commit_parent(&base_commit, stash_commit, 0)) < 0) |
657
|
0
|
|
|
|
|
|
goto cleanup; |
658
|
2
|
50
|
|
|
|
|
if ((error = git_commit_tree(&base_tree, base_commit)) < 0) |
659
|
0
|
|
|
|
|
|
goto cleanup; |
660
|
|
|
|
|
|
|
|
661
|
2
|
50
|
|
|
|
|
if ((error = git_commit_parent(&index_commit, stash_commit, 1)) < 0) |
662
|
0
|
|
|
|
|
|
goto cleanup; |
663
|
2
|
50
|
|
|
|
|
if ((error = git_commit_tree(&index_tree, index_commit)) < 0) |
664
|
0
|
|
|
|
|
|
goto cleanup; |
665
|
|
|
|
|
|
|
|
666
|
2
|
50
|
|
|
|
|
if ((error = git_commit_parent(&index_parent_commit, index_commit, 0)) < 0) |
667
|
0
|
|
|
|
|
|
goto cleanup; |
668
|
2
|
50
|
|
|
|
|
if ((error = git_commit_tree(&index_parent_tree, index_parent_commit)) < 0) |
669
|
0
|
|
|
|
|
|
goto cleanup; |
670
|
|
|
|
|
|
|
|
671
|
2
|
50
|
|
|
|
|
if (git_commit_parentcount(stash_commit) == 3) { |
672
|
0
|
0
|
|
|
|
|
if ((error = git_commit_parent(&untracked_commit, stash_commit, 2)) < 0) |
673
|
0
|
|
|
|
|
|
goto cleanup; |
674
|
0
|
0
|
|
|
|
|
if ((error = git_commit_tree(&untracked_tree, untracked_commit)) < 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
|
|
|
|
|
|
|
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
|
50
|
|
|
|
|
if (error < 0) { |
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
|
0
|
|
|
|
|
if ((error = git_iterator_for_tree(&ancestor, ancestor_tree, &iter_opts)) < 0 || |
|
|
0
|
|
|
|
|
|
713
|
0
|
0
|
|
|
|
|
(error = git_iterator_for_index(&ours, repo, ours_index, &iter_opts)) < 0 || |
714
|
|
|
|
|
|
|
(error = git_iterator_for_index(&theirs, repo, theirs_index, &iter_opts)) < 0) |
715
|
|
|
|
|
|
|
goto done; |
716
|
|
|
|
|
|
|
|
717
|
0
|
|
|
|
|
|
error = git_merge__iterators(out, repo, ancestor, ours, theirs, NULL); |
718
|
|
|
|
|
|
|
|
719
|
|
|
|
|
|
|
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
|
4
|
50
|
|
|
|
|
if ((error = git_iterator_for_tree(&ancestor, ancestor_tree, &iter_opts)) < 0 || |
|
|
50
|
|
|
|
|
|
740
|
4
|
50
|
|
|
|
|
(error = git_iterator_for_index(&ours, repo, ours_index, &iter_opts)) < 0 || |
741
|
|
|
|
|
|
|
(error = git_iterator_for_tree(&theirs, theirs_tree, &iter_opts)) < 0) |
742
|
|
|
|
|
|
|
goto done; |
743
|
|
|
|
|
|
|
|
744
|
4
|
|
|
|
|
|
error = git_merge__iterators(out, repo, ancestor, ours, theirs, NULL); |
745
|
|
|
|
|
|
|
|
746
|
|
|
|
|
|
|
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
|
50
|
|
|
|
|
if (given_apply_opts != NULL) { |
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
|
50
|
|
|
|
|
if (!opts->checkout_options.our_label) |
767
|
2
|
|
|
|
|
|
opts->checkout_options.our_label = "Updated upstream"; |
768
|
|
|
|
|
|
|
|
769
|
2
|
50
|
|
|
|
|
if (!opts->checkout_options.their_label) |
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
|
0
|
|
|
|
|
GIT_INIT_STRUCTURE_FROM_TEMPLATE( |
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
|
2
|
50
|
|
|
|
|
if ((error = git_repository_head_tree(&head_tree, repo)) < 0 || |
|
|
50
|
|
|
|
|
|
803
|
2
|
|
|
|
|
|
(error = git_diff_tree_to_index( |
804
|
|
|
|
|
|
|
&index_diff, repo, head_tree, index, NULL)) < 0) |
805
|
|
|
|
|
|
|
goto done; |
806
|
|
|
|
|
|
|
|
807
|
2
|
50
|
|
|
|
|
if (git_diff_num_deltas(index_diff) > 0) { |
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
|
|
|
|
|
|
|
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
|
0
|
|
|
|
|
if(entries[0] == NULL) |
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
|
0
|
|
|
|
|
if ((error = git_index_new(&index)) < 0 || |
|
|
0
|
|
|
|
|
|
840
|
|
|
|
|
|
|
(error = git_iterator_for_tree( |
841
|
0
|
0
|
|
|
|
|
&iterators[0], parent_tree, &iterator_options)) < 0 || |
842
|
|
|
|
|
|
|
(error = git_iterator_for_tree( |
843
|
|
|
|
|
|
|
&iterators[1], tree, &iterator_options)) < 0) |
844
|
|
|
|
|
|
|
goto done; |
845
|
|
|
|
|
|
|
|
846
|
0
|
|
|
|
|
|
error = git_iterator_walk(iterators, 2, stage_new_file, index); |
847
|
|
|
|
|
|
|
|
848
|
|
|
|
|
|
|
done: |
849
|
0
|
0
|
|
|
|
|
if (error < 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
|
50
|
|
|
|
|
GIT_ERROR_CHECK_VERSION(given_opts, GIT_STASH_APPLY_OPTIONS_VERSION, "git_stash_apply_options"); |
881
|
|
|
|
|
|
|
|
882
|
2
|
|
|
|
|
|
normalize_apply_options(&opts, given_opts); |
883
|
2
|
|
|
|
|
|
checkout_strategy = opts.checkout_options.checkout_strategy; |
884
|
|
|
|
|
|
|
|
885
|
2
|
50
|
|
|
|
|
NOTIFY_PROGRESS(opts, GIT_STASH_APPLY_PROGRESS_LOADING_STASH); |
|
|
50
|
|
|
|
|
|
886
|
|
|
|
|
|
|
|
887
|
|
|
|
|
|
|
/* Retrieve commit corresponding to the given stash */ |
888
|
2
|
50
|
|
|
|
|
if ((error = retrieve_stash_commit(&stash_commit, repo, index)) < 0) |
889
|
0
|
|
|
|
|
|
goto cleanup; |
890
|
|
|
|
|
|
|
|
891
|
|
|
|
|
|
|
/* Retrieve all trees in the stash */ |
892
|
2
|
50
|
|
|
|
|
if ((error = retrieve_stash_trees( |
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
|
50
|
|
|
|
|
if ((error = git_repository_index(&repo_index, repo)) < 0) |
899
|
0
|
|
|
|
|
|
goto cleanup; |
900
|
|
|
|
|
|
|
|
901
|
2
|
50
|
|
|
|
|
NOTIFY_PROGRESS(opts, GIT_STASH_APPLY_PROGRESS_ANALYZE_INDEX); |
|
|
50
|
|
|
|
|
|
902
|
|
|
|
|
|
|
|
903
|
2
|
50
|
|
|
|
|
if ((error = ensure_clean_index(repo, repo_index)) < 0) |
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
|
50
|
|
|
|
|
if ((error = merge_index_and_tree( |
911
|
|
|
|
|
|
|
&unstashed_index, repo, index_parent_tree, repo_index, index_tree)) < 0) |
912
|
0
|
|
|
|
|
|
goto cleanup; |
913
|
|
|
|
|
|
|
|
914
|
2
|
50
|
|
|
|
|
if (git_index_has_conflicts(unstashed_index)) { |
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
|
0
|
|
|
|
|
} else if ((opts.flags & GIT_STASH_APPLY_REINSTATE_INDEX) == 0) { |
923
|
0
|
0
|
|
|
|
|
if ((error = stage_new_files( |
924
|
0
|
0
|
|
|
|
|
&stash_adds, stash_parent_tree, stash_tree)) < 0 || |
925
|
0
|
|
|
|
|
|
(error = merge_indexes( |
926
|
|
|
|
|
|
|
&unstashed_index, repo, stash_parent_tree, repo_index, stash_adds)) < 0) |
927
|
|
|
|
|
|
|
goto cleanup; |
928
|
|
|
|
|
|
|
} |
929
|
|
|
|
|
|
|
|
930
|
2
|
50
|
|
|
|
|
NOTIFY_PROGRESS(opts, GIT_STASH_APPLY_PROGRESS_ANALYZE_MODIFIED); |
|
|
50
|
|
|
|
|
|
931
|
|
|
|
|
|
|
|
932
|
|
|
|
|
|
|
/* Restore modified files in workdir */ |
933
|
2
|
50
|
|
|
|
|
if ((error = merge_index_and_tree( |
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
|
50
|
|
|
|
|
if (untracked_tree) { |
939
|
0
|
0
|
|
|
|
|
NOTIFY_PROGRESS(opts, GIT_STASH_APPLY_PROGRESS_ANALYZE_UNTRACKED); |
|
|
0
|
|
|
|
|
|
940
|
|
|
|
|
|
|
|
941
|
0
|
0
|
|
|
|
|
if ((error = merge_index_and_tree(&untracked_index, repo, NULL, repo_index, untracked_tree)) < 0) |
942
|
0
|
|
|
|
|
|
goto cleanup; |
943
|
|
|
|
|
|
|
} |
944
|
|
|
|
|
|
|
|
945
|
2
|
50
|
|
|
|
|
if (untracked_index) { |
946
|
0
|
|
|
|
|
|
opts.checkout_options.checkout_strategy |= GIT_CHECKOUT_DONT_UPDATE_INDEX; |
947
|
|
|
|
|
|
|
|
948
|
0
|
0
|
|
|
|
|
NOTIFY_PROGRESS(opts, GIT_STASH_APPLY_PROGRESS_CHECKOUT_UNTRACKED); |
|
|
0
|
|
|
|
|
|
949
|
|
|
|
|
|
|
|
950
|
0
|
0
|
|
|
|
|
if ((error = git_checkout_index(repo, untracked_index, &opts.checkout_options)) < 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
|
50
|
|
|
|
|
if (!git_index_has_conflicts(modified_index)) |
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
|
50
|
|
|
|
|
NOTIFY_PROGRESS(opts, GIT_STASH_APPLY_PROGRESS_CHECKOUT_MODIFIED); |
|
|
50
|
|
|
|
|
|
972
|
|
|
|
|
|
|
|
973
|
2
|
50
|
|
|
|
|
if ((error = git_checkout_index(repo, modified_index, &opts.checkout_options)) < 0) |
974
|
0
|
|
|
|
|
|
goto cleanup; |
975
|
|
|
|
|
|
|
|
976
|
2
|
50
|
|
|
|
|
if (unstashed_index && !git_index_has_conflicts(modified_index)) { |
|
|
50
|
|
|
|
|
|
977
|
2
|
50
|
|
|
|
|
if ((error = git_index_read_index(repo_index, unstashed_index)) < 0) |
978
|
0
|
|
|
|
|
|
goto cleanup; |
979
|
|
|
|
|
|
|
} |
980
|
|
|
|
|
|
|
|
981
|
2
|
50
|
|
|
|
|
NOTIFY_PROGRESS(opts, GIT_STASH_APPLY_PROGRESS_DONE); |
|
|
50
|
|
|
|
|
|
982
|
|
|
|
|
|
|
|
983
|
2
|
|
|
|
|
|
error = git_index_write(repo_index); |
984
|
|
|
|
|
|
|
|
985
|
|
|
|
|
|
|
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
|
100
|
|
|
|
|
if (error == GIT_ENOTFOUND) { |
1013
|
1
|
|
|
|
|
|
git_error_clear(); |
1014
|
1
|
|
|
|
|
|
return 0; |
1015
|
|
|
|
|
|
|
} |
1016
|
8
|
50
|
|
|
|
|
if (error < 0) |
1017
|
0
|
|
|
|
|
|
goto cleanup; |
1018
|
|
|
|
|
|
|
|
1019
|
8
|
50
|
|
|
|
|
if ((error = git_reflog_read(&reflog, repo, GIT_REFS_STASH_FILE)) < 0) |
1020
|
0
|
|
|
|
|
|
goto cleanup; |
1021
|
|
|
|
|
|
|
|
1022
|
8
|
|
|
|
|
|
max = git_reflog_entrycount(reflog); |
1023
|
16
|
100
|
|
|
|
|
for (i = 0; i < max; i++) { |
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
|
100
|
|
|
|
|
if (error) { |
1032
|
4
|
|
|
|
|
|
git_error_set_after_callback(error); |
1033
|
4
|
|
|
|
|
|
break; |
1034
|
|
|
|
|
|
|
} |
1035
|
|
|
|
|
|
|
} |
1036
|
|
|
|
|
|
|
|
1037
|
|
|
|
|
|
|
cleanup: |
1038
|
8
|
|
|
|
|
|
git_reference_free(stash); |
1039
|
8
|
|
|
|
|
|
git_reflog_free(reflog); |
1040
|
9
|
|
|
|
|
|
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
|
50
|
|
|
|
|
if ((error = git_transaction_new(&tx, repo)) < 0) |
1054
|
0
|
|
|
|
|
|
return error; |
1055
|
|
|
|
|
|
|
|
1056
|
3
|
50
|
|
|
|
|
if ((error = git_transaction_lock_ref(tx, GIT_REFS_STASH_FILE)) < 0) |
1057
|
0
|
|
|
|
|
|
goto cleanup; |
1058
|
|
|
|
|
|
|
|
1059
|
3
|
50
|
|
|
|
|
if ((error = git_reference_lookup(&stash, repo, GIT_REFS_STASH_FILE)) < 0) |
1060
|
0
|
|
|
|
|
|
goto cleanup; |
1061
|
|
|
|
|
|
|
|
1062
|
3
|
50
|
|
|
|
|
if ((error = git_reflog_read(&reflog, repo, GIT_REFS_STASH_FILE)) < 0) |
1063
|
0
|
|
|
|
|
|
goto cleanup; |
1064
|
|
|
|
|
|
|
|
1065
|
3
|
|
|
|
|
|
max = git_reflog_entrycount(reflog); |
1066
|
|
|
|
|
|
|
|
1067
|
3
|
50
|
|
|
|
|
if (!max || index > max - 1) { |
|
|
50
|
|
|
|
|
|
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
|
50
|
|
|
|
|
if ((error = git_reflog_drop(reflog, index, true)) < 0) |
1074
|
0
|
|
|
|
|
|
goto cleanup; |
1075
|
|
|
|
|
|
|
|
1076
|
3
|
50
|
|
|
|
|
if ((error = git_transaction_set_reflog(tx, GIT_REFS_STASH_FILE, reflog)) < 0) |
1077
|
0
|
|
|
|
|
|
goto cleanup; |
1078
|
|
|
|
|
|
|
|
1079
|
3
|
100
|
|
|
|
|
if (max == 1) { |
1080
|
1
|
50
|
|
|
|
|
if ((error = git_transaction_remove(tx, GIT_REFS_STASH_FILE)) < 0) |
1081
|
0
|
|
|
|
|
|
goto cleanup; |
1082
|
2
|
50
|
|
|
|
|
} else if (index == 0) { |
1083
|
|
|
|
|
|
|
const git_reflog_entry *entry; |
1084
|
|
|
|
|
|
|
|
1085
|
2
|
|
|
|
|
|
entry = git_reflog_entry_byindex(reflog, 0); |
1086
|
2
|
50
|
|
|
|
|
if ((error = git_transaction_set_target(tx, GIT_REFS_STASH_FILE, &entry->oid_cur, NULL, NULL)) < 0) |
1087
|
0
|
|
|
|
|
|
goto cleanup; |
1088
|
|
|
|
|
|
|
} |
1089
|
|
|
|
|
|
|
|
1090
|
3
|
|
|
|
|
|
error = git_transaction_commit(tx); |
1091
|
|
|
|
|
|
|
|
1092
|
|
|
|
|
|
|
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
|
50
|
|
|
|
|
if ((error = git_stash_apply(repo, index, options)) < 0) |
1107
|
0
|
|
|
|
|
|
return error; |
1108
|
|
|
|
|
|
|
|
1109
|
1
|
|
|
|
|
|
return git_stash_drop(repo, index); |
1110
|
|
|
|
|
|
|
} |