mirror of
https://github.com/jesseduffield/lazygit.git
synced 2025-04-15 11:56:37 +02:00
Several custom patch commands on parts of an added file would fail with the confusing error message "error: new file XXX depends on old contents". These were dropping the custom patch from the original commit, moving the patch to a new commit, moving it to a later commit, or moving it to the index. We fix this by converting the patch header from an added file to a diff against an empty file. We do this not just for the purpose of applying the patch, but also for rendering it and copying it to the clip board. I'm not sure it matters much in these cases, but it does feel more correct for a filtered patch to be presented this way.
117 lines
3.2 KiB
Go
117 lines
3.2 KiB
Go
package helpers
|
|
|
|
import (
|
|
"errors"
|
|
|
|
"github.com/jesseduffield/lazygit/pkg/commands/patch"
|
|
"github.com/jesseduffield/lazygit/pkg/commands/types/enums"
|
|
"github.com/jesseduffield/lazygit/pkg/gui/patch_exploring"
|
|
"github.com/jesseduffield/lazygit/pkg/gui/types"
|
|
)
|
|
|
|
type IPatchBuildingHelper interface {
|
|
ValidateNormalWorkingTreeState() (bool, error)
|
|
}
|
|
|
|
type PatchBuildingHelper struct {
|
|
c *HelperCommon
|
|
}
|
|
|
|
func NewPatchBuildingHelper(
|
|
c *HelperCommon,
|
|
) *PatchBuildingHelper {
|
|
return &PatchBuildingHelper{
|
|
c: c,
|
|
}
|
|
}
|
|
|
|
func (self *PatchBuildingHelper) ValidateNormalWorkingTreeState() (bool, error) {
|
|
if self.c.Git().Status.WorkingTreeState() != enums.REBASE_MODE_NONE {
|
|
return false, errors.New(self.c.Tr.CantPatchWhileRebasingError)
|
|
}
|
|
return true, nil
|
|
}
|
|
|
|
// takes us from the patch building panel back to the commit files panel
|
|
func (self *PatchBuildingHelper) Escape() error {
|
|
return self.c.PopContext()
|
|
}
|
|
|
|
// kills the custom patch and returns us back to the commit files panel if needed
|
|
func (self *PatchBuildingHelper) Reset() error {
|
|
self.c.Git().Patch.PatchBuilder.Reset()
|
|
|
|
if self.c.CurrentStaticContext().GetKind() != types.SIDE_CONTEXT {
|
|
if err := self.Escape(); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
if err := self.c.Refresh(types.RefreshOptions{
|
|
Scope: []types.RefreshableView{types.COMMIT_FILES},
|
|
}); err != nil {
|
|
return err
|
|
}
|
|
|
|
// refreshing the current context so that the secondary panel is hidden if necessary.
|
|
return self.c.PostRefreshUpdate(self.c.CurrentContext())
|
|
}
|
|
|
|
func (self *PatchBuildingHelper) RefreshPatchBuildingPanel(opts types.OnFocusOpts) error {
|
|
selectedLineIdx := -1
|
|
if opts.ClickedWindowName == "main" {
|
|
selectedLineIdx = opts.ClickedViewLineIdx
|
|
}
|
|
|
|
if !self.c.Git().Patch.PatchBuilder.Active() {
|
|
return self.Escape()
|
|
}
|
|
|
|
// get diff from commit file that's currently selected
|
|
path := self.c.Contexts().CommitFiles.GetSelectedPath()
|
|
if path == "" {
|
|
return nil
|
|
}
|
|
|
|
ref := self.c.Contexts().CommitFiles.CommitFileTreeViewModel.GetRef()
|
|
to := ref.RefName()
|
|
from, reverse := self.c.Modes().Diffing.GetFromAndReverseArgsForDiff(ref.ParentRefName())
|
|
diff, err := self.c.Git().WorkingTree.ShowFileDiff(from, to, reverse, path, true)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
secondaryDiff := self.c.Git().Patch.PatchBuilder.RenderPatchForFile(patch.RenderPatchForFileOpts{
|
|
Filename: path,
|
|
Plain: false,
|
|
Reverse: false,
|
|
TurnAddedFilesIntoDiffAgainstEmptyFile: true,
|
|
})
|
|
|
|
context := self.c.Contexts().CustomPatchBuilder
|
|
|
|
oldState := context.GetState()
|
|
|
|
state := patch_exploring.NewState(diff, selectedLineIdx, oldState, self.c.Log)
|
|
context.SetState(state)
|
|
if state == nil {
|
|
return self.Escape()
|
|
}
|
|
|
|
mainContent := context.GetContentToRender(true)
|
|
|
|
self.c.Contexts().CustomPatchBuilder.FocusSelection()
|
|
|
|
return self.c.RenderToMainViews(types.RefreshMainOpts{
|
|
Pair: self.c.MainViewPairs().PatchBuilding,
|
|
Main: &types.ViewUpdateOpts{
|
|
Task: types.NewRenderStringWithoutScrollTask(mainContent),
|
|
Title: self.c.Tr.Patch,
|
|
},
|
|
Secondary: &types.ViewUpdateOpts{
|
|
Task: types.NewRenderStringWithoutScrollTask(secondaryDiff),
|
|
Title: self.c.Tr.CustomPatch,
|
|
},
|
|
})
|
|
}
|