Skip to content

Commit a98e085

Browse files
CyberShadowlafriks
andcommitted
Show git-notes (#6984)
* Show git-notes * Make git-notes heading text localizable * Refactor git-notes data fetching to a separate function * Display the author and time of git notes * Move note bubble inside the commit bubble * Revert "Move note bubble inside the commit bubble" This reverts commit c0951fe. * Add test for git-notes * testing ui * Polish CSS * Apply suggestions from code review Co-Authored-By: Lauris BH <[email protected]>
1 parent d5a98a2 commit a98e085

File tree

13 files changed

+146
-1
lines changed

13 files changed

+146
-1
lines changed

modules/git/notes.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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+
"gopkg.in/src-d/go-git.v4/plumbing"
11+
)
12+
13+
// NotesRef is the git ref where Gitea will look for git-notes data.
14+
// The value ("refs/notes/commits") is the default ref used by git-notes.
15+
const NotesRef = "refs/notes/commits"
16+
17+
// Note stores information about a note created using git-notes.
18+
type Note struct {
19+
Message []byte
20+
Commit *Commit
21+
}
22+
23+
// GetNote retrieves the git-notes data for a given commit.
24+
func GetNote(repo *Repository, commitID string, note *Note) error {
25+
notes, err := repo.GetCommit(NotesRef)
26+
if err != nil {
27+
return err
28+
}
29+
30+
entry, err := notes.GetTreeEntryByPath(commitID)
31+
if err != nil {
32+
return err
33+
}
34+
35+
blob := entry.Blob()
36+
dataRc, err := blob.DataAsync()
37+
if err != nil {
38+
return err
39+
}
40+
41+
defer dataRc.Close()
42+
d, err := ioutil.ReadAll(dataRc)
43+
if err != nil {
44+
return err
45+
}
46+
note.Message = d
47+
48+
commit, err := repo.gogitRepo.CommitObject(plumbing.Hash(notes.ID))
49+
if err != nil {
50+
return err
51+
}
52+
53+
lastCommits, err := getLastCommitForPaths(commit, "", []string{commitID})
54+
if err != nil {
55+
return err
56+
}
57+
note.Commit = convertCommit(lastCommits[commitID])
58+
59+
return nil
60+
}

modules/git/notes_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
"path/filepath"
9+
"testing"
10+
11+
"github.com/stretchr/testify/assert"
12+
)
13+
14+
func TestGetNotes(t *testing.T) {
15+
bareRepo1Path := filepath.Join(testReposDir, "repo1_bare")
16+
bareRepo1, err := OpenRepository(bareRepo1Path)
17+
assert.NoError(t, err)
18+
19+
note := Note{}
20+
err = GetNote(bareRepo1, "95bb4d39648ee7e325106df01a621c530863a653", &note)
21+
assert.NoError(t, err)
22+
assert.Equal(t, []byte("Note contents\n"), note.Message)
23+
assert.Equal(t, "Vladimir Panteleev", note.Commit.Author.Name)
24+
}

