Commit

786fc390465f535cc2a09922b064e457faa98881

stash: reserve exit status 1 for conflicts

"git stash apply", "pop" and "branch" exit with status 1 both when
applying the stash entry resulted in conflicts and when they fail for
other reasons, so callers cannot tell the two apart.

Follow the convention of "git merge-tree" and the merge strategies,
which exit with status 1 to indicate conflicts and with a different
non-zero status for errors: those subcommands now exit with status 1
only when applying the stash entry resulted in conflicts, in which
case the stash entry is left in place, and exit with status 128, the
status die() uses, when they fail for other reasons.  Document the
exit statuses.

The only subcommand implementations that can return a positive value
are "apply", "pop" and "branch", which return the value of
do_apply_stash(): "apply" returns it directly, and "pop" and "branch"
drop the stash entry, via do_drop_stash(), which always returns 0,
only when the application succeeded.  do_apply_stash() only returns a
positive value when the three-way merge was unclean.  cmd_stash() now
maps negative values to 128 and passes positive values through as the
exit status, so exit status 1 unambiguously indicates conflicts.
enum stash_apply_result makes the convention explicit, and the
autostash helpers use it to tell users that their stashed changes
were saved when applying them fails.

Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>

Harald Nordgren <haraldnordgren@gmail.com> · 2026-09-03 14:39 UTC · committed by Junio C Hamano

