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