mirror of
https://github.com/jesseduffield/lazygit.git
synced 2026-09-10 07:36:27 -04:00
Commit d94f2f05 dropped the GIT_OPTIONAL_LOCKS=0 env var that we used
to set on every git command, and re-added lock suppression only as a
--no-optional-locks flag on the background files refresh. The intent
was sound — a foreground `git status` should persist git's refreshed
stat-cache — but the change was too broad: it stopped suppressing
optional locks for every other command too.
The one that bites is the main-view diff. When a folder containing
submodules is selected, we render `git diff --submodule -- <dir>`, and
`--submodule` makes git run `git status` inside each submodule to
describe its "modified" state. That status now grabs the submodule's
index.lock. It runs as a PTY task on its own goroutine, so it races
any submodule-mutating action the user triggers — e.g. resetting a
submodule runs `git -C <submodule> stash`, which then fails with
"index.lock: File exists". This is what made submodule/reset_folder
flaky. `git status` is in fact the only command that takes the
optional lock, but the env var also covered its use inside `git diff
--submodule`, inside PTY-run commands, and inside git's own submodule
child processes — none of which a per-command flag reaches cleanly.
Invert the polarity to match how it worked before d94f2f05: the git
command builder disables optional locks on every command by default,
and the single command that benefits from taking the lock — the
foreground files refresh — opts back in. This restores the original
contention avoidance (including against the user's terminal git) while
keeping d94f2f05's stat-cache-persistence win for the foreground
refresh.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
164 lines
4.4 KiB
Go
164 lines
4.4 KiB
Go
package git_commands
|
|
|
|
import (
|
|
"strings"
|
|
|
|
"github.com/jesseduffield/lazygit/pkg/commands/oscommands"
|
|
)
|
|
|
|
// OptionalLocksEnvVar is the name of the environment variable that tells git
|
|
// whether it may take "optional" locks — chiefly the index.lock that `git
|
|
// status` grabs to write back a refreshed stat-cache. We set it to 0 on every
|
|
// git command by default (see NewGitCmdObjBuilder) so our invocations never
|
|
// contend for index.lock, neither with each other (e.g. a main-view `git diff
|
|
// --submodule`, which runs `git status` inside submodules, racing a submodule
|
|
// action) nor with git commands the user runs in a terminal. The one command
|
|
// that opts back in is the foreground files refresh; see FileLoader.gitStatus.
|
|
const OptionalLocksEnvVar = "GIT_OPTIONAL_LOCKS"
|
|
|
|
// convenience struct for building git commands. Especially useful when
|
|
// including conditional args
|
|
type GitCommandBuilder struct {
|
|
// command string
|
|
args []string
|
|
}
|
|
|
|
func NewGitCmd(command string) *GitCommandBuilder {
|
|
return &GitCommandBuilder{args: []string{command}}
|
|
}
|
|
|
|
func (self *GitCommandBuilder) Arg(args ...string) *GitCommandBuilder {
|
|
self.args = append(self.args, args...)
|
|
|
|
return self
|
|
}
|
|
|
|
func (self *GitCommandBuilder) ArgIf(condition bool, ifTrue ...string) *GitCommandBuilder {
|
|
if condition {
|
|
self.Arg(ifTrue...)
|
|
}
|
|
|
|
return self
|
|
}
|
|
|
|
func (self *GitCommandBuilder) ArgIfElse(condition bool, ifTrue string, ifFalse string) *GitCommandBuilder {
|
|
if condition {
|
|
return self.Arg(ifTrue)
|
|
}
|
|
return self.Arg(ifFalse)
|
|
}
|
|
|
|
// GlobalArg adds top-level options for git itself (e.g. --no-optional-locks).
|
|
// Unlike Arg, these are prepended before the command, where git expects them.
|
|
func (self *GitCommandBuilder) GlobalArg(args ...string) *GitCommandBuilder {
|
|
self.args = append(append([]string{}, args...), self.args...)
|
|
|
|
return self
|
|
}
|
|
|
|
func (self *GitCommandBuilder) GlobalArgIf(condition bool, args ...string) *GitCommandBuilder {
|
|
if condition {
|
|
self.GlobalArg(args...)
|
|
}
|
|
|
|
return self
|
|
}
|
|
|
|
func (self *GitCommandBuilder) Config(value string) *GitCommandBuilder {
|
|
// config settings come before the command
|
|
self.args = append([]string{"-c", value}, self.args...)
|
|
|
|
return self
|
|
}
|
|
|
|
func (self *GitCommandBuilder) ConfigIf(condition bool, ifTrue string) *GitCommandBuilder {
|
|
if condition {
|
|
self.Config(ifTrue)
|
|
}
|
|
|
|
return self
|
|
}
|
|
|
|
// the -C arg will make git do a `cd` to the directory before doing anything else
|
|
func (self *GitCommandBuilder) Dir(path string) *GitCommandBuilder {
|
|
// repo path comes before the command
|
|
self.args = append([]string{"-C", path}, self.args...)
|
|
|
|
return self
|
|
}
|
|
|
|
func (self *GitCommandBuilder) DirIf(condition bool, path string) *GitCommandBuilder {
|
|
if condition {
|
|
return self.Dir(path)
|
|
}
|
|
|
|
return self
|
|
}
|
|
|
|
// Note, you may prefer to use the Dir method instead of this one
|
|
func (self *GitCommandBuilder) Worktree(path string) *GitCommandBuilder {
|
|
// worktree arg comes before the command
|
|
self.args = append([]string{"--work-tree", path}, self.args...)
|
|
|
|
return self
|
|
}
|
|
|
|
func (self *GitCommandBuilder) WorktreePathIf(condition bool, path string) *GitCommandBuilder {
|
|
if condition {
|
|
return self.Worktree(path)
|
|
}
|
|
|
|
return self
|
|
}
|
|
|
|
// Note, you may prefer to use the Dir method instead of this one
|
|
func (self *GitCommandBuilder) GitDir(path string) *GitCommandBuilder {
|
|
// git dir arg comes before the command
|
|
self.args = append([]string{"--git-dir", path}, self.args...)
|
|
|
|
return self
|
|
}
|
|
|
|
func (self *GitCommandBuilder) GitDirIf(condition bool, path string) *GitCommandBuilder {
|
|
if condition {
|
|
return self.GitDir(path)
|
|
}
|
|
|
|
return self
|
|
}
|
|
|
|
func (self *GitCommandBuilder) ToArgv() []string {
|
|
return append([]string{"git"}, self.args...)
|
|
}
|
|
|
|
func (self *GitCommandBuilder) ToString() string {
|
|
return strings.Join(self.ToArgv(), " ")
|
|
}
|
|
|
|
// runGitCmdOnPaths runs `git <subcommand> -- <paths...>`, splitting into
|
|
// multiple calls if needed to stay under the OS command-line length limit.
|
|
// Windows CreateProcess has a ~32 KB limit; we use 30 KB as a safe threshold.
|
|
func runGitCmdOnPaths(subcommand string, paths []string, cmd oscommands.ICmdObjBuilder) error {
|
|
const maxArgBytes = 30_000
|
|
|
|
start := 0
|
|
for start < len(paths) {
|
|
end := start
|
|
total := 0
|
|
for end < len(paths) {
|
|
total += len(paths[end]) + 1 // +1 for the separating space
|
|
if total > maxArgBytes && end > start {
|
|
break
|
|
}
|
|
end++
|
|
}
|
|
if err := cmd.New(NewGitCmd(subcommand).Arg("--").
|
|
Arg(paths[start:end]...).
|
|
ToArgv()).Run(); err != nil {
|
|
return err
|
|
}
|
|
start = end
|
|
}
|
|
return nil
|
|
}
|