diff options
author | Jesse Duffield <jessedduffield@gmail.com> | 2019-11-05 13:01:58 +1100 |
---|---|---|
committer | Jesse Duffield <jessedduffield@gmail.com> | 2019-11-05 19:22:01 +1100 |
commit | 48347d4d8687bd15244637053851643dfa973753 (patch) | |
tree | c15a0c2b3e2abaaba660a8a17d3b5d3726c1ada0 /pkg/commands | |
parent | 61deaaddb7ee15356d5af41edb9e89a58bbf2ab9 (diff) |
use fallback approach for applying patch
Diffstat (limited to 'pkg/commands')
-rw-r--r-- | pkg/commands/git.go | 1 | ||||
-rw-r--r-- | pkg/commands/patch_manager.go | 27 |
2 files changed, 19 insertions, 9 deletions
diff --git a/pkg/commands/git.go b/pkg/commands/git.go index c829f8607..42c1749af 100644 --- a/pkg/commands/git.go +++ b/pkg/commands/git.go @@ -610,6 +610,7 @@ func (c *GitCommand) Diff(file *File, plain bool, cached bool) string { } func (c *GitCommand) ApplyPatch(patch string, reverse bool, cached bool, extraFlags string) error { + c.Log.Warn(patch) 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 diff --git a/pkg/commands/patch_manager.go b/pkg/commands/patch_manager.go index f5aa5b712..1c747c33d 100644 --- a/pkg/commands/patch_manager.go +++ b/pkg/commands/patch_manager.go @@ -87,7 +87,7 @@ func (p *PatchManager) RemoveFileLineRange(filename string, firstLineIdx, lastLi } } -func (p *PatchManager) RenderPlainPatchForFile(filename string, reverse bool) string { +func (p *PatchManager) RenderPlainPatchForFile(filename string, reverse bool, keepOriginalHeader bool) string { info := p.fileInfoMap[filename] if info == nil { return "" @@ -101,14 +101,14 @@ func (p *PatchManager) RenderPlainPatchForFile(filename string, reverse bool) st case PART: // generate a new diff with just the selected lines m := NewPatchModifier(p.Log, filename, info.diff) - return m.ModifiedPatchForLines(info.includedLineIndices, reverse, true) + return m.ModifiedPatchForLines(info.includedLineIndices, reverse, keepOriginalHeader) default: return "" } } -func (p *PatchManager) RenderPatchForFile(filename string, plain bool, reverse bool) string { - patch := p.RenderPlainPatchForFile(filename, reverse) +func (p *PatchManager) RenderPatchForFile(filename string, plain bool, reverse bool, keepOriginalHeader bool) string { + patch := p.RenderPlainPatchForFile(filename, reverse, keepOriginalHeader) if plain { return patch } @@ -133,7 +133,7 @@ func (p *PatchManager) RenderEachFilePatch(plain bool) []string { sort.Strings(filenames) output := []string{} for _, filename := range filenames { - patch := p.RenderPatchForFile(filename, plain, false) + patch := p.RenderPatchForFile(filename, plain, false, true) if patch != "" { output = append(output, patch) } @@ -180,11 +180,20 @@ func (p *PatchManager) ApplyPatches(reverse bool) error { } } - patch := p.RenderPatchForFile(filename, true, reverseOnGenerate) - if patch == "" { - continue + var err error + // first run we try with the original header, then without + for _, keepOriginalHeader := range []bool{true, false} { + patch := p.RenderPatchForFile(filename, true, reverseOnGenerate, keepOriginalHeader) + if patch == "" { + continue + } + if err = p.ApplyPatch(patch, reverseOnApply, false, "--index --3way"); err != nil { + continue + } + break } - if err := p.ApplyPatch(patch, reverseOnApply, false, "--index --3way"); err != nil { + + if err != nil { return err } } |