2021-12-29 05:33:38 +02:00
package commands
import (
2023-07-10 10:52:08 +02:00
"strings"
"time"
2021-12-29 05:33:38 +02:00
"github.com/jesseduffield/lazygit/pkg/commands/oscommands"
"github.com/sirupsen/logrus"
)
// here we're wrapping the default command runner in some git-specific stuff e.g. retry logic if we get an error due to the presence of .git/index.lock
2023-07-10 10:52:08 +02:00
const (
WaitTime = 50 * time . Millisecond
RetryCount = 5
)
2021-12-29 05:33:38 +02:00
type gitCmdObjRunner struct {
log * logrus . Entry
innerRunner oscommands . ICmdObjRunner
}
func ( self * gitCmdObjRunner ) Run ( cmdObj oscommands . ICmdObj ) error {
_ , err := self . RunWithOutput ( cmdObj )
return err
}
func ( self * gitCmdObjRunner ) RunWithOutput ( cmdObj oscommands . ICmdObj ) ( string , error ) {
2023-07-10 10:52:08 +02:00
var output string
var err error
for i := 0 ; i < RetryCount ; i ++ {
newCmdObj := cmdObj . Clone ( )
output , err = self . innerRunner . RunWithOutput ( newCmdObj )
if err == nil || ! strings . Contains ( output , ".git/index.lock" ) {
return output , err
}
// if we have an error based on the index lock, we should wait a bit and then retry
self . log . Warn ( "index.lock prevented command from running. Retrying command after a small wait" )
time . Sleep ( WaitTime )
}
return output , err
2021-12-29 05:33:38 +02:00
}
2022-08-02 01:32:28 +02:00
func ( self * gitCmdObjRunner ) RunWithOutputs ( cmdObj oscommands . ICmdObj ) ( string , string , error ) {
2023-07-10 10:52:08 +02:00
var stdout , stderr string
var err error
for i := 0 ; i < RetryCount ; i ++ {
newCmdObj := cmdObj . Clone ( )
stdout , stderr , err = self . innerRunner . RunWithOutputs ( newCmdObj )
if err == nil || ! strings . Contains ( stdout + stderr , ".git/index.lock" ) {
return stdout , stderr , err
}
// if we have an error based on the index lock, we should wait a bit and then retry
self . log . Warn ( "index.lock prevented command from running. Retrying command after a small wait" )
time . Sleep ( WaitTime )
}
return stdout , stderr , err
2022-08-02 01:32:28 +02:00
}
2023-07-10 10:52:08 +02:00
// Retry logic not implemented here, but these commands typically don't need to obtain a lock.
2021-12-30 02:22:29 +02:00
func ( self * gitCmdObjRunner ) RunAndProcessLines ( cmdObj oscommands . ICmdObj , onLine func ( line string ) ( bool , error ) ) error {
return self . innerRunner . RunAndProcessLines ( cmdObj , onLine )
2021-12-29 05:33:38 +02:00
}