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