Skip to content

Commit 911c6a1

Browse files
committed
Require minimum 7 fields and access fields unconditionally
Only the message field remains optionally accessed since it's the only field that can legitimately be missing from git log output.
1 parent 77ad441 commit 911c6a1

File tree

2 files changed

+15
-25
lines changed

2 files changed

+15
-25
lines changed

pkg/commands/git_commands/commit_loader.go

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -202,32 +202,22 @@ func (self *CommitLoader) MergeRebasingCommits(hashPool *utils.StringPool, commi
202202
func (self *CommitLoader) extractCommitFromLine(hashPool *utils.StringPool, line string, showDivergence bool) *models.Commit {
203203
split := strings.SplitN(line, "\x00", 8)
204204

205-
// Ensure we have the minimum required fields (at least 5 for basic functionality)
206-
if len(split) < 5 {
207-
self.Log.Warnf("Malformed git log line: expected at least 5 fields, got %d. Line: %s", len(split), line)
205+
// Ensure we have the minimum required fields (at least 7 for basic functionality)
206+
if len(split) < 7 {
207+
self.Log.Warnf("Malformed git log line: expected at least 7 fields, got %d. Line: %s", len(split), line)
208208
return nil
209209
}
210210

211211
hash := split[0]
212212
unixTimestamp := split[1]
213213
authorName := split[2]
214214
authorEmail := split[3]
215-
216-
// Safe access to optional fields with defaults
217-
parentHashes := ""
218-
if len(split) > 4 {
219-
parentHashes = split[4]
220-
}
221-
215+
parentHashes := split[4]
222216
divergence := models.DivergenceNone
223-
if showDivergence && len(split) > 5 {
217+
if showDivergence {
224218
divergence = lo.Ternary(split[5] == "<", models.DivergenceLeft, models.DivergenceRight)
225219
}
226-
227-
extraInfo := ""
228-
if len(split) > 6 {
229-
extraInfo = strings.TrimSpace(split[6])
230-
}
220+
extraInfo := strings.TrimSpace(split[6])
231221

232222
message := ""
233223
if len(split) > 7 {

pkg/commands/git_commands/commit_loader_test.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -646,32 +646,32 @@ func TestCommitLoader_extractCommitFromLine(t *testing.T) {
646646
expectedNil: true,
647647
},
648648
{
649-
testName: "minimal valid line with 5 fields",
649+
testName: "malformed line with only 5 fields",
650650
line: "hash\x00timestamp\x00author\x00email\x00parents",
651651
showDivergence: false,
652-
expectedNil: false,
652+
expectedNil: true,
653653
},
654654
{
655-
testName: "line with 6 fields including divergence",
655+
testName: "malformed line with only 6 fields",
656656
line: "hash\x00timestamp\x00author\x00email\x00parents\x00<",
657657
showDivergence: true,
658-
expectedNil: false,
658+
expectedNil: true,
659659
},
660660
{
661-
testName: "line with 7 fields including extraInfo but no message",
661+
testName: "minimal valid line with 7 fields (no message)",
662662
line: "hash\x00timestamp\x00author\x00email\x00parents\x00>\x00extraInfo",
663663
showDivergence: true,
664664
expectedNil: false,
665665
},
666666
{
667-
testName: "line missing divergence field when expected",
668-
line: "hash\x00timestamp\x00author\x00email\x00parents",
667+
testName: "minimal valid line with 7 fields (empty extraInfo)",
668+
line: "hash\x00timestamp\x00author\x00email\x00parents\x00>\x00",
669669
showDivergence: true,
670670
expectedNil: false,
671671
},
672672
{
673-
testName: "line missing extraInfo and message fields",
674-
line: "hash\x00timestamp\x00author\x00email\x00parents\x00<",
673+
testName: "valid line with 8 fields (complete)",
674+
line: "hash\x00timestamp\x00author\x00email\x00parents\x00<\x00extraInfo\x00message",
675675
showDivergence: true,
676676
expectedNil: false,
677677
},

0 commit comments

Comments
 (0)