2018-08-12 19:31:27 +10:00
package commands
import (
2019-02-19 23:36:29 +11:00
"io/ioutil"
2018-08-12 19:31:27 +10:00
"os"
2019-11-05 12:42:07 +11:00
"path/filepath"
2018-08-12 19:50:55 +10:00
"strings"
2021-04-05 21:08:33 +10:00
"time"
2019-02-18 21:29:43 +11:00
2019-02-11 21:30:27 +11:00
"github.com/go-errors/errors"
2020-10-06 20:50:54 +11:00
gogit "github.com/jesseduffield/go-git/v5"
2020-09-29 19:10:57 +10:00
"github.com/jesseduffield/lazygit/pkg/commands/oscommands"
2020-08-15 11:18:40 +10:00
"github.com/jesseduffield/lazygit/pkg/commands/patch"
2019-02-18 21:29:43 +11:00
"github.com/jesseduffield/lazygit/pkg/config"
2020-09-27 16:17:26 +10:00
"github.com/jesseduffield/lazygit/pkg/env"
2018-08-28 18:01:53 +10:00
"github.com/jesseduffield/lazygit/pkg/i18n"
2018-08-12 21:04:47 +10:00
"github.com/jesseduffield/lazygit/pkg/utils"
2018-08-25 08:51:47 +10:00
"github.com/sirupsen/logrus"
2018-08-12 19:31:27 +10:00
)
2019-11-17 12:07:36 +11:00
// this takes something like:
// * (HEAD detached at 264fc6f5)
// remotes
// and returns '264fc6f5' as the second match
const CurrentBranchNameRegex = ` (?m)^\*.*?([^ ]*?)\)?$ `
2018-09-02 17:15:27 +02:00
// GitCommand is our main git interface
type GitCommand struct {
2019-11-04 19:47:25 +11:00
Log * logrus . Entry
2020-09-29 19:10:57 +10:00
OSCommand * oscommands . OSCommand
2019-11-04 19:47:25 +11:00
Repo * gogit . Repository
2020-10-04 11:00:48 +11:00
Tr * i18n . TranslationSet
2019-11-04 19:47:25 +11:00
Config config . AppConfigurer
2020-12-21 09:38:36 +11:00
getGitConfigValue func ( string ) ( string , error )
2019-11-04 19:47:25 +11:00
removeFile func ( string ) error
DotGitDir string
onSuccessfulContinue func ( ) error
2020-08-15 11:18:40 +10:00
PatchManager * patch . PatchManager
2020-05-15 21:26:02 +10:00
// Push to current determines whether the user has configured to push to the remote branch of the same name as the current or not
PushToCurrent bool
2018-09-02 17:15:27 +02:00
}
// NewGitCommand it runs git commands
2020-10-04 11:00:48 +11:00
func NewGitCommand ( log * logrus . Entry , osCommand * oscommands . OSCommand , tr * i18n . TranslationSet , config config . AppConfigurer ) ( * GitCommand , error ) {
2018-09-02 17:15:27 +02:00
var repo * gogit . Repository
2020-05-15 21:26:02 +10:00
// see what our default push behaviour is
output , err := osCommand . RunCommandWithOutput ( "git config --get push.default" )
pushToCurrent := false
if err != nil {
log . Errorf ( "error reading git config: %v" , err )
} else {
pushToCurrent = strings . TrimSpace ( output ) == "current"
}
2020-09-27 15:36:04 +10:00
if err := navigateToRepoRootDirectory ( os . Stat , os . Chdir ) ; err != nil {
return nil , err
}
2020-10-04 11:00:48 +11:00
if repo , err = setupRepository ( gogit . PlainOpen , tr . GitconfigParseErr ) ; err != nil {
2020-09-27 15:36:04 +10:00
return nil , err
2018-09-02 17:15:27 +02:00
}
2019-05-12 17:04:32 +10:00
dotGitDir , err := findDotGitDir ( os . Stat , ioutil . ReadFile )
if err != nil {
return nil , err
}
2019-11-05 18:10:47 +11:00
gitCommand := & GitCommand {
2020-12-21 09:38:36 +11:00
Log : log ,
OSCommand : osCommand ,
Tr : tr ,
Repo : repo ,
Config : config ,
getGitConfigValue : getGitConfigValue ,
removeFile : os . RemoveAll ,
DotGitDir : dotGitDir ,
PushToCurrent : pushToCurrent ,
2019-11-05 18:10:47 +11:00
}
2020-08-22 18:29:09 +10:00
gitCommand . PatchManager = patch . NewPatchManager ( log , gitCommand . ApplyPatch , gitCommand . ShowFileDiff )
2019-11-05 18:10:47 +11:00
return gitCommand , nil
2018-09-02 17:15:27 +02:00
}
2020-09-29 20:03:39 +10:00
func navigateToRepoRootDirectory ( stat func ( string ) ( os . FileInfo , error ) , chdir func ( string ) error ) error {
gitDir := env . GetGitDirEnv ( )
if gitDir != "" {
// we've been given the git directory explicitly so no need to navigate to it
_ , err := stat ( gitDir )
if err != nil {
return utils . WrapError ( err )
2020-09-29 08:47:14 +10:00
}
2018-08-12 19:50:55 +10:00
2020-09-29 20:03:39 +10:00
return nil
2018-11-29 17:57:28 +01:00
}
2020-09-29 20:03:39 +10:00
// we haven't been given the git dir explicitly so we assume it's in the current working directory as `.git/` (or an ancestor directory)
2020-08-11 21:18:38 +10:00
2020-09-29 20:03:39 +10:00
for {
_ , err := stat ( ".git" )
2020-08-11 21:18:38 +10:00
2020-09-29 20:03:39 +10:00
if err == nil {
return nil
2018-11-25 13:15:36 +01:00
}
2018-08-12 19:50:55 +10:00
2020-09-29 20:03:39 +10:00
if ! os . IsNotExist ( err ) {
return utils . WrapError ( err )
2020-03-26 20:29:35 +11:00
}
2018-09-10 21:57:08 +02:00
2020-09-29 20:03:39 +10:00
if err = chdir ( ".." ) ; err != nil {
return utils . WrapError ( err )
2019-02-20 19:47:01 +11:00
}
2021-03-30 22:17:42 +11:00
currentPath , err := os . Getwd ( )
if err != nil {
return err
}
atRoot := currentPath == filepath . Dir ( currentPath )
if atRoot {
// we should never really land here: the code that creates GitCommand should
// verify we're in a git directory
return errors . New ( "Must open lazygit in a git repository" )
}
2019-02-20 19:47:01 +11:00
}
2018-08-12 20:22:20 +10:00
}
2020-09-29 20:03:39 +10:00
// resolvePath takes a path containing a symlink and returns the true path
func resolvePath ( path string ) ( string , error ) {
l , err := os . Lstat ( path )
2019-03-02 20:00:26 +11:00
if err != nil {
return "" , err
}
2020-08-07 17:52:17 +10:00
2020-09-29 20:03:39 +10:00
if l . Mode ( ) & os . ModeSymlink == 0 {
return path , nil
2020-08-07 17:52:17 +10:00
}
2020-09-29 20:03:39 +10:00
return filepath . EvalSymlinks ( path )
2020-08-07 17:52:17 +10:00
}
2020-10-04 11:00:48 +11:00
func setupRepository ( openGitRepository func ( string ) ( * gogit . Repository , error ) , gitConfigParseErrorStr string ) ( * gogit . Repository , error ) {
2020-09-29 20:03:39 +10:00
unresolvedPath := env . GetGitDirEnv ( )
if unresolvedPath == "" {
var err error
unresolvedPath , err = os . Getwd ( )
2020-08-07 17:52:17 +10:00
if err != nil {
2020-09-29 20:03:39 +10:00
return nil , err
2020-03-28 12:52:45 +11:00
}
2019-11-04 19:47:25 +11:00
}
2020-09-29 20:03:39 +10:00
path , err := resolvePath ( unresolvedPath )
2019-02-18 21:29:43 +11:00
if err != nil {
2019-02-18 23:27:54 +11:00
return nil , err
2019-02-18 21:29:43 +11:00
}
2020-09-29 20:03:39 +10:00
repository , err := openGitRepository ( path )
2019-02-24 13:51:52 +11:00
if err != nil {
2020-09-29 20:03:39 +10:00
if strings . Contains ( err . Error ( ) , ` unquoted '\' must be followed by new line ` ) {
2020-10-04 11:00:48 +11:00
return nil , errors . New ( gitConfigParseErrorStr )
2019-03-11 13:04:08 +11:00
}
2020-09-29 20:03:39 +10:00
return nil , err
2019-05-30 22:45:56 +10:00
}
2020-09-29 20:03:39 +10:00
return repository , err
2019-05-30 22:45:56 +10:00
}
2019-11-04 19:47:25 +11:00
2020-09-29 20:03:39 +10:00
func findDotGitDir ( stat func ( string ) ( os . FileInfo , error ) , readFile func ( filename string ) ( [ ] byte , error ) ) ( string , error ) {
if env . GetGitDirEnv ( ) != "" {
return env . GetGitDirEnv ( ) , nil
2019-11-04 19:47:25 +11:00
}
2020-09-29 20:03:39 +10:00
f , err := stat ( ".git" )
2019-11-04 19:47:25 +11:00
if err != nil {
2020-09-29 20:03:39 +10:00
return "" , err
2019-11-04 19:47:25 +11:00
}
2020-09-29 20:03:39 +10:00
if f . IsDir ( ) {
return ".git" , nil
2019-11-04 19:47:25 +11:00
}
2020-09-29 20:03:39 +10:00
fileBytes , err := readFile ( ".git" )
2020-03-01 21:00:44 +11:00
if err != nil {
2020-09-29 20:03:39 +10:00
return "" , err
2020-03-28 12:08:13 +11:00
}
2020-09-29 20:03:39 +10:00
fileContent := string ( fileBytes )
if ! strings . HasPrefix ( fileContent , "gitdir: " ) {
return "" , errors . New ( ".git is a file which suggests we are in a submodule but the file's contents do not contain a gitdir pointing to the actual .git directory" )
2020-03-28 12:08:13 +11:00
}
2020-09-29 20:03:39 +10:00
return strings . TrimSpace ( strings . TrimPrefix ( fileContent , "gitdir: " ) ) , nil
2020-09-27 16:02:20 +10:00
}
2021-03-30 22:17:42 +11:00
func VerifyInGitRepo ( osCommand * oscommands . OSCommand ) error {
return osCommand . RunCommand ( "git rev-parse --git-dir" )
}
2021-04-05 21:08:33 +10:00
func ( c * GitCommand ) RunCommand ( formatString string , formatArgs ... interface { } ) error {
_ , err := c . RunCommandWithOutput ( formatString , formatArgs ... )
return err
}
func ( c * GitCommand ) RunCommandWithOutput ( formatString string , formatArgs ... interface { } ) ( string , error ) {
// TODO: have this retry logic in other places we run the command
waitTime := 50 * time . Millisecond
retryCount := 5
attempt := 0
for {
output , err := c . OSCommand . RunCommandWithOutput ( formatString , formatArgs ... )
if err != nil {
// if we have an error based on the index lock, we should wait a bit and then retry
if strings . Contains ( output , ".git/index.lock" ) {
c . Log . Error ( output )
c . Log . Info ( "index.lock prevented command from running. Retrying command after a small wait" )
attempt ++
time . Sleep ( waitTime )
if attempt < retryCount {
continue
} else if attempt == retryCount {
// delete the lock file because some other process must have died leaving it there
// TODO: ensure this is the actual location of the lock
c . Log . Warn ( "WARNING: removing index.lock file after retrying command 5 times" )
if err := os . Remove ( ".git/index.lock" ) ; err != nil {
return "" , err
}
continue
}
}
}
return output , err
}
}