mirror of
				https://github.com/go-gitea/gitea
				synced 2025-10-31 11:28:24 +00:00 
			
		
		
		
	[backport/v1.9] Fix reading git notes from nested trees (#8189)
* 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
This commit is contained in:
		
				
					committed by
					
						 techknowlogick
						techknowlogick
					
				
			
			
				
	
			
			
			
						parent
						
							91ea086ebe
						
					
				
				
					commit
					6883c007d3
				
			| @@ -7,7 +7,7 @@ package git | |||||||
| import ( | import ( | ||||||
| 	"io/ioutil" | 	"io/ioutil" | ||||||
|  |  | ||||||
| 	"gopkg.in/src-d/go-git.v4/plumbing" | 	"gopkg.in/src-d/go-git.v4/plumbing/object" | ||||||
| ) | ) | ||||||
|  |  | ||||||
| // NotesRef is the git ref where Gitea will look for git-notes data. | // NotesRef is the git ref where Gitea will look for git-notes data. | ||||||
| @@ -27,13 +27,28 @@ func GetNote(repo *Repository, commitID string, note *Note) error { | |||||||
| 		return err | 		return err | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	entry, err := notes.GetTreeEntryByPath(commitID) | 	remainingCommitID := commitID | ||||||
| 	if err != nil { | 	path := "" | ||||||
| 		return err | 	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() | 	blob := file.Blob | ||||||
| 	dataRc, err := blob.DataAsync() | 	dataRc, err := blob.Reader() | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return err | 		return err | ||||||
| 	} | 	} | ||||||
| @@ -45,26 +60,21 @@ func GetNote(repo *Repository, commitID string, note *Note) error { | |||||||
| 	} | 	} | ||||||
| 	note.Message = d | 	note.Message = d | ||||||
|  |  | ||||||
| 	commit, err := repo.gogitRepo.CommitObject(plumbing.Hash(notes.ID)) |  | ||||||
| 	if err != nil { |  | ||||||
| 		return err |  | ||||||
| 	} |  | ||||||
|  |  | ||||||
| 	commitNodeIndex, commitGraphFile := repo.CommitNodeIndex() | 	commitNodeIndex, commitGraphFile := repo.CommitNodeIndex() | ||||||
| 	if commitGraphFile != nil { | 	if commitGraphFile != nil { | ||||||
| 		defer commitGraphFile.Close() | 		defer commitGraphFile.Close() | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	commitNode, err := commitNodeIndex.Get(commit.Hash) | 	commitNode, err := commitNodeIndex.Get(notes.ID) | ||||||
| 	if err != nil { |  | ||||||
| 		return nil |  | ||||||
| 	} |  | ||||||
|  |  | ||||||
| 	lastCommits, err := getLastCommitForPaths(commitNode, "", []string{commitID}) |  | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return err | 		return err | ||||||
| 	} | 	} | ||||||
| 	note.Commit = convertCommit(lastCommits[commitID]) |  | ||||||
|  | 	lastCommits, err := getLastCommitForPaths(commitNode, "", []string{path}) | ||||||
|  | 	if err != nil { | ||||||
|  | 		return err | ||||||
|  | 	} | ||||||
|  | 	note.Commit = convertCommit(lastCommits[path]) | ||||||
|  |  | ||||||
| 	return nil | 	return nil | ||||||
| } | } | ||||||
|   | |||||||
| @@ -22,3 +22,17 @@ func TestGetNotes(t *testing.T) { | |||||||
| 	assert.Equal(t, []byte("Note contents\n"), note.Message) | 	assert.Equal(t, []byte("Note contents\n"), note.Message) | ||||||
| 	assert.Equal(t, "Vladimir Panteleev", note.Commit.Author.Name) | 	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", ¬e) | ||||||
|  | 	assert.NoError(t, err) | ||||||
|  | 	assert.Equal(t, []byte("Note 2"), note.Message) | ||||||
|  | 	err = GetNote(repo, "ba0a96fa63532d6c5087ecef070b0250ed72fa47", ¬e) | ||||||
|  | 	assert.NoError(t, err) | ||||||
|  | 	assert.Equal(t, []byte("Note 1"), note.Message) | ||||||
|  | } | ||||||
|   | |||||||
							
								
								
									
										1
									
								
								modules/git/tests/repos/repo3_notes/COMMIT_EDITMSG
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								modules/git/tests/repos/repo3_notes/COMMIT_EDITMSG
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1 @@ | |||||||
|  | 2 | ||||||
							
								
								
									
										1
									
								
								modules/git/tests/repos/repo3_notes/HEAD
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								modules/git/tests/repos/repo3_notes/HEAD
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1 @@ | |||||||
|  | ref: refs/heads/master | ||||||
							
								
								
									
										7
									
								
								modules/git/tests/repos/repo3_notes/config
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										7
									
								
								modules/git/tests/repos/repo3_notes/config
									
									
									
									
									
										Normal file
									
								
							| @@ -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
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								modules/git/tests/repos/repo3_notes/description
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1 @@ | |||||||
|  | Unnamed repository; edit this file 'description' to name the repository. | ||||||
							
								
								
									
										
											BIN
										
									
								
								modules/git/tests/repos/repo3_notes/index
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								modules/git/tests/repos/repo3_notes/index
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							
							
								
								
									
										2
									
								
								modules/git/tests/repos/repo3_notes/logs/HEAD
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										2
									
								
								modules/git/tests/repos/repo3_notes/logs/HEAD
									
									
									
									
									
										Normal file
									
								
							| @@ -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 | ||||||
