]> Kevux Git Server - rit/commitdiff
read-cache: report lock error when refreshing index
authorHan Young <hanyang.tony@bytedance.com>
Thu, 3 Jul 2025 07:45:02 +0000 (15:45 +0800)
committerJunio C Hamano <gitster@pobox.com>
Mon, 7 Jul 2025 13:49:07 +0000 (06:49 -0700)
In the repo_refresh_and_write_index of read-cache.c, we return -1 to
indicate that writing the index to disk failed.
However, callers do not use this information. Commands such as stash print
  "could not write index"
and then exit, which does not help to discover the exact problem.

We can let repo_hold_locked_index print the error message if the locking
failed.

Signed-off-by: Han Young <hanyang.tony@bytedance.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
read-cache.c
t/t3903-stash.sh

index c0bb760..c44efc8 100644 (file)
@@ -1456,7 +1456,8 @@ int repo_refresh_and_write_index(struct repository *repo,
        struct lock_file lock_file = LOCK_INIT;
        int fd, ret = 0;
 
-       fd = repo_hold_locked_index(repo, &lock_file, 0);
+       fd = repo_hold_locked_index(repo, &lock_file,
+                                   gentle ? 0 : LOCK_REPORT_ON_ERROR);
        if (!gentle && fd < 0)
                return -1;
        if (refresh_index(repo->index, refresh_flags, pathspec, seen, header_msg))
index 74666ff..72ab9da 100755 (executable)
@@ -1549,11 +1549,9 @@ test_expect_success 'stash create reports a locked index' '
                echo change >A.file &&
                touch .git/index.lock &&
 
-               cat >expect <<-EOF &&
-               error: could not write index
-               EOF
                test_must_fail git stash create 2>err &&
-               test_cmp expect err
+               test_grep "error: could not write index" err &&
+               test_grep "error: Unable to create '.*index.lock'" err
        )
 '
 
@@ -1566,11 +1564,9 @@ test_expect_success 'stash push reports a locked index' '
                echo change >A.file &&
                touch .git/index.lock &&
 
-               cat >expect <<-EOF &&
-               error: could not write index
-               EOF
                test_must_fail git stash push 2>err &&
-               test_cmp expect err
+               test_grep "error: could not write index" err &&
+               test_grep "error: Unable to create '.*index.lock'" err
        )
 '
 
@@ -1584,11 +1580,9 @@ test_expect_success 'stash apply reports a locked index' '
                git stash push &&
                touch .git/index.lock &&
 
-               cat >expect <<-EOF &&
-               error: could not write index
-               EOF
                test_must_fail git stash apply 2>err &&
-               test_cmp expect err
+               test_grep "error: could not write index" err &&
+               test_grep "error: Unable to create '.*index.lock'" err
        )
 '