diff options
-rw-r--r-- | pkg/commands/git.go | 1 | ||||
-rw-r--r-- | pkg/commands/patch_manager.go | 27 | ||||
-rw-r--r-- | pkg/gui/staging_panel.go | 2 |
3 files changed, 20 insertions, 10 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 } } diff --git a/pkg/gui/staging_panel.go b/pkg/gui/staging_panel.go index 13878e2f0..47240d87e 100644 --- a/pkg/gui/staging_panel.go +++ b/pkg/gui/staging_panel.go @@ -68,7 +68,7 @@ func (gui *Gui) refreshStagingPanel() error { return err } - secondaryColorDiff := gui.GitCommand.PatchManager.RenderPatchForFile(commitFile.Name, false, false) + secondaryColorDiff := gui.GitCommand.PatchManager.RenderPatchForFile(commitFile.Name, false, false, true) if err != nil { return err } |