* 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
Unified · Split
| @@ -6,6 +6,8 @@ package git · 5 unmodified lines | |||
| 6 | 6 | ||
| 7 | import ( | 7 | import ( |
| 8 | "io/ioutil" | 8 | "io/ioutil" |
| 9 | |||
| 10 | "gopkg.in/src-d/go-git.v4/plumbing/object" | ||
| 9 | ) | 11 | ) |
| 10 | 12 | ||
| 11 | // NotesRef is the git ref where Gitea will look for git-notes data. | 13 | // 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 { · 13 unmodified lines | |||
| 25 | return err | 27 | return err |
| 26 | } | 28 | } |
| 27 | 29 | ||
| 28 | entry, err := notes.GetTreeEntryByPath(commitID) | 30 | remainingCommitID := commitID |
| 29 | if err != nil { | 31 | path := "" |
| 30 | return err | 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 | } | ||
| 31 | } | 48 | } |
| 32 | 49 | ||
| 33 | blob := entry.Blob() | 50 | blob := file.Blob |
| 34 | dataRc, err := blob.DataAsync() | 51 | dataRc, err := blob.Reader() |
| 35 | if err != nil { | 52 | if err != nil { |
| 36 | return err | 53 | return err |
| 37 | } | 54 | } |
| @@ -43,26 +60,21 @@ func GetNote(repo *Repository, commitID string, note *Note) error { · 5 unmodified lines | |||
| 43 | } | 60 | } |
| 44 | note.Message = d | 61 | note.Message = d |
| 45 | 62 | ||
| 46 | commit, err := repo.gogitRepo.CommitObject(notes.ID) | ||
| 47 | if err != nil { | ||
| 48 | return err | ||
| 49 | } | ||
| 50 | |||
| 51 | commitNodeIndex, commitGraphFile := repo.CommitNodeIndex() | 63 | commitNodeIndex, commitGraphFile := repo.CommitNodeIndex() |
| 52 | if commitGraphFile != nil { | 64 | if commitGraphFile != nil { |
| 53 | defer commitGraphFile.Close() | 65 | defer commitGraphFile.Close() |
| 54 | } | 66 | } |
| 55 | 67 | ||
| 56 | commitNode, err := commitNodeIndex.Get(commit.Hash) | 68 | commitNode, err := commitNodeIndex.Get(notes.ID) |
| 57 | if err != nil { | 69 | if err != nil { |
| 58 | return nil | 70 | return err |
| 59 | } | 71 | } |
| 60 | 72 | ||
| 61 | lastCommits, err := getLastCommitForPaths(commitNode, "", []string{commitID}) | 73 | lastCommits, err := getLastCommitForPaths(commitNode, "", []string{path}) |
| 62 | if err != nil { | 74 | if err != nil { |
| 63 | return err | 75 | return err |
| 64 | } | 76 | } |
| 65 | note.Commit = convertCommit(lastCommits[commitID]) | 77 | note.Commit = convertCommit(lastCommits[path]) |
| 66 | 78 | ||
| 67 | return nil | 79 | return nil |
| 68 | } | 80 | } |
| @@ -22,3 +22,17 @@ func TestGetNotes(t *testing.T) { · 21 unmodified lines | |||
| 22 | assert.Equal(t, []byte("Note contents\n"), note.Message) | 22 | assert.Equal(t, []byte("Note contents\n"), note.Message) |
| 23 | assert.Equal(t, "Vladimir Panteleev", note.Commit.Author.Name) | 23 | assert.Equal(t, "Vladimir Panteleev", note.Commit.Author.Name) |
| 24 | } | 24 | } |
| 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", ¬e) | ||
| 33 | assert.NoError(t, err) | ||
| 34 | assert.Equal(t, []byte("Note 2"), note.Message) | ||
| 35 | err = GetNote(repo, "ba0a96fa63532d6c5087ecef070b0250ed72fa47", ¬e) | ||
| 36 | assert.NoError(t, err) | ||
| 37 | assert.Equal(t, []byte("Note 1"), note.Message) | ||
| 38 | } | ||
| @@ -0,0 +1 @@ | |||
| 1 | 2 | ||
| @@ -0,0 +1 @@ | |||
| 1 | ref: refs/heads/master | ||
| @@ -0,0 +1,7 @@ | |||
| 1 | [core] | ||
| 2 | repositoryformatversion = 0 | ||
| 3 | filemode = false | ||
| 4 | bare = false | ||
| 5 | logallrefupdates = true | ||
| 6 | symlinks = false | ||
| 7 | ignorecase = true | ||
| @@ -0,0 +1 @@ | |||
| 1 | Unnamed repository; edit this file 'description' to name the repository. | ||
Binary file not shown.
| @@ -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 | ||
| @@ -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 | ||
Binary file not shown.
Binary file not shown.
| @@ -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 | ||
Binary file not shown.
Binary file not shown.
Binary file not shown.
| @@ -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� | ||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
| @@ -0,0 +1 @@ | |||
| 1 | 3e668dbfac39cbc80a9ff9c61eb565d944453ba4 | ||
| @@ -0,0 +1 @@ | |||
| 1 | 654c8b6b63c08bf37f638d3f521626b7fbbd4d37 | ||