modules/git/repo_ref_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,14 @@ func TestRepository_GetRefs(t *testing.T) {
1919
refs, err := bareRepo1.GetRefs()
2020

2121
assert.NoError(t, err)
22-
assert.Len(t, refs, 4)
22+
assert.Len(t, refs, 5)
2323

2424
expectedRefs := []string{
2525
BranchPrefix + "branch1",
2626
BranchPrefix + "branch2",
2727
BranchPrefix + "master",
2828
TagPrefix + "test",
29+
NotesRef,
2930
}
3031

3132
for _, ref := range refs {
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
x��M
2+
�0F]���B�&&m"�@\�Of�6�HG����
3+
~˷x��y���� ��?[����B�&
4+
H<b�yߙNGt��ڨ��~.�"�1x�Ix`����&=㚸,}��{�X���� �p��)���j�}^ 1AZ����3�,����I0
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ca6b5ddf303169a72d2a2971acde4f6eea194e5c

modules/templates/helper.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ func NewFuncMap() []template.FuncMap {
125125
"RenderCommitMessage": RenderCommitMessage,
126126
"RenderCommitMessageLink": RenderCommitMessageLink,
127127
"RenderCommitBody": RenderCommitBody,
128+
"RenderNote": RenderNote,
128129
"IsMultilineCommitMessage": IsMultilineCommitMessage,
129130
"ThemeColorMetaTag": func() string {
130131
return setting.UI.ThemeColorMetaTag
@@ -392,6 +393,17 @@ func RenderCommitBody(msg, urlPrefix string, metas map[string]string) template.H
392393
return template.HTML(strings.Join(body[1:], "\n"))
393394
}
394395

396+
// RenderNote renders the contents of a git-notes file as a commit message.
397+
func RenderNote(msg, urlPrefix string, metas map[string]string) template.HTML {
398+
cleanMsg := template.HTMLEscapeString(msg)
399+
fullMessage, err := markup.RenderCommitMessage([]byte(cleanMsg), urlPrefix, "", metas)
400+
if err != nil {
401+
log.Error("RenderNote: %v", err)
402+
return ""
403+
}
404+
return template.HTML(string(fullMessage))
405+
}
406+
395407
// IsMultilineCommitMessage checks to see if a commit message contains multiple lines.
396408
func IsMultilineCommitMessage(msg string) bool {
397409
return strings.Count(strings.TrimSpace(msg), "\n") >= 1

options/locale/locale_en-US.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1314,6 +1314,7 @@ settings.unarchive.error = An error occured while trying to un-archive the repo.
13141314
diff.browse_source = Browse Source
13151315
diff.parent = parent
13161316
diff.commit = commit
1317+
diff.git-notes = Notes
13171318
diff.data_not_available = Diff Content Not Available
13181319
diff.show_diff_stats = Show Diff Stats
13191320
diff.show_split_view = Split View

public/css/index.css

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -803,6 +803,8 @@ footer .ui.left,footer .ui.right{line-height:40px}
803803
.stats-table .table-cell.tiny{height:.5em}
804804
tbody.commit-list{vertical-align:baseline}
805805
.commit-body{white-space:pre-wrap}
806+
.git-notes.top{text-align:left}
807+
.git-notes .commit-body{margin:0}
806808
@media only screen and (max-width:767px){.ui.stackable.menu.mobile--margin-between-items>.item{margin-top:5px;margin-bottom:5px}
807809
.ui.stackable.menu.mobile--no-negative-margins{margin-left:0;margin-right:0}
808810
}

public/less/_repository.less

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2219,6 +2219,15 @@ tbody.commit-list {
22192219
white-space: pre-wrap;
22202220
}
22212221

2222+
.git-notes {
2223+
&.top {
2224+
text-align: left;
2225+
}
2226+
.commit-body {
2227+
margin: 0;
2228+
}
2229+
}
2230+
22222231
@media only screen and (max-width: 767px) {
22232232
.ui.stackable.menu {
22242233
&.mobile--margin-between-items > .item {

routers/repo/commit.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"code.gitea.io/gitea/modules/git"
1616
"code.gitea.io/gitea/modules/log"
1717
"code.gitea.io/gitea/modules/setting"
18+
"code.gitea.io/gitea/modules/templates"
1819
)
1920

2021
const (
@@ -246,6 +247,15 @@ func Diff(ctx *context.Context) {
246247
ctx.Data["Parents"] = parents
247248
ctx.Data["DiffNotAvailable"] = diff.NumFiles() == 0
248249
ctx.Data["SourcePath"] = setting.AppSubURL + "/" + path.Join(userName, repoName, "src", "commit", commitID)
250+
251+
note := &git.Note{}
252+
err = git.GetNote(ctx.Repo.GitRepo, commitID, note)
253+
if err == nil {
254+
ctx.Data["Note"] = string(templates.ToUTF8WithFallback(note.Message))
255+
ctx.Data["NoteCommit"] = note.Commit
256+
ctx.Data["NoteAuthor"] = models.ValidateCommitWithEmail(note.Commit)
257+
}
258+
249259
if commit.ParentCount() > 0 {
250260
ctx.Data["BeforeSourcePath"] = setting.AppSubURL + "/" + path.Join(userName, repoName, "src", "commit", parents[0])
251261
}

templates/repo/diff/page.tmpl

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,27 @@
6565
</div>
6666
{{end}}
6767
{{end}}
68+
{{if .Note}}
69+
<div class="ui top attached info segment message git-notes">
70+
<i class="sticky note icon"></i>
71+
{{.i18n.Tr "repo.diff.git-notes"}}:
72+
{{if .NoteAuthor}}
73+
<a href="{{.NoteAuthor.HomeLink}}">
74+
{{if .NoteAuthor.FullName}}
75+
<strong>{{.NoteAuthor.FullName}}</strong>
76+
{{else}}
77+
<strong>{{.NoteCommit.Author.Name}}</strong>
78+
{{end}}
79+
</a>
80+
{{else}}
81+
<strong>{{.NoteCommit.Author.Name}}</strong>
82+
{{end}}
83+
<span class="text grey" id="note-authored-time">{{TimeSince .NoteCommit.Author.When $.Lang}}</span>
84+
</div>
85+
<div class="ui bottom attached info segment git-notes">
86+
<pre class="commit-body">{{RenderNote .Note $.RepoLink $.Repository.ComposeMetas}}</pre>
87+
</div>
88+
{{end}}
6889
{{end}}
6990

7091
{{template "repo/diff/box" .}}

0 commit comments

Comments
 (0)