Skip to content

Commit 446fdd2

Browse files
authored
Merge pull request #14 from anIcedAntFA/fix/commit-messsage-repeated-when-log-all-branches
🐛 fix(parser): handle deduplicate commit messsage
2 parents 1a19135 + d693dc9 commit 446fdd2

File tree

3 files changed

+22
-8
lines changed

3 files changed

+22
-8
lines changed

cmd/gohome/cmd/report.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -201,10 +201,7 @@ func processCommits(repos []string, author, period string, cfg *viperconfig.Conf
201201
continue
202202
}
203203

204-
commits := make([]entity.Commit, 0, len(rawLogs))
205-
for _, line := range rawLogs {
206-
commits = append(commits, parserSvc.Parse(line))
207-
}
204+
commits := parserSvc.Parse(rawLogs)
208205

209206
if len(commits) > 0 {
210207
foundAny = true

internal/parser/parser.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func NewService() *Service {
2020
}
2121

2222
// Parse converts a raw log line into a Commit entity.
23-
func (s *Service) Parse(rawLine string) entity.Commit {
23+
func (s *Service) parseSingleLine(rawLine string) entity.Commit {
2424
emoji := s.extractEmoji(rawLine)
2525
if emoji == "" {
2626
emoji = "-"
@@ -49,6 +49,23 @@ func (s *Service) Parse(rawLine string) entity.Commit {
4949
return commit
5050
}
5151

52+
// Parse parses a slice of raw log lines and returns parsed commits with duplicate
53+
// commit messages removed (keeps the first occurrence).
54+
func (s *Service) Parse(rawLogs []string) []entity.Commit {
55+
seen := make(map[string]bool)
56+
result := make([]entity.Commit, 0, len(rawLogs))
57+
58+
for _, raw := range rawLogs {
59+
if !seen[raw] {
60+
seen[raw] = true
61+
c := s.parseSingleLine(raw)
62+
result = append(result, c)
63+
}
64+
}
65+
66+
return result
67+
}
68+
5269
func (s *Service) extractEmoji(input string) string {
5370
var emoji strings.Builder
5471

internal/parser/parser_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ func TestParse(t *testing.T) {
297297
for _, tt := range tests {
298298
t.Run(tt.name, func(t *testing.T) {
299299
service := NewService()
300-
got := service.Parse(tt.input)
300+
got := service.parseSingleLine(tt.input)
301301

302302
if got.Type != tt.wantType {
303303
t.Errorf("Parse(%q).Type = %q, want %q", tt.input, got.Type, tt.wantType)
@@ -454,7 +454,7 @@ func TestParseRawField(t *testing.T) {
454454

455455
for _, input := range testInputs {
456456
t.Run(input, func(t *testing.T) {
457-
got := service.Parse(input)
457+
got := service.parseSingleLine(input)
458458
if got.Raw != input {
459459
t.Errorf("Parse(%q).Raw = %q, want %q", input, got.Raw, input)
460460
}
@@ -501,7 +501,7 @@ func TestParseDefaultValues(t *testing.T) {
501501
for _, tt := range tests {
502502
t.Run(tt.name, func(t *testing.T) {
503503
service := NewService()
504-
got := service.Parse(tt.input)
504+
got := service.parseSingleLine(tt.input)
505505
tt.checkFunc(t, got)
506506
})
507507
}

0 commit comments

Comments
 (0)