]>
Commit | Line | Data |
---|---|---|
8a0fc8d1 JT |
1 | #include "builtin.h" |
2 | #include "config.h" | |
3 | #include "parse-options.h" | |
4 | #include "refs.h" | |
5 | #include "lockfile.h" | |
6 | #include "cache-tree.h" | |
7 | #include "unpack-trees.h" | |
8 | #include "merge-recursive.h" | |
9 | #include "argv-array.h" | |
10 | #include "run-command.h" | |
11 | #include "dir.h" | |
12 | #include "rerere.h" | |
dc7bd382 PSU |
13 | #include "revision.h" |
14 | #include "log-tree.h" | |
d4788af8 PSU |
15 | #include "diffcore.h" |
16 | ||
17 | #define INCLUDE_ALL_FILES 2 | |
8a0fc8d1 | 18 | |
40af1468 PSU |
19 | static const char * const git_stash_usage[] = { |
20 | N_("git stash list [<options>]"), | |
21 | N_("git stash show [<options>] [<stash>]"), | |
22 | N_("git stash drop [-q|--quiet] [<stash>]"), | |
23 | N_("git stash ( pop | apply ) [--index] [-q|--quiet] [<stash>]"), | |
24 | N_("git stash branch <branchname> [<stash>]"), | |
25 | N_("git stash clear"), | |
26 | N_("git stash [push [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\n" | |
d553f538 PSU |
27 | " [-u|--include-untracked] [-a|--all] [-m|--message <message>]\n" |
28 | " [--] [<pathspec>...]]"), | |
40af1468 | 29 | N_("git stash save [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\n" |
64fe9c26 | 30 | " [-u|--include-untracked] [-a|--all] [<message>]"), |
4e2dd393 JT |
31 | NULL |
32 | }; | |
33 | ||
40af1468 PSU |
34 | static const char * const git_stash_list_usage[] = { |
35 | N_("git stash list [<options>]"), | |
130f2697 PSU |
36 | NULL |
37 | }; | |
38 | ||
40af1468 PSU |
39 | static const char * const git_stash_show_usage[] = { |
40 | N_("git stash show [<options>] [<stash>]"), | |
dc7bd382 PSU |
41 | NULL |
42 | }; | |
43 | ||
40af1468 PSU |
44 | static const char * const git_stash_drop_usage[] = { |
45 | N_("git stash drop [-q|--quiet] [<stash>]"), | |
8a0fc8d1 JT |
46 | NULL |
47 | }; | |
48 | ||
40af1468 PSU |
49 | static const char * const git_stash_pop_usage[] = { |
50 | N_("git stash pop [--index] [-q|--quiet] [<stash>]"), | |
c4de61d7 JT |
51 | NULL |
52 | }; | |
53 | ||
40af1468 PSU |
54 | static const char * const git_stash_apply_usage[] = { |
55 | N_("git stash apply [--index] [-q|--quiet] [<stash>]"), | |
8a0fc8d1 JT |
56 | NULL |
57 | }; | |
58 | ||
40af1468 PSU |
59 | static const char * const git_stash_branch_usage[] = { |
60 | N_("git stash branch <branchname> [<stash>]"), | |
577c1995 JT |
61 | NULL |
62 | }; | |
63 | ||
40af1468 PSU |
64 | static const char * const git_stash_clear_usage[] = { |
65 | N_("git stash clear"), | |
4e2dd393 JT |
66 | NULL |
67 | }; | |
68 | ||
40af1468 PSU |
69 | static const char * const git_stash_store_usage[] = { |
70 | N_("git stash store [-m|--message <message>] [-q|--quiet] <commit>"), | |
41e0dd55 PSU |
71 | NULL |
72 | }; | |
73 | ||
40af1468 PSU |
74 | static const char * const git_stash_push_usage[] = { |
75 | N_("git stash [push [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\n" | |
d553f538 PSU |
76 | " [-u|--include-untracked] [-a|--all] [-m|--message <message>]\n" |
77 | " [--] [<pathspec>...]]"), | |
78 | NULL | |
79 | }; | |
80 | ||
40af1468 PSU |
81 | static const char * const git_stash_save_usage[] = { |
82 | N_("git stash save [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\n" | |
64fe9c26 PSU |
83 | " [-u|--include-untracked] [-a|--all] [<message>]"), |
84 | NULL | |
85 | }; | |
86 | ||
8a0fc8d1 JT |
87 | static const char *ref_stash = "refs/stash"; |
88 | static struct strbuf stash_index_path = STRBUF_INIT; | |
89 | ||
90 | /* | |
91 | * w_commit is set to the commit containing the working tree | |
92 | * b_commit is set to the base commit | |
93 | * i_commit is set to the commit containing the index tree | |
94 | * u_commit is set to the commit containing the untracked files tree | |
95 | * w_tree is set to the working tree | |
96 | * b_tree is set to the base tree | |
97 | * i_tree is set to the index tree | |
98 | * u_tree is set to the untracked files tree | |
99 | */ | |
100 | struct stash_info { | |
101 | struct object_id w_commit; | |
102 | struct object_id b_commit; | |
103 | struct object_id i_commit; | |
104 | struct object_id u_commit; | |
105 | struct object_id w_tree; | |
106 | struct object_id b_tree; | |
107 | struct object_id i_tree; | |
108 | struct object_id u_tree; | |
109 | struct strbuf revision; | |
110 | int is_stash_ref; | |
111 | int has_u; | |
112 | }; | |
113 | ||
114 | static void free_stash_info(struct stash_info *info) | |
115 | { | |
116 | strbuf_release(&info->revision); | |
117 | } | |
118 | ||
119 | static void assert_stash_like(struct stash_info *info, const char *revision) | |
120 | { | |
121 | if (get_oidf(&info->b_commit, "%s^1", revision) || | |
122 | get_oidf(&info->w_tree, "%s:", revision) || | |
123 | get_oidf(&info->b_tree, "%s^1:", revision) || | |
124 | get_oidf(&info->i_tree, "%s^2:", revision)) | |
125 | die(_("'%s' is not a stash-like commit"), revision); | |
126 | } | |
127 | ||
128 | static int get_stash_info(struct stash_info *info, int argc, const char **argv) | |
129 | { | |
130 | int ret; | |
131 | char *end_of_rev; | |
132 | char *expanded_ref; | |
133 | const char *revision; | |
134 | const char *commit = NULL; | |
135 | struct object_id dummy; | |
136 | struct strbuf symbolic = STRBUF_INIT; | |
137 | ||
138 | if (argc > 1) { | |
139 | int i; | |
140 | struct strbuf refs_msg = STRBUF_INIT; | |
141 | ||
142 | for (i = 0; i < argc; i++) | |
143 | strbuf_addf(&refs_msg, " '%s'", argv[i]); | |
144 | ||
145 | fprintf_ln(stderr, _("Too many revisions specified:%s"), | |
146 | refs_msg.buf); | |
147 | strbuf_release(&refs_msg); | |
148 | ||
149 | return -1; | |
150 | } | |
151 | ||
152 | if (argc == 1) | |
153 | commit = argv[0]; | |
154 | ||
155 | strbuf_init(&info->revision, 0); | |
156 | if (!commit) { | |
157 | if (!ref_exists(ref_stash)) { | |
158 | free_stash_info(info); | |
159 | fprintf_ln(stderr, _("No stash entries found.")); | |
160 | return -1; | |
161 | } | |
162 | ||
163 | strbuf_addf(&info->revision, "%s@{0}", ref_stash); | |
164 | } else if (strspn(commit, "0123456789") == strlen(commit)) { | |
165 | strbuf_addf(&info->revision, "%s@{%s}", ref_stash, commit); | |
166 | } else { | |
167 | strbuf_addstr(&info->revision, commit); | |
168 | } | |
169 | ||
170 | revision = info->revision.buf; | |
171 | ||
172 | if (get_oid(revision, &info->w_commit)) { | |
173 | error(_("%s is not a valid reference"), revision); | |
174 | free_stash_info(info); | |
175 | return -1; | |
176 | } | |
177 | ||
178 | assert_stash_like(info, revision); | |
179 | ||
180 | info->has_u = !get_oidf(&info->u_tree, "%s^3:", revision); | |
181 | ||
182 | end_of_rev = strchrnul(revision, '@'); | |
183 | strbuf_add(&symbolic, revision, end_of_rev - revision); | |
184 | ||
185 | ret = dwim_ref(symbolic.buf, symbolic.len, &dummy, &expanded_ref); | |
186 | strbuf_release(&symbolic); | |
187 | switch (ret) { | |
188 | case 0: /* Not found, but valid ref */ | |
189 | info->is_stash_ref = 0; | |
190 | break; | |
191 | case 1: | |
192 | info->is_stash_ref = !strcmp(expanded_ref, ref_stash); | |
193 | break; | |
194 | default: /* Invalid or ambiguous */ | |
195 | free_stash_info(info); | |
196 | } | |
197 | ||
198 | free(expanded_ref); | |
199 | return !(ret == 0 || ret == 1); | |
200 | } | |
201 | ||
4e2dd393 JT |
202 | static int do_clear_stash(void) |
203 | { | |
204 | struct object_id obj; | |
205 | if (get_oid(ref_stash, &obj)) | |
206 | return 0; | |
207 | ||
208 | return delete_ref(NULL, ref_stash, &obj, 0); | |
209 | } | |
210 | ||
211 | static int clear_stash(int argc, const char **argv, const char *prefix) | |
212 | { | |
213 | struct option options[] = { | |
214 | OPT_END() | |
215 | }; | |
216 | ||
217 | argc = parse_options(argc, argv, prefix, options, | |
40af1468 | 218 | git_stash_clear_usage, |
4e2dd393 JT |
219 | PARSE_OPT_STOP_AT_NON_OPTION); |
220 | ||
221 | if (argc) | |
222 | return error(_("git stash clear with parameters is " | |
223 | "unimplemented")); | |
224 | ||
225 | return do_clear_stash(); | |
226 | } | |
227 | ||
8a0fc8d1 JT |
228 | static int reset_tree(struct object_id *i_tree, int update, int reset) |
229 | { | |
230 | int nr_trees = 1; | |
231 | struct unpack_trees_options opts; | |
232 | struct tree_desc t[MAX_UNPACK_TREES]; | |
233 | struct tree *tree; | |
234 | struct lock_file lock_file = LOCK_INIT; | |
235 | ||
236 | read_cache_preload(NULL); | |
237 | if (refresh_cache(REFRESH_QUIET)) | |
238 | return -1; | |
239 | ||
240 | hold_locked_index(&lock_file, LOCK_DIE_ON_ERROR); | |
241 | ||
242 | memset(&opts, 0, sizeof(opts)); | |
243 | ||
244 | tree = parse_tree_indirect(i_tree); | |
245 | if (parse_tree(tree)) | |
246 | return -1; | |
247 | ||
248 | init_tree_desc(t, tree->buffer, tree->size); | |
249 | ||
250 | opts.head_idx = 1; | |
251 | opts.src_index = &the_index; | |
252 | opts.dst_index = &the_index; | |
253 | opts.merge = 1; | |
254 | opts.reset = reset; | |
255 | opts.update = update; | |
256 | opts.fn = oneway_merge; | |
257 | ||
258 | if (unpack_trees(nr_trees, t, &opts)) | |
259 | return -1; | |
260 | ||
261 | if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK)) | |
262 | return error(_("unable to write new index file")); | |
263 | ||
264 | return 0; | |
265 | } | |
266 | ||
267 | static int diff_tree_binary(struct strbuf *out, struct object_id *w_commit) | |
268 | { | |
269 | struct child_process cp = CHILD_PROCESS_INIT; | |
270 | const char *w_commit_hex = oid_to_hex(w_commit); | |
271 | ||
272 | /* | |
273 | * Diff-tree would not be very hard to replace with a native function, | |
274 | * however it should be done together with apply_cached. | |
275 | */ | |
276 | cp.git_cmd = 1; | |
277 | argv_array_pushl(&cp.args, "diff-tree", "--binary", NULL); | |
278 | argv_array_pushf(&cp.args, "%s^2^..%s^2", w_commit_hex, w_commit_hex); | |
279 | ||
280 | return pipe_command(&cp, NULL, 0, out, 0, NULL, 0); | |
281 | } | |
282 | ||
283 | static int apply_cached(struct strbuf *out) | |
284 | { | |
285 | struct child_process cp = CHILD_PROCESS_INIT; | |
286 | ||
287 | /* | |
288 | * Apply currently only reads either from stdin or a file, thus | |
289 | * apply_all_patches would have to be updated to optionally take a | |
290 | * buffer. | |
291 | */ | |
292 | cp.git_cmd = 1; | |
293 | argv_array_pushl(&cp.args, "apply", "--cached", NULL); | |
294 | return pipe_command(&cp, out->buf, out->len, NULL, 0, NULL, 0); | |
295 | } | |
296 | ||
297 | static int reset_head(void) | |
298 | { | |
299 | struct child_process cp = CHILD_PROCESS_INIT; | |
300 | ||
301 | /* | |
302 | * Reset is overall quite simple, however there is no current public | |
303 | * API for resetting. | |
304 | */ | |
305 | cp.git_cmd = 1; | |
306 | argv_array_push(&cp.args, "reset"); | |
307 | ||
308 | return run_command(&cp); | |
309 | } | |
310 | ||
d4788af8 PSU |
311 | static void add_diff_to_buf(struct diff_queue_struct *q, |
312 | struct diff_options *options, | |
313 | void *data) | |
314 | { | |
315 | int i; | |
316 | ||
317 | for (i = 0; i < q->nr; i++) { | |
318 | strbuf_addstr(data, q->queue[i]->one->path); | |
319 | ||
320 | /* NUL-terminate: will be fed to update-index -z */ | |
321 | strbuf_addch(data, '\0'); | |
322 | } | |
323 | } | |
324 | ||
8a0fc8d1 JT |
325 | static int get_newly_staged(struct strbuf *out, struct object_id *c_tree) |
326 | { | |
327 | struct child_process cp = CHILD_PROCESS_INIT; | |
328 | const char *c_tree_hex = oid_to_hex(c_tree); | |
329 | ||
330 | /* | |
331 | * diff-index is very similar to diff-tree above, and should be | |
332 | * converted together with update_index. | |
333 | */ | |
334 | cp.git_cmd = 1; | |
335 | argv_array_pushl(&cp.args, "diff-index", "--cached", "--name-only", | |
336 | "--diff-filter=A", NULL); | |
337 | argv_array_push(&cp.args, c_tree_hex); | |
338 | return pipe_command(&cp, NULL, 0, out, 0, NULL, 0); | |
339 | } | |
340 | ||
341 | static int update_index(struct strbuf *out) | |
342 | { | |
343 | struct child_process cp = CHILD_PROCESS_INIT; | |
344 | ||
345 | /* | |
346 | * Update-index is very complicated and may need to have a public | |
347 | * function exposed in order to remove this forking. | |
348 | */ | |
349 | cp.git_cmd = 1; | |
350 | argv_array_pushl(&cp.args, "update-index", "--add", "--stdin", NULL); | |
351 | return pipe_command(&cp, out->buf, out->len, NULL, 0, NULL, 0); | |
352 | } | |
353 | ||
354 | static int restore_untracked(struct object_id *u_tree) | |
355 | { | |
356 | int res; | |
357 | struct child_process cp = CHILD_PROCESS_INIT; | |
358 | ||
359 | /* | |
360 | * We need to run restore files from a given index, but without | |
361 | * affecting the current index, so we use GIT_INDEX_FILE with | |
362 | * run_command to fork processes that will not interfere. | |
363 | */ | |
364 | cp.git_cmd = 1; | |
365 | argv_array_push(&cp.args, "read-tree"); | |
366 | argv_array_push(&cp.args, oid_to_hex(u_tree)); | |
367 | argv_array_pushf(&cp.env_array, "GIT_INDEX_FILE=%s", | |
368 | stash_index_path.buf); | |
369 | if (run_command(&cp)) { | |
370 | remove_path(stash_index_path.buf); | |
371 | return -1; | |
372 | } | |
373 | ||
374 | child_process_init(&cp); | |
375 | cp.git_cmd = 1; | |
376 | argv_array_pushl(&cp.args, "checkout-index", "--all", NULL); | |
377 | argv_array_pushf(&cp.env_array, "GIT_INDEX_FILE=%s", | |
378 | stash_index_path.buf); | |
379 | ||
380 | res = run_command(&cp); | |
381 | remove_path(stash_index_path.buf); | |
382 | return res; | |
383 | } | |
384 | ||
385 | static int do_apply_stash(const char *prefix, struct stash_info *info, | |
386 | int index, int quiet) | |
387 | { | |
388 | int ret; | |
389 | int has_index = index; | |
390 | struct merge_options o; | |
391 | struct object_id c_tree; | |
392 | struct object_id index_tree; | |
393 | struct commit *result; | |
394 | const struct object_id *bases[1]; | |
395 | ||
396 | read_cache_preload(NULL); | |
397 | if (refresh_cache(REFRESH_QUIET)) | |
398 | return -1; | |
399 | ||
400 | if (write_cache_as_tree(&c_tree, 0, NULL)) | |
401 | return error(_("cannot apply a stash in the middle of a merge")); | |
402 | ||
403 | if (index) { | |
404 | if (oideq(&info->b_tree, &info->i_tree) || | |
405 | oideq(&c_tree, &info->i_tree)) { | |
406 | has_index = 0; | |
407 | } else { | |
408 | struct strbuf out = STRBUF_INIT; | |
409 | ||
410 | if (diff_tree_binary(&out, &info->w_commit)) { | |
411 | strbuf_release(&out); | |
412 | return error(_("could not generate diff %s^!."), | |
413 | oid_to_hex(&info->w_commit)); | |
414 | } | |
415 | ||
416 | ret = apply_cached(&out); | |
417 | strbuf_release(&out); | |
418 | if (ret) | |
419 | return error(_("conflicts in index." | |
420 | "Try without --index.")); | |
421 | ||
422 | discard_cache(); | |
423 | read_cache(); | |
424 | if (write_cache_as_tree(&index_tree, 0, NULL)) | |
425 | return error(_("could not save index tree")); | |
426 | ||
427 | reset_head(); | |
428 | } | |
429 | } | |
430 | ||
431 | if (info->has_u && restore_untracked(&info->u_tree)) | |
432 | return error(_("could not restore untracked files from stash")); | |
433 | ||
434 | init_merge_options(&o); | |
435 | ||
436 | o.branch1 = "Updated upstream"; | |
437 | o.branch2 = "Stashed changes"; | |
438 | ||
439 | if (oideq(&info->b_tree, &c_tree)) | |
440 | o.branch1 = "Version stash was based on"; | |
441 | ||
442 | if (quiet) | |
443 | o.verbosity = 0; | |
444 | ||
445 | if (o.verbosity >= 3) | |
446 | printf_ln(_("Merging %s with %s"), o.branch1, o.branch2); | |
447 | ||
448 | bases[0] = &info->b_tree; | |
449 | ||
450 | ret = merge_recursive_generic(&o, &c_tree, &info->w_tree, 1, bases, | |
451 | &result); | |
452 | if (ret) { | |
453 | rerere(0); | |
454 | ||
455 | if (index) | |
456 | fprintf_ln(stderr, _("Index was not unstashed.")); | |
457 | ||
458 | return ret; | |
459 | } | |
460 | ||
461 | if (has_index) { | |
462 | if (reset_tree(&index_tree, 0, 0)) | |
463 | return -1; | |
464 | } else { | |
465 | struct strbuf out = STRBUF_INIT; | |
466 | ||
467 | if (get_newly_staged(&out, &c_tree)) { | |
468 | strbuf_release(&out); | |
469 | return -1; | |
470 | } | |
471 | ||
472 | if (reset_tree(&c_tree, 0, 1)) { | |
473 | strbuf_release(&out); | |
474 | return -1; | |
475 | } | |
476 | ||
477 | ret = update_index(&out); | |
478 | strbuf_release(&out); | |
479 | if (ret) | |
480 | return -1; | |
481 | ||
482 | discard_cache(); | |
483 | } | |
484 | ||
485 | if (quiet) { | |
486 | if (refresh_cache(REFRESH_QUIET)) | |
487 | warning("could not refresh index"); | |
488 | } else { | |
489 | struct child_process cp = CHILD_PROCESS_INIT; | |
490 | ||
491 | /* | |
492 | * Status is quite simple and could be replaced with calls to | |
493 | * wt_status in the future, but it adds complexities which may | |
494 | * require more tests. | |
495 | */ | |
496 | cp.git_cmd = 1; | |
497 | cp.dir = prefix; | |
498 | argv_array_push(&cp.args, "status"); | |
499 | run_command(&cp); | |
500 | } | |
501 | ||
502 | return 0; | |
503 | } | |
504 | ||
505 | static int apply_stash(int argc, const char **argv, const char *prefix) | |
506 | { | |
507 | int ret; | |
508 | int quiet = 0; | |
509 | int index = 0; | |
510 | struct stash_info info; | |
511 | struct option options[] = { | |
512 | OPT__QUIET(&quiet, N_("be quiet, only report errors")), | |
513 | OPT_BOOL(0, "index", &index, | |
514 | N_("attempt to recreate the index")), | |
515 | OPT_END() | |
516 | }; | |
517 | ||
518 | argc = parse_options(argc, argv, prefix, options, | |
40af1468 | 519 | git_stash_apply_usage, 0); |
8a0fc8d1 JT |
520 | |
521 | if (get_stash_info(&info, argc, argv)) | |
522 | return -1; | |
523 | ||
524 | ret = do_apply_stash(prefix, &info, index, quiet); | |
525 | free_stash_info(&info); | |
526 | return ret; | |
527 | } | |
528 | ||
4e2dd393 JT |
529 | static int do_drop_stash(const char *prefix, struct stash_info *info, int quiet) |
530 | { | |
531 | int ret; | |
532 | struct child_process cp_reflog = CHILD_PROCESS_INIT; | |
533 | struct child_process cp = CHILD_PROCESS_INIT; | |
534 | ||
535 | /* | |
536 | * reflog does not provide a simple function for deleting refs. One will | |
537 | * need to be added to avoid implementing too much reflog code here | |
538 | */ | |
539 | ||
540 | cp_reflog.git_cmd = 1; | |
541 | argv_array_pushl(&cp_reflog.args, "reflog", "delete", "--updateref", | |
542 | "--rewrite", NULL); | |
543 | argv_array_push(&cp_reflog.args, info->revision.buf); | |
544 | ret = run_command(&cp_reflog); | |
545 | if (!ret) { | |
546 | if (!quiet) | |
547 | printf_ln(_("Dropped %s (%s)"), info->revision.buf, | |
548 | oid_to_hex(&info->w_commit)); | |
549 | } else { | |
550 | return error(_("%s: Could not drop stash entry"), | |
551 | info->revision.buf); | |
552 | } | |
553 | ||
554 | /* | |
555 | * This could easily be replaced by get_oid, but currently it will throw | |
556 | * a fatal error when a reflog is empty, which we can not recover from. | |
557 | */ | |
558 | cp.git_cmd = 1; | |
559 | /* Even though --quiet is specified, rev-parse still outputs the hash */ | |
560 | cp.no_stdout = 1; | |
561 | argv_array_pushl(&cp.args, "rev-parse", "--verify", "--quiet", NULL); | |
562 | argv_array_pushf(&cp.args, "%s@{0}", ref_stash); | |
563 | ret = run_command(&cp); | |
564 | ||
565 | /* do_clear_stash if we just dropped the last stash entry */ | |
566 | if (ret) | |
567 | do_clear_stash(); | |
568 | ||
569 | return 0; | |
570 | } | |
571 | ||
572 | static void assert_stash_ref(struct stash_info *info) | |
573 | { | |
574 | if (!info->is_stash_ref) { | |
575 | error(_("'%s' is not a stash reference"), info->revision.buf); | |
576 | free_stash_info(info); | |
577 | exit(1); | |
578 | } | |
579 | } | |
580 | ||
581 | static int drop_stash(int argc, const char **argv, const char *prefix) | |
582 | { | |
583 | int ret; | |
584 | int quiet = 0; | |
585 | struct stash_info info; | |
586 | struct option options[] = { | |
587 | OPT__QUIET(&quiet, N_("be quiet, only report errors")), | |
588 | OPT_END() | |
589 | }; | |
590 | ||
591 | argc = parse_options(argc, argv, prefix, options, | |
40af1468 | 592 | git_stash_drop_usage, 0); |
4e2dd393 JT |
593 | |
594 | if (get_stash_info(&info, argc, argv)) | |
595 | return -1; | |
596 | ||
597 | assert_stash_ref(&info); | |
598 | ||
599 | ret = do_drop_stash(prefix, &info, quiet); | |
600 | free_stash_info(&info); | |
601 | return ret; | |
602 | } | |
603 | ||
c4de61d7 JT |
604 | static int pop_stash(int argc, const char **argv, const char *prefix) |
605 | { | |
606 | int ret; | |
607 | int index = 0; | |
608 | int quiet = 0; | |
609 | struct stash_info info; | |
610 | struct option options[] = { | |
611 | OPT__QUIET(&quiet, N_("be quiet, only report errors")), | |
612 | OPT_BOOL(0, "index", &index, | |
613 | N_("attempt to recreate the index")), | |
614 | OPT_END() | |
615 | }; | |
616 | ||
617 | argc = parse_options(argc, argv, prefix, options, | |
40af1468 | 618 | git_stash_pop_usage, 0); |
c4de61d7 JT |
619 | |
620 | if (get_stash_info(&info, argc, argv)) | |
621 | return -1; | |
622 | ||
623 | assert_stash_ref(&info); | |
624 | if ((ret = do_apply_stash(prefix, &info, index, quiet))) | |
625 | printf_ln(_("The stash entry is kept in case " | |
626 | "you need it again.")); | |
627 | else | |
628 | ret = do_drop_stash(prefix, &info, quiet); | |
629 | ||
630 | free_stash_info(&info); | |
631 | return ret; | |
632 | } | |
633 | ||
577c1995 JT |
634 | static int branch_stash(int argc, const char **argv, const char *prefix) |
635 | { | |
636 | int ret; | |
637 | const char *branch = NULL; | |
638 | struct stash_info info; | |
639 | struct child_process cp = CHILD_PROCESS_INIT; | |
640 | struct option options[] = { | |
641 | OPT_END() | |
642 | }; | |
643 | ||
644 | argc = parse_options(argc, argv, prefix, options, | |
40af1468 | 645 | git_stash_branch_usage, 0); |
577c1995 JT |
646 | |
647 | if (!argc) { | |
648 | fprintf_ln(stderr, _("No branch name specified")); | |
649 | return -1; | |
650 | } | |
651 | ||
652 | branch = argv[0]; | |
653 | ||
654 | if (get_stash_info(&info, argc - 1, argv + 1)) | |
655 | return -1; | |
656 | ||
657 | cp.git_cmd = 1; | |
658 | argv_array_pushl(&cp.args, "checkout", "-b", NULL); | |
659 | argv_array_push(&cp.args, branch); | |
660 | argv_array_push(&cp.args, oid_to_hex(&info.b_commit)); | |
661 | ret = run_command(&cp); | |
662 | if (!ret) | |
663 | ret = do_apply_stash(prefix, &info, 1, 0); | |
664 | if (!ret && info.is_stash_ref) | |
665 | ret = do_drop_stash(prefix, &info, 0); | |
666 | ||
667 | free_stash_info(&info); | |
668 | ||
669 | return ret; | |
670 | } | |
671 | ||
130f2697 PSU |
672 | static int list_stash(int argc, const char **argv, const char *prefix) |
673 | { | |
674 | struct child_process cp = CHILD_PROCESS_INIT; | |
675 | struct option options[] = { | |
676 | OPT_END() | |
677 | }; | |
678 | ||
679 | argc = parse_options(argc, argv, prefix, options, | |
40af1468 | 680 | git_stash_list_usage, |
130f2697 PSU |
681 | PARSE_OPT_KEEP_UNKNOWN); |
682 | ||
683 | if (!ref_exists(ref_stash)) | |
684 | return 0; | |
685 | ||
686 | cp.git_cmd = 1; | |
687 | argv_array_pushl(&cp.args, "log", "--format=%gd: %gs", "-g", | |
688 | "--first-parent", "-m", NULL); | |
689 | argv_array_pushv(&cp.args, argv); | |
690 | argv_array_push(&cp.args, ref_stash); | |
691 | argv_array_push(&cp.args, "--"); | |
692 | return run_command(&cp); | |
693 | } | |
694 | ||
dc7bd382 PSU |
695 | static int show_stat = 1; |
696 | static int show_patch; | |
697 | ||
698 | static int git_stash_config(const char *var, const char *value, void *cb) | |
699 | { | |
700 | if (!strcmp(var, "stash.showstat")) { | |
701 | show_stat = git_config_bool(var, value); | |
702 | return 0; | |
703 | } | |
704 | if (!strcmp(var, "stash.showpatch")) { | |
705 | show_patch = git_config_bool(var, value); | |
706 | return 0; | |
707 | } | |
708 | return git_default_config(var, value, cb); | |
709 | } | |
710 | ||
711 | static int show_stash(int argc, const char **argv, const char *prefix) | |
712 | { | |
713 | int i; | |
714 | int opts = 0; | |
715 | int ret = 0; | |
716 | struct stash_info info; | |
717 | struct rev_info rev; | |
718 | struct argv_array stash_args = ARGV_ARRAY_INIT; | |
719 | struct option options[] = { | |
720 | OPT_END() | |
721 | }; | |
722 | ||
723 | init_diff_ui_defaults(); | |
724 | git_config(git_diff_ui_config, NULL); | |
725 | init_revisions(&rev, prefix); | |
726 | ||
727 | for (i = 1; i < argc; i++) { | |
728 | if (argv[i][0] != '-') | |
729 | argv_array_push(&stash_args, argv[i]); | |
730 | else | |
731 | opts++; | |
732 | } | |
733 | ||
734 | ret = get_stash_info(&info, stash_args.argc, stash_args.argv); | |
735 | argv_array_clear(&stash_args); | |
736 | if (ret) | |
737 | return -1; | |
738 | ||
739 | /* | |
740 | * The config settings are applied only if there are not passed | |
741 | * any options. | |
742 | */ | |
743 | if (!opts) { | |
744 | git_config(git_stash_config, NULL); | |
745 | if (show_stat) | |
746 | rev.diffopt.output_format = DIFF_FORMAT_DIFFSTAT; | |
747 | ||
748 | if (show_patch) | |
749 | rev.diffopt.output_format |= DIFF_FORMAT_PATCH; | |
750 | ||
751 | if (!show_stat && !show_patch) { | |
752 | free_stash_info(&info); | |
753 | return 0; | |
754 | } | |
755 | } | |
756 | ||
757 | argc = setup_revisions(argc, argv, &rev, NULL); | |
758 | if (argc > 1) { | |
759 | free_stash_info(&info); | |
40af1468 | 760 | usage_with_options(git_stash_show_usage, options); |
dc7bd382 PSU |
761 | } |
762 | ||
763 | rev.diffopt.flags.recursive = 1; | |
764 | setup_diff_pager(&rev.diffopt); | |
765 | diff_tree_oid(&info.b_commit, &info.w_commit, "", &rev.diffopt); | |
766 | log_tree_diff_flush(&rev); | |
767 | ||
768 | free_stash_info(&info); | |
769 | return diff_result_code(&rev.diffopt, 0); | |
770 | } | |
771 | ||
41e0dd55 PSU |
772 | static int do_store_stash(const struct object_id *w_commit, const char *stash_msg, |
773 | int quiet) | |
774 | { | |
775 | if (!stash_msg) | |
776 | stash_msg = "Created via \"git stash store\"."; | |
777 | ||
778 | if (update_ref(stash_msg, ref_stash, w_commit, NULL, | |
779 | REF_FORCE_CREATE_REFLOG, | |
780 | quiet ? UPDATE_REFS_QUIET_ON_ERR : | |
781 | UPDATE_REFS_MSG_ON_ERR)) { | |
782 | if (!quiet) { | |
783 | fprintf_ln(stderr, _("Cannot update %s with %s"), | |
784 | ref_stash, oid_to_hex(w_commit)); | |
785 | } | |
786 | return -1; | |
787 | } | |
788 | ||
789 | return 0; | |
790 | } | |
791 | ||
792 | static int store_stash(int argc, const char **argv, const char *prefix) | |
793 | { | |
794 | int quiet = 0; | |
795 | const char *stash_msg = NULL; | |
796 | struct object_id obj; | |
797 | struct object_context dummy; | |
798 | struct option options[] = { | |
799 | OPT__QUIET(&quiet, N_("be quiet")), | |
800 | OPT_STRING('m', "message", &stash_msg, "message", | |
801 | N_("stash message")), | |
802 | OPT_END() | |
803 | }; | |
804 | ||
805 | argc = parse_options(argc, argv, prefix, options, | |
40af1468 | 806 | git_stash_store_usage, |
41e0dd55 PSU |
807 | PARSE_OPT_KEEP_UNKNOWN); |
808 | ||
809 | if (argc != 1) { | |
810 | if (!quiet) | |
811 | fprintf_ln(stderr, _("\"git stash store\" requires one " | |
812 | "<commit> argument")); | |
813 | return -1; | |
814 | } | |
815 | ||
816 | if (get_oid_with_context(argv[0], quiet ? GET_OID_QUIETLY : 0, &obj, | |
817 | &dummy)) { | |
818 | if (!quiet) | |
819 | fprintf_ln(stderr, _("Cannot update %s with %s"), | |
820 | ref_stash, argv[0]); | |
821 | return -1; | |
822 | } | |
823 | ||
824 | return do_store_stash(&obj, stash_msg, quiet); | |
825 | } | |
826 | ||
d4788af8 PSU |
827 | static void add_pathspecs(struct argv_array *args, |
828 | struct pathspec ps) { | |
829 | int i; | |
830 | ||
831 | for (i = 0; i < ps.nr; i++) | |
832 | argv_array_push(args, ps.items[i].match); | |
833 | } | |
834 | ||
835 | /* | |
836 | * `untracked_files` will be filled with the names of untracked files. | |
837 | * The return value is: | |
838 | * | |
839 | * = 0 if there are not any untracked files | |
840 | * > 0 if there are untracked files | |
841 | */ | |
842 | static int get_untracked_files(struct pathspec ps, int include_untracked, | |
843 | struct strbuf *untracked_files) | |
844 | { | |
845 | int i; | |
846 | int max_len; | |
847 | int found = 0; | |
848 | char *seen; | |
849 | struct dir_struct dir; | |
850 | ||
851 | memset(&dir, 0, sizeof(dir)); | |
852 | if (include_untracked != INCLUDE_ALL_FILES) | |
853 | setup_standard_excludes(&dir); | |
854 | ||
855 | seen = xcalloc(ps.nr, 1); | |
856 | ||
857 | max_len = fill_directory(&dir, the_repository->index, &ps); | |
858 | for (i = 0; i < dir.nr; i++) { | |
859 | struct dir_entry *ent = dir.entries[i]; | |
860 | if (dir_path_match(&the_index, ent, &ps, max_len, seen)) { | |
861 | found++; | |
862 | strbuf_addstr(untracked_files, ent->name); | |
863 | /* NUL-terminate: will be fed to update-index -z */ | |
864 | strbuf_addch(untracked_files, '\0'); | |
865 | } | |
866 | free(ent); | |
867 | } | |
868 | ||
869 | free(seen); | |
870 | free(dir.entries); | |
871 | free(dir.ignored); | |
872 | clear_directory(&dir); | |
873 | return found; | |
874 | } | |
875 | ||
876 | /* | |
ef0f0b45 | 877 | * The return value of `check_changes_tracked_files()` can be: |
d4788af8 PSU |
878 | * |
879 | * < 0 if there was an error | |
880 | * = 0 if there are no changes. | |
881 | * > 0 if there are changes. | |
882 | */ | |
ef0f0b45 | 883 | static int check_changes_tracked_files(struct pathspec ps) |
d4788af8 PSU |
884 | { |
885 | int result; | |
886 | struct rev_info rev; | |
887 | struct object_id dummy; | |
d4788af8 PSU |
888 | |
889 | /* No initial commit. */ | |
890 | if (get_oid("HEAD", &dummy)) | |
891 | return -1; | |
892 | ||
893 | if (read_cache() < 0) | |
894 | return -1; | |
895 | ||
896 | init_revisions(&rev, NULL); | |
897 | rev.prune_data = ps; | |
898 | ||
899 | rev.diffopt.flags.quick = 1; | |
900 | rev.diffopt.flags.ignore_submodules = 1; | |
901 | rev.abbrev = 0; | |
902 | ||
903 | add_head_to_pending(&rev); | |
904 | diff_setup_done(&rev.diffopt); | |
905 | ||
906 | result = run_diff_index(&rev, 1); | |
907 | if (diff_result_code(&rev.diffopt, result)) | |
908 | return 1; | |
909 | ||
910 | object_array_clear(&rev.pending); | |
911 | result = run_diff_files(&rev, 0); | |
912 | if (diff_result_code(&rev.diffopt, result)) | |
913 | return 1; | |
914 | ||
ef0f0b45 PSU |
915 | return 0; |
916 | } | |
917 | ||
918 | /* | |
919 | * The function will fill `untracked_files` with the names of untracked files | |
920 | * It will return 1 if there were any changes and 0 if there were not. | |
921 | */ | |
922 | static int check_changes(struct pathspec ps, int include_untracked, | |
923 | struct strbuf *untracked_files) | |
924 | { | |
925 | int ret = 0; | |
926 | if (check_changes_tracked_files(ps)) | |
927 | ret = 1; | |
928 | ||
d4788af8 | 929 | if (include_untracked && get_untracked_files(ps, include_untracked, |
ef0f0b45 PSU |
930 | untracked_files)) |
931 | ret = 1; | |
d4788af8 | 932 | |
ef0f0b45 | 933 | return ret; |
d4788af8 PSU |
934 | } |
935 | ||
936 | static int save_untracked_files(struct stash_info *info, struct strbuf *msg, | |
937 | struct strbuf files) | |
938 | { | |
939 | int ret = 0; | |
940 | struct strbuf untracked_msg = STRBUF_INIT; | |
d4788af8 | 941 | struct child_process cp_upd_index = CHILD_PROCESS_INIT; |
48ee24ab | 942 | struct index_state istate = { NULL }; |
d4788af8 PSU |
943 | |
944 | cp_upd_index.git_cmd = 1; | |
945 | argv_array_pushl(&cp_upd_index.args, "update-index", "-z", "--add", | |
946 | "--remove", "--stdin", NULL); | |
947 | argv_array_pushf(&cp_upd_index.env_array, "GIT_INDEX_FILE=%s", | |
948 | stash_index_path.buf); | |
949 | ||
950 | strbuf_addf(&untracked_msg, "untracked files on %s\n", msg->buf); | |
951 | if (pipe_command(&cp_upd_index, files.buf, files.len, NULL, 0, | |
952 | NULL, 0)) { | |
953 | ret = -1; | |
954 | goto done; | |
955 | } | |
956 | ||
48ee24ab PSU |
957 | if (write_index_as_tree(&info->u_tree, &istate, stash_index_path.buf, 0, |
958 | NULL)) { | |
d4788af8 PSU |
959 | ret = -1; |
960 | goto done; | |
961 | } | |
d4788af8 PSU |
962 | |
963 | if (commit_tree(untracked_msg.buf, untracked_msg.len, | |
964 | &info->u_tree, NULL, &info->u_commit, NULL, NULL)) { | |
965 | ret = -1; | |
966 | goto done; | |
967 | } | |
968 | ||
969 | done: | |
48ee24ab | 970 | discard_index(&istate); |
d4788af8 | 971 | strbuf_release(&untracked_msg); |
d4788af8 PSU |
972 | remove_path(stash_index_path.buf); |
973 | return ret; | |
974 | } | |
975 | ||
976 | static int stash_patch(struct stash_info *info, struct pathspec ps, | |
1ac528c0 | 977 | struct strbuf *out_patch, int quiet) |
d4788af8 PSU |
978 | { |
979 | int ret = 0; | |
d4788af8 PSU |
980 | struct child_process cp_read_tree = CHILD_PROCESS_INIT; |
981 | struct child_process cp_add_i = CHILD_PROCESS_INIT; | |
d4788af8 | 982 | struct child_process cp_diff_tree = CHILD_PROCESS_INIT; |
48ee24ab | 983 | struct index_state istate = { NULL }; |
d4788af8 PSU |
984 | |
985 | remove_path(stash_index_path.buf); | |
986 | ||
987 | cp_read_tree.git_cmd = 1; | |
988 | argv_array_pushl(&cp_read_tree.args, "read-tree", "HEAD", NULL); | |
989 | argv_array_pushf(&cp_read_tree.env_array, "GIT_INDEX_FILE=%s", | |
990 | stash_index_path.buf); | |
991 | if (run_command(&cp_read_tree)) { | |
992 | ret = -1; | |
993 | goto done; | |
994 | } | |
995 | ||
996 | /* Find out what the user wants. */ | |
997 | cp_add_i.git_cmd = 1; | |
998 | argv_array_pushl(&cp_add_i.args, "add--interactive", "--patch=stash", | |
999 | "--", NULL); | |
1000 | add_pathspecs(&cp_add_i.args, ps); | |
1001 | argv_array_pushf(&cp_add_i.env_array, "GIT_INDEX_FILE=%s", | |
1002 | stash_index_path.buf); | |
1003 | if (run_command(&cp_add_i)) { | |
1004 | ret = -1; | |
1005 | goto done; | |
1006 | } | |
1007 | ||
1008 | /* State of the working tree. */ | |
48ee24ab PSU |
1009 | if (write_index_as_tree(&info->w_tree, &istate, stash_index_path.buf, 0, |
1010 | NULL)) { | |
d4788af8 PSU |
1011 | ret = -1; |
1012 | goto done; | |
1013 | } | |
1014 | ||
d4788af8 PSU |
1015 | cp_diff_tree.git_cmd = 1; |
1016 | argv_array_pushl(&cp_diff_tree.args, "diff-tree", "-p", "HEAD", | |
1017 | oid_to_hex(&info->w_tree), "--", NULL); | |
1018 | if (pipe_command(&cp_diff_tree, NULL, 0, out_patch, 0, NULL, 0)) { | |
1019 | ret = -1; | |
1020 | goto done; | |
1021 | } | |
1022 | ||
1023 | if (!out_patch->len) { | |
1ac528c0 PSU |
1024 | if (!quiet) |
1025 | fprintf_ln(stderr, _("No changes selected")); | |
d4788af8 PSU |
1026 | ret = 1; |
1027 | } | |
1028 | ||
1029 | done: | |
48ee24ab | 1030 | discard_index(&istate); |
d4788af8 PSU |
1031 | remove_path(stash_index_path.buf); |
1032 | return ret; | |
1033 | } | |
1034 | ||
1035 | static int stash_working_tree(struct stash_info *info, struct pathspec ps) | |
1036 | { | |
1037 | int ret = 0; | |
1038 | struct rev_info rev; | |
1039 | struct child_process cp_upd_index = CHILD_PROCESS_INIT; | |
d4788af8 | 1040 | struct strbuf diff_output = STRBUF_INIT; |
48ee24ab | 1041 | struct index_state istate = { NULL }; |
d4788af8 PSU |
1042 | |
1043 | init_revisions(&rev, NULL); | |
1044 | ||
1045 | set_alternate_index_output(stash_index_path.buf); | |
1046 | if (reset_tree(&info->i_tree, 0, 0)) { | |
1047 | ret = -1; | |
1048 | goto done; | |
1049 | } | |
1050 | set_alternate_index_output(NULL); | |
1051 | ||
1052 | rev.prune_data = ps; | |
1053 | rev.diffopt.output_format = DIFF_FORMAT_CALLBACK; | |
1054 | rev.diffopt.format_callback = add_diff_to_buf; | |
1055 | rev.diffopt.format_callback_data = &diff_output; | |
1056 | ||
1057 | if (read_cache_preload(&rev.diffopt.pathspec) < 0) { | |
1058 | ret = -1; | |
1059 | goto done; | |
1060 | } | |
1061 | ||
1062 | add_pending_object(&rev, parse_object(the_repository, &info->b_commit), | |
1063 | ""); | |
1064 | if (run_diff_index(&rev, 0)) { | |
1065 | ret = -1; | |
1066 | goto done; | |
1067 | } | |
1068 | ||
1069 | cp_upd_index.git_cmd = 1; | |
1070 | argv_array_pushl(&cp_upd_index.args, "update-index", "-z", "--add", | |
1071 | "--remove", "--stdin", NULL); | |
1072 | argv_array_pushf(&cp_upd_index.env_array, "GIT_INDEX_FILE=%s", | |
1073 | stash_index_path.buf); | |
1074 | ||
1075 | if (pipe_command(&cp_upd_index, diff_output.buf, diff_output.len, | |
1076 | NULL, 0, NULL, 0)) { | |
1077 | ret = -1; | |
1078 | goto done; | |
1079 | } | |
1080 | ||
48ee24ab PSU |
1081 | if (write_index_as_tree(&info->w_tree, &istate, stash_index_path.buf, 0, |
1082 | NULL)) { | |
d4788af8 PSU |
1083 | ret = -1; |
1084 | goto done; | |
1085 | } | |
1086 | ||
d4788af8 | 1087 | done: |
48ee24ab | 1088 | discard_index(&istate); |
d4788af8 | 1089 | UNLEAK(rev); |
d4788af8 PSU |
1090 | object_array_clear(&rev.pending); |
1091 | strbuf_release(&diff_output); | |
1092 | remove_path(stash_index_path.buf); | |
1093 | return ret; | |
1094 | } | |
1095 | ||
1096 | static int do_create_stash(struct pathspec ps, struct strbuf *stash_msg_buf, | |
1097 | int include_untracked, int patch_mode, | |
1ac528c0 PSU |
1098 | struct stash_info *info, struct strbuf *patch, |
1099 | int quiet) | |
d4788af8 PSU |
1100 | { |
1101 | int ret = 0; | |
1102 | int flags = 0; | |
1103 | int untracked_commit_option = 0; | |
1104 | const char *head_short_sha1 = NULL; | |
1105 | const char *branch_ref = NULL; | |
1106 | const char *branch_name = "(no branch)"; | |
1107 | struct commit *head_commit = NULL; | |
1108 | struct commit_list *parents = NULL; | |
1109 | struct strbuf msg = STRBUF_INIT; | |
1110 | struct strbuf commit_tree_label = STRBUF_INIT; | |
1111 | struct strbuf untracked_files = STRBUF_INIT; | |
d4788af8 PSU |
1112 | |
1113 | prepare_fallback_ident("git stash", "git@stash"); | |
1114 | ||
1115 | read_cache_preload(NULL); | |
1116 | refresh_cache(REFRESH_QUIET); | |
1117 | ||
1118 | if (get_oid("HEAD", &info->b_commit)) { | |
1ac528c0 PSU |
1119 | if (!quiet) |
1120 | fprintf_ln(stderr, _("You do not have " | |
1121 | "the initial commit yet")); | |
d4788af8 PSU |
1122 | ret = -1; |
1123 | goto done; | |
1124 | } else { | |
1125 | head_commit = lookup_commit(the_repository, &info->b_commit); | |
1126 | } | |
1127 | ||
ef0f0b45 | 1128 | if (!check_changes(ps, include_untracked, &untracked_files)) { |
d4788af8 PSU |
1129 | ret = 1; |
1130 | goto done; | |
1131 | } | |
1132 | ||
1133 | branch_ref = resolve_ref_unsafe("HEAD", 0, NULL, &flags); | |
1134 | if (flags & REF_ISSYMREF) | |
1135 | branch_name = strrchr(branch_ref, '/') + 1; | |
1136 | head_short_sha1 = find_unique_abbrev(&head_commit->object.oid, | |
1137 | DEFAULT_ABBREV); | |
1138 | strbuf_addf(&msg, "%s: %s ", branch_name, head_short_sha1); | |
1139 | pp_commit_easy(CMIT_FMT_ONELINE, head_commit, &msg); | |
1140 | ||
1141 | strbuf_addf(&commit_tree_label, "index on %s\n", msg.buf); | |
1142 | commit_list_insert(head_commit, &parents); | |
1143 | if (write_cache_as_tree(&info->i_tree, 0, NULL) || | |
1144 | commit_tree(commit_tree_label.buf, commit_tree_label.len, | |
1145 | &info->i_tree, parents, &info->i_commit, NULL, NULL)) { | |
1ac528c0 PSU |
1146 | if (!quiet) |
1147 | fprintf_ln(stderr, _("Cannot save the current " | |
1148 | "index state")); | |
d4788af8 PSU |
1149 | ret = -1; |
1150 | goto done; | |
1151 | } | |
1152 | ||
ef0f0b45 | 1153 | if (include_untracked) { |
d4788af8 | 1154 | if (save_untracked_files(info, &msg, untracked_files)) { |
1ac528c0 PSU |
1155 | if (!quiet) |
1156 | fprintf_ln(stderr, _("Cannot save " | |
1157 | "the untracked files")); | |
d4788af8 PSU |
1158 | ret = -1; |
1159 | goto done; | |
1160 | } | |
1161 | untracked_commit_option = 1; | |
1162 | } | |
1163 | if (patch_mode) { | |
1ac528c0 | 1164 | ret = stash_patch(info, ps, patch, quiet); |
d4788af8 | 1165 | if (ret < 0) { |
1ac528c0 PSU |
1166 | if (!quiet) |
1167 | fprintf_ln(stderr, _("Cannot save the current " | |
1168 | "worktree state")); | |
d4788af8 PSU |
1169 | goto done; |
1170 | } else if (ret > 0) { | |
1171 | goto done; | |
1172 | } | |
1173 | } else { | |
1174 | if (stash_working_tree(info, ps)) { | |
1ac528c0 PSU |
1175 | if (!quiet) |
1176 | fprintf_ln(stderr, _("Cannot save the current " | |
1177 | "worktree state")); | |
d4788af8 PSU |
1178 | ret = -1; |
1179 | goto done; | |
1180 | } | |
1181 | } | |
1182 | ||
1183 | if (!stash_msg_buf->len) | |
1184 | strbuf_addf(stash_msg_buf, "WIP on %s", msg.buf); | |
1185 | else | |
1186 | strbuf_insertf(stash_msg_buf, 0, "On %s: ", branch_name); | |
1187 | ||
1188 | /* | |
1189 | * `parents` will be empty after calling `commit_tree()`, so there is | |
1190 | * no need to call `free_commit_list()` | |
1191 | */ | |
1192 | parents = NULL; | |
1193 | if (untracked_commit_option) | |
1194 | commit_list_insert(lookup_commit(the_repository, | |
1195 | &info->u_commit), | |
1196 | &parents); | |
1197 | commit_list_insert(lookup_commit(the_repository, &info->i_commit), | |
1198 | &parents); | |
1199 | commit_list_insert(head_commit, &parents); | |
1200 | ||
1201 | if (commit_tree(stash_msg_buf->buf, stash_msg_buf->len, &info->w_tree, | |
1202 | parents, &info->w_commit, NULL, NULL)) { | |
1ac528c0 PSU |
1203 | if (!quiet) |
1204 | fprintf_ln(stderr, _("Cannot record " | |
1205 | "working tree state")); | |
d4788af8 PSU |
1206 | ret = -1; |
1207 | goto done; | |
1208 | } | |
1209 | ||
1210 | done: | |
1211 | strbuf_release(&commit_tree_label); | |
1212 | strbuf_release(&msg); | |
1213 | strbuf_release(&untracked_files); | |
1214 | return ret; | |
1215 | } | |
1216 | ||
1217 | static int create_stash(int argc, const char **argv, const char *prefix) | |
1218 | { | |
d4788af8 | 1219 | int ret = 0; |
d4788af8 PSU |
1220 | struct strbuf stash_msg_buf = STRBUF_INIT; |
1221 | struct stash_info info; | |
1222 | struct pathspec ps; | |
d4788af8 | 1223 | |
40af1468 PSU |
1224 | /* Starting with argv[1], since argv[0] is "create" */ |
1225 | strbuf_join_argv(&stash_msg_buf, argc - 1, ++argv, ' '); | |
d4788af8 PSU |
1226 | |
1227 | memset(&ps, 0, sizeof(ps)); | |
ef0f0b45 PSU |
1228 | if (!check_changes_tracked_files(ps)) |
1229 | return 0; | |
1230 | ||
40af1468 | 1231 | ret = do_create_stash(ps, &stash_msg_buf, 0, 0, &info, |
1ac528c0 | 1232 | NULL, 0); |
d4788af8 PSU |
1233 | if (!ret) |
1234 | printf_ln("%s", oid_to_hex(&info.w_commit)); | |
1235 | ||
1236 | strbuf_release(&stash_msg_buf); | |
ef0f0b45 | 1237 | return ret; |
d4788af8 PSU |
1238 | } |
1239 | ||
d553f538 PSU |
1240 | static int do_push_stash(struct pathspec ps, const char *stash_msg, int quiet, |
1241 | int keep_index, int patch_mode, int include_untracked) | |
1242 | { | |
1243 | int ret = 0; | |
1244 | struct stash_info info; | |
1245 | struct strbuf patch = STRBUF_INIT; | |
1246 | struct strbuf stash_msg_buf = STRBUF_INIT; | |
ef0f0b45 | 1247 | struct strbuf untracked_files = STRBUF_INIT; |
d553f538 PSU |
1248 | |
1249 | if (patch_mode && keep_index == -1) | |
1250 | keep_index = 1; | |
1251 | ||
1252 | if (patch_mode && include_untracked) { | |
1253 | fprintf_ln(stderr, _("Can't use --patch and --include-untracked" | |
1254 | " or --all at the same time")); | |
1255 | ret = -1; | |
1256 | goto done; | |
1257 | } | |
1258 | ||
1259 | read_cache_preload(NULL); | |
1260 | if (!include_untracked && ps.nr) { | |
1261 | int i; | |
1262 | char *ps_matched = xcalloc(ps.nr, 1); | |
1263 | ||
1264 | for (i = 0; i < active_nr; i++) | |
1265 | ce_path_match(&the_index, active_cache[i], &ps, | |
1266 | ps_matched); | |
1267 | ||
1268 | if (report_path_error(ps_matched, &ps, NULL)) { | |
1269 | fprintf_ln(stderr, _("Did you forget to 'git add'?")); | |
1270 | ret = -1; | |
1271 | free(ps_matched); | |
1272 | goto done; | |
1273 | } | |
1274 | free(ps_matched); | |
1275 | } | |
1276 | ||
1277 | if (refresh_cache(REFRESH_QUIET)) { | |
1278 | ret = -1; | |
1279 | goto done; | |
1280 | } | |
1281 | ||
ef0f0b45 | 1282 | if (!check_changes(ps, include_untracked, &untracked_files)) { |
d553f538 PSU |
1283 | if (!quiet) |
1284 | printf_ln(_("No local changes to save")); | |
1285 | goto done; | |
1286 | } | |
1287 | ||
1288 | if (!reflog_exists(ref_stash) && do_clear_stash()) { | |
1289 | ret = -1; | |
1ac528c0 PSU |
1290 | if (!quiet) |
1291 | fprintf_ln(stderr, _("Cannot initialize stash")); | |
d553f538 PSU |
1292 | goto done; |
1293 | } | |
1294 | ||
1295 | if (stash_msg) | |
1296 | strbuf_addstr(&stash_msg_buf, stash_msg); | |
1297 | if (do_create_stash(ps, &stash_msg_buf, include_untracked, patch_mode, | |
1ac528c0 | 1298 | &info, &patch, quiet)) { |
d553f538 PSU |
1299 | ret = -1; |
1300 | goto done; | |
1301 | } | |
1302 | ||
1303 | if (do_store_stash(&info.w_commit, stash_msg_buf.buf, 1)) { | |
1304 | ret = -1; | |
1ac528c0 PSU |
1305 | if (!quiet) |
1306 | fprintf_ln(stderr, _("Cannot save the current status")); | |
d553f538 PSU |
1307 | goto done; |
1308 | } | |
1309 | ||
1ac528c0 PSU |
1310 | if (!quiet) |
1311 | printf_ln(_("Saved working directory and index state %s"), | |
1312 | stash_msg_buf.buf); | |
d553f538 PSU |
1313 | |
1314 | if (!patch_mode) { | |
1315 | if (include_untracked && !ps.nr) { | |
1316 | struct child_process cp = CHILD_PROCESS_INIT; | |
1317 | ||
1318 | cp.git_cmd = 1; | |
1319 | argv_array_pushl(&cp.args, "clean", "--force", | |
1320 | "--quiet", "-d", NULL); | |
1321 | if (include_untracked == INCLUDE_ALL_FILES) | |
1322 | argv_array_push(&cp.args, "-x"); | |
1323 | if (run_command(&cp)) { | |
1324 | ret = -1; | |
1325 | goto done; | |
1326 | } | |
1327 | } | |
1328 | discard_cache(); | |
1329 | if (ps.nr) { | |
1330 | struct child_process cp_add = CHILD_PROCESS_INIT; | |
1331 | struct child_process cp_diff = CHILD_PROCESS_INIT; | |
1332 | struct child_process cp_apply = CHILD_PROCESS_INIT; | |
1333 | struct strbuf out = STRBUF_INIT; | |
1334 | ||
1335 | cp_add.git_cmd = 1; | |
1336 | argv_array_push(&cp_add.args, "add"); | |
1337 | if (!include_untracked) | |
1338 | argv_array_push(&cp_add.args, "-u"); | |
1339 | if (include_untracked == INCLUDE_ALL_FILES) | |
1340 | argv_array_push(&cp_add.args, "--force"); | |
1341 | argv_array_push(&cp_add.args, "--"); | |
1342 | add_pathspecs(&cp_add.args, ps); | |
1343 | if (run_command(&cp_add)) { | |
1344 | ret = -1; | |
1345 | goto done; | |
1346 | } | |
1347 | ||
1348 | cp_diff.git_cmd = 1; | |
1349 | argv_array_pushl(&cp_diff.args, "diff-index", "-p", | |
1350 | "--cached", "--binary", "HEAD", "--", | |
1351 | NULL); | |
1352 | add_pathspecs(&cp_diff.args, ps); | |
1353 | if (pipe_command(&cp_diff, NULL, 0, &out, 0, NULL, 0)) { | |
1354 | ret = -1; | |
1355 | goto done; | |
1356 | } | |
1357 | ||
1358 | cp_apply.git_cmd = 1; | |
1359 | argv_array_pushl(&cp_apply.args, "apply", "--index", | |
1360 | "-R", NULL); | |
1361 | if (pipe_command(&cp_apply, out.buf, out.len, NULL, 0, | |
1362 | NULL, 0)) { | |
1363 | ret = -1; | |
1364 | goto done; | |
1365 | } | |
1366 | } else { | |
1367 | struct child_process cp = CHILD_PROCESS_INIT; | |
1368 | cp.git_cmd = 1; | |
1369 | argv_array_pushl(&cp.args, "reset", "--hard", "-q", | |
1370 | NULL); | |
1371 | if (run_command(&cp)) { | |
1372 | ret = -1; | |
1373 | goto done; | |
1374 | } | |
1375 | } | |
1376 | ||
1377 | if (keep_index == 1 && !is_null_oid(&info.i_tree)) { | |
1378 | struct child_process cp_ls = CHILD_PROCESS_INIT; | |
1379 | struct child_process cp_checkout = CHILD_PROCESS_INIT; | |
1380 | struct strbuf out = STRBUF_INIT; | |
1381 | ||
1382 | if (reset_tree(&info.i_tree, 0, 1)) { | |
1383 | ret = -1; | |
1384 | goto done; | |
1385 | } | |
1386 | ||
1387 | cp_ls.git_cmd = 1; | |
1388 | argv_array_pushl(&cp_ls.args, "ls-files", "-z", | |
1389 | "--modified", "--", NULL); | |
1390 | ||
1391 | add_pathspecs(&cp_ls.args, ps); | |
1392 | if (pipe_command(&cp_ls, NULL, 0, &out, 0, NULL, 0)) { | |
1393 | ret = -1; | |
1394 | goto done; | |
1395 | } | |
1396 | ||
1397 | cp_checkout.git_cmd = 1; | |
1398 | argv_array_pushl(&cp_checkout.args, "checkout-index", | |
1399 | "-z", "--force", "--stdin", NULL); | |
1400 | if (pipe_command(&cp_checkout, out.buf, out.len, NULL, | |
1401 | 0, NULL, 0)) { | |
1402 | ret = -1; | |
1403 | goto done; | |
1404 | } | |
1405 | } | |
1406 | goto done; | |
1407 | } else { | |
1408 | struct child_process cp = CHILD_PROCESS_INIT; | |
1409 | ||
1410 | cp.git_cmd = 1; | |
1411 | argv_array_pushl(&cp.args, "apply", "-R", NULL); | |
1412 | ||
1413 | if (pipe_command(&cp, patch.buf, patch.len, NULL, 0, NULL, 0)) { | |
1ac528c0 PSU |
1414 | if (!quiet) |
1415 | fprintf_ln(stderr, _("Cannot remove " | |
1416 | "worktree changes")); | |
d553f538 PSU |
1417 | ret = -1; |
1418 | goto done; | |
1419 | } | |
1420 | ||
1421 | if (keep_index < 1) { | |
1422 | struct child_process cp = CHILD_PROCESS_INIT; | |
1423 | ||
1424 | cp.git_cmd = 1; | |
1425 | argv_array_pushl(&cp.args, "reset", "-q", "--", NULL); | |
1426 | add_pathspecs(&cp.args, ps); | |
1427 | if (run_command(&cp)) { | |
1428 | ret = -1; | |
1429 | goto done; | |
1430 | } | |
1431 | } | |
1432 | goto done; | |
1433 | } | |
1434 | ||
1435 | done: | |
1436 | strbuf_release(&stash_msg_buf); | |
1437 | return ret; | |
1438 | } | |
1439 | ||
1440 | static int push_stash(int argc, const char **argv, const char *prefix) | |
1441 | { | |
1442 | int keep_index = -1; | |
1443 | int patch_mode = 0; | |
1444 | int include_untracked = 0; | |
1445 | int quiet = 0; | |
1446 | const char *stash_msg = NULL; | |
1447 | struct pathspec ps; | |
1448 | struct option options[] = { | |
1449 | OPT_BOOL('k', "keep-index", &keep_index, | |
1450 | N_("keep index")), | |
1451 | OPT_BOOL('p', "patch", &patch_mode, | |
1452 | N_("stash in patch mode")), | |
1453 | OPT__QUIET(&quiet, N_("quiet mode")), | |
1454 | OPT_BOOL('u', "include-untracked", &include_untracked, | |
1455 | N_("include untracked files in stash")), | |
1456 | OPT_SET_INT('a', "all", &include_untracked, | |
1457 | N_("include ignore files"), 2), | |
1458 | OPT_STRING('m', "message", &stash_msg, N_("message"), | |
1459 | N_("stash message")), | |
1460 | OPT_END() | |
1461 | }; | |
1462 | ||
40af1468 PSU |
1463 | if (argc) |
1464 | argc = parse_options(argc, argv, prefix, options, | |
1465 | git_stash_push_usage, | |
1466 | 0); | |
d553f538 PSU |
1467 | |
1468 | parse_pathspec(&ps, 0, PATHSPEC_PREFER_FULL, prefix, argv); | |
1469 | return do_push_stash(ps, stash_msg, quiet, keep_index, patch_mode, | |
1470 | include_untracked); | |
1471 | } | |
1472 | ||
64fe9c26 PSU |
1473 | static int save_stash(int argc, const char **argv, const char *prefix) |
1474 | { | |
1475 | int keep_index = -1; | |
1476 | int patch_mode = 0; | |
1477 | int include_untracked = 0; | |
1478 | int quiet = 0; | |
1479 | int ret = 0; | |
1480 | const char *stash_msg = NULL; | |
1481 | struct pathspec ps; | |
1482 | struct strbuf stash_msg_buf = STRBUF_INIT; | |
1483 | struct option options[] = { | |
1484 | OPT_BOOL('k', "keep-index", &keep_index, | |
1485 | N_("keep index")), | |
1486 | OPT_BOOL('p', "patch", &patch_mode, | |
1487 | N_("stash in patch mode")), | |
1488 | OPT__QUIET(&quiet, N_("quiet mode")), | |
1489 | OPT_BOOL('u', "include-untracked", &include_untracked, | |
1490 | N_("include untracked files in stash")), | |
1491 | OPT_SET_INT('a', "all", &include_untracked, | |
1492 | N_("include ignore files"), 2), | |
1493 | OPT_STRING('m', "message", &stash_msg, "message", | |
1494 | N_("stash message")), | |
1495 | OPT_END() | |
1496 | }; | |
1497 | ||
1498 | argc = parse_options(argc, argv, prefix, options, | |
40af1468 | 1499 | git_stash_save_usage, |
64fe9c26 PSU |
1500 | PARSE_OPT_KEEP_DASHDASH); |
1501 | ||
1502 | if (argc) | |
1503 | stash_msg = strbuf_join_argv(&stash_msg_buf, argc, argv, ' '); | |
1504 | ||
1505 | memset(&ps, 0, sizeof(ps)); | |
1506 | ret = do_push_stash(ps, stash_msg, quiet, keep_index, | |
1507 | patch_mode, include_untracked); | |
1508 | ||
1509 | strbuf_release(&stash_msg_buf); | |
1510 | return ret; | |
1511 | } | |
1512 | ||
40af1468 | 1513 | int cmd_stash(int argc, const char **argv, const char *prefix) |
8a0fc8d1 | 1514 | { |
40af1468 | 1515 | int i = -1; |
8a0fc8d1 JT |
1516 | pid_t pid = getpid(); |
1517 | const char *index_file; | |
40af1468 | 1518 | struct argv_array args = ARGV_ARRAY_INIT; |
8a0fc8d1 JT |
1519 | |
1520 | struct option options[] = { | |
1521 | OPT_END() | |
1522 | }; | |
1523 | ||
d4788af8 | 1524 | git_config(git_diff_basic_config, NULL); |
8a0fc8d1 | 1525 | |
40af1468 | 1526 | argc = parse_options(argc, argv, prefix, options, git_stash_usage, |
8a0fc8d1 JT |
1527 | PARSE_OPT_KEEP_UNKNOWN | PARSE_OPT_KEEP_DASHDASH); |
1528 | ||
1529 | index_file = get_index_file(); | |
1530 | strbuf_addf(&stash_index_path, "%s.stash.%" PRIuMAX, index_file, | |
1531 | (uintmax_t)pid); | |
1532 | ||
40af1468 PSU |
1533 | if (!argc) |
1534 | return !!push_stash(0, NULL, prefix); | |
1535 | else if (!strcmp(argv[0], "apply")) | |
8a0fc8d1 | 1536 | return !!apply_stash(argc, argv, prefix); |
4e2dd393 JT |
1537 | else if (!strcmp(argv[0], "clear")) |
1538 | return !!clear_stash(argc, argv, prefix); | |
1539 | else if (!strcmp(argv[0], "drop")) | |
1540 | return !!drop_stash(argc, argv, prefix); | |
c4de61d7 JT |
1541 | else if (!strcmp(argv[0], "pop")) |
1542 | return !!pop_stash(argc, argv, prefix); | |
577c1995 JT |
1543 | else if (!strcmp(argv[0], "branch")) |
1544 | return !!branch_stash(argc, argv, prefix); | |
130f2697 PSU |
1545 | else if (!strcmp(argv[0], "list")) |
1546 | return !!list_stash(argc, argv, prefix); | |
dc7bd382 PSU |
1547 | else if (!strcmp(argv[0], "show")) |
1548 | return !!show_stash(argc, argv, prefix); | |
41e0dd55 PSU |
1549 | else if (!strcmp(argv[0], "store")) |
1550 | return !!store_stash(argc, argv, prefix); | |
d4788af8 PSU |
1551 | else if (!strcmp(argv[0], "create")) |
1552 | return !!create_stash(argc, argv, prefix); | |
d553f538 PSU |
1553 | else if (!strcmp(argv[0], "push")) |
1554 | return !!push_stash(argc, argv, prefix); | |
64fe9c26 PSU |
1555 | else if (!strcmp(argv[0], "save")) |
1556 | return !!save_stash(argc, argv, prefix); | |
40af1468 PSU |
1557 | else if (*argv[0] != '-') |
1558 | usage_msg_opt(xstrfmt(_("unknown subcommand: %s"), argv[0]), | |
1559 | git_stash_usage, options); | |
1560 | ||
1561 | if (strcmp(argv[0], "-p")) { | |
1562 | while (++i < argc && strcmp(argv[i], "--")) { | |
1563 | /* | |
1564 | * `akpqu` is a string which contains all short options, | |
1565 | * except `-m` which is verified separately. | |
1566 | */ | |
1567 | if ((strlen(argv[i]) == 2) && *argv[i] == '-' && | |
1568 | strchr("akpqu", argv[i][1])) | |
1569 | continue; | |
1570 | ||
1571 | if (!strcmp(argv[i], "--all") || | |
1572 | !strcmp(argv[i], "--keep-index") || | |
1573 | !strcmp(argv[i], "--no-keep-index") || | |
1574 | !strcmp(argv[i], "--patch") || | |
1575 | !strcmp(argv[i], "--quiet") || | |
1576 | !strcmp(argv[i], "--include-untracked")) | |
1577 | continue; | |
1578 | ||
1579 | /* | |
1580 | * `-m` and `--message=` are verified separately because | |
1581 | * they need to be immediately followed by a string | |
1582 | * (i.e.`-m"foobar"` or `--message="foobar"`). | |
1583 | */ | |
1584 | if (starts_with(argv[i], "-m") || | |
1585 | starts_with(argv[i], "--message=")) | |
1586 | continue; | |
1587 | ||
1588 | usage_with_options(git_stash_usage, options); | |
1589 | } | |
1590 | } | |
8a0fc8d1 | 1591 | |
40af1468 PSU |
1592 | argv_array_push(&args, "push"); |
1593 | argv_array_pushv(&args, argv); | |
1594 | return !!push_stash(args.argc, args.argv, prefix); | |
8a0fc8d1 | 1595 | } |