Skip to content

Commit 048cede

Browse files
committed
fix(rebase): correct parent commit selection logic when git.log.showWholeGraph=true
- Able to accurately select the parent commit now, preventing incorrect operations on merge commits and complex histories. - Updated method signatures to accept a parent commit index for precise targeting. - Improved english confirmation prompts and translations to clarify actions affect the parent commit below. - Fixed: squash "s", drop "d", reword "r/R", discard "d", edit/interactive-rebase "e", fixup "f", amend "a/A, patch "Ctrl-P" - Not yet fixed: interative-rebase "i" fix: GetParentCommit edge cases
1 parent 32a701c commit 048cede

File tree

12 files changed

+139
-69
lines changed

12 files changed

+139
-69
lines changed

pkg/commands/git_commands/patch.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@ func (self *PatchCommands) SaveTemporaryPatch(patch string) (string, error) {
8888
}
8989

9090
// DeletePatchesFromCommit applies a patch in reverse for a commit
91-
func (self *PatchCommands) DeletePatchesFromCommit(commits []*models.Commit, commitIndex int) error {
92-
if err := self.rebase.BeginInteractiveRebaseForCommit(commits, commitIndex, false); err != nil {
91+
func (self *PatchCommands) DeletePatchesFromCommit(commits []*models.Commit, commitIndex int, parentIdx int) error {
92+
if err := self.rebase.BeginInteractiveRebaseForCommit(commits, commitIndex, parentIdx, false); err != nil {
9393
return err
9494
}
9595

@@ -113,12 +113,12 @@ func (self *PatchCommands) DeletePatchesFromCommit(commits []*models.Commit, com
113113
return self.rebase.ContinueRebase()
114114
}
115115

116-
func (self *PatchCommands) MovePatchToSelectedCommit(commits []*models.Commit, sourceCommitIdx int, destinationCommitIdx int) error {
116+
func (self *PatchCommands) MovePatchToSelectedCommit(commits []*models.Commit, sourceCommitIdx int, destinationCommitIdx int, parentIdx int) error {
117117
if sourceCommitIdx < destinationCommitIdx {
118118
// Passing true for keepCommitsThatBecomeEmpty: if the moved-from
119119
// commit becomes empty, we want to keep it, mainly for consistency with
120120
// moving the patch to a *later* commit, which behaves the same.
121-
if err := self.rebase.BeginInteractiveRebaseForCommit(commits, destinationCommitIdx, true); err != nil {
121+
if err := self.rebase.BeginInteractiveRebaseForCommit(commits, destinationCommitIdx, parentIdx, true); err != nil {
122122
return err
123123
}
124124

@@ -217,14 +217,14 @@ func (self *PatchCommands) MovePatchToSelectedCommit(commits []*models.Commit, s
217217
return self.rebase.ContinueRebase()
218218
}
219219

220-
func (self *PatchCommands) MovePatchIntoIndex(commits []*models.Commit, commitIdx int, stash bool) error {
220+
func (self *PatchCommands) MovePatchIntoIndex(commits []*models.Commit, commitIdx int, parentIdx int, stash bool) error {
221221
if stash {
222222
if err := self.stash.Push(fmt.Sprintf(self.Tr.AutoStashForMovingPatchToIndex, commits[commitIdx].ShortHash())); err != nil {
223223
return err
224224
}
225225
}
226226

227-
if err := self.rebase.BeginInteractiveRebaseForCommit(commits, commitIdx, false); err != nil {
227+
if err := self.rebase.BeginInteractiveRebaseForCommit(commits, commitIdx, parentIdx, false); err != nil {
228228
return err
229229
}
230230

@@ -275,10 +275,11 @@ func (self *PatchCommands) MovePatchIntoIndex(commits []*models.Commit, commitId
275275
func (self *PatchCommands) PullPatchIntoNewCommit(
276276
commits []*models.Commit,
277277
commitIdx int,
278+
parentIdx int,
278279
commitSummary string,
279280
commitDescription string,
280281
) error {
281-
if err := self.rebase.BeginInteractiveRebaseForCommit(commits, commitIdx, false); err != nil {
282+
if err := self.rebase.BeginInteractiveRebaseForCommit(commits, commitIdx, parentIdx, false); err != nil {
282283
return err
283284
}
284285

@@ -318,10 +319,11 @@ func (self *PatchCommands) PullPatchIntoNewCommit(
318319
func (self *PatchCommands) PullPatchIntoNewCommitBefore(
319320
commits []*models.Commit,
320321
commitIdx int,
322+
parentIdx int,
321323
commitSummary string,
322324
commitDescription string,
323325
) error {
324-
if err := self.rebase.BeginInteractiveRebaseForCommit(commits, commitIdx+1, true); err != nil {
326+
if err := self.rebase.BeginInteractiveRebaseForCommit(commits, commitIdx+1, parentIdx, true); err != nil {
325327
return err
326328
}
327329

pkg/commands/git_commands/rebase.go

Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,14 @@ func NewRebaseCommands(
3434
}
3535
}
3636

37-
func (self *RebaseCommands) RewordCommit(commits []*models.Commit, index int, summary string, description string) error {
37+
func (self *RebaseCommands) RewordCommit(commits []*models.Commit, index int, parentIdx int, summary string, description string) error {
3838
// This check is currently unreachable (handled in LocalCommitsController.reword),
3939
// but kept as a safeguard in case this method is used elsewhere.
4040
if self.config.NeedsGpgSubprocessForCommit() {
4141
return errors.New(self.Tr.DisabledForGPG)
4242
}
4343

44-
err := self.BeginInteractiveRebaseForCommit(commits, index, false)
44+
err := self.BeginInteractiveRebaseForCommit(commits, index, parentIdx, false)
4545
if err != nil {
4646
return err
4747
}
@@ -55,44 +55,44 @@ func (self *RebaseCommands) RewordCommit(commits []*models.Commit, index int, su
5555
return self.ContinueRebase()
5656
}
5757

58-
func (self *RebaseCommands) RewordCommitInEditor(commits []*models.Commit, index int) (*oscommands.CmdObj, error) {
58+
func (self *RebaseCommands) RewordCommitInEditor(commits []*models.Commit, index int, parentIdx int) (*oscommands.CmdObj, error) {
5959
changes := []daemon.ChangeTodoAction{{
6060
Hash: commits[index].Hash(),
6161
NewAction: todo.Reword,
6262
}}
6363
self.os.LogCommand(logTodoChanges(changes), false)
6464

6565
return self.PrepareInteractiveRebaseCommand(PrepareInteractiveRebaseCommandOpts{
66-
baseHashOrRoot: getBaseHashOrRoot(commits, index+1),
66+
baseHashOrRoot: getBaseHashOrRoot(commits, parentIdx),
6767
instruction: daemon.NewChangeTodoActionsInstruction(changes),
6868
}), nil
6969
}
7070

71-
func (self *RebaseCommands) ResetCommitAuthor(commits []*models.Commit, start, end int) error {
72-
return self.GenericAmend(commits, start, end, func(_ *models.Commit) error {
71+
func (self *RebaseCommands) ResetCommitAuthor(commits []*models.Commit, start, end int, parentIdx int) error {
72+
return self.GenericAmend(commits, start, end, parentIdx, func(_ *models.Commit) error {
7373
return self.commit.ResetAuthor()
7474
})
7575
}
7676

77-
func (self *RebaseCommands) SetCommitAuthor(commits []*models.Commit, start, end int, value string) error {
78-
return self.GenericAmend(commits, start, end, func(_ *models.Commit) error {
77+
func (self *RebaseCommands) SetCommitAuthor(commits []*models.Commit, start, end int, parentIdx int, value string) error {
78+
return self.GenericAmend(commits, start, end, parentIdx, func(_ *models.Commit) error {
7979
return self.commit.SetAuthor(value)
8080
})
8181
}
8282

83-
func (self *RebaseCommands) AddCommitCoAuthor(commits []*models.Commit, start, end int, value string) error {
84-
return self.GenericAmend(commits, start, end, func(commit *models.Commit) error {
83+
func (self *RebaseCommands) AddCommitCoAuthor(commits []*models.Commit, start, end int, parentIdx int, value string) error {
84+
return self.GenericAmend(commits, start, end, parentIdx, func(commit *models.Commit) error {
8585
return self.commit.AddCoAuthor(commit.Hash(), value)
8686
})
8787
}
8888

89-
func (self *RebaseCommands) GenericAmend(commits []*models.Commit, start, end int, f func(commit *models.Commit) error) error {
89+
func (self *RebaseCommands) GenericAmend(commits []*models.Commit, start, end int, parentIdx int, f func(commit *models.Commit) error) error {
9090
if start == end && models.IsHeadCommit(commits, start) {
9191
// we've selected the top commit so no rebase is required
9292
return f(commits[start])
9393
}
9494

95-
err := self.BeginInteractiveRebaseForCommitRange(commits, start, end, false)
95+
err := self.BeginInteractiveRebaseForCommitRange(commits, start, end, parentIdx, false)
9696
if err != nil {
9797
return err
9898
}
@@ -139,13 +139,8 @@ func (self *RebaseCommands) MoveCommitsUp(commits []*models.Commit, startIdx int
139139
}).Run()
140140
}
141141

142-
func (self *RebaseCommands) InteractiveRebase(commits []*models.Commit, startIdx int, endIdx int, action todo.TodoCommand) error {
143-
baseIndex := endIdx + 1
144-
if action == todo.Squash || action == todo.Fixup {
145-
baseIndex++
146-
}
147-
148-
baseHashOrRoot := getBaseHashOrRoot(commits, baseIndex)
142+
func (self *RebaseCommands) InteractiveRebase(commits []*models.Commit, startIdx int, endIdx int, parentIdx int, action todo.TodoCommand) error {
143+
baseHashOrRoot := getBaseHashOrRoot(commits, parentIdx)
149144

150145
changes := lo.FilterMap(commits[startIdx:endIdx+1], func(commit *models.Commit, _ int) (daemon.ChangeTodoAction, bool) {
151146
return daemon.ChangeTodoAction{
@@ -294,7 +289,7 @@ func (self *RebaseCommands) getHashOfLastCommitMade() (string, error) {
294289
}
295290

296291
// AmendTo amends the given commit with whatever files are staged
297-
func (self *RebaseCommands) AmendTo(commits []*models.Commit, commitIndex int) error {
292+
func (self *RebaseCommands) AmendTo(commits []*models.Commit, commitIndex int, parentIdx int) error {
298293
commit := commits[commitIndex]
299294

300295
if err := self.commit.CreateFixupCommit(commit.Hash()); err != nil {
@@ -307,7 +302,7 @@ func (self *RebaseCommands) AmendTo(commits []*models.Commit, commitIndex int) e
307302
}
308303

309304
return self.PrepareInteractiveRebaseCommand(PrepareInteractiveRebaseCommandOpts{
310-
baseHashOrRoot: getBaseHashOrRoot(commits, commitIndex+1),
305+
baseHashOrRoot: getBaseHashOrRoot(commits, parentIdx),
311306
overrideEditor: true,
312307
instruction: daemon.NewMoveFixupCommitDownInstruction(commit.Hash(), fixupHash, true),
313308
}).Run()
@@ -401,7 +396,7 @@ func (self *RebaseCommands) SquashAllAboveFixupCommits(commit *models.Commit) er
401396
// BeginInteractiveRebaseForCommit starts an interactive rebase to edit the current
402397
// commit and pick all others. After this you'll want to call `self.ContinueRebase()
403398
func (self *RebaseCommands) BeginInteractiveRebaseForCommit(
404-
commits []*models.Commit, commitIndex int, keepCommitsThatBecomeEmpty bool,
399+
commits []*models.Commit, commitIndex int, parentIdx int, keepCommitsThatBecomeEmpty bool,
405400
) error {
406401
if commitIndex < len(commits) && commits[commitIndex].IsMerge() {
407402
if self.config.NeedsGpgSubprocessForCommit() {
@@ -415,11 +410,11 @@ func (self *RebaseCommands) BeginInteractiveRebaseForCommit(
415410
}).Run()
416411
}
417412

418-
return self.BeginInteractiveRebaseForCommitRange(commits, commitIndex, commitIndex, keepCommitsThatBecomeEmpty)
413+
return self.BeginInteractiveRebaseForCommitRange(commits, commitIndex, commitIndex, parentIdx, keepCommitsThatBecomeEmpty)
419414
}
420415

421416
func (self *RebaseCommands) BeginInteractiveRebaseForCommitRange(
422-
commits []*models.Commit, start, end int, keepCommitsThatBecomeEmpty bool,
417+
commits []*models.Commit, start, end int, parentIdx int, keepCommitsThatBecomeEmpty bool,
423418
) error {
424419
if len(commits)-1 < end {
425420
return errors.New("index outside of range of commits")
@@ -442,7 +437,7 @@ func (self *RebaseCommands) BeginInteractiveRebaseForCommitRange(
442437
self.os.LogCommand(logTodoChanges(changes), false)
443438

444439
return self.PrepareInteractiveRebaseCommand(PrepareInteractiveRebaseCommandOpts{
445-
baseHashOrRoot: getBaseHashOrRoot(commits, end+1),
440+
baseHashOrRoot: getBaseHashOrRoot(commits, parentIdx),
446441
overrideEditor: true,
447442
keepCommitsThatBecomeEmpty: keepCommitsThatBecomeEmpty,
448443
instruction: daemon.NewChangeTodoActionsInstruction(changes),
@@ -515,8 +510,8 @@ func (self *RebaseCommands) runSkipEditorCommand(cmdObj *oscommands.CmdObj) erro
515510
}
516511

517512
// DiscardOldFileChanges discards changes to a file from an old commit
518-
func (self *RebaseCommands) DiscardOldFileChanges(commits []*models.Commit, commitIndex int, filePaths []string) error {
519-
if err := self.BeginInteractiveRebaseForCommit(commits, commitIndex, false); err != nil {
513+
func (self *RebaseCommands) DiscardOldFileChanges(commits []*models.Commit, commitIndex int, parentIdx int, filePaths []string) error {
514+
if err := self.BeginInteractiveRebaseForCommit(commits, commitIndex, parentIdx, false); err != nil {
520515
return err
521516
}
522517

pkg/commands/git_commands/rebase_test.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ func TestRebaseDiscardOldFileChanges(t *testing.T) {
100100
gitConfigMockResponses map[string]string
101101
commitOpts []models.NewCommitOpts
102102
commitIndex int
103+
parentIndex int
103104
fileName []string
104105
runner *oscommands.FakeCmdObjRunner
105106
test func(error)
@@ -111,6 +112,7 @@ func TestRebaseDiscardOldFileChanges(t *testing.T) {
111112
gitConfigMockResponses: nil,
112113
commitOpts: []models.NewCommitOpts{},
113114
commitIndex: 0,
115+
parentIndex: 0,
114116
fileName: []string{"test999.txt"},
115117
runner: oscommands.NewFakeRunner(t),
116118
test: func(err error) {
@@ -122,6 +124,7 @@ func TestRebaseDiscardOldFileChanges(t *testing.T) {
122124
gitConfigMockResponses: map[string]string{"commit.gpgSign": "true"},
123125
commitOpts: []models.NewCommitOpts{{Name: "commit", Hash: "123456"}},
124126
commitIndex: 0,
127+
parentIndex: 0,
125128
fileName: []string{"test999.txt"},
126129
runner: oscommands.NewFakeRunner(t),
127130
test: func(err error) {
@@ -136,6 +139,7 @@ func TestRebaseDiscardOldFileChanges(t *testing.T) {
136139
{Name: "commit2", Hash: "abcdef"},
137140
},
138141
commitIndex: 0,
142+
parentIndex: 1,
139143
fileName: []string{"test999.txt"},
140144
runner: oscommands.NewFakeRunner(t).
141145
ExpectGitArgs([]string{"rebase", "--interactive", "--autostash", "--keep-empty", "--no-autosquash", "--rebase-merges", "abcdef"}, "", nil).
@@ -163,7 +167,7 @@ func TestRebaseDiscardOldFileChanges(t *testing.T) {
163167
commits := lo.Map(s.commitOpts,
164168
func(opts models.NewCommitOpts, _ int) *models.Commit { return models.NewCommit(hashPool, opts) })
165169

166-
s.test(instance.DiscardOldFileChanges(commits, s.commitIndex, s.fileName))
170+
s.test(instance.DiscardOldFileChanges(commits, s.commitIndex, s.parentIndex, s.fileName))
167171
s.runner.CheckForMissingCalls()
168172
})
169173
}

pkg/gui/controllers/commits_files_controller.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,10 @@ func (self *CommitFilesController) discard(selectedNodes []*filetree.CommitFileN
323323
})
324324
}
325325

326-
err := self.c.Git().Rebase.DiscardOldFileChanges(self.c.Model().Commits, self.c.Contexts().LocalCommits.GetSelectedLineIdx(), filePaths)
326+
selectedCommits, _, endIdx := self.c.Contexts().LocalCommits.GetSelectedItems()
327+
_, parentIdx := self.c.Helpers().Commits.GetParentCommit(selectedCommits, endIdx, 1)
328+
329+
err := self.c.Git().Rebase.DiscardOldFileChanges(self.c.Model().Commits, self.c.Contexts().LocalCommits.GetSelectedLineIdx(), parentIdx, filePaths)
327330
if err := self.c.Helpers().MergeAndRebase.CheckMergeOrRebase(err); err != nil {
328331
return err
329332
}

pkg/gui/controllers/custom_patch_options_menu_action.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,10 @@ func (self *CustomPatchOptionsMenuAction) handleDeletePatchFromCommit() error {
145145

146146
return self.c.WithWaitingStatus(self.c.Tr.RebasingStatus, func(gocui.Task) error {
147147
commitIndex := self.getPatchCommitIndex()
148+
selectedCommits, _, endIdx := self.c.Contexts().LocalCommits.GetSelectedItems()
149+
_, parentIdx := self.c.Helpers().Commits.GetParentCommit(selectedCommits, endIdx, 1)
148150
self.c.LogAction(self.c.Tr.Actions.RemovePatchFromCommit)
149-
err := self.c.Git().Patch.DeletePatchesFromCommit(self.c.Model().Commits, commitIndex)
151+
err := self.c.Git().Patch.DeletePatchesFromCommit(self.c.Model().Commits, commitIndex, parentIdx)
150152
return self.c.Helpers().MergeAndRebase.CheckMergeOrRebase(err)
151153
})
152154
}
@@ -160,8 +162,10 @@ func (self *CustomPatchOptionsMenuAction) handleMovePatchToSelectedCommit() erro
160162

161163
return self.c.WithWaitingStatus(self.c.Tr.RebasingStatus, func(gocui.Task) error {
162164
commitIndex := self.getPatchCommitIndex()
165+
selectedCommits, _, endIdx := self.c.Contexts().LocalCommits.GetSelectedItems()
166+
_, parentIdx := self.c.Helpers().Commits.GetParentCommit(selectedCommits, endIdx, 1)
163167
self.c.LogAction(self.c.Tr.Actions.MovePatchToSelectedCommit)
164-
err := self.c.Git().Patch.MovePatchToSelectedCommit(self.c.Model().Commits, commitIndex, self.c.Contexts().LocalCommits.GetSelectedLineIdx())
168+
err := self.c.Git().Patch.MovePatchToSelectedCommit(self.c.Model().Commits, commitIndex, self.c.Contexts().LocalCommits.GetSelectedLineIdx(), parentIdx)
165169
return self.c.Helpers().MergeAndRebase.CheckMergeOrRebase(err)
166170
})
167171
}
@@ -180,8 +184,10 @@ func (self *CustomPatchOptionsMenuAction) handleMovePatchIntoWorkingTree() error
180184
HandleConfirm: func() error {
181185
return self.c.WithWaitingStatus(self.c.Tr.RebasingStatus, func(gocui.Task) error {
182186
commitIndex := self.getPatchCommitIndex()
187+
selectedCommits, _, endIdx := self.c.Contexts().LocalCommits.GetSelectedItems()
188+
_, parentIdx := self.c.Helpers().Commits.GetParentCommit(selectedCommits, endIdx, 1)
183189
self.c.LogAction(self.c.Tr.Actions.MovePatchIntoIndex)
184-
err := self.c.Git().Patch.MovePatchIntoIndex(self.c.Model().Commits, commitIndex, mustStash)
190+
err := self.c.Git().Patch.MovePatchIntoIndex(self.c.Model().Commits, commitIndex, parentIdx, mustStash)
185191
return self.c.Helpers().MergeAndRebase.CheckMergeOrRebase(err)
186192
})
187193
},
@@ -196,6 +202,8 @@ func (self *CustomPatchOptionsMenuAction) handlePullPatchIntoNewCommit() error {
196202
self.returnFocusFromPatchExplorerIfNecessary()
197203

198204
commitIndex := self.getPatchCommitIndex()
205+
selectedCommits, _, endIdx := self.c.Contexts().LocalCommits.GetSelectedItems()
206+
_, parentIdx := self.c.Helpers().Commits.GetParentCommit(selectedCommits, endIdx, 1)
199207
self.c.Helpers().Commits.OpenCommitMessagePanel(
200208
&helpers.OpenCommitMessagePanelOpts{
201209
// Pass a commit index of one less than the moved-from commit, so that
@@ -209,7 +217,7 @@ func (self *CustomPatchOptionsMenuAction) handlePullPatchIntoNewCommit() error {
209217
return self.c.WithWaitingStatus(self.c.Tr.RebasingStatus, func(gocui.Task) error {
210218
self.c.Helpers().Commits.CloseCommitMessagePanel()
211219
self.c.LogAction(self.c.Tr.Actions.MovePatchIntoNewCommit)
212-
err := self.c.Git().Patch.PullPatchIntoNewCommit(self.c.Model().Commits, commitIndex, summary, description)
220+
err := self.c.Git().Patch.PullPatchIntoNewCommit(self.c.Model().Commits, commitIndex, parentIdx, summary, description)
213221
if err := self.c.Helpers().MergeAndRebase.CheckMergeOrRebase(err); err != nil {
214222
return err
215223
}
@@ -231,6 +239,8 @@ func (self *CustomPatchOptionsMenuAction) handlePullPatchIntoNewCommitBefore() e
231239
self.returnFocusFromPatchExplorerIfNecessary()
232240

233241
commitIndex := self.getPatchCommitIndex()
242+
selectedCommits, _, endIdx := self.c.Contexts().LocalCommits.GetSelectedItems()
243+
_, parentIdx := self.c.Helpers().Commits.GetParentCommit(selectedCommits, endIdx, 2)
234244
self.c.Helpers().Commits.OpenCommitMessagePanel(
235245
&helpers.OpenCommitMessagePanelOpts{
236246
// Pass a commit index of one less than the moved-from commit, so that
@@ -244,7 +254,7 @@ func (self *CustomPatchOptionsMenuAction) handlePullPatchIntoNewCommitBefore() e
244254
return self.c.WithWaitingStatus(self.c.Tr.RebasingStatus, func(gocui.Task) error {
245255
self.c.Helpers().Commits.CloseCommitMessagePanel()
246256
self.c.LogAction(self.c.Tr.Actions.MovePatchIntoNewCommit)
247-
err := self.c.Git().Patch.PullPatchIntoNewCommitBefore(self.c.Model().Commits, commitIndex, summary, description)
257+
err := self.c.Git().Patch.PullPatchIntoNewCommitBefore(self.c.Model().Commits, commitIndex, parentIdx, summary, description)
248258
if err := self.c.Helpers().MergeAndRebase.CheckMergeOrRebase(err); err != nil {
249259
return err
250260
}

pkg/gui/controllers/helpers/commits_helper.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
"github.com/jesseduffield/gocui"
1010
"github.com/jesseduffield/lazygit/pkg/commands/git_commands"
11+
"github.com/jesseduffield/lazygit/pkg/commands/models"
1112
"github.com/jesseduffield/lazygit/pkg/gui/types"
1213
"github.com/samber/lo"
1314
)
@@ -232,6 +233,39 @@ func (self *CommitsHelper) OpenCommitMenu(suggestionFunc func(string) []*types.S
232233
})
233234
}
234235

236+
func (self *CommitsHelper) GetParentCommit(selectedCommits []*models.Commit, endIdx int, depth int) (*models.Commit, int) {
237+
commits := self.c.Model().Commits
238+
239+
currentCommit := selectedCommits[len(selectedCommits)-1]
240+
parentIndex := endIdx
241+
var parentCommit *models.Commit
242+
243+
for d := 0; d < depth; d++ {
244+
selectedParents := currentCommit.Parents()
245+
if len(selectedParents) == 0 {
246+
self.c.Log.Warn("Selected commit has no parents, the nearest commit is used")
247+
return commits[parentIndex], parentIndex + 1
248+
}
249+
parentHash := selectedParents[0]
250+
found := false
251+
for i, commit := range commits {
252+
if commit.Hash() == parentHash {
253+
parentIndex = i
254+
parentCommit = commits[parentIndex]
255+
currentCommit = parentCommit
256+
found = true
257+
break
258+
}
259+
}
260+
if !found {
261+
self.c.Log.Warnf("Parent commit %s not found in visible commit list, the nearest commit is used ", parentHash)
262+
return commits[parentIndex], parentIndex + 1
263+
}
264+
}
265+
266+
return parentCommit, parentIndex
267+
}
268+
235269
func (self *CommitsHelper) addCoAuthor(suggestionFunc func(string) []*types.Suggestion) error {
236270
self.c.Prompt(types.PromptOpts{
237271
Title: self.c.Tr.AddCoAuthorPromptTitle,

0 commit comments

Comments
 (0)