-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: use FSM for git output parsing
- Loading branch information
1 parent
0fbd10b
commit b30c543
Showing
5 changed files
with
139 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package git | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
"strings" | ||
"time" | ||
|
||
"golang.org/x/exp/slog" | ||
) | ||
|
||
type GitOutput string | ||
|
||
func (gt GitOutput) Commits() ([]Commit, error) { | ||
output := strings.TrimSpace(string(gt)) | ||
commits := make([]Commit, 0) | ||
for _, line := range strings.Split(output, "\n") { | ||
message, err := extractCommitMessage(line) | ||
if err != nil { | ||
slog.Error("failed to extract commit message", "gitlogLine", line) | ||
return []Commit{}, fmt.Errorf("failed to extract commit message. %w", err) | ||
} | ||
|
||
commitTime, err := extractTime(line) | ||
if err != nil { | ||
slog.Error("failed to extract timestamp", "gitlogLine", line) | ||
return []Commit{}, fmt.Errorf("failed to extract timestamp. %w", err) | ||
} | ||
|
||
sha, err := extractSha(line) | ||
if err != nil { | ||
slog.Error("failed to extract sha", "gitlogLine", line) | ||
return []Commit{}, fmt.Errorf("failed to extract sha. %w", err) | ||
} | ||
|
||
commits = append(commits, Commit{ | ||
Message: message, | ||
Time: commitTime, | ||
Sha: sha, | ||
}) | ||
} | ||
return commits, nil | ||
} | ||
|
||
func extractCommitMessage(gitlogLine string) (string, error) { | ||
gitlogLine = strings.TrimSpace(gitlogLine) | ||
result := gitoutputPattern.FindStringSubmatch(gitlogLine) | ||
if len(result) < 4 { | ||
return "", fmt.Errorf("couldn't find commit message in git log. %v", gitlogLine) | ||
} | ||
|
||
return result[3], nil | ||
} | ||
|
||
func extractTime(gitlogLine string) (time.Time, error) { | ||
result := gitoutputPattern.FindStringSubmatch(gitlogLine) | ||
if len(result) < 2 { | ||
return time.Time{}, fmt.Errorf("couldn't find timestamp in commit message. %v", gitlogLine) | ||
} | ||
|
||
timestamp, err := strconv.Atoi(result[1]) | ||
if err != nil { | ||
return time.Time{}, fmt.Errorf("failed to extract timestamp in commit message. %w", err) | ||
} | ||
return time.Unix(int64(timestamp), 0), nil | ||
} | ||
|
||
func extractSha(gitlogLine string) (string, error) { | ||
result := gitoutputPattern.FindStringSubmatch(gitlogLine) | ||
if len(result) < 3 { | ||
return "", fmt.Errorf("couldn't find sha in commit message. %v", gitlogLine) | ||
} | ||
|
||
return result[2], nil | ||
} |