Skip to content

Fix reading git notes from nested trees #8026

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Sep 12, 2019
40 changes: 26 additions & 14 deletions modules/git/notes.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ package git

import (
"io/ioutil"

"gopkg.in/src-d/go-git.v4/plumbing/object"
)

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

entry, err := notes.GetTreeEntryByPath(commitID)
if err != nil {
return err
remainingCommitID := commitID
path := ""
currentTree := notes.Tree.gogitTree
var file *object.File
for len(remainingCommitID) > 2 {
file, err = currentTree.File(remainingCommitID)
if err == nil {
path += remainingCommitID
break
}
if err == object.ErrFileNotFound {
currentTree, err = currentTree.Tree(remainingCommitID[0:2])
path += remainingCommitID[0:2] + "/"
remainingCommitID = remainingCommitID[2:]
}
if err != nil {
return err
}
}

blob := entry.Blob()
dataRc, err := blob.DataAsync()
blob := file.Blob
dataRc, err := blob.Reader()
if err != nil {
return err
}
Expand All @@ -43,26 +60,21 @@ func GetNote(repo *Repository, commitID string, note *Note) error {
}
note.Message = d

commit, err := repo.gogitRepo.CommitObject(notes.ID)
if err != nil {
return err
}

commitNodeIndex, commitGraphFile := repo.CommitNodeIndex()
if commitGraphFile != nil {
defer commitGraphFile.Close()
}

commitNode, err := commitNodeIndex.Get(commit.Hash)
commitNode, err := commitNodeIndex.Get(notes.ID)
if err != nil {
return nil
return err
}

lastCommits, err := getLastCommitForPaths(commitNode, "", []string{commitID})
lastCommits, err := getLastCommitForPaths(commitNode, "", []string{path})
if err != nil {
return err
}
note.Commit = convertCommit(lastCommits[commitID])
note.Commit = convertCommit(lastCommits[path])

return nil
}
14 changes: 14 additions & 0 deletions modules/git/notes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,17 @@ func TestGetNotes(t *testing.T) {
assert.Equal(t, []byte("Note contents\n"), note.Message)
assert.Equal(t, "Vladimir Panteleev", note.Commit.Author.Name)
}

func TestGetNestedNotes(t *testing.T) {
repoPath := filepath.Join(testReposDir, "repo3_notes")
repo, err := OpenRepository(repoPath)
assert.NoError(t, err)

note := Note{}
err = GetNote(repo, "3e668dbfac39cbc80a9ff9c61eb565d944453ba4", &note)
assert.NoError(t, err)
assert.Equal(t, []byte("Note 2"), note.Message)
err = GetNote(repo, "ba0a96fa63532d6c5087ecef070b0250ed72fa47", &note)
assert.NoError(t, err)
assert.Equal(t, []byte("Note 1"), note.Message)
}
1 change: 1 addition & 0 deletions modules/git/tests/repos/repo3_notes/COMMIT_EDITMSG
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2
1 change: 1 addition & 0 deletions modules/git/tests/repos/repo3_notes/HEAD
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ref: refs/heads/master
7 changes: 7 additions & 0 deletions modules/git/tests/repos/repo3_notes/config
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[core]
repositoryformatversion = 0
filemode = false
bare = false
logallrefupdates = true
symlinks = false
ignorecase = true
1 change: 1 addition & 0 deletions modules/git/tests/repos/repo3_notes/description
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Unnamed repository; edit this file 'description' to name the repository.
Binary file added modules/git/tests/repos/repo3_notes/index
Binary file not shown.
2 changes: 2 additions & 0 deletions modules/git/tests/repos/repo3_notes/logs/HEAD
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
0000000000000000000000000000000000000000 ba0a96fa63532d6c5087ecef070b0250ed72fa47 Filip Navara <filip.navara@gmail.com> 1567767895 +0200 commit (initial): 1
ba0a96fa63532d6c5087ecef070b0250ed72fa47 3e668dbfac39cbc80a9ff9c61eb565d944453ba4 Filip Navara <filip.navara@gmail.com> 1567767909 +0200 commit: 2
2 changes: 2 additions & 0 deletions modules/git/tests/repos/repo3_notes/logs/refs/heads/master
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
0000000000000000000000000000000000000000 ba0a96fa63532d6c5087ecef070b0250ed72fa47 Filip Navara <filip.navara@gmail.com> 1567767895 +0200 commit (initial): 1
ba0a96fa63532d6c5087ecef070b0250ed72fa47 3e668dbfac39cbc80a9ff9c61eb565d944453ba4 Filip Navara <filip.navara@gmail.com> 1567767909 +0200 commit: 2
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
x��;�0 @�s
�H��&v*!����4�J��(p~
G`|oxzi���;��3� �����6� �$`�"NR�ѺXla�i�K������4r�$�\P0"ỵPQ'F_��V�N�i����*�ʗ��G���ӳK�|�Y�e��H�f��f� ���Em
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +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�
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
1 change: 1 addition & 0 deletions modules/git/tests/repos/repo3_notes/refs/heads/master
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3e668dbfac39cbc80a9ff9c61eb565d944453ba4
1 change: 1 addition & 0 deletions modules/git/tests/repos/repo3_notes/refs/notes/commits
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
654c8b6b63c08bf37f638d3f521626b7fbbd4d37