diff options
author | Filip Navara <filip.navara@gmail.com> | 2019-09-12 03:14:41 +0200 |
---|---|---|
committer | Lunny Xiao <xiaolunwen@gmail.com> | 2019-09-12 09:14:41 +0800 |
commit | 52fda312dfd2ed93e512c8e210c5646e4ae53dbc (patch) | |
tree | 2451976cec416795a76ac7c66a3cf5b9d8d356b2 /modules/git/notes.go | |
parent | 5e67e0100ce0c0ad6cc8e595ab3d097e619e7e56 (diff) | |
download | gitea-52fda312dfd2ed93e512c8e210c5646e4ae53dbc.tar.gz gitea-52fda312dfd2ed93e512c8e210c5646e4ae53dbc.zip |
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
Diffstat (limited to 'modules/git/notes.go')
-rw-r--r-- | modules/git/notes.go | 40 |
1 files changed, 26 insertions, 14 deletions
diff --git a/modules/git/notes.go b/modules/git/notes.go index aea54ab202..e825923682 100644 --- a/modules/git/notes.go +++ b/modules/git/notes.go @@ -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. @@ -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 } @@ -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 } |