# [PATCH] read-cache: report lock error when refreshing index

6 messages from 2025-07-01 to 2025-07-08. Participants: Han Young, Justin Tobler, Junio C Hamano.
Thread: https://gitlist.dev/t/63720

## Han Young, 2025-07-01 11:57

Subject: [PATCH] read-cache: report lock error when refreshing index
Message-ID: <20250701115719.85226-1-hanyang.tony@bytedance.com>
URL: https://gitlist.dev/e/20250701115719.85226-1-hanyang.tony%40bytedance.com

```
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>
---
 read-cache.c     |  2 +-
 t/t3903-stash.sh | 15 +++------------
 2 files changed, 4 insertions(+), 13 deletions(-)

diff --git a/read-cache.c b/read-cache.c
index c0bb760ad..50e842bfa 100644
--- a/read-cache.c
+++ b/read-cache.c
@@ -1456,7 +1456,7 @@ 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))
diff --git a/t/t3903-stash.sh b/t/t3903-stash.sh
index 35b85c790..39098ade4 100755
--- a/t/t3903-stash.sh
+++ b/t/t3903-stash.sh
@@ -1571,11 +1571,8 @@ 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: Unable to create '.*index.lock'" err
 	)
 '
 
@@ -1588,11 +1585,8 @@ 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: Unable to create '.*index.lock'" err
 	)
 '
 
@@ -1606,11 +1600,8 @@ 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: Unable to create '.*index.lock'" err
 	)
 '
 
-- 
2.50.0


```

## Justin Tobler, 2025-07-01 16:08

Subject: Re: [PATCH] read-cache: report lock error when refreshing index
Message-ID: <t4czubzmfuihxzmefwwhcel5qyss35gmodhfhvkfyiwitb5osw@d33acdbtds63>
URL: https://gitlist.dev/e/t4czubzmfuihxzmefwwhcel5qyss35gmodhfhvkfyiwitb5osw%40d33acdbtds63
In-Reply-To: <20250701115719.85226-1-hanyang.tony@bytedance.com>

```
On 25/07/01 07:57PM, Han Young wrote:
> 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.

Ok, so `repo_refresh_and_write_index()` returns -1 when the "index.lock"
cannot be acquired via `repo_hold_locked_index()` or the index write
fails via `write_locked_index()`. The function returns 1 when the index
refreshed fails via `refresh_index()`.

Callers of `repo_refresh_and_write_index()` currently do not
differentiate between any of these failure types though. This patch
wants to begin printing an error message if the lock file fails to be
created. This would provide more insight into why the failure occurred
than simply "error: could not write index". That makes sense.

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

It looks like `repo_hold_locked_index()` already has the
`LOCK_REPORT_ON_ERROR` flag which will print the message we want.

> Signed-off-by: Han Young <hanyang.tony@bytedance.com>
> ---
>  read-cache.c     |  2 +-
>  t/t3903-stash.sh | 15 +++------------
>  2 files changed, 4 insertions(+), 13 deletions(-)
> 
> diff --git a/read-cache.c b/read-cache.c
> index c0bb760ad..50e842bfa 100644
> --- a/read-cache.c
> +++ b/read-cache.c
> @@ -1456,7 +1456,7 @@ 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);

Here we begin passing the `LOCK_REPORT_ON_ERROR` flag to print the error
message only if `gentle` is not set. Makes sense.

>  	if (!gentle && fd < 0)
>  		return -1;
>  	if (refresh_index(repo->index, refresh_flags, pathspec, seen, header_msg))
> diff --git a/t/t3903-stash.sh b/t/t3903-stash.sh
> index 35b85c790..39098ade4 100755
> --- a/t/t3903-stash.sh
> +++ b/t/t3903-stash.sh
> @@ -1571,11 +1571,8 @@ 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: Unable to create '.*index.lock'" err

The test now checks for the explicit lock error message. The check for
the "error: could not write index" message is also removed even though
it should still be present in the output. Should we also continue to
grep for that message too?

>  	)
>  '
>  
> @@ -1588,11 +1585,8 @@ 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: Unable to create '.*index.lock'" err

Same question about testing for the previous error message here.

>  	)
>  '
>  
> @@ -1606,11 +1600,8 @@ 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: Unable to create '.*index.lock'" err

and here

>  	)
>  '

Thanks,
-Justin

```

