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