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