git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Benjamin Woodruff via GitGitGadget" <gitgitgadget@gmail.com>
To: git@vger.kernel.org
Cc: Benjamin Woodruff <github@benjam.info>,
	Benjamin Woodruff <benjamin.woodruff@vercel.com>
Subject: [PATCH 1/2] describe: implement --no-optional-locks
Date: Thu, 06 Mar 2025 05:58:04 +0000	[thread overview]
Message-ID: <46c90ba12a5afd0e697a224c7951771b649b336b.1741240685.git.gitgitgadget@gmail.com> (raw)
In-Reply-To: <pull.1872.git.1741240685.gitgitgadget@gmail.com>

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 --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
-- 
gitgitgadget


  reply	other threads:[~2025-03-06  5:58 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-03-06  5:58 [PATCH 0/2] describe and diff: implement --no-optional-locks Benjamin Woodruff via GitGitGadget
2025-03-06  5:58 ` Benjamin Woodruff via GitGitGadget [this message]
2025-03-06  5:58 ` [PATCH 2/2] " Benjamin Woodruff via GitGitGadget
2025-03-06 16:11 ` [PATCH 0/2] describe and " Junio C Hamano
2025-03-09  3:39   ` Jeff King
2025-03-10 12:25     ` Junio C Hamano
2025-03-10 16:08       ` Jeff King
2025-03-10 18:53         ` Junio C Hamano
2025-03-10 20:50           ` Benjamin Woodruff
2025-03-10 23:04             ` Junio C Hamano
2025-03-11  2:10             ` Jeff King

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=46c90ba12a5afd0e697a224c7951771b649b336b.1741240685.git.gitgitgadget@gmail.com \
    --to=gitgitgadget@gmail.com \
    --cc=benjamin.woodruff@vercel.com \
    --cc=git@vger.kernel.org \
    --cc=github@benjam.info \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).