Skip to content

Commit 8483239

Browse files
authored
Fix index out of bounds panic when repository has massive tags (#4776)
- **PR Description** Fixes an index out of bounds panic that occurs when lazygit starts up in repositories with a large number of tags on a single commit. When a commit has thousands of tags, the git log output can become malformed or truncated, causing the `extractCommitFromLine` function to receive fewer than the expected 8 null-separated fields. This results in an index out of bounds panic when trying to access `split[5]` and beyond. Closes #4765
2 parents 26f2c02 + 1c67093 commit 8483239

File tree

2 files changed

+244
-31
lines changed

2 files changed

+244
-31
lines changed

pkg/commands/git_commands/commit_loader.go

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -202,19 +202,30 @@ 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 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)
208+
return nil
209+
}
210+
205211
hash := split[0]
206212
unixTimestamp := split[1]
207213
authorName := split[2]
208214
authorEmail := split[3]
209-
extraInfo := strings.TrimSpace(split[4])
210-
parentHashes := split[5]
215+
parentHashes := split[4]
211216
divergence := models.DivergenceNone
212217
if showDivergence {
213-
divergence = lo.Ternary(split[6] == "<", models.DivergenceLeft, models.DivergenceRight)
218+
divergence = lo.Ternary(split[5] == "<", models.DivergenceLeft, models.DivergenceRight)
219+
}
220+
extraInfo := strings.TrimSpace(split[6])
221+
222+
// message (and the \x00 before it) might not be present if extraInfo is extremely long
223+
message := ""
224+
if len(split) > 7 {
225+
message = split[7]
214226
}
215-
message := split[7]
216227

217-
tags := []string{}
228+
var tags []string
218229

219230
if extraInfo != "" {
220231
extraInfoFields := strings.Split(extraInfo, ",")
@@ -614,4 +625,4 @@ func (self *CommitLoader) getLogCmd(opts GetCommitsOptions) *oscommands.CmdObj {
614625
return self.cmd.New(cmdArgs).DontLog()
615626
}
616627

617-
const prettyFormat = `--pretty=format:+%H%x00%at%x00%aN%x00%ae%x00%D%x00%P%x00%m%x00%s`
628+
const prettyFormat = `--pretty=format:+%H%x00%at%x00%aN%x00%ae%x00%P%x00%m%x00%D%x00%s`

0 commit comments

Comments
 (0)