Skip to content

Commit 89dc23f

Browse files
committed
Refactor git-notes data fetching to a separate function
1 parent 157f709 commit 89dc23f

File tree

2 files changed

+45
-14
lines changed

2 files changed

+45
-14
lines changed

modules/git/notes.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Copyright 2019 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package git
6+
7+
import (
8+
"io/ioutil"
9+
)
10+
11+
// Note stores information about a note created using git-notes.
12+
type Note struct {
13+
Message []byte
14+
}
15+
16+
// GetNote retrieves the git-notes data for a given commit.
17+
func GetNote(repo *Repository, commitID string, note *Note) error {
18+
notes, err := repo.GetCommit("refs/notes/commits")
19+
if err != nil {
20+
return err
21+
}
22+
23+
entry, err := notes.GetTreeEntryByPath(commitID)
24+
if err != nil {
25+
return err
26+
}
27+
28+
blob := entry.Blob()
29+
dataRc, err := blob.DataAsync()
30+
if err != nil {
31+
return err
32+
}
33+
34+
defer dataRc.Close()
35+
d, err := ioutil.ReadAll(dataRc)
36+
if err != nil {
37+
return err
38+
}
39+
40+
note.Message = d
41+
return nil
42+
}

routers/repo/commit.go

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
package repo
77

88
import (
9-
"io/ioutil"
109
"path"
1110
"strings"
1211

@@ -249,20 +248,10 @@ func Diff(ctx *context.Context) {
249248
ctx.Data["DiffNotAvailable"] = diff.NumFiles() == 0
250249
ctx.Data["SourcePath"] = setting.AppSubURL + "/" + path.Join(userName, repoName, "src", "commit", commitID)
251250

252-
notes, err := ctx.Repo.GitRepo.GetCommit("refs/notes/commits")
251+
note := git.Note{}
252+
err = git.GetNote(ctx.Repo.GitRepo, commitID, &note)
253253
if err == nil {
254-
entry, err := notes.GetTreeEntryByPath(commitID)
255-
if err == nil {
256-
blob := entry.Blob()
257-
dataRc, err := blob.DataAsync()
258-
if err == nil {
259-
d, err := ioutil.ReadAll(dataRc)
260-
dataRc.Close()
261-
if err == nil {
262-
ctx.Data["Note"] = string(templates.ToUTF8WithFallback(d))
263-
}
264-
}
265-
}
254+
ctx.Data["Note"] = string(templates.ToUTF8WithFallback(note.Message))
266255
}
267256

268257
if commit.ParentCount() > 0 {

0 commit comments

Comments
 (0)