1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2024-11-24 08:52:21 +02:00

move git dir env stuff into a centralised package

This commit is contained in:
Jesse Duffield 2020-09-27 16:17:26 +10:00
parent e873816160
commit 75598ea2a1
6 changed files with 49 additions and 19 deletions

View File

@ -11,6 +11,7 @@ import (
"github.com/integrii/flaggy"
"github.com/jesseduffield/lazygit/pkg/app"
"github.com/jesseduffield/lazygit/pkg/config"
"github.com/jesseduffield/lazygit/pkg/env"
)
var (
@ -63,11 +64,11 @@ func main() {
}
if workTree != "" {
os.Setenv("GIT_WORK_TREE", workTree)
env.SetGitWorkTreeEnv(workTree)
}
if gitDir != "" {
os.Setenv("GIT_DIR", gitDir)
env.SetGitDirEnv(gitDir)
}
if versionFlag {

View File

@ -17,6 +17,7 @@ import (
"github.com/aybabtme/humanlog"
"github.com/jesseduffield/lazygit/pkg/commands"
"github.com/jesseduffield/lazygit/pkg/config"
"github.com/jesseduffield/lazygit/pkg/env"
"github.com/jesseduffield/lazygit/pkg/gui"
"github.com/jesseduffield/lazygit/pkg/i18n"
"github.com/jesseduffield/lazygit/pkg/updates"
@ -170,7 +171,7 @@ func (app *App) setupRepo() (bool, error) {
return false, err
}
if os.Getenv("GIT_DIR") != "" {
if env.GetGitDirEnv() != "" {
// we've been given the git dir directly. We'll verify this dir when initializing our GitCommand object
return false, nil
}
@ -226,7 +227,7 @@ func (app *App) Run() error {
}
func gitDir() string {
dir := os.Getenv("GIT_DIR")
dir := env.GetGitDirEnv()
if dir == "" {
return ".git"
}

View File

@ -194,7 +194,7 @@ func (c *CommitListBuilder) getRebasingCommits(rebaseMode string) ([]*Commit, er
func (c *CommitListBuilder) getNormalRebasingCommits() ([]*Commit, error) {
rewrittenCount := 0
bytesContent, err := ioutil.ReadFile(fmt.Sprintf("%s/rebase-apply/rewritten", c.GitCommand.DotGitDir))
bytesContent, err := ioutil.ReadFile(filepath.Join(c.GitCommand.DotGitDir, "rebase-apply/rewritten"))
if err == nil {
content := string(bytesContent)
rewrittenCount = len(strings.Split(content, "\n"))
@ -202,7 +202,7 @@ func (c *CommitListBuilder) getNormalRebasingCommits() ([]*Commit, error) {
// we know we're rebasing, so lets get all the files whose names have numbers
commits := []*Commit{}
err = filepath.Walk(fmt.Sprintf("%s/rebase-apply", c.GitCommand.DotGitDir), func(path string, f os.FileInfo, err error) error {
err = filepath.Walk(filepath.Join(c.GitCommand.DotGitDir, "rebase-apply"), func(path string, f os.FileInfo, err error) error {
if rewrittenCount > 0 {
rewrittenCount--
return nil
@ -246,7 +246,7 @@ func (c *CommitListBuilder) getNormalRebasingCommits() ([]*Commit, error) {
// and extracts out the sha and names of commits that we still have to go
// in the rebase:
func (c *CommitListBuilder) getInteractiveRebasingCommits() ([]*Commit, error) {
bytesContent, err := ioutil.ReadFile(fmt.Sprintf("%s/rebase-merge/git-rebase-todo", c.GitCommand.DotGitDir))
bytesContent, err := ioutil.ReadFile(filepath.Join(c.GitCommand.DotGitDir, "rebase-merge/git-rebase-todo"))
if err != nil {
c.Log.Error(fmt.Sprintf("error occurred reading git-rebase-todo: %s", err.Error()))
// we assume an error means the file doesn't exist so we just return

View File

@ -18,6 +18,7 @@ import (
gogit "github.com/go-git/go-git/v5"
"github.com/jesseduffield/lazygit/pkg/commands/patch"
"github.com/jesseduffield/lazygit/pkg/config"
"github.com/jesseduffield/lazygit/pkg/env"
"github.com/jesseduffield/lazygit/pkg/i18n"
"github.com/jesseduffield/lazygit/pkg/utils"
"github.com/sirupsen/logrus"
@ -35,9 +36,10 @@ func verifyInGitRepo(runCmd func(string, ...interface{}) error) error {
}
func navigateToRepoRootDirectory(stat func(string) (os.FileInfo, error), chdir func(string) error) error {
if os.Getenv("GIT_DIR") != "" {
gitDir := env.GetGitDirEnv()
if gitDir != "" {
// we've been given the git directory explicitly so no need to navigate to it
_, err := stat(os.Getenv("GIT_DIR"))
_, err := stat(gitDir)
if err != nil {
return WrapError(err)
}
@ -45,6 +47,8 @@ func navigateToRepoRootDirectory(stat func(string) (os.FileInfo, error), chdir f
return nil
}
// 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)
for {
_, err := stat(".git")
@ -63,7 +67,7 @@ func navigateToRepoRootDirectory(stat func(string) (os.FileInfo, error), chdir f
}
func setupRepository(openGitRepository func(string) (*gogit.Repository, error), sLocalize func(string) string) (*gogit.Repository, error) {
path := os.Getenv("GIT_DIR")
path := env.GetGitDirEnv()
if path == "" {
path = "."
}
@ -149,8 +153,8 @@ func NewGitCommand(log *logrus.Entry, osCommand *OSCommand, tr *i18n.Localizer,
}
func findDotGitDir(stat func(string) (os.FileInfo, error), readFile func(filename string) ([]byte, error)) (string, error) {
if os.Getenv("GIT_DIR") != "" {
return os.Getenv("GIT_DIR"), nil
if env.GetGitDirEnv() != "" {
return env.GetGitDirEnv(), nil
}
f, err := stat(".git")
@ -614,20 +618,20 @@ func (c *GitCommand) GitStatus(opts GitStatusOptions) (string, error) {
// IsInMergeState states whether we are still mid-merge
func (c *GitCommand) IsInMergeState() (bool, error) {
return c.OSCommand.FileExists(fmt.Sprintf("%s/MERGE_HEAD", c.DotGitDir))
return c.OSCommand.FileExists(filepath.Join(c.DotGitDir, "MERGE_HEAD"))
}
// RebaseMode returns "" for non-rebase mode, "normal" for normal rebase
// and "interactive" for interactive rebase
func (c *GitCommand) RebaseMode() (string, error) {
exists, err := c.OSCommand.FileExists(fmt.Sprintf("%s/rebase-apply", c.DotGitDir))
exists, err := c.OSCommand.FileExists(filepath.Join(c.DotGitDir, "rebase-apply"))
if err != nil {
return "", err
}
if exists {
return "normal", nil
}
exists, err = c.OSCommand.FileExists(fmt.Sprintf("%s/rebase-merge", c.DotGitDir))
exists, err = c.OSCommand.FileExists(filepath.Join(c.DotGitDir, "rebase-merge"))
if exists {
return "interactive", err
} else {
@ -1006,7 +1010,7 @@ func (c *GitCommand) AmendTo(sha string) error {
// EditRebaseTodo sets the action at a given index in the git-rebase-todo file
func (c *GitCommand) EditRebaseTodo(index int, action string) error {
fileName := fmt.Sprintf("%s/rebase-merge/git-rebase-todo", c.DotGitDir)
fileName := filepath.Join(c.DotGitDir, "rebase-merge/git-rebase-todo")
bytes, err := ioutil.ReadFile(fileName)
if err != nil {
return err
@ -1038,7 +1042,7 @@ func (c *GitCommand) getTodoCommitCount(content []string) int {
// MoveTodoDown moves a rebase todo item down by one position
func (c *GitCommand) MoveTodoDown(index int) error {
fileName := fmt.Sprintf("%s/rebase-merge/git-rebase-todo", c.DotGitDir)
fileName := filepath.Join(c.DotGitDir, "rebase-merge/git-rebase-todo")
bytes, err := ioutil.ReadFile(fileName)
if err != nil {
return err

24
pkg/env/env.go vendored Normal file
View File

@ -0,0 +1,24 @@
package env
import "os"
func GetGitDirEnv() string {
return os.Getenv("GIT_DIR")
}
func GetGitWorkTreeEnv() string {
return os.Getenv("GIT_WORK_TREE")
}
func SetGitDirEnv(value string) {
os.Setenv("GIT_DIR", value)
}
func SetGitWorkTreeEnv(value string) {
os.Setenv("GIT_WORK_TREE", value)
}
func UnsetGitDirEnvs() {
_ = os.Unsetenv("GIT_DIR")
_ = os.Unsetenv("GIT_WORK_TREE")
}

View File

@ -6,6 +6,7 @@ import (
"github.com/fatih/color"
"github.com/jesseduffield/lazygit/pkg/commands"
"github.com/jesseduffield/lazygit/pkg/env"
"github.com/jesseduffield/lazygit/pkg/utils"
)
@ -23,8 +24,7 @@ func (gui *Gui) handleCreateRecentReposMenu() error {
yellow.Sprint(innerPath),
},
onPress: func() error {
os.Unsetenv("GIT_WORK_TREE")
os.Unsetenv("GIT_DIR")
env.UnsetGitDirEnvs()
if err := os.Chdir(innerPath); err != nil {
return err
}