2018-08-12 11:31:27 +02:00
package commands
import (
2019-02-19 14:36:29 +02:00
"io/ioutil"
2018-08-12 11:31:27 +02:00
"os"
2019-11-05 03:42:07 +02:00
"path/filepath"
2018-08-12 11:50:55 +02:00
"strings"
2019-02-18 12:29:43 +02:00
2019-02-11 12:30:27 +02:00
"github.com/go-errors/errors"
2020-10-06 11:50:54 +02:00
gogit "github.com/jesseduffield/go-git/v5"
2022-01-08 05:00:36 +02:00
"github.com/jesseduffield/lazygit/pkg/commands/git_commands"
2021-10-23 00:52:19 +02:00
"github.com/jesseduffield/lazygit/pkg/commands/git_config"
2022-01-02 01:21:32 +02:00
"github.com/jesseduffield/lazygit/pkg/commands/loaders"
2020-09-29 11:10:57 +02:00
"github.com/jesseduffield/lazygit/pkg/commands/oscommands"
2020-08-15 03:18:40 +02:00
"github.com/jesseduffield/lazygit/pkg/commands/patch"
2021-12-29 02:37:15 +02:00
"github.com/jesseduffield/lazygit/pkg/common"
2020-09-27 08:17:26 +02:00
"github.com/jesseduffield/lazygit/pkg/env"
2018-08-12 13:04:47 +02:00
"github.com/jesseduffield/lazygit/pkg/utils"
2018-08-12 11:31:27 +02:00
)
2022-01-02 01:34:33 +02:00
// GitCommand is our main git interface
type GitCommand struct {
2022-01-08 05:00:36 +02:00
Branch * git_commands . BranchCommands
Commit * git_commands . CommitCommands
Config * git_commands . ConfigCommands
Custom * git_commands . CustomCommands
File * git_commands . FileCommands
Flow * git_commands . FlowCommands
Patch * git_commands . PatchCommands
Rebase * git_commands . RebaseCommands
Remote * git_commands . RemoteCommands
Stash * git_commands . StashCommands
Status * git_commands . StatusCommands
Submodule * git_commands . SubmoduleCommands
Sync * git_commands . SyncCommands
Tag * git_commands . TagCommands
WorkingTree * git_commands . WorkingTreeCommands
2022-01-07 11:36:11 +02:00
Loaders Loaders
2022-01-02 01:34:33 +02:00
}
2019-11-17 03:07:36 +02:00
2022-01-02 01:21:32 +02:00
type Loaders struct {
Branches * loaders . BranchLoader
CommitFiles * loaders . CommitFileLoader
2022-01-07 11:36:11 +02:00
Commits * loaders . CommitLoader
Files * loaders . FileLoader
2022-01-02 01:21:32 +02:00
ReflogCommits * loaders . ReflogCommitLoader
2022-01-07 11:36:11 +02:00
Remotes * loaders . RemoteLoader
2022-01-02 01:21:32 +02:00
Stash * loaders . StashLoader
Tags * loaders . TagLoader
}
2021-10-23 00:52:19 +02:00
func NewGitCommand (
2021-12-29 02:37:15 +02:00
cmn * common . Common ,
2021-10-23 00:52:19 +02:00
osCommand * oscommands . OSCommand ,
gitConfig git_config . IGitConfig ,
) ( * GitCommand , error ) {
2020-09-27 07:36:04 +02:00
if err := navigateToRepoRootDirectory ( os . Stat , os . Chdir ) ; err != nil {
return nil , err
}
2022-01-02 01:34:33 +02:00
repo , err := setupRepository ( gogit . PlainOpen , cmn . Tr . GitconfigParseErr )
if err != nil {
2020-09-27 07:36:04 +02:00
return nil , err
2018-09-02 17:15:27 +02:00
}
2019-05-12 09:04:32 +02:00
dotGitDir , err := findDotGitDir ( os . Stat , ioutil . ReadFile )
if err != nil {
return nil , err
}
2022-01-02 01:34:33 +02:00
return NewGitCommandAux (
cmn ,
osCommand ,
gitConfig ,
dotGitDir ,
repo ,
) , nil
}
2021-12-29 05:33:38 +02:00
2022-01-02 01:34:33 +02:00
func NewGitCommandAux (
cmn * common . Common ,
osCommand * oscommands . OSCommand ,
gitConfig git_config . IGitConfig ,
dotGitDir string ,
repo * gogit . Repository ,
) * GitCommand {
cmd := NewGitCmdObjBuilder ( cmn . Log , osCommand . Cmd )
2019-11-05 09:10:47 +02:00
2022-01-07 05:45:18 +02:00
// here we're doing a bunch of dependency injection for each of our commands structs.
// This is admittedly messy, but allows us to test each command struct in isolation,
// and allows for better namespacing when compared to having every method living
// on the one struct.
2022-01-08 05:44:07 +02:00
configCommands := git_commands . NewConfigCommands ( cmn , gitConfig , repo )
2022-01-08 05:00:36 +02:00
statusCommands := git_commands . NewStatusCommands ( cmn , osCommand , repo , dotGitDir )
2022-01-02 01:34:33 +02:00
fileLoader := loaders . NewFileLoader ( cmn , cmd , configCommands )
2022-01-08 05:00:36 +02:00
flowCommands := git_commands . NewFlowCommands ( cmn , cmd , configCommands )
remoteCommands := git_commands . NewRemoteCommands ( cmn , cmd )
branchCommands := git_commands . NewBranchCommands ( cmn , cmd )
syncCommands := git_commands . NewSyncCommands ( cmn , cmd )
tagCommands := git_commands . NewTagCommands ( cmn , cmd )
commitCommands := git_commands . NewCommitCommands ( cmn , cmd )
customCommands := git_commands . NewCustomCommands ( cmn , cmd )
fileCommands := git_commands . NewFileCommands ( cmn , cmd , configCommands , osCommand )
submoduleCommands := git_commands . NewSubmoduleCommands ( cmn , cmd , dotGitDir )
workingTreeCommands := git_commands . NewWorkingTreeCommands ( cmn , cmd , submoduleCommands , osCommand , fileLoader )
rebaseCommands := git_commands . NewRebaseCommands (
2022-01-02 01:34:33 +02:00
cmn ,
cmd ,
osCommand ,
commitCommands ,
workingTreeCommands ,
configCommands ,
dotGitDir ,
)
2022-01-08 05:00:36 +02:00
stashCommands := git_commands . NewStashCommands ( cmn , cmd , osCommand , fileLoader , workingTreeCommands )
2022-01-02 01:34:33 +02:00
// TODO: have patch manager take workingTreeCommands in its entirety
patchManager := patch . NewPatchManager ( cmn . Log , workingTreeCommands . ApplyPatch , workingTreeCommands . ShowFileDiff )
2022-01-08 05:00:36 +02:00
patchCommands := git_commands . NewPatchCommands ( cmn , cmd , rebaseCommands , commitCommands , configCommands , statusCommands , patchManager )
2022-01-02 01:34:33 +02:00
return & GitCommand {
Branch : branchCommands ,
Commit : commitCommands ,
Config : configCommands ,
2022-01-07 11:36:11 +02:00
Custom : customCommands ,
File : fileCommands ,
Flow : flowCommands ,
2022-01-02 01:34:33 +02:00
Patch : patchCommands ,
2022-01-07 11:36:11 +02:00
Rebase : rebaseCommands ,
2022-01-02 01:34:33 +02:00
Remote : remoteCommands ,
2022-01-07 11:36:11 +02:00
Stash : stashCommands ,
Status : statusCommands ,
Submodule : submoduleCommands ,
2022-01-02 01:34:33 +02:00
Sync : syncCommands ,
2022-01-07 11:36:11 +02:00
Tag : tagCommands ,
WorkingTree : workingTreeCommands ,
2022-01-02 01:34:33 +02:00
Loaders : Loaders {
Branches : loaders . NewBranchLoader ( cmn , branchCommands . GetRawBranches , branchCommands . CurrentBranchName ) ,
CommitFiles : loaders . NewCommitFileLoader ( cmn , cmd ) ,
2022-01-07 11:36:11 +02:00
Commits : loaders . NewCommitLoader ( cmn , cmd , dotGitDir , branchCommands . CurrentBranchName , statusCommands . RebaseMode ) ,
Files : fileLoader ,
2022-01-02 01:34:33 +02:00
ReflogCommits : loaders . NewReflogCommitLoader ( cmn , cmd ) ,
2022-01-07 11:36:11 +02:00
Remotes : loaders . NewRemoteLoader ( cmn , cmd , repo . Remotes ) ,
2022-01-02 01:34:33 +02:00
Stash : loaders . NewStashLoader ( cmn , cmd ) ,
Tags : loaders . NewTagLoader ( cmn , cmd ) ,
} ,
2022-01-02 01:21:32 +02:00
}
2018-09-02 17:15:27 +02:00
}
2020-09-29 12:03:39 +02: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 00:47:14 +02:00
}
2018-08-12 11:50:55 +02:00
2020-09-29 12:03:39 +02:00
return nil
2018-11-29 18:57:28 +02:00
}
2020-09-29 12:03:39 +02: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 13:18:38 +02:00
2020-09-29 12:03:39 +02:00
for {
_ , err := stat ( ".git" )
2020-08-11 13:18:38 +02:00
2020-09-29 12:03:39 +02:00
if err == nil {
return nil
2018-11-25 14:15:36 +02:00
}
2018-08-12 11:50:55 +02:00
2020-09-29 12:03:39 +02:00
if ! os . IsNotExist ( err ) {
return utils . WrapError ( err )
2020-03-26 11:29:35 +02:00
}
2018-09-10 21:57:08 +02:00
2020-09-29 12:03:39 +02:00
if err = chdir ( ".." ) ; err != nil {
return utils . WrapError ( err )
2019-02-20 10:47:01 +02:00
}
2021-03-30 13:17:42 +02: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 10:47:01 +02:00
}
2018-08-12 12:22:20 +02:00
}
2020-09-29 12:03:39 +02: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 11:00:26 +02:00
if err != nil {
return "" , err
}
2020-08-07 09:52:17 +02:00
2020-09-29 12:03:39 +02:00
if l . Mode ( ) & os . ModeSymlink == 0 {
return path , nil
2020-08-07 09:52:17 +02:00
}
2020-09-29 12:03:39 +02:00
return filepath . EvalSymlinks ( path )
2020-08-07 09:52:17 +02:00
}
2020-10-04 02:00:48 +02:00
func setupRepository ( openGitRepository func ( string ) ( * gogit . Repository , error ) , gitConfigParseErrorStr string ) ( * gogit . Repository , error ) {
2020-09-29 12:03:39 +02:00
unresolvedPath := env . GetGitDirEnv ( )
if unresolvedPath == "" {
var err error
unresolvedPath , err = os . Getwd ( )
2020-08-07 09:52:17 +02:00
if err != nil {
2020-09-29 12:03:39 +02:00
return nil , err
2020-03-28 03:52:45 +02:00
}
2019-11-04 10:47:25 +02:00
}
2020-09-29 12:03:39 +02:00
path , err := resolvePath ( unresolvedPath )
2019-02-18 12:29:43 +02:00
if err != nil {
2019-02-18 14:27:54 +02:00
return nil , err
2019-02-18 12:29:43 +02:00
}
2020-09-29 12:03:39 +02:00
repository , err := openGitRepository ( path )
2019-02-24 04:51:52 +02:00
if err != nil {
2020-09-29 12:03:39 +02:00
if strings . Contains ( err . Error ( ) , ` unquoted '\' must be followed by new line ` ) {
2020-10-04 02:00:48 +02:00
return nil , errors . New ( gitConfigParseErrorStr )
2019-03-11 04:04:08 +02:00
}
2020-09-29 12:03:39 +02:00
return nil , err
2019-05-30 14:45:56 +02:00
}
2020-09-29 12:03:39 +02:00
return repository , err
2019-05-30 14:45:56 +02:00
}
2019-11-04 10:47:25 +02:00
2020-09-29 12:03:39 +02: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 10:47:25 +02:00
}
2020-09-29 12:03:39 +02:00
f , err := stat ( ".git" )
2019-11-04 10:47:25 +02:00
if err != nil {
2020-09-29 12:03:39 +02:00
return "" , err
2019-11-04 10:47:25 +02:00
}
2020-09-29 12:03:39 +02:00
if f . IsDir ( ) {
return ".git" , nil
2019-11-04 10:47:25 +02:00
}
2020-09-29 12:03:39 +02:00
fileBytes , err := readFile ( ".git" )
2020-03-01 12:00:44 +02:00
if err != nil {
2020-09-29 12:03:39 +02:00
return "" , err
2020-03-28 03:08:13 +02:00
}
2020-09-29 12:03:39 +02: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 03:08:13 +02:00
}
2020-09-29 12:03:39 +02:00
return strings . TrimSpace ( strings . TrimPrefix ( fileContent , "gitdir: " ) ) , nil
2020-09-27 08:02:20 +02:00
}
2021-03-30 13:17:42 +02:00
func VerifyInGitRepo ( osCommand * oscommands . OSCommand ) error {
2022-01-05 02:57:32 +02:00
return osCommand . Cmd . New ( "git rev-parse --git-dir" ) . DontLog ( ) . Run ( )
2021-10-20 13:21:16 +02:00
}