## Junio C Hamano, 2025-07-01 19:21

Subject: Re: [PATCH] read-cache: report lock error when refreshing index
Message-ID: <xmqqv7obk98v.fsf@gitster.g>
URL: https://gitlist.dev/e/xmqqv7obk98v.fsf%40gitster.g
In-Reply-To: <t4czubzmfuihxzmefwwhcel5qyss35gmodhfhvkfyiwitb5osw@d33acdbtds63>

```
Justin Tobler <jltobler@gmail.com> writes:

>>  		test_must_fail git stash create 2>err &&
>> -		test_cmp expect err
>> +		test_grep "error: Unable to create '.*index.lock'" err
>
> The test now checks for the explicit lock error message. The check for
> the "error: could not write index" message is also removed even though
> it should still be present in the output. Should we also continue to
> grep for that message too?

Probably.  I was wondering which part of the patch removed the
existing message while reading this update to the test.

Thanks.

```

## Han Young, 2025-07-03 07:45

Subject: [PATCH v2] read-cache: report lock error when refreshing index
Message-ID: <20250703074502.45593-1-hanyang.tony@bytedance.com>
URL: https://gitlist.dev/e/20250703074502.45593-1-hanyang.tony%40bytedance.com
In-Reply-To: <20250701115719.85226-1-hanyang.tony@bytedance.com>

```
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>
---
Changes since v1:
also check the "could not write index" error output

 read-cache.c     |  2 +-
 t/t3903-stash.sh | 18 ++++++------------
 2 files changed, 7 insertions(+), 13 deletions(-)

diff --git a/read-cache.c b/read-cache.c
index c0bb760ad..50e842bfa 100644
--- a/read-cache.c
+++ b/read-cache.c
@@ -1456,7 +1456,7 @@ 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))
diff --git a/t/t3903-stash.sh b/t/t3903-stash.sh
index c58ccb136..0bb4648e3 100755
--- a/t/t3903-stash.sh
+++ b/t/t3903-stash.sh
@@ -1672,11 +1672,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
 	)
 '
 
@@ -1689,11 +1687,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
 	)
 '
 
@@ -1707,11 +1703,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
 	)
 '
 
-- 
2.50.0


```

## Junio C Hamano, 2025-07-07 18:01

Subject: Re: [PATCH v2] read-cache: report lock error when refreshing index
Message-ID: <xmqq8qkz5193.fsf@gitster.g>
URL: https://gitlist.dev/e/xmqq8qkz5193.fsf%40gitster.g
In-Reply-To: <20250703074502.45593-1-hanyang.tony@bytedance.com>

```
Han Young <hanyang.tony@bytedance.com> writes:

> 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>
> ---
> Changes since v1:
> also check the "could not write index" error output
>
>  read-cache.c     |  2 +-
>  t/t3903-stash.sh | 18 ++++++------------
>  2 files changed, 7 insertions(+), 13 deletions(-)
>
> diff --git a/read-cache.c b/read-cache.c
> index c0bb760ad..50e842bfa 100644
> --- a/read-cache.c
> +++ b/read-cache.c
> @@ -1456,7 +1456,7 @@ 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);

Let's wrap this line to stay under 80-columns, i.e.

	fd = repo_hold_locked_index(repo, &lock_file,
				    gentle ? 0 : LOCK_REPORT_ON_ERROR);

No need to resend, as I've done so locally while applying.

```

## Han Young, 2025-07-08 02:47

Subject: Re: [External] Re: [PATCH v2] read-cache: report lock error when refreshing index
Message-ID: <CAG1j3zFz0RQ3mf=BRAQhCDWucOR0J9Z26Wmmz8G6+9dMbNO3Ag@mail.gmail.com>
URL: https://gitlist.dev/e/CAG1j3zFz0RQ3mf%3DBRAQhCDWucOR0J9Z26Wmmz8G6%2B9dMbNO3Ag%40mail.gmail.com
In-Reply-To: <xmqq8qkz5193.fsf@gitster.g>

```
On Tue, Jul 8, 2025 at 2:01 AM Junio C Hamano <gitster@pobox.com> wrote:
> No need to resend, as I've done so locally while applying.

Thank you, I'll be careful next time.

```
