Skip to content

Commit 261f56b

Browse files
committed
some formatting, recover from panics, add some TODOs
1 parent 0e24575 commit 261f56b

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

cmd/root.go

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ var rootCmd = &cobra.Command{
5252
// if the remote flag is set, clone the repo (using repoPath) into memory
5353
if remote {
5454
r, err := git.Clone(memory.NewStorage(), nil, &git.CloneOptions{
55-
URL: repoPath,
55+
URL: repoPath,
56+
SingleBranch: true,
5657
})
5758
handleError(err)
5859
repo = r
@@ -70,11 +71,13 @@ var rootCmd = &cobra.Command{
7071
// TODO (patrickdevivo) at some point this entire scoring logic should be brought out into a subpackage with some tests
7172
// this could also make it possibe for other projects to import the implementation.
7273
decayHours := 24 * decayDays
73-
// since := time.Now().Add(-(time.Duration(decayHours) * time.Hour * 10))
74+
75+
// this ignores any commits older than 100 half-lives,
76+
since := time.Now().Add(-(time.Duration(decayHours) * time.Hour * 10))
7477
commitIter, err := repo.Log(&git.LogOptions{
7578
Order: git.LogOrderCommitterTime,
7679
FileName: fileName,
77-
// Since: &since,
80+
Since: &since,
7881
})
7982
handleError(err)
8083
defer commitIter.Close()
@@ -99,15 +102,26 @@ var rootCmd = &cobra.Command{
99102
}
100103

101104
agg := authors[authorEmail]
105+
hoursAgo := time.Now().Sub(commit.Author.When).Hours()
102106
agg.commits++
103107

108+
// TODO this is a bit hacky, we're absorbing any panics that occur
109+
// in particular, it's meant to capture an index out of range error occurring
110+
// under some conditions in the underlying git/diff dependency. Maybe another reason to use native git...
111+
defer func() {
112+
if err := recover(); err != nil {
113+
agg.score += math.Exp2(-hoursAgo / float64(decayHours))
114+
}
115+
}()
116+
104117
fileStats, err := commit.Stats()
105118
handleError(err)
106119

107120
var additions int
108121
var deletions int
109122
for _, stat := range fileStats {
110123
// ignore diffs in vendor files
124+
// TODO perhaps it's worth allowing for the user to supply file path patterns be ignored?
111125
if enry.IsVendor(stat.Name) {
112126
continue
113127
}
@@ -116,7 +130,6 @@ var rootCmd = &cobra.Command{
116130
}
117131
agg.impact += additions + deletions
118132

119-
hoursAgo := time.Now().Sub(commit.Author.When).Hours()
120133
agg.score += float64(additions+deletions) * math.Exp2(-hoursAgo/float64(decayHours))
121134
return nil
122135
})
@@ -126,13 +139,14 @@ var rootCmd = &cobra.Command{
126139
})
127140

128141
w := tabwriter.NewWriter(os.Stdout, 0, 0, 3, ' ', tabwriter.TabIndent)
129-
fmt.Fprintf(w, "Rank\tEmail\tName\tScore\tCommits\tImpact\n")
142+
fmt.Fprintf(w, "Rank\tEmail\tName\tScore\tImpact\tCommits\n")
130143
for rank, authorEmail := range authorEmails {
131144
agg := authors[authorEmail]
145+
// ignore scores less than 0
132146
if agg.score < 1 {
133147
continue
134148
}
135-
fmt.Fprintf(w, "%d\t%s\t%s\t%d\t%d commits\t%d\n", rank+1, authorEmail, agg.name, int(math.Round(agg.score)), agg.commits, agg.impact)
149+
fmt.Fprintf(w, "%d\t%s\t%s\t%d\t%d\t%d\n", rank+1, authorEmail, agg.name, int(math.Round(agg.score)), agg.impact, agg.commits)
136150
}
137151
w.Flush()
138152
},

0 commit comments

Comments
 (0)