Skip to content

Commit 6883c00

Browse files
filipnavaratechknowlogick
authored andcommitted
[backport/v1.9] Fix reading git notes from nested trees (#8189)
* 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 91ea086 commit 6883c00

23 files changed

+59
-15
lines changed

modules/git/notes.go

+25-15
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ package git
77
import (
88
"io/ioutil"
99

10-
"gopkg.in/src-d/go-git.v4/plumbing"
10+
"gopkg.in/src-d/go-git.v4/plumbing/object"
1111
)
1212

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

30-
entry, err := notes.GetTreeEntryByPath(commitID)
31-
if err != nil {
32-
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+
}
3348
}
3449

35-
blob := entry.Blob()
36-
dataRc, err := blob.DataAsync()
50+
blob := file.Blob
51+
dataRc, err := blob.Reader()
3752
if err != nil {
3853
return err
3954
}
@@ -45,26 +60,21 @@ func GetNote(repo *Repository, commitID string, note *Note) error {
4560
}
4661
note.Message = d
4762

48-
commit, err := repo.gogitRepo.CommitObject(plumbing.Hash(notes.ID))
49-
if err != nil {
50-
return err
51-
}
52-
5363
commitNodeIndex, commitGraphFile := repo.CommitNodeIndex()
5464
if commitGraphFile != nil {
5565
defer commitGraphFile.Close()
5666
}
5767

58-
commitNode, err := commitNodeIndex.Get(commit.Hash)
68+
commitNode, err := commitNodeIndex.Get(notes.ID)
5969
if err != nil {
60-
return nil
70+
return err
6171
}
6272

63-
lastCommits, err := getLastCommitForPaths(commitNode, "", []string{commitID})
73+
lastCommits, err := getLastCommitForPaths(commitNode, "", []string{path})
6474
if err != nil {
6575
return err
6676
}
67-
note.Commit = convertCommit(lastCommits[commitID])
77+
note.Commit = convertCommit(lastCommits[path])
6878

6979
return nil
7080
}

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 <[email protected]> 1567767895 +0200 commit (initial): 1
2+
ba0a96fa63532d6c5087ecef070b0250ed72fa47 3e668dbfac39cbc80a9ff9c61eb565d944453ba4 Filip Navara <[email protected]> 1567767909 +0200 commit: 2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
0000000000000000000000000000000000000000 ba0a96fa63532d6c5087ecef070b0250ed72fa47 Filip Navara <[email protected]> 1567767895 +0200 commit (initial): 1
2+
ba0a96fa63532d6c5087ecef070b0250ed72fa47 3e668dbfac39cbc80a9ff9c61eb565d944453ba4 Filip Navara <[email protected]> 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)