@@ -2,13 +2,16 @@ package git_commands
22
33import (
44"fmt"
5+ "path/filepath"
6+ "time"
57
68"github.com/fsmiamoto/git-todo-parser/todo"
79"github.com/go-errors/errors"
810"github.com/jesseduffield/lazygit/pkg/app/daemon"
911"github.com/jesseduffield/lazygit/pkg/commands/models"
1012"github.com/jesseduffield/lazygit/pkg/commands/patch"
1113"github.com/jesseduffield/lazygit/pkg/commands/types/enums"
14+ "github.com/jesseduffield/lazygit/pkg/utils"
1215)
1316
1417type PatchCommands struct {
@@ -39,14 +42,61 @@ func NewPatchCommands(
3942}
4043}
4144
45+ type ApplyPatchOpts struct {
46+ ThreeWay bool
47+ Cached bool
48+ Index bool
49+ Reverse bool
50+ }
51+
52+ func (self * PatchCommands ) ApplyCustomPatch (reverse bool ) error {
53+ patch := self .PatchBuilder .PatchToApply (reverse )
54+
55+ return self .ApplyPatch (patch , ApplyPatchOpts {
56+ Index : true ,
57+ ThreeWay : true ,
58+ Reverse : reverse ,
59+ })
60+ }
61+
62+ func (self * PatchCommands ) ApplyPatch (patch string , opts ApplyPatchOpts ) error {
63+ filepath , err := self .SaveTemporaryPatch (patch )
64+ if err != nil {
65+ return err
66+ }
67+
68+ return self .applyPatchFile (filepath , opts )
69+ }
70+
71+ func (self * PatchCommands ) applyPatchFile (filepath string , opts ApplyPatchOpts ) error {
72+ cmdStr := NewGitCmd ("apply" ).
73+ ArgIf (opts .ThreeWay , "--3way" ).
74+ ArgIf (opts .Cached , "--cached" ).
75+ ArgIf (opts .Index , "--index" ).
76+ ArgIf (opts .Reverse , "--reverse" ).
77+ Arg (self .cmd .Quote (filepath )).
78+ ToString ()
79+
80+ return self .cmd .New (cmdStr ).Run ()
81+ }
82+
83+ func (self * PatchCommands ) SaveTemporaryPatch (patch string ) (string , error ) {
84+ filepath := filepath .Join (self .os .GetTempDir (), utils .GetCurrentRepoName (), time .Now ().Format ("Jan _2 15.04.05.000000000" )+ ".patch" )
85+ self .Log .Infof ("saving temporary patch to %s" , filepath )
86+ if err := self .os .CreateFileWithContent (filepath , patch ); err != nil {
87+ return "" , err
88+ }
89+ return filepath , nil
90+ }
91+
4292// DeletePatchesFromCommit applies a patch in reverse for a commit
4393func (self * PatchCommands ) DeletePatchesFromCommit (commits []* models.Commit , commitIndex int ) error {
4494if err := self .rebase .BeginInteractiveRebaseForCommit (commits , commitIndex ); err != nil {
4595return err
4696}
4797
4898// apply each patch in reverse
49- if err := self .PatchBuilder . ApplyPatches (true ); err != nil {
99+ if err := self .ApplyCustomPatch (true ); err != nil {
50100_ = self .rebase .AbortRebase ()
51101return err
52102}
@@ -72,7 +122,7 @@ func (self *PatchCommands) MovePatchToSelectedCommit(commits []*models.Commit, s
72122}
73123
74124// apply each patch forward
75- if err := self .PatchBuilder . ApplyPatches (false ); err != nil {
125+ if err := self .ApplyCustomPatch (false ); err != nil {
76126// Don't abort the rebase here; this might cause conflicts, so give
77127// the user a chance to resolve them
78128return err
@@ -121,7 +171,7 @@ func (self *PatchCommands) MovePatchToSelectedCommit(commits []*models.Commit, s
121171}
122172
123173// apply each patch in reverse
124- if err := self .PatchBuilder . ApplyPatches (true ); err != nil {
174+ if err := self .ApplyCustomPatch (true ); err != nil {
125175_ = self .rebase .AbortRebase ()
126176return err
127177}
@@ -144,7 +194,7 @@ func (self *PatchCommands) MovePatchToSelectedCommit(commits []*models.Commit, s
144194self .rebase .onSuccessfulContinue = func () error {
145195// now we should be up to the destination, so let's apply forward these patches to that.
146196// ideally we would ensure we're on the right commit but I'm not sure if that check is necessary
147- if err := self .rebase . workingTree . ApplyPatch (patch , "index" , "3way" ); err != nil {
197+ if err := self .ApplyPatch (patch , ApplyPatchOpts { Index : true , ThreeWay : true } ); err != nil {
148198// Don't abort the rebase here; this might cause conflicts, so give
149199// the user a chance to resolve them
150200return err
@@ -177,7 +227,7 @@ func (self *PatchCommands) MovePatchIntoIndex(commits []*models.Commit, commitId
177227return err
178228}
179229
180- if err := self .PatchBuilder . ApplyPatches (true ); err != nil {
230+ if err := self .ApplyCustomPatch (true ); err != nil {
181231if self .status .WorkingTreeState () == enums .REBASE_MODE_REBASING {
182232_ = self .rebase .AbortRebase ()
183233}
@@ -201,7 +251,7 @@ func (self *PatchCommands) MovePatchIntoIndex(commits []*models.Commit, commitId
201251
202252self .rebase .onSuccessfulContinue = func () error {
203253// add patches to index
204- if err := self .rebase . workingTree . ApplyPatch (patch , "index" , "3way" ); err != nil {
254+ if err := self .ApplyPatch (patch , ApplyPatchOpts { Index : true , ThreeWay : true } ); err != nil {
205255if self .status .WorkingTreeState () == enums .REBASE_MODE_REBASING {
206256_ = self .rebase .AbortRebase ()
207257}
@@ -226,7 +276,7 @@ func (self *PatchCommands) PullPatchIntoNewCommit(commits []*models.Commit, comm
226276return err
227277}
228278
229- if err := self .PatchBuilder . ApplyPatches (true ); err != nil {
279+ if err := self .ApplyCustomPatch (true ); err != nil {
230280_ = self .rebase .AbortRebase ()
231281return err
232282}
@@ -242,7 +292,7 @@ func (self *PatchCommands) PullPatchIntoNewCommit(commits []*models.Commit, comm
242292return err
243293}
244294
245- if err := self .rebase . workingTree . ApplyPatch (patch , "index" , "3way" ); err != nil {
295+ if err := self .ApplyPatch (patch , ApplyPatchOpts { Index : true , ThreeWay : true } ); err != nil {
246296_ = self .rebase .AbortRebase ()
247297return err
248298}
0 commit comments