]>
Commit | Line | Data |
---|---|---|
99370863 | 1 | #define USE_THE_INDEX_VARIABLE |
8a0fc8d1 | 2 | #include "builtin.h" |
0b027f6c | 3 | #include "abspath.h" |
8a0fc8d1 | 4 | #include "config.h" |
32a8f510 | 5 | #include "environment.h" |
f394e093 | 6 | #include "gettext.h" |
df6e8744 | 7 | #include "hash.h" |
41771fa4 | 8 | #include "hex.h" |
dabab1d6 | 9 | #include "object-name.h" |
8a0fc8d1 JT |
10 | #include "parse-options.h" |
11 | #include "refs.h" | |
12 | #include "lockfile.h" | |
13 | #include "cache-tree.h" | |
14 | #include "unpack-trees.h" | |
15 | #include "merge-recursive.h" | |
874cf2a6 | 16 | #include "merge-ort-wrappers.h" |
dbbcd44f | 17 | #include "strvec.h" |
8a0fc8d1 JT |
18 | #include "run-command.h" |
19 | #include "dir.h" | |
c47679d0 | 20 | #include "entry.h" |
fbffdfb1 | 21 | #include "preload-index.h" |
08c46a49 | 22 | #include "read-cache.h" |
8a0fc8d1 | 23 | #include "rerere.h" |
dc7bd382 | 24 | #include "revision.h" |
e38da487 | 25 | #include "setup.h" |
baf889c2 | 26 | #include "sparse-index.h" |
dc7bd382 | 27 | #include "log-tree.h" |
d4788af8 | 28 | #include "diffcore.h" |
758b4d2b | 29 | #include "reflog.h" |
d21878f0 | 30 | #include "add-interactive.h" |
d4788af8 PSU |
31 | |
32 | #define INCLUDE_ALL_FILES 2 | |
8a0fc8d1 | 33 | |
951ec747 ÆAB |
34 | #define BUILTIN_STASH_LIST_USAGE \ |
35 | N_("git stash list [<log-options>]") | |
36 | #define BUILTIN_STASH_SHOW_USAGE \ | |
37 | N_("git stash show [-u | --include-untracked | --only-untracked] [<diff-options>] [<stash>]") | |
38 | #define BUILTIN_STASH_DROP_USAGE \ | |
39 | N_("git stash drop [-q | --quiet] [<stash>]") | |
40 | #define BUILTIN_STASH_POP_USAGE \ | |
41 | N_("git stash pop [--index] [-q | --quiet] [<stash>]") | |
42 | #define BUILTIN_STASH_APPLY_USAGE \ | |
43 | N_("git stash apply [--index] [-q | --quiet] [<stash>]") | |
44 | #define BUILTIN_STASH_BRANCH_USAGE \ | |
45 | N_("git stash branch <branchname> [<stash>]") | |
46 | #define BUILTIN_STASH_STORE_USAGE \ | |
47 | N_("git stash store [(-m | --message) <message>] [-q | --quiet] <commit>") | |
48 | #define BUILTIN_STASH_PUSH_USAGE \ | |
49 | N_("git stash [push [-p | --patch] [-S | --staged] [-k | --[no-]keep-index] [-q | --quiet]\n" \ | |
50 | " [-u | --include-untracked] [-a | --all] [(-m | --message) <message>]\n" \ | |
51 | " [--pathspec-from-file=<file> [--pathspec-file-nul]]\n" \ | |
52 | " [--] [<pathspec>...]]") | |
53 | #define BUILTIN_STASH_SAVE_USAGE \ | |
54 | N_("git stash save [-p | --patch] [-S | --staged] [-k | --[no-]keep-index] [-q | --quiet]\n" \ | |
55 | " [-u | --include-untracked] [-a | --all] [<message>]") | |
56 | #define BUILTIN_STASH_CREATE_USAGE \ | |
57 | N_("git stash create [<message>]") | |
58 | #define BUILTIN_STASH_CLEAR_USAGE \ | |
59 | "git stash clear" | |
60 | ||
40af1468 | 61 | static const char * const git_stash_usage[] = { |
951ec747 ÆAB |
62 | BUILTIN_STASH_LIST_USAGE, |
63 | BUILTIN_STASH_SHOW_USAGE, | |
64 | BUILTIN_STASH_DROP_USAGE, | |
65 | BUILTIN_STASH_POP_USAGE, | |
66 | BUILTIN_STASH_APPLY_USAGE, | |
67 | BUILTIN_STASH_BRANCH_USAGE, | |
68 | BUILTIN_STASH_PUSH_USAGE, | |
69 | BUILTIN_STASH_SAVE_USAGE, | |
70 | BUILTIN_STASH_CLEAR_USAGE, | |
71 | BUILTIN_STASH_CREATE_USAGE, | |
72 | BUILTIN_STASH_STORE_USAGE, | |
4e2dd393 JT |
73 | NULL |
74 | }; | |
75 | ||
40af1468 | 76 | static const char * const git_stash_list_usage[] = { |
951ec747 | 77 | BUILTIN_STASH_LIST_USAGE, |
130f2697 PSU |
78 | NULL |
79 | }; | |
80 | ||
40af1468 | 81 | static const char * const git_stash_show_usage[] = { |
951ec747 | 82 | BUILTIN_STASH_SHOW_USAGE, |
dc7bd382 PSU |
83 | NULL |
84 | }; | |
85 | ||
40af1468 | 86 | static const char * const git_stash_drop_usage[] = { |
951ec747 | 87 | BUILTIN_STASH_DROP_USAGE, |
8a0fc8d1 JT |
88 | NULL |
89 | }; | |
90 | ||
40af1468 | 91 | static const char * const git_stash_pop_usage[] = { |
951ec747 | 92 | BUILTIN_STASH_POP_USAGE, |
c4de61d7 JT |
93 | NULL |
94 | }; | |
95 | ||
40af1468 | 96 | static const char * const git_stash_apply_usage[] = { |
951ec747 | 97 | BUILTIN_STASH_APPLY_USAGE, |
8a0fc8d1 JT |
98 | NULL |
99 | }; | |
100 | ||
40af1468 | 101 | static const char * const git_stash_branch_usage[] = { |
951ec747 | 102 | BUILTIN_STASH_BRANCH_USAGE, |
577c1995 JT |
103 | NULL |
104 | }; | |
105 | ||
40af1468 | 106 | static const char * const git_stash_clear_usage[] = { |
951ec747 | 107 | BUILTIN_STASH_CLEAR_USAGE, |
4e2dd393 JT |
108 | NULL |
109 | }; | |
110 | ||
40af1468 | 111 | static const char * const git_stash_store_usage[] = { |
951ec747 | 112 | BUILTIN_STASH_STORE_USAGE, |
41e0dd55 PSU |
113 | NULL |
114 | }; | |
115 | ||
40af1468 | 116 | static const char * const git_stash_push_usage[] = { |
951ec747 | 117 | BUILTIN_STASH_PUSH_USAGE, |
d553f538 PSU |
118 | NULL |
119 | }; | |
120 | ||
40af1468 | 121 | static const char * const git_stash_save_usage[] = { |
951ec747 | 122 | BUILTIN_STASH_SAVE_USAGE, |
64fe9c26 PSU |
123 | NULL |
124 | }; | |
125 | ||
3e885f02 | 126 | static const char ref_stash[] = "refs/stash"; |
8a0fc8d1 JT |
127 | static struct strbuf stash_index_path = STRBUF_INIT; |
128 | ||
129 | /* | |
130 | * w_commit is set to the commit containing the working tree | |
131 | * b_commit is set to the base commit | |
132 | * i_commit is set to the commit containing the index tree | |
133 | * u_commit is set to the commit containing the untracked files tree | |
134 | * w_tree is set to the working tree | |
135 | * b_tree is set to the base tree | |
136 | * i_tree is set to the index tree | |
137 | * u_tree is set to the untracked files tree | |
138 | */ | |
139 | struct stash_info { | |
140 | struct object_id w_commit; | |
141 | struct object_id b_commit; | |
142 | struct object_id i_commit; | |
143 | struct object_id u_commit; | |
144 | struct object_id w_tree; | |
145 | struct object_id b_tree; | |
146 | struct object_id i_tree; | |
147 | struct object_id u_tree; | |
148 | struct strbuf revision; | |
149 | int is_stash_ref; | |
150 | int has_u; | |
151 | }; | |
152 | ||
5e480176 ÆAB |
153 | #define STASH_INFO_INIT { \ |
154 | .revision = STRBUF_INIT, \ | |
155 | } | |
156 | ||
8a0fc8d1 JT |
157 | static void free_stash_info(struct stash_info *info) |
158 | { | |
159 | strbuf_release(&info->revision); | |
160 | } | |
161 | ||
162 | static void assert_stash_like(struct stash_info *info, const char *revision) | |
163 | { | |
164 | if (get_oidf(&info->b_commit, "%s^1", revision) || | |
165 | get_oidf(&info->w_tree, "%s:", revision) || | |
166 | get_oidf(&info->b_tree, "%s^1:", revision) || | |
167 | get_oidf(&info->i_tree, "%s^2:", revision)) | |
168 | die(_("'%s' is not a stash-like commit"), revision); | |
169 | } | |
170 | ||
171 | static int get_stash_info(struct stash_info *info, int argc, const char **argv) | |
172 | { | |
173 | int ret; | |
174 | char *end_of_rev; | |
175 | char *expanded_ref; | |
176 | const char *revision; | |
177 | const char *commit = NULL; | |
178 | struct object_id dummy; | |
179 | struct strbuf symbolic = STRBUF_INIT; | |
180 | ||
181 | if (argc > 1) { | |
182 | int i; | |
183 | struct strbuf refs_msg = STRBUF_INIT; | |
184 | ||
185 | for (i = 0; i < argc; i++) | |
186 | strbuf_addf(&refs_msg, " '%s'", argv[i]); | |
187 | ||
188 | fprintf_ln(stderr, _("Too many revisions specified:%s"), | |
189 | refs_msg.buf); | |
190 | strbuf_release(&refs_msg); | |
191 | ||
192 | return -1; | |
193 | } | |
194 | ||
195 | if (argc == 1) | |
196 | commit = argv[0]; | |
197 | ||
8a0fc8d1 JT |
198 | if (!commit) { |
199 | if (!ref_exists(ref_stash)) { | |
8a0fc8d1 JT |
200 | fprintf_ln(stderr, _("No stash entries found.")); |
201 | return -1; | |
202 | } | |
203 | ||
204 | strbuf_addf(&info->revision, "%s@{0}", ref_stash); | |
205 | } else if (strspn(commit, "0123456789") == strlen(commit)) { | |
206 | strbuf_addf(&info->revision, "%s@{%s}", ref_stash, commit); | |
207 | } else { | |
208 | strbuf_addstr(&info->revision, commit); | |
209 | } | |
210 | ||
211 | revision = info->revision.buf; | |
212 | ||
d850b7a5 | 213 | if (repo_get_oid(the_repository, revision, &info->w_commit)) |
5e480176 | 214 | return error(_("%s is not a valid reference"), revision); |
8a0fc8d1 JT |
215 | |
216 | assert_stash_like(info, revision); | |
217 | ||
218 | info->has_u = !get_oidf(&info->u_tree, "%s^3:", revision); | |
219 | ||
220 | end_of_rev = strchrnul(revision, '@'); | |
221 | strbuf_add(&symbolic, revision, end_of_rev - revision); | |
222 | ||
12cb1c10 ÆAB |
223 | ret = repo_dwim_ref(the_repository, symbolic.buf, symbolic.len, |
224 | &dummy, &expanded_ref, 0); | |
8a0fc8d1 JT |
225 | strbuf_release(&symbolic); |
226 | switch (ret) { | |
227 | case 0: /* Not found, but valid ref */ | |
228 | info->is_stash_ref = 0; | |
229 | break; | |
230 | case 1: | |
231 | info->is_stash_ref = !strcmp(expanded_ref, ref_stash); | |
232 | break; | |
233 | default: /* Invalid or ambiguous */ | |
5e480176 | 234 | break; |
8a0fc8d1 JT |
235 | } |
236 | ||
237 | free(expanded_ref); | |
238 | return !(ret == 0 || ret == 1); | |
239 | } | |
240 | ||
4e2dd393 JT |
241 | static int do_clear_stash(void) |
242 | { | |
243 | struct object_id obj; | |
d850b7a5 | 244 | if (repo_get_oid(the_repository, ref_stash, &obj)) |
4e2dd393 JT |
245 | return 0; |
246 | ||
247 | return delete_ref(NULL, ref_stash, &obj, 0); | |
248 | } | |
249 | ||
250 | static int clear_stash(int argc, const char **argv, const char *prefix) | |
251 | { | |
252 | struct option options[] = { | |
253 | OPT_END() | |
254 | }; | |
255 | ||
256 | argc = parse_options(argc, argv, prefix, options, | |
40af1468 | 257 | git_stash_clear_usage, |
4e2dd393 JT |
258 | PARSE_OPT_STOP_AT_NON_OPTION); |
259 | ||
260 | if (argc) | |
b8657347 | 261 | return error(_("git stash clear with arguments is " |
4e2dd393 JT |
262 | "unimplemented")); |
263 | ||
264 | return do_clear_stash(); | |
265 | } | |
266 | ||
8a0fc8d1 JT |
267 | static int reset_tree(struct object_id *i_tree, int update, int reset) |
268 | { | |
269 | int nr_trees = 1; | |
270 | struct unpack_trees_options opts; | |
271 | struct tree_desc t[MAX_UNPACK_TREES]; | |
272 | struct tree *tree; | |
273 | struct lock_file lock_file = LOCK_INIT; | |
274 | ||
07047d68 ÆAB |
275 | repo_read_index_preload(the_repository, NULL, 0); |
276 | if (refresh_index(&the_index, REFRESH_QUIET, NULL, NULL, NULL)) | |
8a0fc8d1 JT |
277 | return -1; |
278 | ||
07047d68 | 279 | repo_hold_locked_index(the_repository, &lock_file, LOCK_DIE_ON_ERROR); |
8a0fc8d1 JT |
280 | |
281 | memset(&opts, 0, sizeof(opts)); | |
282 | ||
283 | tree = parse_tree_indirect(i_tree); | |
284 | if (parse_tree(tree)) | |
285 | return -1; | |
286 | ||
287 | init_tree_desc(t, tree->buffer, tree->size); | |
288 | ||
289 | opts.head_idx = 1; | |
290 | opts.src_index = &the_index; | |
291 | opts.dst_index = &the_index; | |
292 | opts.merge = 1; | |
480d3d6b | 293 | opts.reset = reset ? UNPACK_RESET_PROTECT_UNTRACKED : 0; |
8a0fc8d1 | 294 | opts.update = update; |
480d3d6b | 295 | if (update) |
1b5f3733 | 296 | opts.preserve_ignored = 0; /* FIXME: !overwrite_ignore */ |
8a0fc8d1 JT |
297 | opts.fn = oneway_merge; |
298 | ||
299 | if (unpack_trees(nr_trees, t, &opts)) | |
300 | return -1; | |
301 | ||
302 | if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK)) | |
303 | return error(_("unable to write new index file")); | |
304 | ||
305 | return 0; | |
306 | } | |
307 | ||
308 | static int diff_tree_binary(struct strbuf *out, struct object_id *w_commit) | |
309 | { | |
310 | struct child_process cp = CHILD_PROCESS_INIT; | |
311 | const char *w_commit_hex = oid_to_hex(w_commit); | |
312 | ||
313 | /* | |
314 | * Diff-tree would not be very hard to replace with a native function, | |
315 | * however it should be done together with apply_cached. | |
316 | */ | |
317 | cp.git_cmd = 1; | |
22f9b7f3 JK |
318 | strvec_pushl(&cp.args, "diff-tree", "--binary", NULL); |
319 | strvec_pushf(&cp.args, "%s^2^..%s^2", w_commit_hex, w_commit_hex); | |
8a0fc8d1 JT |
320 | |
321 | return pipe_command(&cp, NULL, 0, out, 0, NULL, 0); | |
322 | } | |
323 | ||
324 | static int apply_cached(struct strbuf *out) | |
325 | { | |
326 | struct child_process cp = CHILD_PROCESS_INIT; | |
327 | ||
328 | /* | |
329 | * Apply currently only reads either from stdin or a file, thus | |
330 | * apply_all_patches would have to be updated to optionally take a | |
331 | * buffer. | |
332 | */ | |
333 | cp.git_cmd = 1; | |
22f9b7f3 | 334 | strvec_pushl(&cp.args, "apply", "--cached", NULL); |
8a0fc8d1 JT |
335 | return pipe_command(&cp, out->buf, out->len, NULL, 0, NULL, 0); |
336 | } | |
337 | ||
338 | static int reset_head(void) | |
339 | { | |
340 | struct child_process cp = CHILD_PROCESS_INIT; | |
341 | ||
342 | /* | |
343 | * Reset is overall quite simple, however there is no current public | |
344 | * API for resetting. | |
345 | */ | |
346 | cp.git_cmd = 1; | |
4b8b0f6f | 347 | strvec_pushl(&cp.args, "reset", "--quiet", "--refresh", NULL); |
8a0fc8d1 JT |
348 | |
349 | return run_command(&cp); | |
350 | } | |
351 | ||
3d40e372 EN |
352 | static int is_path_a_directory(const char *path) |
353 | { | |
354 | /* | |
355 | * This function differs from abspath.c:is_directory() in that | |
356 | * here we use lstat() instead of stat(); we do not want to | |
357 | * follow symbolic links here. | |
358 | */ | |
359 | struct stat st; | |
360 | return (!lstat(path, &st) && S_ISDIR(st.st_mode)); | |
361 | } | |
362 | ||
d4788af8 | 363 | static void add_diff_to_buf(struct diff_queue_struct *q, |
71006d77 | 364 | struct diff_options *options UNUSED, |
d4788af8 PSU |
365 | void *data) |
366 | { | |
367 | int i; | |
368 | ||
369 | for (i = 0; i < q->nr; i++) { | |
3d40e372 EN |
370 | if (is_path_a_directory(q->queue[i]->one->path)) |
371 | continue; | |
372 | ||
d4788af8 PSU |
373 | strbuf_addstr(data, q->queue[i]->one->path); |
374 | ||
375 | /* NUL-terminate: will be fed to update-index -z */ | |
376 | strbuf_addch(data, '\0'); | |
377 | } | |
378 | } | |
379 | ||
8a0fc8d1 JT |
380 | static int restore_untracked(struct object_id *u_tree) |
381 | { | |
382 | int res; | |
383 | struct child_process cp = CHILD_PROCESS_INIT; | |
384 | ||
385 | /* | |
386 | * We need to run restore files from a given index, but without | |
387 | * affecting the current index, so we use GIT_INDEX_FILE with | |
388 | * run_command to fork processes that will not interfere. | |
389 | */ | |
390 | cp.git_cmd = 1; | |
22f9b7f3 JK |
391 | strvec_push(&cp.args, "read-tree"); |
392 | strvec_push(&cp.args, oid_to_hex(u_tree)); | |
29fda24d | 393 | strvec_pushf(&cp.env, "GIT_INDEX_FILE=%s", |
f6d8942b | 394 | stash_index_path.buf); |
8a0fc8d1 JT |
395 | if (run_command(&cp)) { |
396 | remove_path(stash_index_path.buf); | |
397 | return -1; | |
398 | } | |
399 | ||
400 | child_process_init(&cp); | |
401 | cp.git_cmd = 1; | |
22f9b7f3 | 402 | strvec_pushl(&cp.args, "checkout-index", "--all", NULL); |
29fda24d | 403 | strvec_pushf(&cp.env, "GIT_INDEX_FILE=%s", |
f6d8942b | 404 | stash_index_path.buf); |
8a0fc8d1 JT |
405 | |
406 | res = run_command(&cp); | |
407 | remove_path(stash_index_path.buf); | |
408 | return res; | |
409 | } | |
410 | ||
b34ab4a4 EN |
411 | static void unstage_changes_unless_new(struct object_id *orig_tree) |
412 | { | |
413 | /* | |
414 | * When we enter this function, there has been a clean merge of | |
415 | * relevant trees, and the merge logic always stages whatever merges | |
416 | * cleanly. We want to unstage those changes, unless it corresponds | |
417 | * to a file that didn't exist as of orig_tree. | |
ba359fd5 EN |
418 | * |
419 | * However, if any SKIP_WORKTREE path is modified relative to | |
420 | * orig_tree, then we want to clear the SKIP_WORKTREE bit and write | |
421 | * it to the worktree before unstaging. | |
b34ab4a4 EN |
422 | */ |
423 | ||
ba359fd5 | 424 | struct checkout state = CHECKOUT_INIT; |
b34ab4a4 EN |
425 | struct diff_options diff_opts; |
426 | struct lock_file lock = LOCK_INIT; | |
427 | int i; | |
428 | ||
ba359fd5 EN |
429 | /* If any entries have skip_worktree set, we'll have to check 'em out */ |
430 | state.force = 1; | |
431 | state.quiet = 1; | |
432 | state.refresh_cache = 1; | |
433 | state.istate = &the_index; | |
434 | ||
b34ab4a4 EN |
435 | /* |
436 | * Step 1: get a difference between orig_tree (which corresponding | |
437 | * to the index before a merge was run) and the current index | |
438 | * (reflecting the changes brought in by the merge). | |
439 | */ | |
08539032 | 440 | repo_diff_setup(the_repository, &diff_opts); |
b34ab4a4 EN |
441 | diff_opts.flags.recursive = 1; |
442 | diff_opts.detect_rename = 0; | |
443 | diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT; | |
444 | diff_setup_done(&diff_opts); | |
445 | ||
446 | do_diff_cache(orig_tree, &diff_opts); | |
447 | diffcore_std(&diff_opts); | |
448 | ||
449 | /* Iterate over the paths that changed due to the merge... */ | |
450 | for (i = 0; i < diff_queued_diff.nr; i++) { | |
451 | struct diff_filepair *p; | |
452 | struct cache_entry *ce; | |
453 | int pos; | |
454 | ||
455 | /* Look up the path's position in the current index. */ | |
456 | p = diff_queued_diff.queue[i]; | |
457 | pos = index_name_pos(&the_index, p->two->path, | |
458 | strlen(p->two->path)); | |
459 | ||
460 | /* | |
ba359fd5 EN |
461 | * Step 2: Place changes in the working tree |
462 | * | |
463 | * Stash is about restoring changes *to the working tree*. | |
464 | * So if the merge successfully got a new version of some | |
465 | * path, but left it out of the working tree, then clear the | |
466 | * SKIP_WORKTREE bit and write it to the working tree. | |
467 | */ | |
dc594180 | 468 | if (pos >= 0 && ce_skip_worktree(the_index.cache[pos])) { |
ba359fd5 EN |
469 | struct stat st; |
470 | ||
dc594180 | 471 | ce = the_index.cache[pos]; |
ba359fd5 EN |
472 | if (!lstat(ce->name, &st)) { |
473 | /* Conflicting path present; relocate it */ | |
474 | struct strbuf new_path = STRBUF_INIT; | |
475 | int fd; | |
476 | ||
477 | strbuf_addf(&new_path, | |
478 | "%s.stash.XXXXXX", ce->name); | |
479 | fd = xmkstemp(new_path.buf); | |
480 | close(fd); | |
481 | printf(_("WARNING: Untracked file in way of " | |
482 | "tracked file! Renaming\n " | |
483 | " %s -> %s\n" | |
484 | " to make room.\n"), | |
485 | ce->name, new_path.buf); | |
486 | if (rename(ce->name, new_path.buf)) | |
487 | die("Failed to move %s to %s\n", | |
488 | ce->name, new_path.buf); | |
489 | strbuf_release(&new_path); | |
490 | } | |
491 | checkout_entry(ce, &state, NULL, NULL); | |
492 | ce->ce_flags &= ~CE_SKIP_WORKTREE; | |
493 | } | |
494 | ||
495 | /* | |
496 | * Step 3: "unstage" changes, as long as they are still tracked | |
b34ab4a4 EN |
497 | */ |
498 | if (p->one->oid_valid) { | |
499 | /* | |
500 | * Path existed in orig_tree; restore index entry | |
501 | * from that tree in order to "unstage" the changes. | |
502 | */ | |
503 | int option = ADD_CACHE_OK_TO_REPLACE; | |
504 | if (pos < 0) | |
505 | option = ADD_CACHE_OK_TO_ADD; | |
506 | ||
507 | ce = make_cache_entry(&the_index, | |
508 | p->one->mode, | |
509 | &p->one->oid, | |
510 | p->one->path, | |
511 | 0, 0); | |
512 | add_index_entry(&the_index, ce, option); | |
513 | } | |
514 | } | |
515 | diff_flush(&diff_opts); | |
516 | ||
517 | /* | |
ba359fd5 | 518 | * Step 4: write the new index to disk |
b34ab4a4 EN |
519 | */ |
520 | repo_hold_locked_index(the_repository, &lock, LOCK_DIE_ON_ERROR); | |
521 | if (write_locked_index(&the_index, &lock, | |
522 | COMMIT_LOCK | SKIP_IF_UNCHANGED)) | |
d2058cb2 | 523 | die(_("could not write index")); |
b34ab4a4 EN |
524 | } |
525 | ||
8a0fc8d1 JT |
526 | static int do_apply_stash(const char *prefix, struct stash_info *info, |
527 | int index, int quiet) | |
528 | { | |
874cf2a6 | 529 | int clean, ret; |
8a0fc8d1 JT |
530 | int has_index = index; |
531 | struct merge_options o; | |
532 | struct object_id c_tree; | |
533 | struct object_id index_tree; | |
874cf2a6 VD |
534 | struct tree *head, *merge, *merge_base; |
535 | struct lock_file lock = LOCK_INIT; | |
8a0fc8d1 | 536 | |
07047d68 ÆAB |
537 | repo_read_index_preload(the_repository, NULL, 0); |
538 | if (repo_refresh_and_write_index(the_repository, REFRESH_QUIET, 0, 0, | |
539 | NULL, NULL, NULL)) | |
d2058cb2 | 540 | return error(_("could not write index")); |
8a0fc8d1 | 541 | |
99370863 ÆAB |
542 | if (write_index_as_tree(&c_tree, &the_index, get_index_file(), 0, |
543 | NULL)) | |
8a0fc8d1 JT |
544 | return error(_("cannot apply a stash in the middle of a merge")); |
545 | ||
546 | if (index) { | |
547 | if (oideq(&info->b_tree, &info->i_tree) || | |
548 | oideq(&c_tree, &info->i_tree)) { | |
549 | has_index = 0; | |
550 | } else { | |
551 | struct strbuf out = STRBUF_INIT; | |
552 | ||
553 | if (diff_tree_binary(&out, &info->w_commit)) { | |
554 | strbuf_release(&out); | |
555 | return error(_("could not generate diff %s^!."), | |
556 | oid_to_hex(&info->w_commit)); | |
557 | } | |
558 | ||
559 | ret = apply_cached(&out); | |
560 | strbuf_release(&out); | |
561 | if (ret) | |
eaf53415 | 562 | return error(_("conflicts in index. " |
8a0fc8d1 JT |
563 | "Try without --index.")); |
564 | ||
07047d68 ÆAB |
565 | discard_index(&the_index); |
566 | repo_read_index(the_repository); | |
99370863 ÆAB |
567 | if (write_index_as_tree(&index_tree, &the_index, |
568 | get_index_file(), 0, NULL)) | |
8a0fc8d1 JT |
569 | return error(_("could not save index tree")); |
570 | ||
571 | reset_head(); | |
07047d68 ÆAB |
572 | discard_index(&the_index); |
573 | repo_read_index(the_repository); | |
8a0fc8d1 JT |
574 | } |
575 | } | |
576 | ||
e36adf71 | 577 | init_merge_options(&o, the_repository); |
8a0fc8d1 JT |
578 | |
579 | o.branch1 = "Updated upstream"; | |
580 | o.branch2 = "Stashed changes"; | |
874cf2a6 | 581 | o.ancestor = "Stash base"; |
8a0fc8d1 JT |
582 | |
583 | if (oideq(&info->b_tree, &c_tree)) | |
584 | o.branch1 = "Version stash was based on"; | |
585 | ||
586 | if (quiet) | |
587 | o.verbosity = 0; | |
588 | ||
589 | if (o.verbosity >= 3) | |
590 | printf_ln(_("Merging %s with %s"), o.branch1, o.branch2); | |
591 | ||
874cf2a6 VD |
592 | head = lookup_tree(o.repo, &c_tree); |
593 | merge = lookup_tree(o.repo, &info->w_tree); | |
594 | merge_base = lookup_tree(o.repo, &info->b_tree); | |
595 | ||
596 | repo_hold_locked_index(o.repo, &lock, LOCK_DIE_ON_ERROR); | |
597 | clean = merge_ort_nonrecursive(&o, head, merge, merge_base); | |
598 | ||
599 | /* | |
600 | * If 'clean' >= 0, reverse the value for 'ret' so 'ret' is 0 when the | |
601 | * merge was clean, and nonzero if the merge was unclean or encountered | |
602 | * an error. | |
603 | */ | |
604 | ret = clean >= 0 ? !clean : clean; | |
605 | ||
606 | if (ret < 0) | |
607 | rollback_lock_file(&lock); | |
608 | else if (write_locked_index(o.repo->index, &lock, | |
609 | COMMIT_LOCK | SKIP_IF_UNCHANGED)) | |
610 | ret = error(_("could not write index")); | |
8a0fc8d1 | 611 | |
8a0fc8d1 | 612 | if (ret) { |
b26a71b1 | 613 | repo_rerere(the_repository, 0); |
8a0fc8d1 JT |
614 | |
615 | if (index) | |
616 | fprintf_ln(stderr, _("Index was not unstashed.")); | |
617 | ||
71cade5a | 618 | goto restore_untracked; |
8a0fc8d1 JT |
619 | } |
620 | ||
621 | if (has_index) { | |
622 | if (reset_tree(&index_tree, 0, 0)) | |
71cade5a | 623 | ret = -1; |
8a0fc8d1 | 624 | } else { |
b34ab4a4 | 625 | unstage_changes_unless_new(&c_tree); |
8a0fc8d1 JT |
626 | } |
627 | ||
71cade5a | 628 | restore_untracked: |
bee8691f | 629 | if (info->has_u && restore_untracked(&info->u_tree)) |
71cade5a | 630 | ret = error(_("could not restore untracked files from stash")); |
bee8691f | 631 | |
df53c808 | 632 | if (!quiet) { |
8a0fc8d1 JT |
633 | struct child_process cp = CHILD_PROCESS_INIT; |
634 | ||
635 | /* | |
636 | * Status is quite simple and could be replaced with calls to | |
637 | * wt_status in the future, but it adds complexities which may | |
638 | * require more tests. | |
639 | */ | |
640 | cp.git_cmd = 1; | |
641 | cp.dir = prefix; | |
29fda24d | 642 | strvec_pushf(&cp.env, GIT_WORK_TREE_ENVIRONMENT"=%s", |
f6d8942b | 643 | absolute_path(get_git_work_tree())); |
29fda24d | 644 | strvec_pushf(&cp.env, GIT_DIR_ENVIRONMENT"=%s", |
f6d8942b | 645 | absolute_path(get_git_dir())); |
22f9b7f3 | 646 | strvec_push(&cp.args, "status"); |
8a0fc8d1 JT |
647 | run_command(&cp); |
648 | } | |
649 | ||
71cade5a | 650 | return ret; |
8a0fc8d1 JT |
651 | } |
652 | ||
653 | static int apply_stash(int argc, const char **argv, const char *prefix) | |
654 | { | |
5e480176 | 655 | int ret = -1; |
8a0fc8d1 JT |
656 | int quiet = 0; |
657 | int index = 0; | |
5e480176 | 658 | struct stash_info info = STASH_INFO_INIT; |
8a0fc8d1 JT |
659 | struct option options[] = { |
660 | OPT__QUIET(&quiet, N_("be quiet, only report errors")), | |
661 | OPT_BOOL(0, "index", &index, | |
662 | N_("attempt to recreate the index")), | |
663 | OPT_END() | |
664 | }; | |
665 | ||
666 | argc = parse_options(argc, argv, prefix, options, | |
40af1468 | 667 | git_stash_apply_usage, 0); |
8a0fc8d1 JT |
668 | |
669 | if (get_stash_info(&info, argc, argv)) | |
5e480176 | 670 | goto cleanup; |
8a0fc8d1 JT |
671 | |
672 | ret = do_apply_stash(prefix, &info, index, quiet); | |
5e480176 | 673 | cleanup: |
8a0fc8d1 JT |
674 | free_stash_info(&info); |
675 | return ret; | |
676 | } | |
677 | ||
5cf88fd8 ÆAB |
678 | static int reject_reflog_ent(struct object_id *ooid UNUSED, |
679 | struct object_id *noid UNUSED, | |
680 | const char *email UNUSED, | |
681 | timestamp_t timestamp UNUSED, | |
682 | int tz UNUSED, const char *message UNUSED, | |
683 | void *cb_data UNUSED) | |
4f44c565 RS |
684 | { |
685 | return 1; | |
686 | } | |
687 | ||
688 | static int reflog_is_empty(const char *refname) | |
689 | { | |
690 | return !for_each_reflog_ent(refname, reject_reflog_ent, NULL); | |
691 | } | |
692 | ||
eabf7405 | 693 | static int do_drop_stash(struct stash_info *info, int quiet) |
4e2dd393 | 694 | { |
758b4d2b JC |
695 | if (!reflog_delete(info->revision.buf, |
696 | EXPIRE_REFLOGS_REWRITE | EXPIRE_REFLOGS_UPDATE_REF, | |
697 | 0)) { | |
4e2dd393 JT |
698 | if (!quiet) |
699 | printf_ln(_("Dropped %s (%s)"), info->revision.buf, | |
700 | oid_to_hex(&info->w_commit)); | |
701 | } else { | |
702 | return error(_("%s: Could not drop stash entry"), | |
703 | info->revision.buf); | |
704 | } | |
705 | ||
4f44c565 | 706 | if (reflog_is_empty(ref_stash)) |
4e2dd393 JT |
707 | do_clear_stash(); |
708 | ||
709 | return 0; | |
710 | } | |
711 | ||
5e480176 ÆAB |
712 | static int get_stash_info_assert(struct stash_info *info, int argc, |
713 | const char **argv) | |
4e2dd393 | 714 | { |
5e480176 ÆAB |
715 | int ret = get_stash_info(info, argc, argv); |
716 | ||
717 | if (ret < 0) | |
718 | return ret; | |
719 | ||
720 | if (!info->is_stash_ref) | |
721 | return error(_("'%s' is not a stash reference"), info->revision.buf); | |
722 | ||
723 | return 0; | |
4e2dd393 JT |
724 | } |
725 | ||
726 | static int drop_stash(int argc, const char **argv, const char *prefix) | |
727 | { | |
5e480176 | 728 | int ret = -1; |
4e2dd393 | 729 | int quiet = 0; |
5e480176 | 730 | struct stash_info info = STASH_INFO_INIT; |
4e2dd393 JT |
731 | struct option options[] = { |
732 | OPT__QUIET(&quiet, N_("be quiet, only report errors")), | |
733 | OPT_END() | |
734 | }; | |
735 | ||
736 | argc = parse_options(argc, argv, prefix, options, | |
40af1468 | 737 | git_stash_drop_usage, 0); |
4e2dd393 | 738 | |
5e480176 ÆAB |
739 | if (get_stash_info_assert(&info, argc, argv)) |
740 | goto cleanup; | |
4e2dd393 | 741 | |
eabf7405 | 742 | ret = do_drop_stash(&info, quiet); |
5e480176 | 743 | cleanup: |
4e2dd393 JT |
744 | free_stash_info(&info); |
745 | return ret; | |
746 | } | |
747 | ||
c4de61d7 JT |
748 | static int pop_stash(int argc, const char **argv, const char *prefix) |
749 | { | |
5e480176 | 750 | int ret = -1; |
c4de61d7 JT |
751 | int index = 0; |
752 | int quiet = 0; | |
5e480176 | 753 | struct stash_info info = STASH_INFO_INIT; |
c4de61d7 JT |
754 | struct option options[] = { |
755 | OPT__QUIET(&quiet, N_("be quiet, only report errors")), | |
756 | OPT_BOOL(0, "index", &index, | |
757 | N_("attempt to recreate the index")), | |
758 | OPT_END() | |
759 | }; | |
760 | ||
761 | argc = parse_options(argc, argv, prefix, options, | |
40af1468 | 762 | git_stash_pop_usage, 0); |
c4de61d7 | 763 | |
5e480176 ÆAB |
764 | if (get_stash_info_assert(&info, argc, argv)) |
765 | goto cleanup; | |
c4de61d7 | 766 | |
c4de61d7 JT |
767 | if ((ret = do_apply_stash(prefix, &info, index, quiet))) |
768 | printf_ln(_("The stash entry is kept in case " | |
769 | "you need it again.")); | |
770 | else | |
eabf7405 | 771 | ret = do_drop_stash(&info, quiet); |
c4de61d7 | 772 | |
5e480176 | 773 | cleanup: |
c4de61d7 JT |
774 | free_stash_info(&info); |
775 | return ret; | |
776 | } | |
777 | ||
577c1995 JT |
778 | static int branch_stash(int argc, const char **argv, const char *prefix) |
779 | { | |
5e480176 | 780 | int ret = -1; |
577c1995 | 781 | const char *branch = NULL; |
5e480176 | 782 | struct stash_info info = STASH_INFO_INIT; |
577c1995 JT |
783 | struct child_process cp = CHILD_PROCESS_INIT; |
784 | struct option options[] = { | |
785 | OPT_END() | |
786 | }; | |
787 | ||
788 | argc = parse_options(argc, argv, prefix, options, | |
40af1468 | 789 | git_stash_branch_usage, 0); |
577c1995 JT |
790 | |
791 | if (!argc) { | |
792 | fprintf_ln(stderr, _("No branch name specified")); | |
793 | return -1; | |
794 | } | |
795 | ||
796 | branch = argv[0]; | |
797 | ||
798 | if (get_stash_info(&info, argc - 1, argv + 1)) | |
5e480176 | 799 | goto cleanup; |
577c1995 JT |
800 | |
801 | cp.git_cmd = 1; | |
22f9b7f3 JK |
802 | strvec_pushl(&cp.args, "checkout", "-b", NULL); |
803 | strvec_push(&cp.args, branch); | |
804 | strvec_push(&cp.args, oid_to_hex(&info.b_commit)); | |
577c1995 JT |
805 | ret = run_command(&cp); |
806 | if (!ret) | |
807 | ret = do_apply_stash(prefix, &info, 1, 0); | |
808 | if (!ret && info.is_stash_ref) | |
eabf7405 | 809 | ret = do_drop_stash(&info, 0); |
577c1995 | 810 | |
5e480176 | 811 | cleanup: |
577c1995 | 812 | free_stash_info(&info); |
577c1995 JT |
813 | return ret; |
814 | } | |
815 | ||
130f2697 PSU |
816 | static int list_stash(int argc, const char **argv, const char *prefix) |
817 | { | |
818 | struct child_process cp = CHILD_PROCESS_INIT; | |
819 | struct option options[] = { | |
820 | OPT_END() | |
821 | }; | |
822 | ||
823 | argc = parse_options(argc, argv, prefix, options, | |
40af1468 | 824 | git_stash_list_usage, |
99d86d60 | 825 | PARSE_OPT_KEEP_UNKNOWN_OPT); |
130f2697 PSU |
826 | |
827 | if (!ref_exists(ref_stash)) | |
828 | return 0; | |
829 | ||
830 | cp.git_cmd = 1; | |
22f9b7f3 | 831 | strvec_pushl(&cp.args, "log", "--format=%gd: %gs", "-g", |
1e20a407 | 832 | "--first-parent", NULL); |
22f9b7f3 JK |
833 | strvec_pushv(&cp.args, argv); |
834 | strvec_push(&cp.args, ref_stash); | |
835 | strvec_push(&cp.args, "--"); | |
130f2697 PSU |
836 | return run_command(&cp); |
837 | } | |
838 | ||
dc7bd382 PSU |
839 | static int show_stat = 1; |
840 | static int show_patch; | |
0af760e2 | 841 | static int show_include_untracked; |
dc7bd382 | 842 | |
a4e7e317 GC |
843 | static int git_stash_config(const char *var, const char *value, |
844 | const struct config_context *ctx, void *cb) | |
dc7bd382 PSU |
845 | { |
846 | if (!strcmp(var, "stash.showstat")) { | |
847 | show_stat = git_config_bool(var, value); | |
848 | return 0; | |
849 | } | |
850 | if (!strcmp(var, "stash.showpatch")) { | |
851 | show_patch = git_config_bool(var, value); | |
852 | return 0; | |
853 | } | |
0af760e2 DL |
854 | if (!strcmp(var, "stash.showincludeuntracked")) { |
855 | show_include_untracked = git_config_bool(var, value); | |
856 | return 0; | |
857 | } | |
a4e7e317 | 858 | return git_diff_basic_config(var, value, ctx, cb); |
dc7bd382 PSU |
859 | } |
860 | ||
d3c7bf73 DL |
861 | static void diff_include_untracked(const struct stash_info *info, struct diff_options *diff_opt) |
862 | { | |
863 | const struct object_id *oid[] = { &info->w_commit, &info->u_tree }; | |
864 | struct tree *tree[ARRAY_SIZE(oid)]; | |
865 | struct tree_desc tree_desc[ARRAY_SIZE(oid)]; | |
866 | struct unpack_trees_options unpack_tree_opt = { 0 }; | |
867 | int i; | |
868 | ||
869 | for (i = 0; i < ARRAY_SIZE(oid); i++) { | |
870 | tree[i] = parse_tree_indirect(oid[i]); | |
871 | if (parse_tree(tree[i]) < 0) | |
872 | die(_("failed to parse tree")); | |
873 | init_tree_desc(&tree_desc[i], tree[i]->buffer, tree[i]->size); | |
874 | } | |
875 | ||
876 | unpack_tree_opt.head_idx = -1; | |
877 | unpack_tree_opt.src_index = &the_index; | |
878 | unpack_tree_opt.dst_index = &the_index; | |
879 | unpack_tree_opt.merge = 1; | |
880 | unpack_tree_opt.fn = stash_worktree_untracked_merge; | |
881 | ||
882 | if (unpack_trees(ARRAY_SIZE(tree_desc), tree_desc, &unpack_tree_opt)) | |
883 | die(_("failed to unpack trees")); | |
884 | ||
885 | do_diff_cache(&info->b_commit, diff_opt); | |
886 | } | |
887 | ||
dc7bd382 PSU |
888 | static int show_stash(int argc, const char **argv, const char *prefix) |
889 | { | |
890 | int i; | |
5e480176 ÆAB |
891 | int ret = -1; |
892 | struct stash_info info = STASH_INFO_INIT; | |
dc7bd382 | 893 | struct rev_info rev; |
22f9b7f3 JK |
894 | struct strvec stash_args = STRVEC_INIT; |
895 | struct strvec revision_args = STRVEC_INIT; | |
d3c7bf73 DL |
896 | enum { |
897 | UNTRACKED_NONE, | |
898 | UNTRACKED_INCLUDE, | |
899 | UNTRACKED_ONLY | |
af5cd44b | 900 | } show_untracked = show_include_untracked ? UNTRACKED_INCLUDE : UNTRACKED_NONE; |
dc7bd382 | 901 | struct option options[] = { |
d3c7bf73 DL |
902 | OPT_SET_INT('u', "include-untracked", &show_untracked, |
903 | N_("include untracked files in the stash"), | |
904 | UNTRACKED_INCLUDE), | |
905 | OPT_SET_INT_F(0, "only-untracked", &show_untracked, | |
906 | N_("only show untracked files in the stash"), | |
907 | UNTRACKED_ONLY, PARSE_OPT_NONEG), | |
dc7bd382 PSU |
908 | OPT_END() |
909 | }; | |
5e480176 | 910 | int do_usage = 0; |
dc7bd382 PSU |
911 | |
912 | init_diff_ui_defaults(); | |
913 | git_config(git_diff_ui_config, NULL); | |
035c7de9 | 914 | repo_init_revisions(the_repository, &rev, prefix); |
dc7bd382 | 915 | |
d3c7bf73 | 916 | argc = parse_options(argc, argv, prefix, options, git_stash_show_usage, |
99d86d60 | 917 | PARSE_OPT_KEEP_ARGV0 | PARSE_OPT_KEEP_UNKNOWN_OPT | |
d3c7bf73 DL |
918 | PARSE_OPT_KEEP_DASHDASH); |
919 | ||
22f9b7f3 | 920 | strvec_push(&revision_args, argv[0]); |
dc7bd382 PSU |
921 | for (i = 1; i < argc; i++) { |
922 | if (argv[i][0] != '-') | |
22f9b7f3 | 923 | strvec_push(&stash_args, argv[i]); |
dc7bd382 | 924 | else |
22f9b7f3 | 925 | strvec_push(&revision_args, argv[i]); |
dc7bd382 PSU |
926 | } |
927 | ||
5e480176 ÆAB |
928 | if (get_stash_info(&info, stash_args.nr, stash_args.v)) |
929 | goto cleanup; | |
dc7bd382 PSU |
930 | |
931 | /* | |
932 | * The config settings are applied only if there are not passed | |
933 | * any options. | |
934 | */ | |
d70a9eb6 | 935 | if (revision_args.nr == 1) { |
dc7bd382 PSU |
936 | if (show_stat) |
937 | rev.diffopt.output_format = DIFF_FORMAT_DIFFSTAT; | |
938 | ||
939 | if (show_patch) | |
940 | rev.diffopt.output_format |= DIFF_FORMAT_PATCH; | |
941 | ||
942 | if (!show_stat && !show_patch) { | |
5e480176 ÆAB |
943 | ret = 0; |
944 | goto cleanup; | |
dc7bd382 PSU |
945 | } |
946 | } | |
947 | ||
d70a9eb6 | 948 | argc = setup_revisions(revision_args.nr, revision_args.v, &rev, NULL); |
5e480176 ÆAB |
949 | if (argc > 1) |
950 | goto usage; | |
8e407bc8 TG |
951 | if (!rev.diffopt.output_format) { |
952 | rev.diffopt.output_format = DIFF_FORMAT_PATCH; | |
953 | diff_setup_done(&rev.diffopt); | |
954 | } | |
dc7bd382 PSU |
955 | |
956 | rev.diffopt.flags.recursive = 1; | |
957 | setup_diff_pager(&rev.diffopt); | |
d3c7bf73 DL |
958 | switch (show_untracked) { |
959 | case UNTRACKED_NONE: | |
960 | diff_tree_oid(&info.b_commit, &info.w_commit, "", &rev.diffopt); | |
961 | break; | |
962 | case UNTRACKED_ONLY: | |
1ff595d2 DL |
963 | if (info.has_u) |
964 | diff_root_tree_oid(&info.u_tree, "", &rev.diffopt); | |
d3c7bf73 DL |
965 | break; |
966 | case UNTRACKED_INCLUDE: | |
1ff595d2 DL |
967 | if (info.has_u) |
968 | diff_include_untracked(&info, &rev.diffopt); | |
969 | else | |
970 | diff_tree_oid(&info.b_commit, &info.w_commit, "", &rev.diffopt); | |
d3c7bf73 DL |
971 | break; |
972 | } | |
dc7bd382 PSU |
973 | log_tree_diff_flush(&rev); |
974 | ||
5cc6b2d7 | 975 | ret = diff_result_code(&rev.diffopt); |
5e480176 ÆAB |
976 | cleanup: |
977 | strvec_clear(&stash_args); | |
dc7bd382 | 978 | free_stash_info(&info); |
0139c58a | 979 | release_revisions(&rev); |
5e480176 ÆAB |
980 | if (do_usage) |
981 | usage_with_options(git_stash_show_usage, options); | |
982 | return ret; | |
983 | usage: | |
984 | do_usage = 1; | |
985 | goto cleanup; | |
dc7bd382 PSU |
986 | } |
987 | ||
41e0dd55 PSU |
988 | static int do_store_stash(const struct object_id *w_commit, const char *stash_msg, |
989 | int quiet) | |
990 | { | |
d9b66345 JH |
991 | struct stash_info info; |
992 | char revision[GIT_MAX_HEXSZ]; | |
993 | ||
994 | oid_to_hex_r(revision, w_commit); | |
995 | assert_stash_like(&info, revision); | |
996 | ||
41e0dd55 PSU |
997 | if (!stash_msg) |
998 | stash_msg = "Created via \"git stash store\"."; | |
999 | ||
1000 | if (update_ref(stash_msg, ref_stash, w_commit, NULL, | |
1001 | REF_FORCE_CREATE_REFLOG, | |
1002 | quiet ? UPDATE_REFS_QUIET_ON_ERR : | |
1003 | UPDATE_REFS_MSG_ON_ERR)) { | |
1004 | if (!quiet) { | |
1005 | fprintf_ln(stderr, _("Cannot update %s with %s"), | |
1006 | ref_stash, oid_to_hex(w_commit)); | |
1007 | } | |
1008 | return -1; | |
1009 | } | |
1010 | ||
1011 | return 0; | |
1012 | } | |
1013 | ||
1014 | static int store_stash(int argc, const char **argv, const char *prefix) | |
1015 | { | |
1016 | int quiet = 0; | |
1017 | const char *stash_msg = NULL; | |
1018 | struct object_id obj; | |
1019 | struct object_context dummy; | |
1020 | struct option options[] = { | |
1021 | OPT__QUIET(&quiet, N_("be quiet")), | |
1022 | OPT_STRING('m', "message", &stash_msg, "message", | |
1023 | N_("stash message")), | |
1024 | OPT_END() | |
1025 | }; | |
1026 | ||
1027 | argc = parse_options(argc, argv, prefix, options, | |
40af1468 | 1028 | git_stash_store_usage, |
99d86d60 | 1029 | PARSE_OPT_KEEP_UNKNOWN_OPT); |
41e0dd55 PSU |
1030 | |
1031 | if (argc != 1) { | |
1032 | if (!quiet) | |
1033 | fprintf_ln(stderr, _("\"git stash store\" requires one " | |
1034 | "<commit> argument")); | |
1035 | return -1; | |
1036 | } | |
1037 | ||
e36adf71 JH |
1038 | if (get_oid_with_context(the_repository, |
1039 | argv[0], quiet ? GET_OID_QUIETLY : 0, &obj, | |
41e0dd55 PSU |
1040 | &dummy)) { |
1041 | if (!quiet) | |
1042 | fprintf_ln(stderr, _("Cannot update %s with %s"), | |
1043 | ref_stash, argv[0]); | |
1044 | return -1; | |
1045 | } | |
1046 | ||
1047 | return do_store_stash(&obj, stash_msg, quiet); | |
1048 | } | |
1049 | ||
22f9b7f3 | 1050 | static void add_pathspecs(struct strvec *args, |
7db9302d | 1051 | const struct pathspec *ps) { |
d4788af8 PSU |
1052 | int i; |
1053 | ||
7db9302d | 1054 | for (i = 0; i < ps->nr; i++) |
22f9b7f3 | 1055 | strvec_push(args, ps->items[i].original); |
d4788af8 PSU |
1056 | } |
1057 | ||
1058 | /* | |
1059 | * `untracked_files` will be filled with the names of untracked files. | |
1060 | * The return value is: | |
1061 | * | |
1062 | * = 0 if there are not any untracked files | |
1063 | * > 0 if there are untracked files | |
1064 | */ | |
7db9302d | 1065 | static int get_untracked_files(const struct pathspec *ps, int include_untracked, |
d4788af8 PSU |
1066 | struct strbuf *untracked_files) |
1067 | { | |
1068 | int i; | |
d4788af8 | 1069 | int found = 0; |
ce93a4c6 | 1070 | struct dir_struct dir = DIR_INIT; |
d4788af8 | 1071 | |
d4788af8 PSU |
1072 | if (include_untracked != INCLUDE_ALL_FILES) |
1073 | setup_standard_excludes(&dir); | |
1074 | ||
95c11ecc | 1075 | fill_directory(&dir, the_repository->index, ps); |
d4788af8 PSU |
1076 | for (i = 0; i < dir.nr; i++) { |
1077 | struct dir_entry *ent = dir.entries[i]; | |
95c11ecc EN |
1078 | found++; |
1079 | strbuf_addstr(untracked_files, ent->name); | |
1080 | /* NUL-terminate: will be fed to update-index -z */ | |
1081 | strbuf_addch(untracked_files, '\0'); | |
d4788af8 PSU |
1082 | } |
1083 | ||
eceba532 | 1084 | dir_clear(&dir); |
d4788af8 PSU |
1085 | return found; |
1086 | } | |
1087 | ||
1088 | /* | |
ef0f0b45 | 1089 | * The return value of `check_changes_tracked_files()` can be: |
d4788af8 PSU |
1090 | * |
1091 | * < 0 if there was an error | |
1092 | * = 0 if there are no changes. | |
1093 | * > 0 if there are changes. | |
1094 | */ | |
7db9302d | 1095 | static int check_changes_tracked_files(const struct pathspec *ps) |
d4788af8 | 1096 | { |
d4788af8 PSU |
1097 | struct rev_info rev; |
1098 | struct object_id dummy; | |
7db9302d | 1099 | int ret = 0; |
d4788af8 PSU |
1100 | |
1101 | /* No initial commit. */ | |
d850b7a5 | 1102 | if (repo_get_oid(the_repository, "HEAD", &dummy)) |
d4788af8 PSU |
1103 | return -1; |
1104 | ||
07047d68 | 1105 | if (repo_read_index(the_repository) < 0) |
d4788af8 PSU |
1106 | return -1; |
1107 | ||
035c7de9 | 1108 | repo_init_revisions(the_repository, &rev, NULL); |
7db9302d | 1109 | copy_pathspec(&rev.prune_data, ps); |
d4788af8 PSU |
1110 | |
1111 | rev.diffopt.flags.quick = 1; | |
1112 | rev.diffopt.flags.ignore_submodules = 1; | |
1113 | rev.abbrev = 0; | |
1114 | ||
1115 | add_head_to_pending(&rev); | |
1116 | diff_setup_done(&rev.diffopt); | |
1117 | ||
25bd3acd | 1118 | run_diff_index(&rev, DIFF_INDEX_CACHED); |
5cc6b2d7 | 1119 | if (diff_result_code(&rev.diffopt)) { |
7db9302d TG |
1120 | ret = 1; |
1121 | goto done; | |
1122 | } | |
d4788af8 | 1123 | |
25bd3acd | 1124 | run_diff_files(&rev, 0); |
5cc6b2d7 | 1125 | if (diff_result_code(&rev.diffopt)) { |
7db9302d TG |
1126 | ret = 1; |
1127 | goto done; | |
1128 | } | |
d4788af8 | 1129 | |
7db9302d | 1130 | done: |
1878b5ed | 1131 | release_revisions(&rev); |
7db9302d | 1132 | return ret; |
ef0f0b45 PSU |
1133 | } |
1134 | ||
1135 | /* | |
1136 | * The function will fill `untracked_files` with the names of untracked files | |
1137 | * It will return 1 if there were any changes and 0 if there were not. | |
1138 | */ | |
7db9302d | 1139 | static int check_changes(const struct pathspec *ps, int include_untracked, |
ef0f0b45 PSU |
1140 | struct strbuf *untracked_files) |
1141 | { | |
1142 | int ret = 0; | |
1143 | if (check_changes_tracked_files(ps)) | |
1144 | ret = 1; | |
1145 | ||
d4788af8 | 1146 | if (include_untracked && get_untracked_files(ps, include_untracked, |
ef0f0b45 PSU |
1147 | untracked_files)) |
1148 | ret = 1; | |
d4788af8 | 1149 | |
ef0f0b45 | 1150 | return ret; |
d4788af8 PSU |
1151 | } |
1152 | ||
1153 | static int save_untracked_files(struct stash_info *info, struct strbuf *msg, | |
1154 | struct strbuf files) | |
1155 | { | |
1156 | int ret = 0; | |
1157 | struct strbuf untracked_msg = STRBUF_INIT; | |
d4788af8 | 1158 | struct child_process cp_upd_index = CHILD_PROCESS_INIT; |
6269f8ea | 1159 | struct index_state istate = INDEX_STATE_INIT(the_repository); |
d4788af8 PSU |
1160 | |
1161 | cp_upd_index.git_cmd = 1; | |
22f9b7f3 | 1162 | strvec_pushl(&cp_upd_index.args, "update-index", "-z", "--add", |
f6d8942b | 1163 | "--remove", "--stdin", NULL); |
29fda24d | 1164 | strvec_pushf(&cp_upd_index.env, "GIT_INDEX_FILE=%s", |
d4788af8 PSU |
1165 | stash_index_path.buf); |
1166 | ||
1167 | strbuf_addf(&untracked_msg, "untracked files on %s\n", msg->buf); | |
1168 | if (pipe_command(&cp_upd_index, files.buf, files.len, NULL, 0, | |
1169 | NULL, 0)) { | |
1170 | ret = -1; | |
1171 | goto done; | |
1172 | } | |
1173 | ||
48ee24ab PSU |
1174 | if (write_index_as_tree(&info->u_tree, &istate, stash_index_path.buf, 0, |
1175 | NULL)) { | |
d4788af8 PSU |
1176 | ret = -1; |
1177 | goto done; | |
1178 | } | |
d4788af8 PSU |
1179 | |
1180 | if (commit_tree(untracked_msg.buf, untracked_msg.len, | |
1181 | &info->u_tree, NULL, &info->u_commit, NULL, NULL)) { | |
1182 | ret = -1; | |
1183 | goto done; | |
1184 | } | |
1185 | ||
1186 | done: | |
2f6b1eb7 | 1187 | release_index(&istate); |
d4788af8 | 1188 | strbuf_release(&untracked_msg); |
d4788af8 PSU |
1189 | remove_path(stash_index_path.buf); |
1190 | return ret; | |
1191 | } | |
1192 | ||
a8a6e068 SO |
1193 | static int stash_staged(struct stash_info *info, struct strbuf *out_patch, |
1194 | int quiet) | |
41a28eb6 SO |
1195 | { |
1196 | int ret = 0; | |
1197 | struct child_process cp_diff_tree = CHILD_PROCESS_INIT; | |
6269f8ea | 1198 | struct index_state istate = INDEX_STATE_INIT(the_repository); |
41a28eb6 SO |
1199 | |
1200 | if (write_index_as_tree(&info->w_tree, &istate, the_repository->index_file, | |
1201 | 0, NULL)) { | |
1202 | ret = -1; | |
1203 | goto done; | |
1204 | } | |
1205 | ||
1206 | cp_diff_tree.git_cmd = 1; | |
1207 | strvec_pushl(&cp_diff_tree.args, "diff-tree", "-p", "-U1", "HEAD", | |
1208 | oid_to_hex(&info->w_tree), "--", NULL); | |
1209 | if (pipe_command(&cp_diff_tree, NULL, 0, out_patch, 0, NULL, 0)) { | |
1210 | ret = -1; | |
1211 | goto done; | |
1212 | } | |
1213 | ||
1214 | if (!out_patch->len) { | |
1215 | if (!quiet) | |
1216 | fprintf_ln(stderr, _("No staged changes")); | |
1217 | ret = 1; | |
1218 | } | |
1219 | ||
1220 | done: | |
2f6b1eb7 | 1221 | release_index(&istate); |
41a28eb6 SO |
1222 | return ret; |
1223 | } | |
1224 | ||
7db9302d | 1225 | static int stash_patch(struct stash_info *info, const struct pathspec *ps, |
1ac528c0 | 1226 | struct strbuf *out_patch, int quiet) |
d4788af8 PSU |
1227 | { |
1228 | int ret = 0; | |
d4788af8 | 1229 | struct child_process cp_read_tree = CHILD_PROCESS_INIT; |
d4788af8 | 1230 | struct child_process cp_diff_tree = CHILD_PROCESS_INIT; |
6269f8ea | 1231 | struct index_state istate = INDEX_STATE_INIT(the_repository); |
6610e462 | 1232 | char *old_index_env = NULL, *old_repo_index_file; |
d4788af8 PSU |
1233 | |
1234 | remove_path(stash_index_path.buf); | |
1235 | ||
1236 | cp_read_tree.git_cmd = 1; | |
22f9b7f3 | 1237 | strvec_pushl(&cp_read_tree.args, "read-tree", "HEAD", NULL); |
29fda24d | 1238 | strvec_pushf(&cp_read_tree.env, "GIT_INDEX_FILE=%s", |
f6d8942b | 1239 | stash_index_path.buf); |
d4788af8 PSU |
1240 | if (run_command(&cp_read_tree)) { |
1241 | ret = -1; | |
1242 | goto done; | |
1243 | } | |
1244 | ||
1245 | /* Find out what the user wants. */ | |
6610e462 JS |
1246 | old_repo_index_file = the_repository->index_file; |
1247 | the_repository->index_file = stash_index_path.buf; | |
1248 | old_index_env = xstrdup_or_null(getenv(INDEX_ENVIRONMENT)); | |
1249 | setenv(INDEX_ENVIRONMENT, the_repository->index_file, 1); | |
1250 | ||
d21878f0 | 1251 | ret = !!run_add_p(the_repository, ADD_P_STASH, NULL, ps); |
6610e462 JS |
1252 | |
1253 | the_repository->index_file = old_repo_index_file; | |
1254 | if (old_index_env && *old_index_env) | |
1255 | setenv(INDEX_ENVIRONMENT, old_index_env, 1); | |
1256 | else | |
1257 | unsetenv(INDEX_ENVIRONMENT); | |
1258 | FREE_AND_NULL(old_index_env); | |
d4788af8 PSU |
1259 | |
1260 | /* State of the working tree. */ | |
48ee24ab PSU |
1261 | if (write_index_as_tree(&info->w_tree, &istate, stash_index_path.buf, 0, |
1262 | NULL)) { | |
d4788af8 PSU |
1263 | ret = -1; |
1264 | goto done; | |
1265 | } | |
1266 | ||
d4788af8 | 1267 | cp_diff_tree.git_cmd = 1; |
22f9b7f3 | 1268 | strvec_pushl(&cp_diff_tree.args, "diff-tree", "-p", "-U1", "HEAD", |
f6d8942b | 1269 | oid_to_hex(&info->w_tree), "--", NULL); |
d4788af8 PSU |
1270 | if (pipe_command(&cp_diff_tree, NULL, 0, out_patch, 0, NULL, 0)) { |
1271 | ret = -1; | |
1272 | goto done; | |
1273 | } | |
1274 | ||
1275 | if (!out_patch->len) { | |
1ac528c0 PSU |
1276 | if (!quiet) |
1277 | fprintf_ln(stderr, _("No changes selected")); | |
d4788af8 PSU |
1278 | ret = 1; |
1279 | } | |
1280 | ||
1281 | done: | |
2f6b1eb7 | 1282 | release_index(&istate); |
d4788af8 PSU |
1283 | remove_path(stash_index_path.buf); |
1284 | return ret; | |
1285 | } | |
1286 | ||
7db9302d | 1287 | static int stash_working_tree(struct stash_info *info, const struct pathspec *ps) |
d4788af8 PSU |
1288 | { |
1289 | int ret = 0; | |
1290 | struct rev_info rev; | |
1291 | struct child_process cp_upd_index = CHILD_PROCESS_INIT; | |
d4788af8 | 1292 | struct strbuf diff_output = STRBUF_INIT; |
6269f8ea | 1293 | struct index_state istate = INDEX_STATE_INIT(the_repository); |
d4788af8 | 1294 | |
035c7de9 | 1295 | repo_init_revisions(the_repository, &rev, NULL); |
7db9302d | 1296 | copy_pathspec(&rev.prune_data, ps); |
d4788af8 PSU |
1297 | |
1298 | set_alternate_index_output(stash_index_path.buf); | |
1299 | if (reset_tree(&info->i_tree, 0, 0)) { | |
1300 | ret = -1; | |
1301 | goto done; | |
1302 | } | |
1303 | set_alternate_index_output(NULL); | |
1304 | ||
d4788af8 PSU |
1305 | rev.diffopt.output_format = DIFF_FORMAT_CALLBACK; |
1306 | rev.diffopt.format_callback = add_diff_to_buf; | |
1307 | rev.diffopt.format_callback_data = &diff_output; | |
1308 | ||
07047d68 | 1309 | if (repo_read_index_preload(the_repository, &rev.diffopt.pathspec, 0) < 0) { |
d4788af8 PSU |
1310 | ret = -1; |
1311 | goto done; | |
1312 | } | |
1313 | ||
1314 | add_pending_object(&rev, parse_object(the_repository, &info->b_commit), | |
1315 | ""); | |
25bd3acd | 1316 | run_diff_index(&rev, 0); |
d4788af8 PSU |
1317 | |
1318 | cp_upd_index.git_cmd = 1; | |
22f9b7f3 | 1319 | strvec_pushl(&cp_upd_index.args, "update-index", |
f6d8942b JK |
1320 | "--ignore-skip-worktree-entries", |
1321 | "-z", "--add", "--remove", "--stdin", NULL); | |
29fda24d | 1322 | strvec_pushf(&cp_upd_index.env, "GIT_INDEX_FILE=%s", |
f6d8942b | 1323 | stash_index_path.buf); |
d4788af8 PSU |
1324 | |
1325 | if (pipe_command(&cp_upd_index, diff_output.buf, diff_output.len, | |
1326 | NULL, 0, NULL, 0)) { | |
1327 | ret = -1; | |
1328 | goto done; | |
1329 | } | |
1330 | ||
48ee24ab PSU |
1331 | if (write_index_as_tree(&info->w_tree, &istate, stash_index_path.buf, 0, |
1332 | NULL)) { | |
d4788af8 PSU |
1333 | ret = -1; |
1334 | goto done; | |
1335 | } | |
1336 | ||
d4788af8 | 1337 | done: |
2f6b1eb7 | 1338 | release_index(&istate); |
1878b5ed | 1339 | release_revisions(&rev); |
d4788af8 PSU |
1340 | strbuf_release(&diff_output); |
1341 | remove_path(stash_index_path.buf); | |
1342 | return ret; | |
1343 | } | |
1344 | ||
7db9302d | 1345 | static int do_create_stash(const struct pathspec *ps, struct strbuf *stash_msg_buf, |
41a28eb6 | 1346 | int include_untracked, int patch_mode, int only_staged, |
1ac528c0 PSU |
1347 | struct stash_info *info, struct strbuf *patch, |
1348 | int quiet) | |
d4788af8 PSU |
1349 | { |
1350 | int ret = 0; | |
1351 | int flags = 0; | |
1352 | int untracked_commit_option = 0; | |
1353 | const char *head_short_sha1 = NULL; | |
1354 | const char *branch_ref = NULL; | |
1355 | const char *branch_name = "(no branch)"; | |
1356 | struct commit *head_commit = NULL; | |
1357 | struct commit_list *parents = NULL; | |
1358 | struct strbuf msg = STRBUF_INIT; | |
1359 | struct strbuf commit_tree_label = STRBUF_INIT; | |
1360 | struct strbuf untracked_files = STRBUF_INIT; | |
d4788af8 PSU |
1361 | |
1362 | prepare_fallback_ident("git stash", "git@stash"); | |
1363 | ||
07047d68 ÆAB |
1364 | repo_read_index_preload(the_repository, NULL, 0); |
1365 | if (repo_refresh_and_write_index(the_repository, REFRESH_QUIET, 0, 0, | |
1366 | NULL, NULL, NULL) < 0) { | |
d2058cb2 | 1367 | ret = error(_("could not write index")); |
34933d0e TG |
1368 | goto done; |
1369 | } | |
d4788af8 | 1370 | |
d850b7a5 | 1371 | if (repo_get_oid(the_repository, "HEAD", &info->b_commit)) { |
1ac528c0 PSU |
1372 | if (!quiet) |
1373 | fprintf_ln(stderr, _("You do not have " | |
1374 | "the initial commit yet")); | |
d4788af8 PSU |
1375 | ret = -1; |
1376 | goto done; | |
1377 | } else { | |
1378 | head_commit = lookup_commit(the_repository, &info->b_commit); | |
1379 | } | |
1380 | ||
ef0f0b45 | 1381 | if (!check_changes(ps, include_untracked, &untracked_files)) { |
d4788af8 PSU |
1382 | ret = 1; |
1383 | goto done; | |
1384 | } | |
1385 | ||
1386 | branch_ref = resolve_ref_unsafe("HEAD", 0, NULL, &flags); | |
1387 | if (flags & REF_ISSYMREF) | |
ceaf037f | 1388 | skip_prefix(branch_ref, "refs/heads/", &branch_name); |
d850b7a5 ÆAB |
1389 | head_short_sha1 = repo_find_unique_abbrev(the_repository, |
1390 | &head_commit->object.oid, | |
1391 | DEFAULT_ABBREV); | |
d4788af8 PSU |
1392 | strbuf_addf(&msg, "%s: %s ", branch_name, head_short_sha1); |
1393 | pp_commit_easy(CMIT_FMT_ONELINE, head_commit, &msg); | |
1394 | ||
1395 | strbuf_addf(&commit_tree_label, "index on %s\n", msg.buf); | |
1396 | commit_list_insert(head_commit, &parents); | |
99370863 ÆAB |
1397 | if (write_index_as_tree(&info->i_tree, &the_index, get_index_file(), 0, |
1398 | NULL) || | |
d4788af8 PSU |
1399 | commit_tree(commit_tree_label.buf, commit_tree_label.len, |
1400 | &info->i_tree, parents, &info->i_commit, NULL, NULL)) { | |
1ac528c0 PSU |
1401 | if (!quiet) |
1402 | fprintf_ln(stderr, _("Cannot save the current " | |
1403 | "index state")); | |
d4788af8 PSU |
1404 | ret = -1; |
1405 | goto done; | |
1406 | } | |
1407 | ||
ef0f0b45 | 1408 | if (include_untracked) { |
d4788af8 | 1409 | if (save_untracked_files(info, &msg, untracked_files)) { |
1ac528c0 PSU |
1410 | if (!quiet) |
1411 | fprintf_ln(stderr, _("Cannot save " | |
1412 | "the untracked files")); | |
d4788af8 PSU |
1413 | ret = -1; |
1414 | goto done; | |
1415 | } | |
1416 | untracked_commit_option = 1; | |
1417 | } | |
1418 | if (patch_mode) { | |
1ac528c0 | 1419 | ret = stash_patch(info, ps, patch, quiet); |
d4788af8 | 1420 | if (ret < 0) { |
1ac528c0 PSU |
1421 | if (!quiet) |
1422 | fprintf_ln(stderr, _("Cannot save the current " | |
1423 | "worktree state")); | |
d4788af8 PSU |
1424 | goto done; |
1425 | } else if (ret > 0) { | |
1426 | goto done; | |
1427 | } | |
41a28eb6 | 1428 | } else if (only_staged) { |
a8a6e068 | 1429 | ret = stash_staged(info, patch, quiet); |
41a28eb6 SO |
1430 | if (ret < 0) { |
1431 | if (!quiet) | |
1432 | fprintf_ln(stderr, _("Cannot save the current " | |
1433 | "staged state")); | |
1434 | goto done; | |
1435 | } else if (ret > 0) { | |
1436 | goto done; | |
1437 | } | |
d4788af8 PSU |
1438 | } else { |
1439 | if (stash_working_tree(info, ps)) { | |
1ac528c0 PSU |
1440 | if (!quiet) |
1441 | fprintf_ln(stderr, _("Cannot save the current " | |
1442 | "worktree state")); | |
d4788af8 PSU |
1443 | ret = -1; |
1444 | goto done; | |
1445 | } | |
1446 | } | |
1447 | ||
1448 | if (!stash_msg_buf->len) | |
1449 | strbuf_addf(stash_msg_buf, "WIP on %s", msg.buf); | |
1450 | else | |
1451 | strbuf_insertf(stash_msg_buf, 0, "On %s: ", branch_name); | |
1452 | ||
1453 | /* | |
1454 | * `parents` will be empty after calling `commit_tree()`, so there is | |
1455 | * no need to call `free_commit_list()` | |
1456 | */ | |
1457 | parents = NULL; | |
1458 | if (untracked_commit_option) | |
1459 | commit_list_insert(lookup_commit(the_repository, | |
1460 | &info->u_commit), | |
1461 | &parents); | |
1462 | commit_list_insert(lookup_commit(the_repository, &info->i_commit), | |
1463 | &parents); | |
1464 | commit_list_insert(head_commit, &parents); | |
1465 | ||
1466 | if (commit_tree(stash_msg_buf->buf, stash_msg_buf->len, &info->w_tree, | |
1467 | parents, &info->w_commit, NULL, NULL)) { | |
1ac528c0 PSU |
1468 | if (!quiet) |
1469 | fprintf_ln(stderr, _("Cannot record " | |
1470 | "working tree state")); | |
d4788af8 PSU |
1471 | ret = -1; |
1472 | goto done; | |
1473 | } | |
1474 | ||
1475 | done: | |
1476 | strbuf_release(&commit_tree_label); | |
1477 | strbuf_release(&msg); | |
1478 | strbuf_release(&untracked_files); | |
1479 | return ret; | |
1480 | } | |
1481 | ||
5247b762 | 1482 | static int create_stash(int argc, const char **argv, const char *prefix UNUSED) |
d4788af8 | 1483 | { |
5e480176 | 1484 | int ret; |
d4788af8 | 1485 | struct strbuf stash_msg_buf = STRBUF_INIT; |
5e480176 | 1486 | struct stash_info info = STASH_INFO_INIT; |
d4788af8 | 1487 | struct pathspec ps; |
d4788af8 | 1488 | |
40af1468 PSU |
1489 | /* Starting with argv[1], since argv[0] is "create" */ |
1490 | strbuf_join_argv(&stash_msg_buf, argc - 1, ++argv, ' '); | |
d4788af8 PSU |
1491 | |
1492 | memset(&ps, 0, sizeof(ps)); | |
7db9302d | 1493 | if (!check_changes_tracked_files(&ps)) |
ef0f0b45 PSU |
1494 | return 0; |
1495 | ||
41a28eb6 | 1496 | ret = do_create_stash(&ps, &stash_msg_buf, 0, 0, 0, &info, |
1ac528c0 | 1497 | NULL, 0); |
d4788af8 PSU |
1498 | if (!ret) |
1499 | printf_ln("%s", oid_to_hex(&info.w_commit)); | |
1500 | ||
5e480176 | 1501 | free_stash_info(&info); |
d4788af8 | 1502 | strbuf_release(&stash_msg_buf); |
ef0f0b45 | 1503 | return ret; |
d4788af8 PSU |
1504 | } |
1505 | ||
7db9302d | 1506 | static int do_push_stash(const struct pathspec *ps, const char *stash_msg, int quiet, |
41a28eb6 | 1507 | int keep_index, int patch_mode, int include_untracked, int only_staged) |
d553f538 PSU |
1508 | { |
1509 | int ret = 0; | |
5e480176 | 1510 | struct stash_info info = STASH_INFO_INIT; |
d553f538 PSU |
1511 | struct strbuf patch = STRBUF_INIT; |
1512 | struct strbuf stash_msg_buf = STRBUF_INIT; | |
ef0f0b45 | 1513 | struct strbuf untracked_files = STRBUF_INIT; |
d553f538 PSU |
1514 | |
1515 | if (patch_mode && keep_index == -1) | |
1516 | keep_index = 1; | |
1517 | ||
1518 | if (patch_mode && include_untracked) { | |
1519 | fprintf_ln(stderr, _("Can't use --patch and --include-untracked" | |
1520 | " or --all at the same time")); | |
1521 | ret = -1; | |
1522 | goto done; | |
1523 | } | |
1524 | ||
41a28eb6 SO |
1525 | /* --patch overrides --staged */ |
1526 | if (patch_mode) | |
1527 | only_staged = 0; | |
1528 | ||
1529 | if (only_staged && include_untracked) { | |
1530 | fprintf_ln(stderr, _("Can't use --staged and --include-untracked" | |
1531 | " or --all at the same time")); | |
1532 | ret = -1; | |
1533 | goto done; | |
1534 | } | |
1535 | ||
07047d68 | 1536 | repo_read_index_preload(the_repository, NULL, 0); |
7db9302d | 1537 | if (!include_untracked && ps->nr) { |
d553f538 | 1538 | int i; |
7db9302d | 1539 | char *ps_matched = xcalloc(ps->nr, 1); |
d553f538 | 1540 | |
a0291201 DS |
1541 | /* TODO: audit for interaction with sparse-index. */ |
1542 | ensure_full_index(&the_index); | |
dc594180 ÆAB |
1543 | for (i = 0; i < the_index.cache_nr; i++) |
1544 | ce_path_match(&the_index, the_index.cache[i], ps, | |
d553f538 PSU |
1545 | ps_matched); |
1546 | ||
42844973 | 1547 | if (report_path_error(ps_matched, ps)) { |
d553f538 PSU |
1548 | fprintf_ln(stderr, _("Did you forget to 'git add'?")); |
1549 | ret = -1; | |
1550 | free(ps_matched); | |
1551 | goto done; | |
1552 | } | |
1553 | free(ps_matched); | |
1554 | } | |
1555 | ||
07047d68 ÆAB |
1556 | if (repo_refresh_and_write_index(the_repository, REFRESH_QUIET, 0, 0, |
1557 | NULL, NULL, NULL)) { | |
d2058cb2 | 1558 | ret = error(_("could not write index")); |
d553f538 PSU |
1559 | goto done; |
1560 | } | |
1561 | ||
ef0f0b45 | 1562 | if (!check_changes(ps, include_untracked, &untracked_files)) { |
d553f538 PSU |
1563 | if (!quiet) |
1564 | printf_ln(_("No local changes to save")); | |
1565 | goto done; | |
1566 | } | |
1567 | ||
1568 | if (!reflog_exists(ref_stash) && do_clear_stash()) { | |
1569 | ret = -1; | |
1ac528c0 PSU |
1570 | if (!quiet) |
1571 | fprintf_ln(stderr, _("Cannot initialize stash")); | |
d553f538 PSU |
1572 | goto done; |
1573 | } | |
1574 | ||
1575 | if (stash_msg) | |
1576 | strbuf_addstr(&stash_msg_buf, stash_msg); | |
41a28eb6 | 1577 | if (do_create_stash(ps, &stash_msg_buf, include_untracked, patch_mode, only_staged, |
1ac528c0 | 1578 | &info, &patch, quiet)) { |
d553f538 PSU |
1579 | ret = -1; |
1580 | goto done; | |
1581 | } | |
1582 | ||
1583 | if (do_store_stash(&info.w_commit, stash_msg_buf.buf, 1)) { | |
1584 | ret = -1; | |
1ac528c0 PSU |
1585 | if (!quiet) |
1586 | fprintf_ln(stderr, _("Cannot save the current status")); | |
d553f538 PSU |
1587 | goto done; |
1588 | } | |
1589 | ||
1ac528c0 PSU |
1590 | if (!quiet) |
1591 | printf_ln(_("Saved working directory and index state %s"), | |
1592 | stash_msg_buf.buf); | |
d553f538 | 1593 | |
41a28eb6 | 1594 | if (!(patch_mode || only_staged)) { |
7db9302d | 1595 | if (include_untracked && !ps->nr) { |
d553f538 PSU |
1596 | struct child_process cp = CHILD_PROCESS_INIT; |
1597 | ||
1598 | cp.git_cmd = 1; | |
ff5b7913 | 1599 | if (startup_info->original_cwd) { |
0fce211c | 1600 | cp.dir = startup_info->original_cwd; |
29fda24d | 1601 | strvec_pushf(&cp.env, "%s=%s", |
ff5b7913 EN |
1602 | GIT_WORK_TREE_ENVIRONMENT, |
1603 | the_repository->worktree); | |
1604 | } | |
22f9b7f3 | 1605 | strvec_pushl(&cp.args, "clean", "--force", |
0fce211c | 1606 | "--quiet", "-d", ":/", NULL); |
d553f538 | 1607 | if (include_untracked == INCLUDE_ALL_FILES) |
22f9b7f3 | 1608 | strvec_push(&cp.args, "-x"); |
d553f538 PSU |
1609 | if (run_command(&cp)) { |
1610 | ret = -1; | |
1611 | goto done; | |
1612 | } | |
1613 | } | |
07047d68 | 1614 | discard_index(&the_index); |
7db9302d | 1615 | if (ps->nr) { |
d553f538 PSU |
1616 | struct child_process cp_add = CHILD_PROCESS_INIT; |
1617 | struct child_process cp_diff = CHILD_PROCESS_INIT; | |
1618 | struct child_process cp_apply = CHILD_PROCESS_INIT; | |
1619 | struct strbuf out = STRBUF_INIT; | |
1620 | ||
1621 | cp_add.git_cmd = 1; | |
22f9b7f3 | 1622 | strvec_push(&cp_add.args, "add"); |
d553f538 | 1623 | if (!include_untracked) |
22f9b7f3 | 1624 | strvec_push(&cp_add.args, "-u"); |
d553f538 | 1625 | if (include_untracked == INCLUDE_ALL_FILES) |
22f9b7f3 JK |
1626 | strvec_push(&cp_add.args, "--force"); |
1627 | strvec_push(&cp_add.args, "--"); | |
d553f538 PSU |
1628 | add_pathspecs(&cp_add.args, ps); |
1629 | if (run_command(&cp_add)) { | |
1630 | ret = -1; | |
1631 | goto done; | |
1632 | } | |
1633 | ||
1634 | cp_diff.git_cmd = 1; | |
22f9b7f3 | 1635 | strvec_pushl(&cp_diff.args, "diff-index", "-p", |
f6d8942b JK |
1636 | "--cached", "--binary", "HEAD", "--", |
1637 | NULL); | |
d553f538 PSU |
1638 | add_pathspecs(&cp_diff.args, ps); |
1639 | if (pipe_command(&cp_diff, NULL, 0, &out, 0, NULL, 0)) { | |
1640 | ret = -1; | |
1641 | goto done; | |
1642 | } | |
1643 | ||
1644 | cp_apply.git_cmd = 1; | |
22f9b7f3 | 1645 | strvec_pushl(&cp_apply.args, "apply", "--index", |
f6d8942b | 1646 | "-R", NULL); |
d553f538 PSU |
1647 | if (pipe_command(&cp_apply, out.buf, out.len, NULL, 0, |
1648 | NULL, 0)) { | |
1649 | ret = -1; | |
1650 | goto done; | |
1651 | } | |
1652 | } else { | |
1653 | struct child_process cp = CHILD_PROCESS_INIT; | |
1654 | cp.git_cmd = 1; | |
94b7f156 | 1655 | /* BUG: this nukes untracked files in the way */ |
22f9b7f3 | 1656 | strvec_pushl(&cp.args, "reset", "--hard", "-q", |
f6d8942b | 1657 | "--no-recurse-submodules", NULL); |
d553f538 PSU |
1658 | if (run_command(&cp)) { |
1659 | ret = -1; | |
1660 | goto done; | |
1661 | } | |
1662 | } | |
1663 | ||
1664 | if (keep_index == 1 && !is_null_oid(&info.i_tree)) { | |
b932f6a5 | 1665 | struct child_process cp = CHILD_PROCESS_INIT; |
d553f538 | 1666 | |
b932f6a5 | 1667 | cp.git_cmd = 1; |
22f9b7f3 | 1668 | strvec_pushl(&cp.args, "checkout", "--no-overlay", |
f6d8942b | 1669 | oid_to_hex(&info.i_tree), "--", NULL); |
b932f6a5 | 1670 | if (!ps->nr) |
22f9b7f3 | 1671 | strvec_push(&cp.args, ":/"); |
b932f6a5 TG |
1672 | else |
1673 | add_pathspecs(&cp.args, ps); | |
1674 | if (run_command(&cp)) { | |
d553f538 PSU |
1675 | ret = -1; |
1676 | goto done; | |
1677 | } | |
1678 | } | |
1679 | goto done; | |
1680 | } else { | |
1681 | struct child_process cp = CHILD_PROCESS_INIT; | |
1682 | ||
1683 | cp.git_cmd = 1; | |
22f9b7f3 | 1684 | strvec_pushl(&cp.args, "apply", "-R", NULL); |
d553f538 PSU |
1685 | |
1686 | if (pipe_command(&cp, patch.buf, patch.len, NULL, 0, NULL, 0)) { | |
1ac528c0 PSU |
1687 | if (!quiet) |
1688 | fprintf_ln(stderr, _("Cannot remove " | |
1689 | "worktree changes")); | |
d553f538 PSU |
1690 | ret = -1; |
1691 | goto done; | |
1692 | } | |
1693 | ||
1694 | if (keep_index < 1) { | |
1695 | struct child_process cp = CHILD_PROCESS_INIT; | |
1696 | ||
1697 | cp.git_cmd = 1; | |
4b8b0f6f VD |
1698 | strvec_pushl(&cp.args, "reset", "-q", "--refresh", "--", |
1699 | NULL); | |
d553f538 PSU |
1700 | add_pathspecs(&cp.args, ps); |
1701 | if (run_command(&cp)) { | |
1702 | ret = -1; | |
1703 | goto done; | |
1704 | } | |
1705 | } | |
1706 | goto done; | |
1707 | } | |
1708 | ||
1709 | done: | |
b6046abc | 1710 | strbuf_release(&patch); |
5e480176 | 1711 | free_stash_info(&info); |
d553f538 | 1712 | strbuf_release(&stash_msg_buf); |
b6046abc | 1713 | strbuf_release(&untracked_files); |
d553f538 PSU |
1714 | return ret; |
1715 | } | |
1716 | ||
8c3713ce AM |
1717 | static int push_stash(int argc, const char **argv, const char *prefix, |
1718 | int push_assumed) | |
d553f538 | 1719 | { |
8c3713ce | 1720 | int force_assume = 0; |
d553f538 | 1721 | int keep_index = -1; |
41a28eb6 | 1722 | int only_staged = 0; |
d553f538 PSU |
1723 | int patch_mode = 0; |
1724 | int include_untracked = 0; | |
1725 | int quiet = 0; | |
8a98758a | 1726 | int pathspec_file_nul = 0; |
d553f538 | 1727 | const char *stash_msg = NULL; |
8a98758a | 1728 | const char *pathspec_from_file = NULL; |
d553f538 PSU |
1729 | struct pathspec ps; |
1730 | struct option options[] = { | |
1731 | OPT_BOOL('k', "keep-index", &keep_index, | |
1732 | N_("keep index")), | |
41a28eb6 SO |
1733 | OPT_BOOL('S', "staged", &only_staged, |
1734 | N_("stash staged changes only")), | |
d553f538 PSU |
1735 | OPT_BOOL('p', "patch", &patch_mode, |
1736 | N_("stash in patch mode")), | |
1737 | OPT__QUIET(&quiet, N_("quiet mode")), | |
1738 | OPT_BOOL('u', "include-untracked", &include_untracked, | |
1739 | N_("include untracked files in stash")), | |
1740 | OPT_SET_INT('a', "all", &include_untracked, | |
1741 | N_("include ignore files"), 2), | |
1742 | OPT_STRING('m', "message", &stash_msg, N_("message"), | |
1743 | N_("stash message")), | |
8a98758a AM |
1744 | OPT_PATHSPEC_FROM_FILE(&pathspec_from_file), |
1745 | OPT_PATHSPEC_FILE_NUL(&pathspec_file_nul), | |
d553f538 PSU |
1746 | OPT_END() |
1747 | }; | |
7615cf94 | 1748 | int ret; |
d553f538 | 1749 | |
8c3713ce AM |
1750 | if (argc) { |
1751 | force_assume = !strcmp(argv[0], "-p"); | |
40af1468 | 1752 | argc = parse_options(argc, argv, prefix, options, |
ca7990ce | 1753 | push_assumed ? git_stash_usage : |
40af1468 | 1754 | git_stash_push_usage, |
8c3713ce AM |
1755 | PARSE_OPT_KEEP_DASHDASH); |
1756 | } | |
1757 | ||
1758 | if (argc) { | |
1759 | if (!strcmp(argv[0], "--")) { | |
1760 | argc--; | |
1761 | argv++; | |
1762 | } else if (push_assumed && !force_assume) { | |
1763 | die("subcommand wasn't specified; 'push' can't be assumed due to unexpected token '%s'", | |
1764 | argv[0]); | |
1765 | } | |
1766 | } | |
d553f538 | 1767 | |
1366c78c JS |
1768 | parse_pathspec(&ps, 0, PATHSPEC_PREFER_FULL | PATHSPEC_PREFIX_ORIGIN, |
1769 | prefix, argv); | |
8a98758a AM |
1770 | |
1771 | if (pathspec_from_file) { | |
1772 | if (patch_mode) | |
12909b6b | 1773 | die(_("options '%s' and '%s' cannot be used together"), "--pathspec-from-file", "--patch"); |
8a98758a | 1774 | |
41a28eb6 | 1775 | if (only_staged) |
12909b6b | 1776 | die(_("options '%s' and '%s' cannot be used together"), "--pathspec-from-file", "--staged"); |
41a28eb6 | 1777 | |
8a98758a | 1778 | if (ps.nr) |
246cac85 | 1779 | die(_("'%s' and pathspec arguments cannot be used together"), "--pathspec-from-file"); |
8a98758a AM |
1780 | |
1781 | parse_pathspec_file(&ps, 0, | |
1782 | PATHSPEC_PREFER_FULL | PATHSPEC_PREFIX_ORIGIN, | |
1783 | prefix, pathspec_from_file, pathspec_file_nul); | |
1784 | } else if (pathspec_file_nul) { | |
6fa00ee8 | 1785 | die(_("the option '%s' requires '%s'"), "--pathspec-file-nul", "--pathspec-from-file"); |
8a98758a AM |
1786 | } |
1787 | ||
7615cf94 ÆAB |
1788 | ret = do_push_stash(&ps, stash_msg, quiet, keep_index, patch_mode, |
1789 | include_untracked, only_staged); | |
1790 | clear_pathspec(&ps); | |
1791 | return ret; | |
d553f538 PSU |
1792 | } |
1793 | ||
2b057d97 SG |
1794 | static int push_stash_unassumed(int argc, const char **argv, const char *prefix) |
1795 | { | |
1796 | return push_stash(argc, argv, prefix, 0); | |
1797 | } | |
1798 | ||
64fe9c26 PSU |
1799 | static int save_stash(int argc, const char **argv, const char *prefix) |
1800 | { | |
1801 | int keep_index = -1; | |
41a28eb6 | 1802 | int only_staged = 0; |
64fe9c26 PSU |
1803 | int patch_mode = 0; |
1804 | int include_untracked = 0; | |
1805 | int quiet = 0; | |
1806 | int ret = 0; | |
1807 | const char *stash_msg = NULL; | |
1808 | struct pathspec ps; | |
1809 | struct strbuf stash_msg_buf = STRBUF_INIT; | |
1810 | struct option options[] = { | |
1811 | OPT_BOOL('k', "keep-index", &keep_index, | |
1812 | N_("keep index")), | |
41a28eb6 SO |
1813 | OPT_BOOL('S', "staged", &only_staged, |
1814 | N_("stash staged changes only")), | |
64fe9c26 PSU |
1815 | OPT_BOOL('p', "patch", &patch_mode, |
1816 | N_("stash in patch mode")), | |
1817 | OPT__QUIET(&quiet, N_("quiet mode")), | |
1818 | OPT_BOOL('u', "include-untracked", &include_untracked, | |
1819 | N_("include untracked files in stash")), | |
1820 | OPT_SET_INT('a', "all", &include_untracked, | |
1821 | N_("include ignore files"), 2), | |
1822 | OPT_STRING('m', "message", &stash_msg, "message", | |
1823 | N_("stash message")), | |
1824 | OPT_END() | |
1825 | }; | |
1826 | ||
1827 | argc = parse_options(argc, argv, prefix, options, | |
40af1468 | 1828 | git_stash_save_usage, |
64fe9c26 PSU |
1829 | PARSE_OPT_KEEP_DASHDASH); |
1830 | ||
1831 | if (argc) | |
1832 | stash_msg = strbuf_join_argv(&stash_msg_buf, argc, argv, ' '); | |
1833 | ||
1834 | memset(&ps, 0, sizeof(ps)); | |
7db9302d | 1835 | ret = do_push_stash(&ps, stash_msg, quiet, keep_index, |
41a28eb6 | 1836 | patch_mode, include_untracked, only_staged); |
64fe9c26 PSU |
1837 | |
1838 | strbuf_release(&stash_msg_buf); | |
1839 | return ret; | |
1840 | } | |
1841 | ||
40af1468 | 1842 | int cmd_stash(int argc, const char **argv, const char *prefix) |
8a0fc8d1 JT |
1843 | { |
1844 | pid_t pid = getpid(); | |
1845 | const char *index_file; | |
22f9b7f3 | 1846 | struct strvec args = STRVEC_INIT; |
2b057d97 | 1847 | parse_opt_subcommand_fn *fn = NULL; |
8a0fc8d1 | 1848 | struct option options[] = { |
2b057d97 SG |
1849 | OPT_SUBCOMMAND("apply", &fn, apply_stash), |
1850 | OPT_SUBCOMMAND("clear", &fn, clear_stash), | |
1851 | OPT_SUBCOMMAND("drop", &fn, drop_stash), | |
1852 | OPT_SUBCOMMAND("pop", &fn, pop_stash), | |
1853 | OPT_SUBCOMMAND("branch", &fn, branch_stash), | |
1854 | OPT_SUBCOMMAND("list", &fn, list_stash), | |
1855 | OPT_SUBCOMMAND("show", &fn, show_stash), | |
1856 | OPT_SUBCOMMAND("store", &fn, store_stash), | |
1857 | OPT_SUBCOMMAND("create", &fn, create_stash), | |
1858 | OPT_SUBCOMMAND("push", &fn, push_stash_unassumed), | |
1859 | OPT_SUBCOMMAND_F("save", &fn, save_stash, PARSE_OPT_NOCOMPLETE), | |
8a0fc8d1 JT |
1860 | OPT_END() |
1861 | }; | |
1862 | ||
b0c7362d | 1863 | git_config(git_stash_config, NULL); |
90a46272 | 1864 | |
40af1468 | 1865 | argc = parse_options(argc, argv, prefix, options, git_stash_usage, |
2b057d97 SG |
1866 | PARSE_OPT_SUBCOMMAND_OPTIONAL | |
1867 | PARSE_OPT_KEEP_UNKNOWN_OPT | | |
1868 | PARSE_OPT_KEEP_DASHDASH); | |
8a0fc8d1 | 1869 | |
3a58792a VD |
1870 | prepare_repo_settings(the_repository); |
1871 | the_repository->settings.command_requires_full_index = 0; | |
1872 | ||
8a0fc8d1 JT |
1873 | index_file = get_index_file(); |
1874 | strbuf_addf(&stash_index_path, "%s.stash.%" PRIuMAX, index_file, | |
1875 | (uintmax_t)pid); | |
1876 | ||
2b057d97 SG |
1877 | if (fn) |
1878 | return !!fn(argc, argv, prefix); | |
1879 | else if (!argc) | |
1880 | return !!push_stash_unassumed(0, NULL, prefix); | |
40af1468 | 1881 | |
8c3713ce | 1882 | /* Assume 'stash push' */ |
22f9b7f3 JK |
1883 | strvec_push(&args, "push"); |
1884 | strvec_pushv(&args, argv); | |
d70a9eb6 | 1885 | return !!push_stash(args.nr, args.v, prefix, 1); |
8a0fc8d1 | 1886 | } |