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