Skip to content

Commit

Permalink
feat(gitparse): track commit refs
Browse files Browse the repository at this point in the history
  • Loading branch information
Richard Gomez committed Apr 12, 2024
1 parent e383c34 commit 92c0f83
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 7 deletions.
38 changes: 31 additions & 7 deletions pkg/gitparse/gitparse.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ func (d *Diff) finalize() error {

// Commit contains commit header info and diffs.
type Commit struct {
Refs []string
Hash string
Author string
Date time.Time
Expand Down Expand Up @@ -209,7 +210,7 @@ func NewParser(options ...Option) *Parser {
// RepoPath parses the output of the `git log` command for the `source` path.
// The Diff chan will return diffs in the order they are parsed from the log.
func (c *Parser) RepoPath(ctx context.Context, source string, head string, abbreviatedLog bool, excludedGlobs []string, isBare bool) (chan *Diff, error) {
args := []string{"-C", source, "log", "-p", "--full-history", "--date=format:%a %b %d %H:%M:%S %Y %z"}
args := []string{"-C", source, "log", "-p", "--full-history", "--date=format:%a %b %d %H:%M:%S %Y %z", "--decorate"}
if abbreviatedLog {
args = append(args, "--diff-filter=AM")
}
Expand Down Expand Up @@ -302,10 +303,10 @@ func (c *Parser) FromReader(ctx context.Context, stdOut io.Reader, diffChan chan
outReader := bufio.NewReader(stdOut)
var (
currentCommit *Commit

totalLogSize int
totalLogSize int
latestState = Initial
latestRefs [][]byte
)
var latestState = Initial

diff := func(c *Commit, opts ...diffOption) *Diff {
opts = append(opts, withCustomContentWriter(bufferwriter.New(ctx)))
Expand Down Expand Up @@ -365,9 +366,14 @@ func (c *Parser) FromReader(ctx context.Context, stdOut io.Reader, diffChan chan
// Create a new currentDiff and currentCommit
currentCommit = &Commit{Message: strings.Builder{}}
currentDiff = diff(currentCommit)
// Check that the commit line contains a hash and set it.
if len(line) >= 47 {
currentCommit.Hash = string(line[7:47])

hash, refs := parseCommitLine(line)
if hash != nil {
currentCommit.Hash = string(hash)
}
if refs != nil {
latestRefs = refs
ctx.Logger().Info("Found latest branches", "refs", latestRefs)
}
case isMergeLine(isStaged, latestState, line):
latestState = MergeLine
Expand Down Expand Up @@ -566,6 +572,24 @@ func isCommitLine(isStaged bool, latestState ParseState, line []byte) bool {
return false
}

func parseCommitLine(line []byte) (hash []byte, refs [][]byte) {
// Check that the commit line contains a 40-character hash and set it.
// `commit e5575cd6f2d21d3a1a604287c7bf4a7eab2266e0\n`
if len(line) >= 47 {
hash = line[7:47]
}

// Check if the commit line includes branch references.
// `commit e383c347cc3bc4e24d3003da7cdfaae3a08dc19c (HEAD -> feat/branch)\n`
// `commit ff5d9a413f4a7fe9bef8a5acf792e0a3a4f3d04c (origin/main, origin/HEAD, main)\n`
if len(line) > 48 && bytes.HasSuffix(line, []byte("\n")) {
// Skip the `(` at the start and the `)\n` at the end.
refs = bytes.Split(line[49:len(line)-2], []byte(","))
}

return
}

// Author: Bill Rich <[email protected]>
func isAuthorLine(isStaged bool, latestState ParseState, line []byte) bool {
if isStaged || !(latestState == CommitLine || latestState == MergeLine) {
Expand Down
14 changes: 14 additions & 0 deletions pkg/gitparse/gitparse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,20 @@ func TestLineChecksNoStaged(t *testing.T) {
}
}

func TestCommitBranchParse(t *testing.T) {
//cases := map[string][][]byte{}

//for line, expected := range cases {
//hash, branches := parseCommitLine([]byte(line))
//if hash == nil {
// t.Errorf("Failed to get path: %s", line)
//}
//if filename != expected {
// t.Errorf("Expected: %s, Got: %s", expected, filename)
//}
//}
}

func TestBinaryPathParse(t *testing.T) {
cases := map[string]string{
"Binary files a/trufflehog_3.42.0_linux_arm64.tar.gz and /dev/null differ\n": "",
Expand Down

0 comments on commit 92c0f83

Please sign in to comment.