Skip to content

Commit 7e0315e

Browse files
authored
fix: index lock race (#44)
The `commandExecutedMsg` handler already calls `fetchPanelContent` for `FilesPanel`, `CommitsPanel`, `BranchesPanel`, and `StatusPanel`. By removing the duplicate concurrent fetches from the action handlers, the git write operation completes and releases "index.lock" before any read operations begin. Before using `tea.Batch`, all commands were running concurrently resulting in a race condition over `index.lock`.
1 parent 81cec7b commit 7e0315e

1 file changed

Lines changed: 48 additions & 72 deletions

File tree

internal/tui/update.go

Lines changed: 48 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -657,35 +657,28 @@ func (m *Model) handleFilesPanelKeys(msg tea.KeyMsg) tea.Cmd {
657657
}
658658

659659
case key.Matches(msg, keys.StageItem):
660-
return tea.Batch(
661-
func() tea.Msg {
662-
var cmdStr string
663-
var err error
664-
if status[0] == ' ' || status[0] == '?' {
665-
_, cmdStr, err = m.git.AddFiles([]string{filePath})
666-
} else {
667-
_, cmdStr, err = m.git.ResetFiles([]string{filePath})
668-
}
669-
if err != nil {
670-
return errMsg{err}
671-
}
672-
return commandExecutedMsg{cmdStr}
673-
},
674-
m.fetchPanelContent(FilesPanel),
675-
m.fetchPanelContent(StatusPanel),
676-
)
660+
return func() tea.Msg {
661+
var cmdStr string
662+
var err error
663+
if status[0] == ' ' || status[0] == '?' {
664+
_, cmdStr, err = m.git.AddFiles([]string{filePath})
665+
} else {
666+
_, cmdStr, err = m.git.ResetFiles([]string{filePath})
667+
}
668+
if err != nil {
669+
return errMsg{err}
670+
}
671+
return commandExecutedMsg{cmdStr}
672+
}
677673

678674
case key.Matches(msg, keys.StageAll):
679-
return tea.Batch(
680-
func() tea.Msg {
681-
_, cmdStr, err := m.git.AddFiles([]string{"."})
682-
if err != nil {
683-
return errMsg{err}
684-
}
685-
return commandExecutedMsg{cmdStr}
686-
},
687-
m.fetchPanelContent(FilesPanel),
688-
)
675+
return func() tea.Msg {
676+
_, cmdStr, err := m.git.AddFiles([]string{"."})
677+
if err != nil {
678+
return errMsg{err}
679+
}
680+
return commandExecutedMsg{cmdStr}
681+
}
689682

690683
case key.Matches(msg, keys.Discard):
691684
m.mode = modeConfirm
@@ -708,17 +701,13 @@ func (m *Model) handleFilesPanelKeys(msg tea.KeyMsg) tea.Cmd {
708701
}
709702

710703
case key.Matches(msg, keys.StashAll):
711-
return tea.Batch(
712-
func() tea.Msg {
713-
_, cmdStr, err := m.git.StashAll()
714-
if err != nil {
715-
return errMsg{err}
716-
}
717-
return commandExecutedMsg{cmdStr}
718-
},
719-
m.fetchPanelContent(FilesPanel),
720-
m.fetchPanelContent(StashPanel),
721-
)
704+
return func() tea.Msg {
705+
_, cmdStr, err := m.git.StashAll()
706+
if err != nil {
707+
return errMsg{err}
708+
}
709+
return commandExecutedMsg{cmdStr}
710+
}
722711
}
723712
return nil
724713
}
@@ -740,18 +729,13 @@ func (m *Model) handleBranchesPanelKeys(msg tea.KeyMsg) tea.Cmd {
740729

741730
switch {
742731
case key.Matches(msg, keys.Checkout):
743-
return tea.Batch(
744-
func() tea.Msg {
745-
_, cmdStr, err := m.git.Checkout(branchName)
746-
if err != nil {
747-
return errMsg{err}
748-
}
749-
return commandExecutedMsg{cmdStr}
750-
},
751-
m.fetchPanelContent(StatusPanel),
752-
m.fetchPanelContent(BranchesPanel),
753-
m.fetchPanelContent(CommitsPanel),
754-
)
732+
return func() tea.Msg {
733+
_, cmdStr, err := m.git.Checkout(branchName)
734+
if err != nil {
735+
return errMsg{err}
736+
}
737+
return commandExecutedMsg{cmdStr}
738+
}
755739

756740
case key.Matches(msg, keys.NewBranch):
757741
m.mode = modeInput
@@ -909,30 +893,22 @@ func (m *Model) handleStashPanelKeys(msg tea.KeyMsg) tea.Cmd {
909893

910894
switch {
911895
case key.Matches(msg, keys.StashApply):
912-
return tea.Batch(
913-
func() tea.Msg {
914-
_, cmdStr, err := m.git.Stash(git.StashOptions{Apply: true, StashID: stashID})
915-
if err != nil {
916-
return errMsg{err}
917-
}
918-
return commandExecutedMsg{cmdStr}
919-
},
920-
m.fetchPanelContent(FilesPanel),
921-
m.fetchPanelContent(StashPanel),
922-
)
896+
return func() tea.Msg {
897+
_, cmdStr, err := m.git.Stash(git.StashOptions{Apply: true, StashID: stashID})
898+
if err != nil {
899+
return errMsg{err}
900+
}
901+
return commandExecutedMsg{cmdStr}
902+
}
923903

924904
case key.Matches(msg, keys.StashPop):
925-
return tea.Batch(
926-
func() tea.Msg {
927-
_, cmdStr, err := m.git.Stash(git.StashOptions{Pop: true, StashID: stashID})
928-
if err != nil {
929-
return errMsg{err}
930-
}
931-
return commandExecutedMsg{cmdStr}
932-
},
933-
m.fetchPanelContent(FilesPanel),
934-
m.fetchPanelContent(StashPanel),
935-
)
905+
return func() tea.Msg {
906+
_, cmdStr, err := m.git.Stash(git.StashOptions{Pop: true, StashID: stashID})
907+
if err != nil {
908+
return errMsg{err}
909+
}
910+
return commandExecutedMsg{cmdStr}
911+
}
936912

937913
case key.Matches(msg, keys.StashDrop):
938914
m.mode = modeConfirm

0 commit comments

Comments
 (0)