| @@ -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.
										
									
								
							| @@ -0,0 +1,3 @@ | |||||||
|  | x<01><>;<0E>0@<40>s | ||||||
|  | <EFBFBD>H<EFBFBD><EFBFBD>&v*!<21><><EFBFBD><1D>4<EFBFBD>J<EFBFBD><4A>(p~ | ||||||
|  | G`|oxzi<7A><69><EFBFBD><01>;<3B><>3<EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD>6<EFBFBD><0B>$`<60>"NR<4E>ѺXla<6C>i<EFBFBD>K<EFBFBD><4B><EFBFBD><17><><EFBFBD>4r<34>$<24>\P0"ỵPQ'F_<46><5F>V<EFBFBD>N<EFBFBD>i<EFBFBD><69><EFBFBD><EFBFBD>*<2A>ʗ<EFBFBD><CA97>G<EFBFBD><47><EFBFBD>ӳK<D3B3>|<06>Y<EFBFBD>e<EFBFBD><01>H<EFBFBD>f<EFBFBD><66>f<EFBFBD><0C><0F><>Em | ||||||
										
											Binary file not shown.
										
									
								
							
										
											Binary file not shown.
										
									
								
							
										
											Binary file not shown.
										
									
								
							| @@ -0,0 +1 @@ | |||||||
|  | x<01><>;<0E>0<05>}<7D><><EFBFBD>"ǿu$<24>RQr<51><72><EFBFBD>K1F<31><46><EFBFBD>1<1C>nf<6E><66>R-%w<><77>zc<06><>{<7B>%7<><37>h#<23>x<EFBFBD><78><EFBFBD>Q<EFBFBD><51><EFBFBD>fXѻ?j<>K<EFBFBD><4B><0B><>S#8<>צ<EFBFBD><D7A6><EFBFBD>{<7B><>M<EFBFBD><4D>3<EFBFBD>> <20><><EFBFBD><EFBFBD><EFBFBD>6Z<36>Q<EFBFBD>m<EFBFBD><6D><07><0E>8<EFBFBD> | ||||||
										
											Binary file not shown.
										
									
								
							
										
											Binary file not shown.
										
									
								
							
										
											Binary file not shown.
										
									
								
							
										
											Binary file not shown.
										
									
								
							
										
											Binary file not shown.
										
									
								
							
							
								
								
									
										1
									
								
								modules/git/tests/repos/repo3_notes/refs/heads/master
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								modules/git/tests/repos/repo3_notes/refs/heads/master
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1 @@ | |||||||
|  | 3e668dbfac39cbc80a9ff9c61eb565d944453ba4 | ||||||
							
								
								
									
										1
									
								
								modules/git/tests/repos/repo3_notes/refs/notes/commits
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								modules/git/tests/repos/repo3_notes/refs/notes/commits
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1 @@ | |||||||
|  | 654c8b6b63c08bf37f638d3f521626b7fbbd4d37 | ||||||
		Reference in New Issue
	
	Block a user