diff mbox series

[1/2] describe: implement --no-optional-locks

Message ID 46c90ba12a5afd0e697a224c7951771b649b336b.1741240685.git.gitgitgadget@gmail.com (mailing list archive)
State New
Headers show
Series describe and diff: implement --no-optional-locks | expand

Commit Message

Benjamin Woodruff March 6, 2025, 5:58 a.m. UTC
From: Benjamin Woodruff <benjamin.woodruff@vercel.com>

When used with `--dirty`, describe may update the index in the
background for similar performance reasons to `git-status`.

This commit implements the `--no-optional-locks` option for
`git describe`, which allows scripts to bypass this behavior.

Signed-off-by: Benjamin Woodruff <benjamin.woodruff@vercel.com>
---
 Documentation/git-describe.adoc | 12 ++++++++++++
 Documentation/git.adoc          |  3 ++-
 builtin/describe.c              | 12 +++++++-----
 t/t6120-describe.sh             |  8 ++++++++
 4 files changed, 29 insertions(+), 6 deletions(-)
diff mbox series

Patch

diff --git a/Documentation/git-describe.adoc b/Documentation/git-describe.adoc
index 08ff715709c..97b4c656078 100644
--- a/Documentation/git-describe.adoc
+++ b/Documentation/git-describe.adoc
@@ -198,6 +198,18 @@  selected and output.  Here fewest commits different is defined as
 the number of commits which would be shown by `git log tag..input`
 will be the smallest number of commits possible.
 
+BACKGROUND REFRESH
+------------------
+
+By default, `git describe --dirty` will automatically refresh the index,
+similar to the background refresh functionality of
+linkgit:git-status[1]. Writing out the updated index is an optimization
+that isn't strictly necessary. When `describe` is run in the background,
+the lock held during the write may conflict with other simultaneous
+processes, causing them to fail. Scripts running `describe` in the
+background should consider using `git --no-optional-locks status` (see
+linkgit:git[1] for details).
+
 BUGS
 ----
 
diff --git a/Documentation/git.adoc b/Documentation/git.adoc
index 743b7b00e4d..f084b2f0f1e 100644
--- a/Documentation/git.adoc
+++ b/Documentation/git.adoc
@@ -189,7 +189,8 @@  If you just want to run git as if it was started in `<path>` then use
 
 --no-optional-locks::
 	Do not perform optional operations that require locks. This is
-	equivalent to setting the `GIT_OPTIONAL_LOCKS` to `0`.
+	equivalent to setting the `GIT_OPTIONAL_LOCKS` to `0`. This
+	functionality is implemented for `git status` and `git describe`.
 
 --no-advice::
 	Disable all advice hints from being printed.
diff --git a/builtin/describe.c b/builtin/describe.c
index e2e73f3d757..d4fea55352d 100644
--- a/builtin/describe.c
+++ b/builtin/describe.c
@@ -704,7 +704,6 @@  int cmd_describe(int argc,
 		} else if (dirty) {
 			struct lock_file index_lock = LOCK_INIT;
 			struct rev_info revs;
-			int fd;
 
 			setup_work_tree();
 			prepare_repo_settings(the_repository);
@@ -712,10 +711,13 @@  int cmd_describe(int argc,
 			repo_read_index(the_repository);
 			refresh_index(the_repository->index, REFRESH_QUIET|REFRESH_UNMERGED,
 				      NULL, NULL, NULL);
-			fd = repo_hold_locked_index(the_repository,
-						    &index_lock, 0);
-			if (0 <= fd)
-				repo_update_index_if_able(the_repository, &index_lock);
+			if (use_optional_locks()) {
+				int fd = repo_hold_locked_index(the_repository,
+								&index_lock, 0);
+				if (0 <= fd)
+					repo_update_index_if_able(the_repository,
+								  &index_lock);
+			}
 
 			repo_init_revisions(the_repository, &revs, prefix);
 
diff --git a/t/t6120-describe.sh b/t/t6120-describe.sh
index 76843a61691..ae7b6901ed6 100755
--- a/t/t6120-describe.sh
+++ b/t/t6120-describe.sh
@@ -749,4 +749,12 @@  test_expect_success 'do not be fooled by invalid describe format ' '
 	test_must_fail git cat-file -t "refs/tags/super-invalid/./../...../ ~^:/?*[////\\\\\\&}/busted.lock-42-g"$(cat out)
 '
 
+test_expect_success '--no-optional-locks prevents index update' '
+	test_set_magic_mtime .git/index &&
+	git --no-optional-locks describe --dirty &&
+	test_is_magic_mtime .git/index &&
+	git describe --dirty &&
+	! test_is_magic_mtime .git/index
+'
+
 test_done