@@ -122,17 +122,30 @@ func RefBlame(ctx *context.Context) {
122
122
blameParts = append (blameParts , * blamePart )
123
123
}
124
124
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
134
143
commitNames := make (map [string ]models.UserCommit )
144
+ // previousCommits contains links from SHA to parent SHA,
145
+ // if parent also contains the current TreePath.
135
146
previousCommits := make (map [string ]string )
147
+ // and as blameParts can reference the same commits multiple
148
+ // times, we cache the lookup work locally
136
149
commits := list .New ()
137
150
commitCache := map [string ]* git.Commit {}
138
151
commitCache [ctx .Repo .Commit .ID .String ()] = ctx .Repo .Commit
@@ -143,7 +156,9 @@ func RefBlame(ctx *context.Context) {
143
156
continue
144
157
}
145
158
159
+ // find the blamePart commit, to look up parent & email adress for avatars
146
160
commit , ok := commitCache [sha ]
161
+ var err error
147
162
if ! ok {
148
163
commit , err = ctx .Repo .GitRepo .GetCommit (sha )
149
164
if err != nil {
@@ -152,12 +167,12 @@ func RefBlame(ctx *context.Context) {
152
167
} else {
153
168
ctx .ServerError ("Repo.GitRepo.GetCommit" , err )
154
169
}
155
- return
170
+ return nil , nil
156
171
}
157
172
commitCache [sha ] = commit
158
173
}
159
174
160
- // populate parent
175
+ // find parent commit
161
176
if commit .ParentCount () > 0 {
162
177
psha := commit .Parents [0 ]
163
178
previousCommit , ok := commitCache [psha .String ()]
@@ -167,6 +182,7 @@ func RefBlame(ctx *context.Context) {
167
182
commitCache [psha .String ()] = previousCommit
168
183
}
169
184
}
185
+ // only store parent commit ONCE, if it has the file
170
186
if previousCommit != nil {
171
187
if haz1 , _ := previousCommit .HasFile (ctx .Repo .TreePath ); haz1 {
172
188
previousCommits [commit .ID .String ()] = previousCommit .ID .String ()
@@ -181,22 +197,12 @@ func RefBlame(ctx *context.Context) {
181
197
182
198
// populate commit email adresses to later look up avatars.
183
199
commits = models .ValidateCommitsWithEmails (commits )
184
-
185
200
for e := commits .Front (); e != nil ; e = e .Next () {
186
201
c := e .Value .(models.UserCommit )
187
-
188
202
commitNames [c .ID .String ()] = c
189
203
}
190
204
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
200
206
}
201
207
202
208
func renderBlame (ctx * context.Context , blameParts []git.BlamePart , commitNames map [string ]models.UserCommit , previousCommits map [string ]string ) {
0 commit comments