Skip to content

Commit 52fda31

Browse files
filipnavaralunny
authored andcommittedSep 12, 2019
Fix reading git notes from nested trees (#8026)
* Fix reading notes from nested trees The GIT documentation for notes states "Permitted pathnames have the form ab/cd/ef/.../abcdef...: a sequence of directory names of two hexadecimal digits each followed by a filename with the rest of the object ID." * Add test case * Fix new lines
1 parent 5e67e01 commit 52fda31

23 files changed

+60
-14
lines changed
 

‎modules/git/notes.go

+26-14
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ package git
66

77
import (
88
"io/ioutil"
9+
10+
"gopkg.in/src-d/go-git.v4/plumbing/object"
911
)
1012

1113
// NotesRef is the git ref where Gitea will look for git-notes data.
@@ -25,13 +27,28 @@ func GetNote(repo *Repository, commitID string, note *Note) error {
2527
return err
2628
}
2729

28-
entry, err := notes.GetTreeEntryByPath(commitID)
29-
if err != nil {
30-
return err
30+
remainingCommitID := commitID
31+
path := ""
32+
currentTree := notes.Tree.gogitTree
33+
var file *object.File
34+
for len(remainingCommitID) > 2 {
35+
file, err = currentTree.File(remainingCommitID)
36+
if err == nil {
37+
path += remainingCommitID
38+
break
39+
}
40+
if err == object.ErrFileNotFound {
41+
currentTree, err = currentTree.Tree(remainingCommitID[0:2])
42+
path += remainingCommitID[0:2] + "/"
43+
remainingCommitID = remainingCommitID[2:]
44+
}
45+
if err != nil {
46+
return err
47+
}
3148
}
3249

33-
blob := entry.Blob()
34-
dataRc, err := blob.DataAsync()
50+
blob := file.Blob
51+
dataRc, err := blob.Reader()
3552
if err != nil {
3653
return err
3754
}
@@ -43,26 +60,21 @@ func GetNote(repo *Repository, commitID string, note *Note) error {
4360
}
4461
note.Message = d
4562

46-
commit, err := repo.gogitRepo.CommitObject(notes.ID)
47-
if err != nil {
48-
return err
49-
}
50-
5163
commitNodeIndex, commitGraphFile := repo.CommitNodeIndex()
5264
if commitGraphFile != nil {
5365
defer commitGraphFile.Close()
5466
}
5567

56-
commitNode, err := commitNodeIndex.Get(commit.Hash)
68+
commitNode, err := commitNodeIndex.Get(notes.ID)
5769
if err != nil {
58-
return nil
70+
return err
5971
}
6072

61-
lastCommits, err := getLastCommitForPaths(commitNode, "", []string{commitID})
73+
lastCommits, err := getLastCommitForPaths(commitNode, "", []string{path})
6274
if err != nil {
6375
return err
6476
}
65-
note.Commit = convertCommit(lastCommits[commitID])
77+
note.Commit = convertCommit(lastCommits[path])
6678

6779
return nil
6880
}

‎modules/git/notes_test.go

+14
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,17 @@ func TestGetNotes(t *testing.T) {
2222
assert.Equal(t, []byte("Note contents\n"), note.Message)
2323
assert.Equal(t, "Vladimir Panteleev", note.Commit.Author.Name)
2424
}
25+
26+
func TestGetNestedNotes(t *testing.T) {
27+
repoPath := filepath.Join(testReposDir, "repo3_notes")
28+
repo, err := OpenRepository(repoPath)
29+
assert.NoError(t, err)
30+
31+
note := Note{}
32+
err = GetNote(repo, "3e668dbfac39cbc80a9ff9c61eb565d944453ba4", &note)
33+
assert.NoError(t, err)
34+
assert.Equal(t, []byte("Note 2"), note.Message)
35+
err = GetNote(repo, "ba0a96fa63532d6c5087ecef070b0250ed72fa47", &note)
36+
assert.NoError(t, err)
37+
assert.Equal(t, []byte("Note 1"), note.Message)
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
2
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ref: refs/heads/master
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[core]
2+
repositoryformatversion = 0
3+
filemode = false
4+
bare = false
5+
logallrefupdates = true
6+
symlinks = false
7+
ignorecase = true
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Unnamed repository; edit this file 'description' to name the repository.
145 Bytes
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
0000000000000000000000000000000000000000 ba0a96fa63532d6c5087ecef070b0250ed72fa47 Filip Navara <filip.navara@gmail.com> 1567767895 +0200 commit (initial): 1
2+
ba0a96fa63532d6c5087ecef070b0250ed72fa47 3e668dbfac39cbc80a9ff9c61eb565d944453ba4 Filip Navara <filip.navara@gmail.com> 1567767909 +0200 commit: 2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
0000000000000000000000000000000000000000 ba0a96fa63532d6c5087ecef070b0250ed72fa47 Filip Navara <filip.navara@gmail.com> 1567767895 +0200 commit (initial): 1
2+
ba0a96fa63532d6c5087ecef070b0250ed72fa47 3e668dbfac39cbc80a9ff9c61eb565d944453ba4 Filip Navara <filip.navara@gmail.com> 1567767909 +0200 commit: 2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
x��;�0 @�s
2+
�H��&v*!����4�J��(p~
3+
G`|oxzi���;��3� �����6� �$`�"NR�ѺXla�i�K������4r�$�\P0"ỵPQ'F_��V�N�i����*�ʗ��G���ӳK�|�Y�e��H�f��f� ���Em
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
x��;�0�}��"ǿu$�RQr���K1F���1�nf��R-%w��zc�{�%7��h#�x���Q���fXѻ?j�K�� ��S#8�צ���{��M��3�> �����6Z�Q�m���8�
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
3e668dbfac39cbc80a9ff9c61eb565d944453ba4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
654c8b6b63c08bf37f638d3f521626b7fbbd4d37

0 commit comments

Comments
 (0)
Please sign in to comment.