Browse Source

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
release/v1.10
Filip Navara 3 years ago
committed by Lunny Xiao
parent
commit
52fda312df
  1. 40
      modules/git/notes.go
  2. 14
      modules/git/notes_test.go
  3. 1
      modules/git/tests/repos/repo3_notes/COMMIT_EDITMSG
  4. 1
      modules/git/tests/repos/repo3_notes/HEAD
  5. 7
      modules/git/tests/repos/repo3_notes/config
  6. 1
      modules/git/tests/repos/repo3_notes/description
  7. BIN
      modules/git/tests/repos/repo3_notes/index
  8. 2
      modules/git/tests/repos/repo3_notes/logs/HEAD
  9. 2
      modules/git/tests/repos/repo3_notes/logs/refs/heads/master
  10. BIN
      modules/git/tests/repos/repo3_notes/objects/29/7128d6553180486c780e2f747cb6d0014bf1f6
  11. BIN
      modules/git/tests/repos/repo3_notes/objects/2f/7e2ea1e905c14c8a98e7ce47b395592834b9ef
  12. 3
      modules/git/tests/repos/repo3_notes/objects/3e/668dbfac39cbc80a9ff9c61eb565d944453ba4
  13. BIN
      modules/git/tests/repos/repo3_notes/objects/42/716fdb6f261867472899d785123e6ecaa5ca02
  14. BIN
      modules/git/tests/repos/repo3_notes/objects/56/a6051ca2b02b04ef92d5150c9ef600403cb1de
  15. BIN
      modules/git/tests/repos/repo3_notes/objects/61/6c62e75fce60d806f4afe993211705a00a2544
  16. 1
      modules/git/tests/repos/repo3_notes/objects/65/4c8b6b63c08bf37f638d3f521626b7fbbd4d37
  17. BIN
      modules/git/tests/repos/repo3_notes/objects/ba/0a96fa63532d6c5087ecef070b0250ed72fa47
  18. BIN
      modules/git/tests/repos/repo3_notes/objects/c9/34d51cee361fdee21a3f3bb1a285f5ea9bc225
  19. BIN
      modules/git/tests/repos/repo3_notes/objects/d8/263ee9860594d2806b0dfd1bfd17528b0ba2a4
  20. BIN
      modules/git/tests/repos/repo3_notes/objects/f3/6ad903e408cb8f4ed90bda02e3a1fd2fab7907
  21. BIN
      modules/git/tests/repos/repo3_notes/objects/fe/c9fe57e9864fe537f02f825e377c4a8a65ad2e
  22. 1
      modules/git/tests/repos/repo3_notes/refs/heads/master
  23. 1
      modules/git/tests/repos/repo3_notes/refs/notes/commits

40
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
}

14
modules/git/notes_test.go

@ -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
modules/git/tests/repos/repo3_notes/COMMIT_EDITMSG

@ -0,0 +1 @@
2

1
modules/git/tests/repos/repo3_notes/HEAD

@ -0,0 +1 @@
ref: refs/heads/master

7
modules/git/tests/repos/repo3_notes/config

@ -0,0 +1,7 @@
[core]
repositoryformatversion = 0
filemode = false
bare = false
logallrefupdates = true
symlinks = false
ignorecase = true

1
modules/git/tests/repos/repo3_notes/description

@ -0,0 +1 @@
Unnamed repository; edit this file 'description' to name the repository.

BIN
modules/git/tests/repos/repo3_notes/index

2
modules/git/tests/repos/repo3_notes/logs/HEAD

@ -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
modules/git/tests/repos/repo3_notes/logs/refs/heads/master

@ -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

BIN
modules/git/tests/repos/repo3_notes/objects/29/7128d6553180486c780e2f747cb6d0014bf1f6

BIN
modules/git/tests/repos/repo3_notes/objects/2f/7e2ea1e905c14c8a98e7ce47b395592834b9ef

3
modules/git/tests/repos/repo3_notes/objects/3e/668dbfac39cbc80a9ff9c61eb565d944453ba4

@ -0,0 +1,3 @@
xťŽ;Â0 @™s
ďH•ă&v*!ÄÄČś4�JôŁ(p~
G`|oxziťç©‘;´š3Đ –ÂČŢ÷6  ś$`¦"NRäŃşXlałiÍK�¨¨ĺŢ÷4rň$§\P0"yĚŁPQ'F_í±V¸NĎi�›ľµ*śĘ—şĺG—ű¬ÓłKë|ëY„eŔŽH�f·űfË ™ÜăEm

BIN
modules/git/tests/repos/repo3_notes/objects/42/716fdb6f261867472899d785123e6ecaa5ca02

BIN
modules/git/tests/repos/repo3_notes/objects/56/a6051ca2b02b04ef92d5150c9ef600403cb1de

BIN
modules/git/tests/repos/repo3_notes/objects/61/6c62e75fce60d806f4afe993211705a00a2544

1
modules/git/tests/repos/repo3_notes/objects/65/4c8b6b63c08bf37f638d3f521626b7fbbd4d37

@ -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В

BIN
modules/git/tests/repos/repo3_notes/objects/ba/0a96fa63532d6c5087ecef070b0250ed72fa47

BIN
modules/git/tests/repos/repo3_notes/objects/c9/34d51cee361fdee21a3f3bb1a285f5ea9bc225

BIN
modules/git/tests/repos/repo3_notes/objects/d8/263ee9860594d2806b0dfd1bfd17528b0ba2a4

BIN
modules/git/tests/repos/repo3_notes/objects/f3/6ad903e408cb8f4ed90bda02e3a1fd2fab7907

BIN
modules/git/tests/repos/repo3_notes/objects/fe/c9fe57e9864fe537f02f825e377c4a8a65ad2e

1
modules/git/tests/repos/repo3_notes/refs/heads/master

@ -0,0 +1 @@
3e668dbfac39cbc80a9ff9c61eb565d944453ba4

1
modules/git/tests/repos/repo3_notes/refs/notes/commits

@ -0,0 +1 @@
654c8b6b63c08bf37f638d3f521626b7fbbd4d37
Loading…
Cancel
Save