Skip to content

Commit

Permalink
feat: fix permission problem of temp dirs
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryooooooga authored and jesseduffield committed Apr 1, 2022
1 parent 2fbb52f commit 86c2596
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 12 deletions.
8 changes: 7 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,13 @@ func main() {
}
}

appConfig, err := config.NewAppConfig("lazygit", version, commit, date, buildSource, debuggingFlag)
tempDir, err := os.MkdirTemp("", "lazygit-*")
if err != nil {
log.Fatal(err.Error())
}
defer os.RemoveAll(tempDir)

appConfig, err := config.NewAppConfig("lazygit", version, commit, date, buildSource, debuggingFlag, tempDir)
if err != nil {
log.Fatal(err.Error())
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func NewApp(config config.AppConfigurer) (*App, error) {
return app, nil
}

app.OSCommand = oscommands.NewOSCommand(app.Common, oscommands.GetPlatform(), oscommands.NewNullGuiIO(log))
app.OSCommand = oscommands.NewOSCommand(app.Common, config, oscommands.GetPlatform(), oscommands.NewNullGuiIO(log))

app.Updater, err = updates.NewUpdater(app.Common, config, app.OSCommand)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/commands/git_commands/working_tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ func (self *WorkingTreeCommands) WorktreeFileDiffCmdObj(node models.IFile, plain
}

func (self *WorkingTreeCommands) ApplyPatch(patch string, flags ...string) error {
filepath := filepath.Join(oscommands.GetTempDir(), utils.GetCurrentRepoName(), time.Now().Format("Jan _2 15.04.05.000000000")+".patch")
filepath := filepath.Join(self.os.GetTempDir(), utils.GetCurrentRepoName(), time.Now().Format("Jan _2 15.04.05.000000000")+".patch")
self.Log.Infof("saving temporary patch to %s", filepath)
if err := self.os.CreateFileWithContent(filepath, patch); err != nil {
return err
Expand Down
5 changes: 3 additions & 2 deletions pkg/commands/oscommands/dummies.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package oscommands

import (
"github.com/jesseduffield/lazygit/pkg/common"
"github.com/jesseduffield/lazygit/pkg/config"
"github.com/jesseduffield/lazygit/pkg/utils"
)

// NewDummyOSCommand creates a new dummy OSCommand for testing
func NewDummyOSCommand() *OSCommand {
osCmd := NewOSCommand(utils.NewDummyCommon(), dummyPlatform, NewNullGuiIO(utils.NewDummyLog()))
osCmd := NewOSCommand(utils.NewDummyCommon(), config.NewDummyAppConfig(), dummyPlatform, NewNullGuiIO(utils.NewDummyLog()))

return osCmd
}
Expand Down Expand Up @@ -56,7 +57,7 @@ var dummyPlatform = &Platform{
}

func NewDummyOSCommandWithRunner(runner *FakeCmdObjRunner) *OSCommand {
osCommand := NewOSCommand(utils.NewDummyCommon(), dummyPlatform, NewNullGuiIO(utils.NewDummyLog()))
osCommand := NewOSCommand(utils.NewDummyCommon(), config.NewDummyAppConfig(), dummyPlatform, NewNullGuiIO(utils.NewDummyLog()))
osCommand.Cmd = NewDummyCmdObjBuilder(runner)

return osCommand
Expand Down
10 changes: 7 additions & 3 deletions pkg/commands/oscommands/os.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/atotto/clipboard"
"github.com/jesseduffield/generics/slices"
"github.com/jesseduffield/lazygit/pkg/common"
"github.com/jesseduffield/lazygit/pkg/config"
"github.com/jesseduffield/lazygit/pkg/utils"
)

Expand All @@ -27,6 +28,8 @@ type OSCommand struct {
removeFileFn func(string) error

Cmd *CmdObjBuilder

tempDir string
}

// Platform stores the os state
Expand All @@ -39,13 +42,14 @@ type Platform struct {
}

// NewOSCommand os command runner
func NewOSCommand(common *common.Common, platform *Platform, guiIO *guiIO) *OSCommand {
func NewOSCommand(common *common.Common, config config.AppConfigurer, platform *Platform, guiIO *guiIO) *OSCommand {
c := &OSCommand{
Common: common,
Platform: platform,
getenvFn: os.Getenv,
removeFileFn: os.RemoveAll,
guiIO: guiIO,
tempDir: config.GetTempDir(),
}

runner := &cmdObjRunner{log: common.Log, guiIO: guiIO}
Expand Down Expand Up @@ -236,8 +240,8 @@ func (c *OSCommand) Getenv(key string) string {
return c.getenvFn(key)
}

func GetTempDir() string {
return filepath.Join(os.TempDir(), "lazygit")
func (c *OSCommand) GetTempDir() string {
return c.tempDir
}

// GetLazygitPath returns the path of the currently executed file
Expand Down
9 changes: 6 additions & 3 deletions pkg/config/app_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,14 @@ type AppConfigurer interface {
GetUserConfigPaths() []string
GetUserConfigDir() string
ReloadUserConfig() error
GetTempDir() string

GetAppState() *AppState
SaveAppState() error
}

// NewAppConfig makes a new app config
func NewAppConfig(name, version, commit, date string, buildSource string, debuggingFlag bool) (*AppConfig, error) {
func NewAppConfig(name, version, commit, date string, buildSource string, debuggingFlag bool, tempDir string) (*AppConfig, error) {
configDir, err := findOrCreateConfigDir()
if err != nil && !os.IsPermission(err) {
return nil, err
Expand All @@ -74,8 +75,6 @@ func NewAppConfig(name, version, commit, date string, buildSource string, debugg
debuggingFlag = true
}

tempDir := filepath.Join(os.TempDir(), "lazygit")

appState, err := loadAppState()
if err != nil {
return nil, err
Expand Down Expand Up @@ -221,6 +220,10 @@ func (c *AppConfig) ReloadUserConfig() error {
return nil
}

func (c *AppConfig) GetTempDir() string {
return c.TempDir
}

func configFilePath(filename string) (string, error) {
folder, err := findOrCreateConfigDir()
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/gui/gui.go
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ func NewGui(
credentialsHelper.PromptUserForCredential,
)

osCommand := oscommands.NewOSCommand(cmn, oscommands.GetPlatform(), guiIO)
osCommand := oscommands.NewOSCommand(cmn, config, oscommands.GetPlatform(), guiIO)

gui.os = osCommand

Expand Down

0 comments on commit 86c2596

Please sign in to comment.