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