Skip to content

Commit dd53ed2

Browse files
committed
fixup! Fix index out of bounds panic when repository has massive tag lists
1 parent 6f04ee2 commit dd53ed2

File tree

1 file changed

+108
-18
lines changed

1 file changed

+108
-18
lines changed

pkg/commands/git_commands/commit_loader_test.go

Lines changed: 108 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -601,101 +601,191 @@ func TestCommitLoader_extractCommitFromLine(t *testing.T) {
601601
testName string
602602
line string
603603
showDivergence bool
604-
expectedNil bool
604+
expectedCommit *models.Commit
605605
}{
606606
{
607607
testName: "normal commit line with all fields",
608608
line: "0eea75e8c631fba6b58135697835d58ba4c18dbc\x001640826609\x00Jesse Duffield\x00[email protected]\x00b21997d6b4cbdf84b149\x00>\x00HEAD -> better-tests\x00better typing for rebase mode",
609-
showDivergence: true,
610-
expectedNil: false,
609+
showDivergence: false,
610+
expectedCommit: models.NewCommit(hashPool, models.NewCommitOpts{
611+
Hash: "0eea75e8c631fba6b58135697835d58ba4c18dbc",
612+
Name: "better typing for rebase mode",
613+
Tags: nil,
614+
ExtraInfo: "(HEAD -> better-tests)",
615+
UnixTimestamp: 1640826609,
616+
AuthorName: "Jesse Duffield",
617+
AuthorEmail: "[email protected]",
618+
Parents: []string{"b21997d6b4cbdf84b149"},
619+
Divergence: models.DivergenceNone,
620+
}),
611621
},
612622
{
613623
testName: "normal commit line with left divergence",
614624
line: "hash123\x001234567890\x00John Doe\x00[email protected]\x00parent1 parent2\x00<\x00origin/main\x00commit message",
615625
showDivergence: true,
616-
expectedNil: false,
626+
expectedCommit: models.NewCommit(hashPool, models.NewCommitOpts{
627+
Hash: "hash123",
628+
Name: "commit message",
629+
Tags: nil,
630+
ExtraInfo: "(origin/main)",
631+
UnixTimestamp: 1234567890,
632+
AuthorName: "John Doe",
633+
AuthorEmail: "[email protected]",
634+
Parents: []string{"parent1", "parent2"},
635+
Divergence: models.DivergenceLeft,
636+
}),
617637
},
618638
{
619639
testName: "commit line with tags in extraInfo",
620640
line: "abc123\x001640000000\x00Jane Smith\x00[email protected]\x00parenthash\x00>\x00tag: v1.0, tag: release\x00tagged release",
621641
showDivergence: true,
622-
expectedNil: false,
642+
expectedCommit: models.NewCommit(hashPool, models.NewCommitOpts{
643+
Hash: "abc123",
644+
Name: "tagged release",
645+
Tags: []string{"v1.0", "release"},
646+
ExtraInfo: "(tag: v1.0, tag: release)",
647+
UnixTimestamp: 1640000000,
648+
AuthorName: "Jane Smith",
649+
AuthorEmail: "[email protected]",
650+
Parents: []string{"parenthash"},
651+
Divergence: models.DivergenceRight,
652+
}),
623653
},
624654
{
625655
testName: "commit line with empty extraInfo",
626656
line: "def456\x001640000000\x00Bob Wilson\x00[email protected]\x00parenthash\x00>\x00\x00simple commit",
627657
showDivergence: true,
628-
expectedNil: false,
658+
expectedCommit: models.NewCommit(hashPool, models.NewCommitOpts{
659+
Hash: "def456",
660+
Name: "simple commit",
661+
Tags: nil,
662+
ExtraInfo: "",
663+
UnixTimestamp: 1640000000,
664+
AuthorName: "Bob Wilson",
665+
AuthorEmail: "[email protected]",
666+
Parents: []string{"parenthash"},
667+
Divergence: models.DivergenceRight,
668+
}),
629669
},
630670
{
631671
testName: "commit line with no parents (root commit)",
632672
line: "root123\x001640000000\x00Alice Cooper\x00[email protected]\x00\x00>\x00\x00initial commit",
633673
showDivergence: true,
634-
expectedNil: false,
674+
expectedCommit: models.NewCommit(hashPool, models.NewCommitOpts{
675+
Hash: "root123",
676+
Name: "initial commit",
677+
Tags: nil,
678+
ExtraInfo: "",
679+
UnixTimestamp: 1640000000,
680+
AuthorName: "Alice Cooper",
681+
AuthorEmail: "[email protected]",
682+
Parents: nil,
683+
Divergence: models.DivergenceRight,
684+
}),
635685
},
636686
{
637687
testName: "malformed line with only 3 fields",
638688
line: "hash\x00timestamp\x00author",
639689
showDivergence: false,
640-
expectedNil: true,
690+
expectedCommit: nil,
641691
},
642692
{
643693
testName: "malformed line with only 4 fields",
644694
line: "hash\x00timestamp\x00author\x00email",
645695
showDivergence: false,
646-
expectedNil: true,
696+
expectedCommit: nil,
647697
},
648698
{
649699
testName: "malformed line with only 5 fields",
650700
line: "hash\x00timestamp\x00author\x00email\x00parents",
651701
showDivergence: false,
652-
expectedNil: true,
702+
expectedCommit: nil,
653703
},
654704
{
655705
testName: "malformed line with only 6 fields",
656706
line: "hash\x00timestamp\x00author\x00email\x00parents\x00<",
657707
showDivergence: true,
658-
expectedNil: true,
708+
expectedCommit: nil,
659709
},
660710
{
661711
testName: "minimal valid line with 7 fields (no message)",
662712
line: "hash\x00timestamp\x00author\x00email\x00parents\x00>\x00extraInfo",
663713
showDivergence: true,
664-
expectedNil: false,
714+
expectedCommit: models.NewCommit(hashPool, models.NewCommitOpts{
715+
Hash: "hash",
716+
Name: "",
717+
Tags: nil,
718+
ExtraInfo: "(extraInfo)",
719+
UnixTimestamp: 0,
720+
AuthorName: "author",
721+
AuthorEmail: "email",
722+
Parents: []string{"parents"},
723+
Divergence: models.DivergenceRight,
724+
}),
665725
},
666726
{
667727
testName: "minimal valid line with 7 fields (empty extraInfo)",
668728
line: "hash\x00timestamp\x00author\x00email\x00parents\x00>\x00",
669729
showDivergence: true,
670-
expectedNil: false,
730+
expectedCommit: models.NewCommit(hashPool, models.NewCommitOpts{
731+
Hash: "hash",
732+
Name: "",
733+
Tags: nil,
734+
ExtraInfo: "",
735+
UnixTimestamp: 0,
736+
AuthorName: "author",
737+
AuthorEmail: "email",
738+
Parents: []string{"parents"},
739+
Divergence: models.DivergenceRight,
740+
}),
671741
},
672742
{
673743
testName: "valid line with 8 fields (complete)",
674744
line: "hash\x00timestamp\x00author\x00email\x00parents\x00<\x00extraInfo\x00message",
675745
showDivergence: true,
676-
expectedNil: false,
746+
expectedCommit: models.NewCommit(hashPool, models.NewCommitOpts{
747+
Hash: "hash",
748+
Name: "message",
749+
Tags: nil,
750+
ExtraInfo: "(extraInfo)",
751+
UnixTimestamp: 0,
752+
AuthorName: "author",
753+
AuthorEmail: "email",
754+
Parents: []string{"parents"},
755+
Divergence: models.DivergenceLeft,
756+
}),
677757
},
678758
{
679759
testName: "empty line",
680760
line: "",
681761
showDivergence: false,
682-
expectedNil: true,
762+
expectedCommit: nil,
683763
},
684764
{
685765
testName: "line with special characters in commit message",
686766
line: "special123\x001640000000\x00Dev User\x00[email protected]\x00parenthash\x00>\x00\x00fix: handle \x00 null bytes and 'quotes'",
687767
showDivergence: true,
688-
expectedNil: false,
768+
expectedCommit: models.NewCommit(hashPool, models.NewCommitOpts{
769+
Hash: "special123",
770+
Name: "fix: handle \x00 null bytes and 'quotes'",
771+
Tags: nil,
772+
ExtraInfo: "",
773+
UnixTimestamp: 1640000000,
774+
AuthorName: "Dev User",
775+
AuthorEmail: "[email protected]",
776+
Parents: []string{"parenthash"},
777+
Divergence: models.DivergenceRight,
778+
}),
689779
},
690780
}
691781

692782
for _, scenario := range scenarios {
693783
t.Run(scenario.testName, func(t *testing.T) {
694784
result := loader.extractCommitFromLine(hashPool, scenario.line, scenario.showDivergence)
695-
if scenario.expectedNil {
785+
if scenario.expectedCommit == nil {
696786
assert.Nil(t, result)
697787
} else {
698-
assert.NotNil(t, result)
788+
assert.Equal(t, scenario.expectedCommit, result)
699789
}
700790
})
701791
}

0 commit comments

Comments
 (0)