1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2024-11-28 09:08:41 +02:00

create backups of patch files in case something goes wrong

This commit is contained in:
Jesse Duffield 2019-11-05 12:42:07 +11:00
parent 733145d132
commit 0046e9c469
3 changed files with 20 additions and 7 deletions

View File

@ -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 {

View File

@ -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)

View File

@ -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
}