@@ -106,11 +106,12 @@ func (d *Diff) finalize() error {
106
106
107
107
// Commit contains commit header info and diffs.
108
108
type Commit struct {
109
- Hash string
110
- Author string
111
- Date time.Time
112
- Message strings.Builder
113
- Size int // in bytes
109
+ SourceRef string
110
+ Hash string
111
+ Author string
112
+ Date time.Time
113
+ Message strings.Builder
114
+ Size int // in bytes
114
115
115
116
hasDiffs bool
116
117
}
@@ -209,13 +210,22 @@ func NewParser(options ...Option) *Parser {
209
210
// RepoPath parses the output of the `git log` command for the `source` path.
210
211
// The Diff chan will return diffs in the order they are parsed from the log.
211
212
func (c * Parser ) RepoPath (ctx context.Context , source string , head string , abbreviatedLog bool , excludedGlobs []string , isBare bool ) (chan * Diff , error ) {
212
- args := []string {"-C" , source , "log" , "-p" , "--full-history" , "--date=format:%a %b %d %H:%M:%S %Y %z" }
213
+ args := []string {
214
+ "-C" , source ,
215
+ "log" ,
216
+ "-p" , // https://git-scm.com/docs/git-log#Documentation/git-log.txt---patch
217
+ "--full-history" , // https://git-scm.com/docs/git-log#Documentation/git-log.txt---full-history
218
+ "--date=format:%a %b %d %H:%M:%S %Y %z" , // https://git-scm.com/docs/git-log#Documentation/git-log.txt---dateltformatgt
219
+ "--source" , // https://git-scm.com/docs/git-log#Documentation/git-log.txt---source
220
+ }
213
221
if abbreviatedLog {
222
+ // https://git-scm.com/docs/git-log#Documentation/git-log.txt---diff-filterACDMRTUXB82308203
214
223
args = append (args , "--diff-filter=AM" )
215
224
}
216
225
if head != "" {
217
226
args = append (args , head )
218
227
} else {
228
+ // https://git-scm.com/docs/git-log#Documentation/git-log.txt---all
219
229
args = append (args , "--all" )
220
230
}
221
231
for _ , glob := range excludedGlobs {
@@ -302,10 +312,9 @@ func (c *Parser) FromReader(ctx context.Context, stdOut io.Reader, diffChan chan
302
312
outReader := bufio .NewReader (stdOut )
303
313
var (
304
314
currentCommit * Commit
305
-
306
- totalLogSize int
315
+ totalLogSize int
316
+ latestState = Initial
307
317
)
308
- var latestState = Initial
309
318
310
319
diff := func (c * Commit , opts ... diffOption ) * Diff {
311
320
opts = append (opts , withCustomContentWriter (bufferwriter .New (ctx )))
@@ -365,10 +374,18 @@ func (c *Parser) FromReader(ctx context.Context, stdOut io.Reader, diffChan chan
365
374
// Create a new currentDiff and currentCommit
366
375
currentCommit = & Commit {Message : strings.Builder {}}
367
376
currentDiff = diff (currentCommit )
368
- // Check that the commit line contains a hash and set it.
369
- if len (line ) >= 47 {
370
- currentCommit .Hash = string (line [7 :47 ])
377
+
378
+ hash , ref := parseCommitLine (line )
379
+ if hash == nil || ref == nil {
380
+ ctx .Logger ().Error (
381
+ fmt .Errorf (`expected line to match 'commit <hash> <ref>', got "%s"` , line ),
382
+ "Failed to parse CommitLine" )
383
+ latestState = ParseFailure
384
+ continue
371
385
}
386
+
387
+ currentCommit .Hash = string (hash )
388
+ currentCommit .SourceRef = string (ref )
372
389
case isMergeLine (isStaged , latestState , line ):
373
390
latestState = MergeLine
374
391
case isAuthorLine (isStaged , latestState , line ):
@@ -566,6 +583,22 @@ func isCommitLine(isStaged bool, latestState ParseState, line []byte) bool {
566
583
return false
567
584
}
568
585
586
+ func parseCommitLine (line []byte ) (hash []byte , ref []byte ) {
587
+ // Check that the commit line contains a 40-character hash and set it.
588
+ // `commit e5575cd6f2d21d3a1a604287c7bf4a7eab2266e0\n`
589
+ if len (line ) >= 47 {
590
+ hash = line [7 :47 ]
591
+ }
592
+
593
+ // Check if the commit line includes branch references.
594
+ // `commit 2dbbb28727c7c2954438666dafba57bb8c714d3b refs/heads/fix/github-enterprise-gist\n`
595
+ if len (line ) > 48 {
596
+ ref = line [48 : len (line )- 1 ]
597
+ }
598
+
599
+ return
600
+ }
601
+
569
602
// Author: Bill Rich <[email protected] >
570
603
func isAuthorLine (isStaged bool , latestState ParseState , line []byte ) bool {
571
604
if isStaged || ! (latestState == CommitLine || latestState == MergeLine ) {
0 commit comments