Browse files
6 files changed, +160 −60
modified Documentation/git-stash.adoc
@@ -426,6 +426,15 @@ include::includes/cmd-config-section-all.adoc[]
426 426 :git-stash: 1
427 427 include::config/stash.adoc[]
428 428
429 + EXIT STATUS
430 + -----------
431 +
432 + The `git stash` subcommands exit with status 0 on success. The
433 + subcommands that apply a stash entry, i.e. `apply`, `pop` and `branch`,
434 + exit with status 1 when applying the stash entry resulted in conflicts,
435 + in which case the stash entry is left in place, and with a non-zero
436 + status other than 1 when they fail for other reasons.
437 +
429 438
430 439 SEE ALSO
431 440 --------
modified builtin/stash.c
@@ -10,6 +10,7 @@
10 10 #include "object-name.h"
11 11 #include "parse-options.h"
12 12 #include "refs.h"
13 + #include "stash.h"
13 14 #include "lockfile.h"
14 15 #include "cache-tree.h"
15 16 #include "unpack-trees.h"
@@ -640,10 +641,12 @@ static void unstage_changes_unless_new(struct object_id *orig_tree)
640 641 die(_("could not write index"));
641 642 }
642 643
643 - static int do_apply_stash(const char *prefix, struct stash_info *info,
644 - int index, int quiet,
645 - const char *label_ours, const char *label_theirs,
646 - const char *label_base)
644 + static enum stash_apply_result do_apply_stash(const char *prefix,
645 + struct stash_info *info,
646 + int index, int quiet,
647 + const char *label_ours,
648 + const char *label_theirs,
649 + const char *label_base)
647 650 {
648 651 int clean, ret;
649 652 int has_index = index;
@@ -717,8 +720,8 @@ static int do_apply_stash(const char *prefix, struct stash_info *info,
717 720
718 721 /*
719 722 * If 'clean' >= 0, reverse the value for 'ret' so 'ret' is 0 when the
720 - * merge was clean, and nonzero if the merge was unclean or encountered
721 - * an error.
723 + * merge was clean, and 1 if the merge was unclean or a negative value
724 + * if it encountered an error.
722 725 */
723 726 ret = clean >= 0 ? !clean : clean;
724 727
@@ -2492,10 +2495,22 @@ int cmd_stash(int argc,
2492 2495 strbuf_addf(&stash_index_path, "%s.stash.%" PRIuMAX, index_file,
2493 2496 (uintmax_t)pid);
2494 2497
2495 - if (fn)
2496 - return !!fn(argc, argv, prefix, repo);
2497 - else if (!argc)
2498 + if (fn) {
2499 + ret = fn(argc, argv, prefix, repo);
2500 +
2501 + /*
2502 + * The subcommand implementations return 0 on success, a
2503 + * negative value on failure, and STASH_APPLY_CONFLICT
2504 + * when applying a stash entry resulted in conflicts.
2505 + * Map failures to 128, the status die() uses, so that
2506 + * exit status 1 unambiguously indicates conflicts.
2507 + */
2508 + if (ret < 0)
2509 + return 128;
2510 + return ret;
2511 + } else if (!argc) {
2498 2512 return !!push_stash_unassumed(0, NULL, prefix, repo);
2513 + }
2499 2514
2500 2515 /* Assume 'stash push' */
2501 2516 strvec_push(&args, "push");
modified sequencer.c
@@ -19,6 +19,7 @@
19 19 #include "commit.h"
20 20 #include "sequencer.h"
21 21 #include "run-command.h"
22 + #include "stash.h"
22 23 #include "hook.h"
23 24 #include "utf8.h"
24 25 #include "cache-tree.h"
@@ -4727,31 +4728,50 @@ void create_autostash_ref(struct repository *r, const char *refname,
4727 4728 create_autostash_internal(r, NULL, refname, message, silent);
4728 4729 }
4729 4730
4730 - static int apply_save_autostash_oid(const char *stash_oid, int attempt_apply,
4731 - const char *label_ours, const char *label_theirs,
4732 - const char *label_base,
4733 - const char *stash_msg)
4731 + static enum stash_apply_result do_stash_apply(const char *stash_oid,
4732 + const char *label_ours,
4733 + const char *label_theirs,
4734 + const char *label_base)
4734 4735 {
4735 4736 struct child_process child = CHILD_PROCESS_INIT;
4736 - int ret = 0;
4737 4737
4738 - if (attempt_apply) {
4739 - child.git_cmd = 1;
4740 - child.no_stdout = 1;
4741 - child.no_stderr = 1;
4742 - strvec_push(&child.args, "stash");
4743 - strvec_push(&child.args, "apply");
4744 - if (label_ours)
4745 - strvec_pushf(&child.args, "--label-ours=%s", label_ours);
4746 - if (label_theirs)
4747 - strvec_pushf(&child.args, "--label-theirs=%s", label_theirs);
4748 - if (label_base)
4749 - strvec_pushf(&child.args, "--label-base=%s", label_base);
4750 - strvec_push(&child.args, stash_oid);
4751 - ret = run_command(&child);
4752 - }
4753 -
4754 - if (attempt_apply && !ret)
4738 + child.git_cmd = 1;
4739 + child.no_stdout = 1;
4740 + child.no_stderr = 1;
4741 + strvec_push(&child.args, "stash");
4742 + strvec_push(&child.args, "apply");
4743 + if (label_ours)
4744 + strvec_pushf(&child.args, "--label-ours=%s", label_ours);
4745 + if (label_theirs)
4746 + strvec_pushf(&child.args, "--label-theirs=%s", label_theirs);
4747 + if (label_base)
4748 + strvec_pushf(&child.args, "--label-base=%s", label_base);
4749 + strvec_push(&child.args, stash_oid);
4750 +
4751 + switch (run_command(&child)) {
4752 + case 0:
4753 + return STASH_APPLY_CLEAN;
4754 + case STASH_APPLY_CONFLICT:
4755 + return STASH_APPLY_CONFLICT;
4756 + default:
4757 + return STASH_APPLY_ERROR;
4758 + }
4759 + }
4760 +
4761 + static enum stash_apply_result apply_save_autostash_oid(const char *stash_oid,
4762 + int attempt_apply,
4763 + const char *label_ours,
4764 + const char *label_theirs,
4765 + const char *label_base,
4766 + const char *stash_msg)
4767 + {
4768 + enum stash_apply_result ret = STASH_APPLY_CLEAN;
4769 +
4770 + if (attempt_apply)
4771 + ret = do_stash_apply(stash_oid, label_ours, label_theirs,
4772 + label_base);
4773 +
4774 + if (attempt_apply && ret == STASH_APPLY_CLEAN)
4755 4775 fprintf(stderr, _("Applied autostash.\n"));
4756 4776 else {
4757 4777 struct child_process store = CHILD_PROCESS_INIT;
@@ -4765,13 +4785,16 @@ static int apply_save_autostash_oid(const char *stash_oid, int attempt_apply,
4765 4785 strvec_push(&store.args, stash_oid);
4766 4786 if (run_command(&store))
4767 4787 ret = error(_("cannot store %s"), stash_oid);
4768 - else if (attempt_apply)
4788 + else if (attempt_apply && ret == STASH_APPLY_CONFLICT)
4769 4789 fprintf(stderr,
4770 4790 _("Your local changes are stashed, however applying them\n"
4771 4791 "resulted in conflicts. You can either resolve the conflicts\n"
4772 4792 "and then discard the stash with \"git stash drop\", or, if you\n"
4773 4793 "do not want to resolve them now, run \"git reset --hard\" and\n"
4774 4794 "apply the local changes later by running \"git stash pop\".\n"));
4795 + else if (attempt_apply)
4796 + ret = error(_("could not apply autostash; "
4797 + "your changes are safe in the stash"));
4775 4798 else
4776 4799 fprintf(stderr,
4777 4800 _("Autostash exists; creating a new stash entry.\n"
@@ -4783,15 +4806,16 @@ static int apply_save_autostash_oid(const char *stash_oid, int attempt_apply,
4783 4806 return ret;
4784 4807 }
4785 4808
4786 - static int apply_save_autostash(const char *path, int attempt_apply)
4809 + static enum stash_apply_result apply_save_autostash(const char *path,
4810 + int attempt_apply)
4787 4811 {
4788 4812 struct strbuf stash_oid = STRBUF_INIT;
4789 - int ret = 0;
4813 + enum stash_apply_result ret = STASH_APPLY_CLEAN;
4790 4814
4791 4815 if (!read_oneliner(&stash_oid, path,
4792 4816 READ_ONELINER_SKIP_IF_EMPTY)) {
4793 4817 strbuf_release(&stash_oid);
4794 - return 0;
4818 + return STASH_APPLY_CLEAN;
4795 4819 }
4796 4820 strbuf_trim(&stash_oid);
4797 4821
@@ -4803,37 +4827,40 @@ static int apply_save_autostash(const char *path, int attempt_apply)
4803 4827 return ret;
4804 4828 }
4805 4829
4806 - int save_autostash(const char *path)
4830 + enum stash_apply_result save_autostash(const char *path)
4807 4831 {
4808 4832 return apply_save_autostash(path, 0);
4809 4833 }
4810 4834
4811 - int apply_autostash(const char *path)
4835 + enum stash_apply_result apply_autostash(const char *path)
4812 4836 {
4813 4837 return apply_save_autostash(path, 1);
4814 4838 }
4815 4839
4816 - int apply_autostash_oid(const char *stash_oid)
4840 + enum stash_apply_result apply_autostash_oid(const char *stash_oid)
4817 4841 {
4818 4842 return apply_save_autostash_oid(stash_oid, 1, NULL, NULL, NULL, NULL);
4819 4843 }
4820 4844
4821 - static int apply_save_autostash_ref(struct repository *r, const char *refname,
4822 - int attempt_apply,
4823 - const char *label_ours, const char *label_theirs,
4824 - const char *label_base,
4825 - const char *stash_msg)
4845 + static enum stash_apply_result apply_save_autostash_ref(struct repository *r,
4846 + const char *refname,
4847 + int attempt_apply,
4848 + const char *label_ours,
4849 + const char *label_theirs,
4850 + const char *label_base,
4851 + const char *stash_msg)
4826 4852 {
4827 4853 struct object_id stash_oid;
4828 4854 char stash_oid_hex[GIT_MAX_HEXSZ + 1];
4829 - int flag, ret;
4855 + int flag;
4856 + enum stash_apply_result ret;
4830 4857
4831 4858 if (!refs_ref_exists(get_main_ref_store(r), refname))
4832 - return 0;
4859 + return STASH_APPLY_CLEAN;
4833 4860
4834 4861 if (!refs_resolve_ref_unsafe(get_main_ref_store(r), refname,
4835 4862 RESOLVE_REF_READING, &stash_oid, &flag))
4836 - return -1;
4863 + return STASH_APPLY_ERROR;
4837 4864 if (flag & REF_ISSYMREF)
4838 4865 return error(_("autostash reference is a symref"));
4839 4866
@@ -4848,15 +4875,19 @@ static int apply_save_autostash_ref(struct repository *r, const char *refname,
4848 4875 return ret;
4849 4876 }
4850 4877
4851 - int save_autostash_ref(struct repository *r, const char *refname)
4878 + enum stash_apply_result save_autostash_ref(struct repository *r,
4879 + const char *refname)
4852 4880 {
4853 4881 return apply_save_autostash_ref(r, refname, 0,
4854 4882 NULL, NULL, NULL, NULL);
4855 4883 }
4856 4884
4857 - int apply_autostash_ref(struct repository *r, const char *refname,
4858 - const char *label_ours, const char *label_theirs,
4859 - const char *label_base, const char *stash_msg)
4885 + enum stash_apply_result apply_autostash_ref(struct repository *r,
4886 + const char *refname,
4887 + const char *label_ours,
4888 + const char *label_theirs,
4889 + const char *label_base,
4890 + const char *stash_msg)
4860 4891 {
4861 4892 return apply_save_autostash_ref(r, refname, 1,
4862 4893 label_ours, label_theirs, label_base,
modified sequencer.h
@@ -3,6 +3,7 @@
3 3
4 4 #include "strbuf.h"
5 5 #include "strvec.h"
6 + #include "stash.h"
6 7 #include "wt-status.h"
7 8
8 9 struct commit;
@@ -231,13 +232,17 @@ void commit_post_rewrite(struct repository *r,
231 232 void create_autostash(struct repository *r, const char *path);
232 233 void create_autostash_ref(struct repository *r, const char *refname,
233 234 const char *message, bool silent);
234 - int save_autostash(const char *path);
235 - int save_autostash_ref(struct repository *r, const char *refname);
236 - int apply_autostash(const char *path);
237 - int apply_autostash_oid(const char *stash_oid);
238 - int apply_autostash_ref(struct repository *r, const char *refname,
239 - const char *label_ours, const char *label_theirs,
240 - const char *label_base, const char *stash_msg);
235 + enum stash_apply_result save_autostash(const char *path);
236 + enum stash_apply_result save_autostash_ref(struct repository *r,
237 + const char *refname);
238 + enum stash_apply_result apply_autostash(const char *path);
239 + enum stash_apply_result apply_autostash_oid(const char *stash_oid);
240 + enum stash_apply_result apply_autostash_ref(struct repository *r,
241 + const char *refname,
242 + const char *label_ours,
243 + const char *label_theirs,
244 + const char *label_base,
245 + const char *stash_msg);
241 246
242 247 #define SUMMARY_INITIAL_COMMIT (1 << 0)
243 248 #define SUMMARY_SHOW_AUTHOR_DATE (1 << 1)
added stash.h mode 100644
@@ -0,0 +1,21 @@
1 + #ifndef STASH_H
2 + #define STASH_H
3 +
4 + enum stash_apply_result {
5 + /* The stash was applied cleanly, or there was nothing to apply. */
6 + STASH_APPLY_CLEAN = 0,
7 +
8 + /*
9 + * The stash could not be applied because it resulted in
10 + * conflicts. The stash entry is left in place. The "git stash
11 + * apply", "pop" and "branch" subcommands exit with this status
12 + * in this case, mirroring the convention of "git merge-tree" and
13 + * the merge strategies.
14 + */
15 + STASH_APPLY_CONFLICT = 1,
16 +
17 + /* Something went wrong. */
18 + STASH_APPLY_ERROR = -1,
19 + };
20 +
21 + #endif /* STASH_H */
modified t/t3903-stash.sh
@@ -1791,13 +1791,13 @@ test_expect_success 'stash.index=false overridden by --index' '
1791 1791 test_cmp expect file
1792 1792 '
1793 1793
1794 - test_expect_success 'apply with custom conflict labels' '
1794 + test_expect_success 'apply exits 1 on conflicts' '
1795 1795 git reset --hard initial &&
1796 1796 test_commit label-base conflict-file base-content &&
1797 1797 echo stashed >conflict-file &&
1798 1798 git stash push -m "stashed" &&
1799 1799 test_commit label-upstream conflict-file upstream-content &&
1800 - test_must_fail git -c merge.conflictStyle=diff3 stash apply --label-ours=UP --label-theirs=STASH &&
1800 + test_expect_code 1 git -c merge.conflictStyle=diff3 stash apply --label-ours=UP --label-theirs=STASH &&
1801 1801 test_grep "^<<<<<<< UP" conflict-file &&
1802 1802 test_grep "^||||||| Stash base" conflict-file &&
1803 1803 test_grep "^>>>>>>> STASH" conflict-file
@@ -1809,11 +1809,30 @@ test_expect_success 'apply with empty conflict labels' '
1809 1809 echo stashed >conflict-file &&
1810 1810 git stash push -m "stashed" &&
1811 1811 test_commit empty-label-upstream conflict-file upstream-content &&
1812 - test_must_fail git stash apply --label-ours= --label-theirs= &&
1812 + test_expect_code 1 git stash apply --label-ours= --label-theirs= &&
1813 1813 test_grep "^<<<<<<<$" conflict-file &&
1814 1814 test_grep "^>>>>>>>$" conflict-file
1815 1815 '
1816 1816
1817 + test_expect_success 'pop exits 1 on conflicts and keeps the stash entry' '
1818 + git reset --hard initial &&
1819 + echo stashed >file &&
1820 + git stash push -m pop-stashed &&
1821 + test_commit pop-upstream file upstream-content &&
1822 + test_expect_code 1 git stash pop &&
1823 + git stash list >list &&
1824 + test_grep pop-stashed list
1825 + '
1826 +
1827 + test_expect_success 'stash branch exits with a non-1 status on errors' '
1828 + git reset --hard initial &&
1829 + echo stashed >file &&
1830 + git stash push -m branch-stashed &&
1831 + test_expect_code 128 git stash branch conflicting-branch refs/heads/does-not-exist &&
1832 + git stash list >list &&
1833 + test_grep branch-stashed list
1834 + '
1835 +
1817 1836 test_expect_success 'stash show --include-untracked includes untracked files' '
1818 1837 git reset --hard &&
1819 1838