Add HeadCommitIdx helper function

Not used yet, we'll need it in the next commit.
This commit is contained in:
Stefan Haller 2026-06-22 09:15:32 +02:00
parent d673a0f3b9
commit cfb46f440c
2 changed files with 62 additions and 0 deletions

View file

@ -160,3 +160,13 @@ func (c *Commit) IsTODO() bool {
func IsHeadCommit(commits []*Commit, index int) bool {
return !commits[index].IsTODO() && (index == 0 || commits[index-1].IsTODO())
}
func HeadCommitIdx(commits []*Commit) int {
for index, commit := range commits {
if !commit.IsTODO() {
return index
}
}
return -1
}

View file

@ -8,6 +8,49 @@ import (
"github.com/stretchr/testify/assert"
)
func TestHeadCommitIdx(t *testing.T) {
testCases := []struct {
name string
commits []*Commit
expected int
}{
{
name: "first commit without rebase todos",
commits: makeTestCommits("a", "b"),
expected: 0,
},
{
name: "first non-todo commit during an interactive rebase",
commits: []*Commit{
makeTestTodoCommit(todo.Pick),
makeTestTodoCommit(todo.Reword),
makeTestCommit("a"),
makeTestCommit("b"),
},
expected: 2,
},
{
name: "no commits",
commits: nil,
expected: -1,
},
{
name: "only rebase todos",
commits: []*Commit{
makeTestTodoCommit(todo.Pick),
makeTestTodoCommit(todo.Reword),
},
expected: -1,
},
}
for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
assert.Equal(t, testCase.expected, HeadCommitIdx(testCase.commits))
})
}
}
func TestIsHeadCommit(t *testing.T) {
commits := []*Commit{
makeTestTodoCommit(todo.Pick),
@ -20,6 +63,15 @@ func TestIsHeadCommit(t *testing.T) {
assert.False(t, IsHeadCommit(commits, 2))
}
func makeTestCommits(hashes ...string) []*Commit {
commits := make([]*Commit, 0, len(hashes))
for _, hash := range hashes {
commits = append(commits, makeTestCommit(hash))
}
return commits
}
func makeTestCommit(hash string) *Commit {
return NewCommit(&utils.StringPool{}, NewCommitOpts{Hash: hash})
}