Skip to content

Commit

Permalink
Fix reading notes from nested trees
Browse files Browse the repository at this point in the history
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."
  • Loading branch information
filipnavara committed Aug 29, 2019
1 parent 187ae10 commit ac21d4f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 15 deletions.
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
}
2 changes: 1 addition & 1 deletion vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ github.com/willf/bitset
github.com/xanzy/ssh-agent
# github.com/yohcop/openid-go v0.0.0-20160914080427-2c050d2dae53
github.com/yohcop/openid-go
# golang.org/x/crypto v0.0.0-20190820162420-60c769a6c586
# golang.org/x/crypto v0.0.0-20190829043050-9756ffdc2472
golang.org/x/crypto/acme/autocert
golang.org/x/crypto/argon2
golang.org/x/crypto/bcrypt
Expand Down

0 comments on commit ac21d4f

Please sign in to comment.