Turns an iterator over lines from git log into an iterator of commits
(lines iter.Seq[string])
| 56 | |
| 57 | // Turns an iterator over lines from git log into an iterator of commits |
| 58 | func ParseCommits(lines iter.Seq[string]) (iter.Seq[Commit], func() error) { |
| 59 | var iterErr error |
| 60 | |
| 61 | seq := func(yield func(Commit) bool) { |
| 62 | var commit Commit |
| 63 | var diff *FileDiff |
| 64 | now := time.Now() |
| 65 | linesThisCommit := 0 |
| 66 | |
| 67 | for line := range lines { |
| 68 | done := linesThisCommit >= 6 && (len(line) == 0 || rev.IsFullHash(line)) |
| 69 | if done { |
| 70 | if allowCommit(commit, now) { |
| 71 | if !yield(commit) { |
| 72 | return |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | commit = Commit{} |
| 77 | diff = nil |
| 78 | linesThisCommit = 0 |
| 79 | |
| 80 | if len(line) == 0 { |
| 81 | continue |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | switch { |
| 86 | case linesThisCommit == 0: |
| 87 | commit.Hash = line |
| 88 | case linesThisCommit == 1: |
| 89 | commit.ShortHash = line |
| 90 | case linesThisCommit == 2: |
| 91 | parts := strings.Split(line, " ") |
| 92 | commit.IsMerge = len(parts) > 1 |
| 93 | case linesThisCommit == 3: |
| 94 | commit.AuthorName = line |
| 95 | case linesThisCommit == 4: |
| 96 | commit.AuthorEmail = line |
| 97 | case linesThisCommit == 5: |
| 98 | i, err := strconv.Atoi(line) |
| 99 | if err != nil { |
| 100 | iterErr = fmt.Errorf( |
| 101 | "error parsing date from commit %s: %w", |
| 102 | commit.Name(), |
| 103 | err, |
| 104 | ) |
| 105 | return |
| 106 | } |
| 107 | |
| 108 | commit.Date = time.Unix(int64(i), 0) |
| 109 | default: |
| 110 | var err error |
| 111 | |
| 112 | // Handle file diffs |
| 113 | if diff == nil { |
| 114 | diff = &FileDiff{} |
| 115 |