]> git.ipfire.org Git - thirdparty/git.git/commitdiff
stash: be careful what we store
authorJunio C Hamano <gitster@pobox.com>
Wed, 11 Oct 2023 20:44:03 +0000 (13:44 -0700)
committerJunio C Hamano <gitster@pobox.com>
Wed, 11 Oct 2023 23:27:30 +0000 (16:27 -0700)
"git stash store" is meant to store what "git stash create"
produces, as these two are implementation details of the end-user
facing "git stash save" command.  Even though it is clearly
documented as such, users would try silly things like "git stash
store HEAD" to render their stash unusable.

Worse yet, because "git stash drop" does not allow such a stash
entry to be removed, "git stash clear" would be the only way to
recover from such a mishap.  Reuse the logic that allows "drop" to
refrain from working on such a stash entry to teach "store" to avoid
storing an object that is not a stash entry in the first place.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/stash.c
t/t3903-stash.sh

index 3a4f9fd566d27cfea7039f9c92f7a9730f37f912..8073ef4019e1aa32358322c394c56e21b4973115 100644 (file)
@@ -977,6 +977,12 @@ usage:
 static int do_store_stash(const struct object_id *w_commit, const char *stash_msg,
                          int quiet)
 {
+       struct stash_info info;
+       char revision[GIT_MAX_HEXSZ];
+
+       oid_to_hex_r(revision, w_commit);
+       assert_stash_like(&info, revision);
+
        if (!stash_msg)
                stash_msg = "Created via \"git stash store\".";
 
index 376cc8f4ab8429b0488ad23b0f9731c9af237124..35c8569aeac02c7d30cd02c535babd98c970712d 100755 (executable)
@@ -931,6 +931,10 @@ test_expect_success 'store called with invalid commit' '
        test_must_fail git stash store foo
 '
 
+test_expect_success 'store called with non-stash commit' '
+       test_must_fail git stash store HEAD
+'
+
 test_expect_success 'store updates stash ref and reflog' '
        git stash clear &&
        git reset --hard &&