Skip to content

Commit 2495a46

Browse files
committed
split out blamePart processing into function
1 parent a48dcc6 commit 2495a46

File tree

1 file changed

+28
-22
lines changed

1 file changed

+28
-22
lines changed

routers/web/repo/blame.go

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -122,17 +122,30 @@ func RefBlame(ctx *context.Context) {
122122
blameParts = append(blameParts, *blamePart)
123123
}
124124

125-
// PREVIOUS BLAME LINK RESOLVER
126-
//
127-
// we look for the SHAs of the blameParts parent commit. so we..
128-
// 1 resolve the blamePart commit to
129-
// 2 look up its parent
130-
// 3 validate that the parent commit holds the current treepath
131-
// 4 store the parent SHA if successful
132-
// and as blameParts can reference the same commits multiple
133-
// times, we cache the work locally
125+
// Get Topics of this repo
126+
renderRepoTopics(ctx)
127+
if ctx.Written() {
128+
return
129+
}
130+
131+
commitNames, previousCommits := processBlameParts(ctx, blameParts)
132+
if ctx.Written() {
133+
return
134+
}
135+
136+
renderBlame(ctx, blameParts, commitNames, previousCommits)
137+
138+
ctx.HTML(http.StatusOK, tplBlame)
139+
}
140+
141+
func processBlameParts(ctx *context.Context, blameParts []git.BlamePart) (map[string]models.UserCommit, map[string]string) {
142+
// store commit data by SHA to look up avatar info etc
134143
commitNames := make(map[string]models.UserCommit)
144+
// previousCommits contains links from SHA to parent SHA,
145+
// if parent also contains the current TreePath.
135146
previousCommits := make(map[string]string)
147+
// and as blameParts can reference the same commits multiple
148+
// times, we cache the lookup work locally
136149
commits := list.New()
137150
commitCache := map[string]*git.Commit{}
138151
commitCache[ctx.Repo.Commit.ID.String()] = ctx.Repo.Commit
@@ -143,7 +156,9 @@ func RefBlame(ctx *context.Context) {
143156
continue
144157
}
145158

159+
// find the blamePart commit, to look up parent & email adress for avatars
146160
commit, ok := commitCache[sha]
161+
var err error
147162
if !ok {
148163
commit, err = ctx.Repo.GitRepo.GetCommit(sha)
149164
if err != nil {
@@ -152,12 +167,12 @@ func RefBlame(ctx *context.Context) {
152167
} else {
153168
ctx.ServerError("Repo.GitRepo.GetCommit", err)
154169
}
155-
return
170+
return nil, nil
156171
}
157172
commitCache[sha] = commit
158173
}
159174

160-
// populate parent
175+
// find parent commit
161176
if commit.ParentCount() > 0 {
162177
psha := commit.Parents[0]
163178
previousCommit, ok := commitCache[psha.String()]
@@ -167,6 +182,7 @@ func RefBlame(ctx *context.Context) {
167182
commitCache[psha.String()] = previousCommit
168183
}
169184
}
185+
// only store parent commit ONCE, if it has the file
170186
if previousCommit != nil {
171187
if haz1, _ := previousCommit.HasFile(ctx.Repo.TreePath); haz1 {
172188
previousCommits[commit.ID.String()] = previousCommit.ID.String()
@@ -181,22 +197,12 @@ func RefBlame(ctx *context.Context) {
181197

182198
// populate commit email adresses to later look up avatars.
183199
commits = models.ValidateCommitsWithEmails(commits)
184-
185200
for e := commits.Front(); e != nil; e = e.Next() {
186201
c := e.Value.(models.UserCommit)
187-
188202
commitNames[c.ID.String()] = c
189203
}
190204

191-
// Get Topics of this repo
192-
renderRepoTopics(ctx)
193-
if ctx.Written() {
194-
return
195-
}
196-
197-
renderBlame(ctx, blameParts, commitNames, previousCommits)
198-
199-
ctx.HTML(http.StatusOK, tplBlame)
205+
return commitNames, previousCommits
200206
}
201207

202208
func renderBlame(ctx *context.Context, blameParts []git.BlamePart, commitNames map[string]models.UserCommit, previousCommits map[string]string) {

0 commit comments

Comments
 (0)