From 0046e9c469a86c22a98a6671c6d4802c4f8e74c5 Mon Sep 17 00:00:00 2001 From: Jesse Duffield Date: Tue, 5 Nov 2019 12:42:07 +1100 Subject: create backups of patch files in case something goes wrong --- pkg/commands/git.go | 11 +++++------ pkg/commands/os.go | 15 +++++++++++++++ pkg/commands/patch_manager.go | 1 - 3 files changed, 20 insertions(+), 7 deletions(-) diff --git a/pkg/commands/git.go b/pkg/commands/git.go index b9b37ab80..c829f8607 100644 --- a/pkg/commands/git.go +++ b/pkg/commands/git.go @@ -5,7 +5,9 @@ import ( "io/ioutil" "os" "os/exec" + "path/filepath" "strings" + "time" "github.com/mgutz/str" @@ -608,14 +610,11 @@ func (c *GitCommand) Diff(file *File, plain bool, cached bool) string { } func (c *GitCommand) ApplyPatch(patch string, reverse bool, cached bool, extraFlags string) error { - filename, err := c.OSCommand.CreateTempFile("patch", patch) - if err != nil { - c.Log.Error(err) + filepath := filepath.Join(c.Config.GetUserConfigDir(), utils.GetCurrentRepoName(), time.Now().Format(time.StampNano)+".patch") + if err := c.OSCommand.CreateFileWithContent(filepath, patch); err != nil { return err } - defer func() { _ = c.OSCommand.Remove(filename) }() - reverseFlag := "" if reverse { reverseFlag = "--reverse" @@ -626,7 +625,7 @@ func (c *GitCommand) ApplyPatch(patch string, reverse bool, cached bool, extraFl cachedFlag = "--cached" } - return c.OSCommand.RunCommand(fmt.Sprintf("git apply %s %s %s %s", cachedFlag, reverseFlag, extraFlags, c.OSCommand.Quote(filename))) + return c.OSCommand.RunCommand(fmt.Sprintf("git apply %s %s %s %s", cachedFlag, reverseFlag, extraFlags, c.OSCommand.Quote(filepath))) } func (c *GitCommand) FastForward(branchName string) error { diff --git a/pkg/commands/os.go b/pkg/commands/os.go index c61404c41..df1b56efd 100644 --- a/pkg/commands/os.go +++ b/pkg/commands/os.go @@ -262,6 +262,21 @@ func (c *OSCommand) CreateTempFile(filename, content string) (string, error) { return tmpfile.Name(), nil } +// CreateFileWithContent creates a file with the given content +func (c *OSCommand) CreateFileWithContent(path string, content string) error { + if err := os.MkdirAll(filepath.Dir(path), os.ModePerm); err != nil { + c.Log.Error(err) + return err + } + + if err := ioutil.WriteFile(path, []byte(content), 0644); err != nil { + c.Log.Error(err) + return WrapError(err) + } + + return nil +} + // Remove removes a file or directory at the specified path func (c *OSCommand) Remove(filename string) error { err := os.RemoveAll(filename) diff --git a/pkg/commands/patch_manager.go b/pkg/commands/patch_manager.go index 7c0da245d..f5aa5b712 100644 --- a/pkg/commands/patch_manager.go +++ b/pkg/commands/patch_manager.go @@ -184,7 +184,6 @@ func (p *PatchManager) ApplyPatches(reverse bool) error { if patch == "" { continue } - p.Log.Warn(patch) if err := p.ApplyPatch(patch, reverseOnApply, false, "--index --3way"); err != nil { return err } -- cgit v1.2.3