1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-06-08 23:56:15 +02:00

feat: fix permission problem of temp dirs

This commit is contained in:
Ryooooooga 2022-04-01 23:38:41 +09:00 committed by Jesse Duffield
parent 2fbb52fa2c
commit 86c259623c
7 changed files with 26 additions and 12 deletions

View File

@ -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 { if err != nil {
log.Fatal(err.Error()) log.Fatal(err.Error())
} }

View File

@ -122,7 +122,7 @@ func NewApp(config config.AppConfigurer) (*App, error) {
return app, nil 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) app.Updater, err = updates.NewUpdater(app.Common, config, app.OSCommand)
if err != nil { if err != nil {

View File

@ -251,7 +251,7 @@ func (self *WorkingTreeCommands) WorktreeFileDiffCmdObj(node models.IFile, plain
} }
func (self *WorkingTreeCommands) ApplyPatch(patch string, flags ...string) error { 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) self.Log.Infof("saving temporary patch to %s", filepath)
if err := self.os.CreateFileWithContent(filepath, patch); err != nil { if err := self.os.CreateFileWithContent(filepath, patch); err != nil {
return err return err

View File

@ -2,12 +2,13 @@ package oscommands
import ( import (
"github.com/jesseduffield/lazygit/pkg/common" "github.com/jesseduffield/lazygit/pkg/common"
"github.com/jesseduffield/lazygit/pkg/config"
"github.com/jesseduffield/lazygit/pkg/utils" "github.com/jesseduffield/lazygit/pkg/utils"
) )
// NewDummyOSCommand creates a new dummy OSCommand for testing // NewDummyOSCommand creates a new dummy OSCommand for testing
func NewDummyOSCommand() *OSCommand { func NewDummyOSCommand() *OSCommand {
osCmd := NewOSCommand(utils.NewDummyCommon(), dummyPlatform, NewNullGuiIO(utils.NewDummyLog())) osCmd := NewOSCommand(utils.NewDummyCommon(), config.NewDummyAppConfig(), dummyPlatform, NewNullGuiIO(utils.NewDummyLog()))
return osCmd return osCmd
} }
@ -56,7 +57,7 @@ var dummyPlatform = &Platform{
} }
func NewDummyOSCommandWithRunner(runner *FakeCmdObjRunner) *OSCommand { 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) osCommand.Cmd = NewDummyCmdObjBuilder(runner)
return osCommand return osCommand

View File

@ -14,6 +14,7 @@ import (
"github.com/atotto/clipboard" "github.com/atotto/clipboard"
"github.com/jesseduffield/generics/slices" "github.com/jesseduffield/generics/slices"
"github.com/jesseduffield/lazygit/pkg/common" "github.com/jesseduffield/lazygit/pkg/common"
"github.com/jesseduffield/lazygit/pkg/config"
"github.com/jesseduffield/lazygit/pkg/utils" "github.com/jesseduffield/lazygit/pkg/utils"
) )
@ -27,6 +28,8 @@ type OSCommand struct {
removeFileFn func(string) error removeFileFn func(string) error
Cmd *CmdObjBuilder Cmd *CmdObjBuilder
tempDir string
} }
// Platform stores the os state // Platform stores the os state
@ -39,13 +42,14 @@ type Platform struct {
} }
// NewOSCommand os command runner // 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{ c := &OSCommand{
Common: common, Common: common,
Platform: platform, Platform: platform,
getenvFn: os.Getenv, getenvFn: os.Getenv,
removeFileFn: os.RemoveAll, removeFileFn: os.RemoveAll,
guiIO: guiIO, guiIO: guiIO,
tempDir: config.GetTempDir(),
} }
runner := &cmdObjRunner{log: common.Log, guiIO: guiIO} runner := &cmdObjRunner{log: common.Log, guiIO: guiIO}
@ -236,8 +240,8 @@ func (c *OSCommand) Getenv(key string) string {
return c.getenvFn(key) return c.getenvFn(key)
} }
func GetTempDir() string { func (c *OSCommand) GetTempDir() string {
return filepath.Join(os.TempDir(), "lazygit") return c.tempDir
} }
// GetLazygitPath returns the path of the currently executed file // GetLazygitPath returns the path of the currently executed file

View File

@ -43,13 +43,14 @@ type AppConfigurer interface {
GetUserConfigPaths() []string GetUserConfigPaths() []string
GetUserConfigDir() string GetUserConfigDir() string
ReloadUserConfig() error ReloadUserConfig() error
GetTempDir() string
GetAppState() *AppState GetAppState() *AppState
SaveAppState() error SaveAppState() error
} }
// NewAppConfig makes a new app config // 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() configDir, err := findOrCreateConfigDir()
if err != nil && !os.IsPermission(err) { if err != nil && !os.IsPermission(err) {
return nil, err return nil, err
@ -74,8 +75,6 @@ func NewAppConfig(name, version, commit, date string, buildSource string, debugg
debuggingFlag = true debuggingFlag = true
} }
tempDir := filepath.Join(os.TempDir(), "lazygit")
appState, err := loadAppState() appState, err := loadAppState()
if err != nil { if err != nil {
return nil, err return nil, err
@ -221,6 +220,10 @@ func (c *AppConfig) ReloadUserConfig() error {
return nil return nil
} }
func (c *AppConfig) GetTempDir() string {
return c.TempDir
}
func configFilePath(filename string) (string, error) { func configFilePath(filename string) (string, error) {
folder, err := findOrCreateConfigDir() folder, err := findOrCreateConfigDir()
if err != nil { if err != nil {

View File

@ -470,7 +470,7 @@ func NewGui(
credentialsHelper.PromptUserForCredential, credentialsHelper.PromptUserForCredential,
) )
osCommand := oscommands.NewOSCommand(cmn, oscommands.GetPlatform(), guiIO) osCommand := oscommands.NewOSCommand(cmn, config, oscommands.GetPlatform(), guiIO)
gui.os = osCommand gui.os = osCommand