2022-01-08 05:00:36 +02:00
|
|
|
package git_commands
|
2019-11-04 10:47:25 +02:00
|
|
|
|
2020-04-27 18:31:22 +02:00
|
|
|
import (
|
2023-05-19 12:18:02 +02:00
|
|
|
"path/filepath"
|
|
|
|
"time"
|
2020-08-15 03:18:40 +02:00
|
|
|
|
2023-04-06 08:15:15 +02:00
|
|
|
"github.com/fsmiamoto/git-todo-parser/todo"
|
2020-04-27 18:31:22 +02:00
|
|
|
"github.com/go-errors/errors"
|
2023-04-06 11:13:42 +02:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/app/daemon"
|
2020-09-29 12:28:39 +02:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/commands/models"
|
2020-08-15 03:18:40 +02:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/commands/patch"
|
2021-12-30 04:35:10 +02:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/commands/types/enums"
|
2020-04-27 18:31:22 +02:00
|
|
|
)
|
2019-11-04 10:47:25 +02:00
|
|
|
|
2022-01-02 01:34:33 +02:00
|
|
|
type PatchCommands struct {
|
2022-01-18 12:26:21 +02:00
|
|
|
*GitCommon
|
|
|
|
rebase *RebaseCommands
|
|
|
|
commit *CommitCommands
|
|
|
|
status *StatusCommands
|
|
|
|
stash *StashCommands
|
|
|
|
|
2023-03-19 07:09:03 +02:00
|
|
|
PatchBuilder *patch.PatchBuilder
|
2022-01-02 01:34:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewPatchCommands(
|
2022-01-18 12:26:21 +02:00
|
|
|
gitCommon *GitCommon,
|
|
|
|
rebase *RebaseCommands,
|
|
|
|
commit *CommitCommands,
|
|
|
|
status *StatusCommands,
|
|
|
|
stash *StashCommands,
|
2023-03-19 07:09:03 +02:00
|
|
|
patchBuilder *patch.PatchBuilder,
|
2022-01-02 01:34:33 +02:00
|
|
|
) *PatchCommands {
|
|
|
|
return &PatchCommands{
|
2022-01-18 12:26:21 +02:00
|
|
|
GitCommon: gitCommon,
|
|
|
|
rebase: rebase,
|
|
|
|
commit: commit,
|
|
|
|
status: status,
|
|
|
|
stash: stash,
|
2023-03-19 07:09:03 +02:00
|
|
|
PatchBuilder: patchBuilder,
|
2022-01-02 01:34:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-19 12:18:02 +02:00
|
|
|
type ApplyPatchOpts struct {
|
|
|
|
ThreeWay bool
|
|
|
|
Cached bool
|
|
|
|
Index bool
|
|
|
|
Reverse bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *PatchCommands) ApplyCustomPatch(reverse bool) error {
|
|
|
|
patch := self.PatchBuilder.PatchToApply(reverse)
|
|
|
|
|
|
|
|
return self.ApplyPatch(patch, ApplyPatchOpts{
|
|
|
|
Index: true,
|
|
|
|
ThreeWay: true,
|
|
|
|
Reverse: reverse,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *PatchCommands) ApplyPatch(patch string, opts ApplyPatchOpts) error {
|
|
|
|
filepath, err := self.SaveTemporaryPatch(patch)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return self.applyPatchFile(filepath, opts)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *PatchCommands) applyPatchFile(filepath string, opts ApplyPatchOpts) error {
|
2023-05-21 09:00:29 +02:00
|
|
|
cmdArgs := NewGitCmd("apply").
|
2023-05-19 12:18:02 +02:00
|
|
|
ArgIf(opts.ThreeWay, "--3way").
|
|
|
|
ArgIf(opts.Cached, "--cached").
|
|
|
|
ArgIf(opts.Index, "--index").
|
|
|
|
ArgIf(opts.Reverse, "--reverse").
|
2023-05-21 09:00:29 +02:00
|
|
|
Arg(filepath).
|
|
|
|
ToArgv()
|
2023-05-19 12:18:02 +02:00
|
|
|
|
2023-05-21 09:00:29 +02:00
|
|
|
return self.cmd.New(cmdArgs).Run()
|
2023-05-19 12:18:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (self *PatchCommands) SaveTemporaryPatch(patch string) (string, error) {
|
2023-07-28 10:27:14 +02:00
|
|
|
filepath := filepath.Join(self.os.GetTempDir(), self.repoPaths.RepoName(), time.Now().Format("Jan _2 15.04.05.000000000")+".patch")
|
2023-05-19 12:18:02 +02:00
|
|
|
self.Log.Infof("saving temporary patch to %s", filepath)
|
|
|
|
if err := self.os.CreateFileWithContent(filepath, patch); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return filepath, nil
|
|
|
|
}
|
|
|
|
|
2019-11-04 10:47:25 +02:00
|
|
|
// DeletePatchesFromCommit applies a patch in reverse for a commit
|
2022-01-02 01:34:33 +02:00
|
|
|
func (self *PatchCommands) DeletePatchesFromCommit(commits []*models.Commit, commitIndex int) error {
|
2023-05-19 18:46:19 +02:00
|
|
|
if err := self.rebase.BeginInteractiveRebaseForCommit(commits, commitIndex, false); err != nil {
|
2019-11-04 10:47:25 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// apply each patch in reverse
|
2023-05-19 12:18:02 +02:00
|
|
|
if err := self.ApplyCustomPatch(true); err != nil {
|
2023-03-18 09:17:47 +02:00
|
|
|
_ = self.rebase.AbortRebase()
|
2019-11-04 10:47:25 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// time to amend the selected commit
|
2022-01-02 01:34:33 +02:00
|
|
|
if err := self.commit.AmendHead(); err != nil {
|
2019-11-04 10:47:25 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-01-02 01:34:33 +02:00
|
|
|
self.rebase.onSuccessfulContinue = func() error {
|
2023-03-19 07:09:03 +02:00
|
|
|
self.PatchBuilder.Reset()
|
2019-11-05 02:56:29 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-04 10:47:25 +02:00
|
|
|
// continue
|
2022-01-09 04:36:07 +02:00
|
|
|
return self.rebase.ContinueRebase()
|
2019-11-04 10:47:25 +02:00
|
|
|
}
|
|
|
|
|
2022-01-02 01:34:33 +02:00
|
|
|
func (self *PatchCommands) MovePatchToSelectedCommit(commits []*models.Commit, sourceCommitIdx int, destinationCommitIdx int) error {
|
2019-11-04 10:47:25 +02:00
|
|
|
if sourceCommitIdx < destinationCommitIdx {
|
2023-05-19 18:46:19 +02:00
|
|
|
// Passing true for keepCommitsThatBecomeEmpty: if the moved-from
|
|
|
|
// commit becomes empty, we want to keep it, mainly for consistency with
|
|
|
|
// moving the patch to a *later* commit, which behaves the same.
|
|
|
|
if err := self.rebase.BeginInteractiveRebaseForCommit(commits, destinationCommitIdx, true); err != nil {
|
2019-11-04 10:47:25 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// apply each patch forward
|
2023-05-19 12:18:02 +02:00
|
|
|
if err := self.ApplyCustomPatch(false); err != nil {
|
2023-03-18 09:17:47 +02:00
|
|
|
// Don't abort the rebase here; this might cause conflicts, so give
|
|
|
|
// the user a chance to resolve them
|
2019-11-04 10:47:25 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// amend the destination commit
|
2022-01-02 01:34:33 +02:00
|
|
|
if err := self.commit.AmendHead(); err != nil {
|
2019-11-04 10:47:25 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-01-02 01:34:33 +02:00
|
|
|
self.rebase.onSuccessfulContinue = func() error {
|
2023-03-19 07:09:03 +02:00
|
|
|
self.PatchBuilder.Reset()
|
2019-11-05 02:56:29 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-04 10:47:25 +02:00
|
|
|
// continue
|
2022-01-09 04:36:07 +02:00
|
|
|
return self.rebase.ContinueRebase()
|
2019-11-04 10:47:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(commits)-1 < sourceCommitIdx {
|
|
|
|
return errors.New("index outside of range of commits")
|
|
|
|
}
|
|
|
|
|
|
|
|
// we can make this GPG thing possible it just means we need to do this in two parts:
|
|
|
|
// one where we handle the possibility of a credential request, and the other
|
|
|
|
// where we continue the rebase
|
2022-01-02 01:34:33 +02:00
|
|
|
if self.config.UsingGpg() {
|
|
|
|
return errors.New(self.Tr.DisabledForGPG)
|
2019-11-04 10:47:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
baseIndex := sourceCommitIdx + 1
|
2022-03-20 07:19:27 +02:00
|
|
|
|
2023-04-16 07:43:54 +02:00
|
|
|
changes := []daemon.ChangeTodoAction{
|
|
|
|
{Sha: commits[sourceCommitIdx].Sha, NewAction: todo.Edit},
|
|
|
|
{Sha: commits[destinationCommitIdx].Sha, NewAction: todo.Edit},
|
|
|
|
}
|
|
|
|
self.os.LogCommand(logTodoChanges(changes), false)
|
|
|
|
|
2023-04-02 11:07:40 +02:00
|
|
|
err := self.rebase.PrepareInteractiveRebaseCommand(PrepareInteractiveRebaseCommandOpts{
|
|
|
|
baseShaOrRoot: commits[baseIndex].Sha,
|
|
|
|
overrideEditor: true,
|
2023-04-16 07:43:54 +02:00
|
|
|
instruction: daemon.NewChangeTodoActionsInstruction(changes),
|
2023-04-02 11:07:40 +02:00
|
|
|
}).Run()
|
2019-11-04 10:47:25 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// apply each patch in reverse
|
2023-05-19 12:18:02 +02:00
|
|
|
if err := self.ApplyCustomPatch(true); err != nil {
|
2023-03-18 09:17:47 +02:00
|
|
|
_ = self.rebase.AbortRebase()
|
2019-11-04 10:47:25 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// amend the source commit
|
2022-01-02 01:34:33 +02:00
|
|
|
if err := self.commit.AmendHead(); err != nil {
|
2019-11-04 10:47:25 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-03-18 09:17:47 +02:00
|
|
|
patch, err := self.diffHeadAgainstCommit(commits[sourceCommitIdx])
|
|
|
|
if err != nil {
|
|
|
|
_ = self.rebase.AbortRebase()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-01-02 01:34:33 +02:00
|
|
|
if self.rebase.onSuccessfulContinue != nil {
|
2019-11-04 10:47:25 +02:00
|
|
|
return errors.New("You are midway through another rebase operation. Please abort to start again")
|
|
|
|
}
|
|
|
|
|
2022-01-02 01:34:33 +02:00
|
|
|
self.rebase.onSuccessfulContinue = func() error {
|
2019-11-04 10:47:25 +02:00
|
|
|
// now we should be up to the destination, so let's apply forward these patches to that.
|
|
|
|
// ideally we would ensure we're on the right commit but I'm not sure if that check is necessary
|
2023-05-19 12:18:02 +02:00
|
|
|
if err := self.ApplyPatch(patch, ApplyPatchOpts{Index: true, ThreeWay: true}); err != nil {
|
2023-03-18 09:17:47 +02:00
|
|
|
// Don't abort the rebase here; this might cause conflicts, so give
|
|
|
|
// the user a chance to resolve them
|
2019-11-04 10:47:25 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// amend the destination commit
|
2022-01-02 01:34:33 +02:00
|
|
|
if err := self.commit.AmendHead(); err != nil {
|
2019-11-04 10:47:25 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-01-02 01:34:33 +02:00
|
|
|
self.rebase.onSuccessfulContinue = func() error {
|
2023-03-19 07:09:03 +02:00
|
|
|
self.PatchBuilder.Reset()
|
2019-11-05 02:56:29 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-01-09 04:36:07 +02:00
|
|
|
return self.rebase.ContinueRebase()
|
2019-11-04 10:47:25 +02:00
|
|
|
}
|
|
|
|
|
2022-01-09 04:36:07 +02:00
|
|
|
return self.rebase.ContinueRebase()
|
2019-11-04 10:47:25 +02:00
|
|
|
}
|
|
|
|
|
2022-01-02 01:34:33 +02:00
|
|
|
func (self *PatchCommands) MovePatchIntoIndex(commits []*models.Commit, commitIdx int, stash bool) error {
|
2020-03-28 03:43:31 +02:00
|
|
|
if stash {
|
2023-07-01 16:22:32 +02:00
|
|
|
if err := self.stash.Push(self.Tr.StashPrefix + commits[commitIdx].Sha); err != nil {
|
2020-03-28 03:43:31 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-19 18:46:19 +02:00
|
|
|
if err := self.rebase.BeginInteractiveRebaseForCommit(commits, commitIdx, false); err != nil {
|
2019-11-04 10:47:25 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-05-19 12:18:02 +02:00
|
|
|
if err := self.ApplyCustomPatch(true); err != nil {
|
2022-01-02 01:34:33 +02:00
|
|
|
if self.status.WorkingTreeState() == enums.REBASE_MODE_REBASING {
|
2023-03-18 09:17:47 +02:00
|
|
|
_ = self.rebase.AbortRebase()
|
2019-11-04 10:47:25 +02:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// amend the commit
|
2022-01-02 01:34:33 +02:00
|
|
|
if err := self.commit.AmendHead(); err != nil {
|
2019-11-04 10:47:25 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-03-18 09:17:47 +02:00
|
|
|
patch, err := self.diffHeadAgainstCommit(commits[commitIdx])
|
|
|
|
if err != nil {
|
|
|
|
_ = self.rebase.AbortRebase()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-01-02 01:34:33 +02:00
|
|
|
if self.rebase.onSuccessfulContinue != nil {
|
2019-11-04 10:47:25 +02:00
|
|
|
return errors.New("You are midway through another rebase operation. Please abort to start again")
|
|
|
|
}
|
|
|
|
|
2022-01-02 01:34:33 +02:00
|
|
|
self.rebase.onSuccessfulContinue = func() error {
|
2019-11-04 10:47:25 +02:00
|
|
|
// add patches to index
|
2023-05-19 12:18:02 +02:00
|
|
|
if err := self.ApplyPatch(patch, ApplyPatchOpts{Index: true, ThreeWay: true}); err != nil {
|
2022-01-02 01:34:33 +02:00
|
|
|
if self.status.WorkingTreeState() == enums.REBASE_MODE_REBASING {
|
2023-03-18 09:17:47 +02:00
|
|
|
_ = self.rebase.AbortRebase()
|
2019-11-04 10:47:25 +02:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-03-28 03:43:31 +02:00
|
|
|
if stash {
|
2022-01-02 01:34:33 +02:00
|
|
|
if err := self.stash.Apply(0); err != nil {
|
2020-03-28 03:43:31 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-19 07:09:03 +02:00
|
|
|
self.PatchBuilder.Reset()
|
2019-11-04 10:47:25 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-01-09 04:36:07 +02:00
|
|
|
return self.rebase.ContinueRebase()
|
2019-11-04 10:47:25 +02:00
|
|
|
}
|
2020-04-27 18:31:22 +02:00
|
|
|
|
2023-07-20 11:17:01 +02:00
|
|
|
func (self *PatchCommands) PullPatchIntoNewCommit(
|
|
|
|
commits []*models.Commit,
|
|
|
|
commitIdx int,
|
|
|
|
commitSummary string,
|
|
|
|
commitDescription string,
|
|
|
|
) error {
|
2023-05-19 18:46:19 +02:00
|
|
|
if err := self.rebase.BeginInteractiveRebaseForCommit(commits, commitIdx, false); err != nil {
|
2020-04-27 18:31:22 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-05-19 12:18:02 +02:00
|
|
|
if err := self.ApplyCustomPatch(true); err != nil {
|
2023-03-18 09:17:47 +02:00
|
|
|
_ = self.rebase.AbortRebase()
|
2020-04-27 18:31:22 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// amend the commit
|
2022-01-02 01:34:33 +02:00
|
|
|
if err := self.commit.AmendHead(); err != nil {
|
2020-04-27 18:31:22 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-03-18 09:17:47 +02:00
|
|
|
patch, err := self.diffHeadAgainstCommit(commits[commitIdx])
|
|
|
|
if err != nil {
|
|
|
|
_ = self.rebase.AbortRebase()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-05-19 12:18:02 +02:00
|
|
|
if err := self.ApplyPatch(patch, ApplyPatchOpts{Index: true, ThreeWay: true}); err != nil {
|
2023-03-18 09:17:47 +02:00
|
|
|
_ = self.rebase.AbortRebase()
|
2020-04-27 18:31:22 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-07-20 11:17:01 +02:00
|
|
|
if err := self.commit.CommitCmdObj(commitSummary, commitDescription).Run(); err != nil {
|
2020-04-27 18:31:22 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-01-02 01:34:33 +02:00
|
|
|
if self.rebase.onSuccessfulContinue != nil {
|
2020-04-27 18:31:22 +02:00
|
|
|
return errors.New("You are midway through another rebase operation. Please abort to start again")
|
|
|
|
}
|
|
|
|
|
2023-03-19 07:09:03 +02:00
|
|
|
self.PatchBuilder.Reset()
|
2022-01-09 04:36:07 +02:00
|
|
|
return self.rebase.ContinueRebase()
|
2020-04-27 18:31:22 +02:00
|
|
|
}
|
2023-03-18 09:17:47 +02:00
|
|
|
|
|
|
|
// We have just applied a patch in reverse to discard it from a commit; if we
|
|
|
|
// now try to apply the patch again to move it to a later commit, or to the
|
|
|
|
// index, then this would conflict "with itself" in case the patch contained
|
|
|
|
// only some lines of a range of adjacent added lines. To solve this, we
|
|
|
|
// get the diff of HEAD and the original commit and then apply that.
|
|
|
|
func (self *PatchCommands) diffHeadAgainstCommit(commit *models.Commit) (string, error) {
|
2023-05-21 09:00:29 +02:00
|
|
|
cmdArgs := NewGitCmd("diff").Arg("HEAD.." + commit.Sha).ToArgv()
|
2023-05-19 12:18:02 +02:00
|
|
|
|
2023-05-21 09:00:29 +02:00
|
|
|
return self.cmd.New(cmdArgs).RunWithOutput()
|
2023-03-18 09:17:47 +02:00
|
|
|
}
|