Skip to content

Commit d4e9383

Browse files
committed
Merge branch 'jk/no-optional-locks'
Some commands (most notably "git status") makes an opportunistic update when performing a read-only operation to help optimize later operations in the same repository. The new "--no-optional-locks" option can be passed to Git to disable them. * jk/no-optional-locks: git: add --no-optional-locks option
2 parents d9ec072 + 27344d6 commit d4e9383

File tree

6 files changed

+41
-1
lines changed

6 files changed

+41
-1
lines changed

Documentation/git.txt

+12
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,10 @@ foo.bar= ...`) sets `foo.bar` to the empty string which ` git config
159159
Add "icase" magic to all pathspec. This is equivalent to setting
160160
the `GIT_ICASE_PATHSPECS` environment variable to `1`.
161161

162+
--no-optional-locks::
163+
Do not perform optional operations that require locks. This is
164+
equivalent to setting the `GIT_OPTIONAL_LOCKS` to `0`.
165+
162166
GIT COMMANDS
163167
------------
164168

@@ -697,6 +701,14 @@ of clones and fetches.
697701
which feed potentially-untrusted URLS to git commands. See
698702
linkgit:git-config[1] for more details.
699703

704+
`GIT_OPTIONAL_LOCKS`::
705+
If set to `0`, Git will complete any requested operation without
706+
performing any optional sub-operations that require taking a lock.
707+
For example, this will prevent `git status` from refreshing the
708+
index as a side effect. This is useful for processes running in
709+
the background which do not want to cause lock contention with
710+
other operations on the repository. Defaults to `1`.
711+
700712
Discussion[[Discussion]]
701713
------------------------
702714

builtin/commit.c

+4-1
Original file line numberDiff line numberDiff line change
@@ -1392,7 +1392,10 @@ int cmd_status(int argc, const char **argv, const char *prefix)
13921392
read_cache_preload(&s.pathspec);
13931393
refresh_index(&the_index, REFRESH_QUIET|REFRESH_UNMERGED, &s.pathspec, NULL, NULL);
13941394

1395-
fd = hold_locked_index(&index_lock, 0);
1395+
if (use_optional_locks())
1396+
fd = hold_locked_index(&index_lock, 0);
1397+
else
1398+
fd = -1;
13961399

13971400
s.is_initial = get_oid(s.reference, &oid) ? 1 : 0;
13981401
if (!s.is_initial)

cache.h

+6
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,7 @@ static inline enum object_type object_type(unsigned int mode)
444444
#define GIT_NOGLOB_PATHSPECS_ENVIRONMENT "GIT_NOGLOB_PATHSPECS"
445445
#define GIT_ICASE_PATHSPECS_ENVIRONMENT "GIT_ICASE_PATHSPECS"
446446
#define GIT_QUARANTINE_ENVIRONMENT "GIT_QUARANTINE_PATH"
447+
#define GIT_OPTIONAL_LOCKS_ENVIRONMENT "GIT_OPTIONAL_LOCKS"
447448

448449
/*
449450
* This environment variable is expected to contain a boolean indicating
@@ -783,6 +784,11 @@ extern int protect_ntfs;
783784
*/
784785
extern int ref_paranoia;
785786

787+
/*
788+
* Returns the boolean value of $GIT_OPTIONAL_LOCKS (or the default value).
789+
*/
790+
int use_optional_locks(void);
791+
786792
/*
787793
* The character that begins a commented line in user-editable file
788794
* that is subject to stripspace.

environment.c

+5
Original file line numberDiff line numberDiff line change
@@ -338,3 +338,8 @@ void reset_shared_repository(void)
338338
{
339339
need_shared_repository_from_config = 1;
340340
}
341+
342+
int use_optional_locks(void)
343+
{
344+
return git_env_bool(GIT_OPTIONAL_LOCKS_ENVIRONMENT, 1);
345+
}

git.c

+4
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,10 @@ static int handle_options(const char ***argv, int *argc, int *envchanged)
182182
setenv(GIT_ICASE_PATHSPECS_ENVIRONMENT, "1", 1);
183183
if (envchanged)
184184
*envchanged = 1;
185+
} else if (!strcmp(cmd, "--no-optional-locks")) {
186+
setenv(GIT_OPTIONAL_LOCKS_ENVIRONMENT, "0", 1);
187+
if (envchanged)
188+
*envchanged = 1;
185189
} else if (!strcmp(cmd, "--shallow-file")) {
186190
(*argv)++;
187191
(*argc)--;

t/t7508-status.sh

+10
Original file line numberDiff line numberDiff line change
@@ -1670,4 +1670,14 @@ test_expect_success '"Initial commit" should not be noted in commit template' '
16701670
test_i18ngrep ! "Initial commit" output
16711671
'
16721672

1673+
test_expect_success '--no-optional-locks prevents index update' '
1674+
test-chmtime =1234567890 .git/index &&
1675+
git --no-optional-locks status &&
1676+
test-chmtime -v +0 .git/index >out &&
1677+
grep ^1234567890 out &&
1678+
git status &&
1679+
test-chmtime -v +0 .git/index >out &&
1680+
! grep ^1234567890 out
1681+
'
1682+
16731683
test_done

0 commit comments

Comments
 (0)