summaryrefslogtreecommitdiffstats
path: root/pkg/commands/patch_rebases.go
blob: c51a24ed77f2bd14aabc93e5b1103cf2a5809653 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package commands

import (
	"fmt"

	"github.com/go-errors/errors"
	"github.com/jesseduffield/lazygit/pkg/commands/models"
	"github.com/jesseduffield/lazygit/pkg/commands/patch"
)

// DeletePatchesFromCommit applies a patch in reverse for a commit
func (c *GitCommand) DeletePatchesFromCommit(commits []*models.Commit, commitIndex int, p *patch.PatchManager) error {
	if err := c.BeginInteractiveRebaseForCommit(commits, commitIndex); err != nil {
		return err
	}

	// apply each patch in reverse
	if err := p.ApplyPatches(true); err != nil {
		if err := c.GenericMergeOrRebaseAction("rebase", "abort"); err != nil {
			return err
		}
		return err
	}

	// time to amend the selected commit
	if _, err := c.AmendHead(); err != nil {
		return err
	}

	c.onSuccessfulContinue = func() error {
		c.PatchManager.Reset()
		return nil
	}

	// continue
	return c.GenericMergeOrRebaseAction("rebase", "continue")
}

func (c *GitCommand) MovePatchToSelectedCommit(commits []*models.Commit, sourceCommitIdx int, destinationCommitIdx int, p *patch.PatchManager) error {
	if sourceCommitIdx < destinationCommitIdx {
		if err := c.BeginInteractiveRebaseForCommit(commits, destinationCommitIdx); err != nil {
			return err
		}

		// apply each patch forward
		if err := p.ApplyPatches(false); err != nil {
			if err := c.GenericMergeOrRebaseAction("rebase", "abort"); err != nil {
				return err
			}
			return err
		}

		// amend the destination commit
		if _, err := c.AmendHead(); err != nil {
			return err
		}

		c.onSuccessfulContinue = func() error {
			c.PatchManager.Reset()
			return nil
		}

		// continue
		return c.GenericMergeOrRebaseAction("rebase", "continue")
	}

	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
	if c.usingGpg() {
		return errors.New(c.Tr.SLocalize("DisabledForGPG"))
	}

	baseIndex := sourceCommitIdx + 1
	todo := ""
	for i, commit := range commits[0:baseIndex] {
		a := "pick"
		if i == sourceCommitIdx || i == destinationCommitIdx {
			a = "edit"
		}
		todo = a + " " + commit.Sha + " " + commit.Name + "\n" + todo
	}

	cmd, err := c.PrepareInteractiveRebaseCommand(commits[baseIndex].Sha, todo, true)
	if err != nil {
		return err
	}

	if err := c.OSCommand.RunPreparedCommand(cmd); err != nil {
		return err
	}

	// apply each patch in reverse
	if err := p.ApplyPatches(true); err != nil {
		if err := c.GenericMergeOrRebaseAction("rebase", "abort"); err != nil {
			return err
		}
		return err
	}

	// amend the source commit
	if _, err := c.AmendHead(); err != nil {
		return err
	}

	if c.onSuccessfulContinue != nil {
		return errors.New("You are midway through another rebase operation. Please abort to start again")
	}

	c.onSuccessfulContinue = func() error {
		// 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
		if err := p.ApplyPatches(false); err != nil {
			if err := c.GenericMergeOrRebaseAction("rebase", "abort"); err != nil {
				return err
			}
			return err
		}

		// amend the destination commit
		if _, err := c.AmendHead(); err != nil {
			return err
		}

		c.onSuccessfulContinue = func() error {
			c.PatchManager.Reset()
			return nil
		}

		return c.GenericMergeOrRebaseAction("rebase", "continue")
	}

	return c.GenericMergeOrRebaseAction("rebase", "continue")
}

func (c *GitCommand) PullPatchIntoIndex(commits []*models.Commit, commitIdx int, p *patch.PatchManager, stash bool) error {
	if stash {
		if err := c.StashSave(c.Tr.SLocalize("StashPrefix") + commits[commitIdx].Sha); err != nil {
			return err
		}
	}

	if err := c.BeginInteractiveRebaseForCommit(commits, commitIdx); err != nil {
		return err
	}

	if err := p.ApplyPatches(true); err != nil {
		if c.WorkingTreeState() == "rebasing" {
			if err := c.GenericMergeOrRebaseAction("rebase", "abort"); err != nil {
				return err
			}
		}
		return err
	}

	// amend the commit
	if _, err := c.AmendHead(); err != nil {
		return err
	}

	if c.onSuccessfulContinue != nil {
		return errors.New("You are midway through another rebase operation. Please abort to start again")
	}

	c.onSuccessfulContinue = func() error {
		// add patches to index
		if err := p.ApplyPatches(false); err != nil {
			if c.WorkingTreeState() == "rebasing" {
				if err := c.GenericMergeOrRebaseAction("rebase", "abort"); err != nil {
					return err
				}
			}
			return err
		}

		if stash {
			if err := c.StashDo(0, "apply"); err != nil {
				return err
			}
		}

		c.PatchManager.Reset()
		return nil
	}

	return c.GenericMergeOrRebaseAction("rebase", "continue")
}

func (c *GitCommand) PullPatchIntoNewCommit(commits []*models.Commit, commitIdx int, p *patch.PatchManager) error {
	if err := c.BeginInteractiveRebaseForCommit(commits, commitIdx); err != nil {
		return err
	}

	if err := p.ApplyPatches(true); err != nil {
		if err := c.GenericMergeOrRebaseAction("rebase", "abort"); err != nil {
			return err
		}
		return err
	}

	// amend the commit
	if _, err := c.AmendHead(); err != nil {
		return err
	}

	// add patches to index
	if err := p.ApplyPatches(false); err != nil {
		if err := c.GenericMergeOrRebaseAction("rebase", "abort"); err != nil {
			return err
		}
		return err
	}

	head_message, _ := c.GetHeadCommitMessage()
	new_message := fmt.Sprintf("Split from \"%s\"", head_message)
	_, err := c.Commit(new_message, "")
	if err != nil {
		return err
	}

	if c.onSuccessfulContinue != nil {
		return errors.New("You are midway through another rebase operation. Please abort to start again")
	}

	c.PatchManager.Reset()
	return c.GenericMergeOrRebaseAction("rebase", "continue")
}