refactoring [git]: Align READ_OPTIONAL_LOCKING behavior with READ if write side-effect can be skipped

GitOrigin-RevId: 2f4fac1c307f199c9c83c94c78c04c67c6381e48
This commit is contained in:
Ilia.Shulgin
2026-02-16 19:09:56 +00:00
committed by intellij-monorepo-bot
parent d8ed5c2afa
commit 882942ed7b
2 changed files with 15 additions and 5 deletions
@@ -97,6 +97,12 @@ public final class GitCommand {
@ApiStatus.Internal
public enum LockingPolicy {
READ,
/**
* Commands with non-mandatory write side effects (e.g. git status) can be executed without locking the index
* by setting env variable "GIT_OPTIONAL_LOCKS=0" or passing "--no-optional-locks".
* </br>
* See the registry key "git.use.no.optional.locks"
*/
READ_OPTIONAL_LOCKING,
WRITE
}
@@ -416,16 +416,13 @@ public abstract class GitImplBase implements Git {
private static @NotNull AccessToken lock(@NotNull GitLineHandler handler, boolean canSuppressOptionalLocks) {
Project project = handler.project();
LockingPolicy lockingPolicy = handler.getCommand().lockingPolicy();
if (project == null || project.isDefault() || lockingPolicy == READ) {
if (project == null || project.isDefault() || !shouldTakeWriteLock(handler, canSuppressOptionalLocks)) {
return AccessToken.EMPTY_ACCESS_TOKEN;
}
ReadWriteLock executionLock = GitVcs.getInstance(project).getCommandLock();
Lock lock = lockingPolicy == READ_OPTIONAL_LOCKING && canSuppressOptionalLocks
? executionLock.readLock()
: executionLock.writeLock();
Lock lock = executionLock.writeLock();
ProgressIndicatorUtils.awaitWithCheckCanceled(lock);
return new AccessToken() {
@@ -436,6 +433,13 @@ public abstract class GitImplBase implements Git {
};
}
private static boolean shouldTakeWriteLock(@NotNull GitLineHandler handler, boolean canSuppressOptionalLocks) {
LockingPolicy lockingPolicy = handler.getCommand().lockingPolicy();
return lockingPolicy != READ &&
// If lock can't be suppressed, then command can be executed with writing side effects
(lockingPolicy != READ_OPTIONAL_LOCKING || !canSuppressOptionalLocks);
}
public static boolean looksLikeProgress(@NotNull String line) {
if (PROGRESS_PATTERN.matcher(line).matches()) return true;
return ContainerUtil.exists(SUPPRESSED_PROGRESS_INDICATORS, prefix -> {