github.com/gitbundle/modules@v0.0.0-20231025071548-85b91c5c3b01/git/notes_nogogit.go (about) 1 // Copyright 2023 The GitBundle Inc. All rights reserved. 2 // Copyright 2017 The Gitea Authors. All rights reserved. 3 // Use of this source code is governed by a MIT-style 4 // license that can be found in the LICENSE file. 5 6 //go:build !gogit 7 8 package git 9 10 import ( 11 "context" 12 "io" 13 "strings" 14 15 "github.com/gitbundle/modules/log" 16 ) 17 18 // GetNote retrieves the git-notes data for a given commit. 19 // FIXME: Add LastCommitCache support 20 func GetNote(ctx context.Context, repo *Repository, commitID string, note *Note) error { 21 log.Trace("Searching for git note corresponding to the commit %q in the repository %q", commitID, repo.Path) 22 notes, err := repo.GetCommit(NotesRef) 23 if err != nil { 24 if IsErrNotExist(err) { 25 return err 26 } 27 log.Error("Unable to get commit from ref %q. Error: %v", NotesRef, err) 28 return err 29 } 30 31 path := "" 32 33 tree := ¬es.Tree 34 log.Trace("Found tree with ID %q while searching for git note corresponding to the commit %q", tree.ID, commitID) 35 36 var entry *TreeEntry 37 originalCommitID := commitID 38 for len(commitID) > 2 { 39 entry, err = tree.GetTreeEntryByPath(commitID) 40 if err == nil { 41 path += commitID 42 break 43 } 44 if IsErrNotExist(err) { 45 tree, err = tree.SubTree(commitID[0:2]) 46 path += commitID[0:2] + "/" 47 commitID = commitID[2:] 48 } 49 if err != nil { 50 // Err may have been updated by the SubTree we need to recheck if it's again an ErrNotExist 51 if !IsErrNotExist(err) { 52 log.Error("Unable to find git note corresponding to the commit %q. Error: %v", originalCommitID, err) 53 } 54 return err 55 } 56 } 57 58 blob := entry.Blob() 59 dataRc, err := blob.DataAsync() 60 if err != nil { 61 log.Error("Unable to read blob with ID %q. Error: %v", blob.ID, err) 62 return err 63 } 64 closed := false 65 defer func() { 66 if !closed { 67 _ = dataRc.Close() 68 } 69 }() 70 d, err := io.ReadAll(dataRc) 71 if err != nil { 72 log.Error("Unable to read blob with ID %q. Error: %v", blob.ID, err) 73 return err 74 } 75 _ = dataRc.Close() 76 closed = true 77 note.Message = d 78 79 treePath := "" 80 if idx := strings.LastIndex(path, "/"); idx > -1 { 81 treePath = path[:idx] 82 path = path[idx+1:] 83 } 84 85 lastCommits, err := GetLastCommitForPaths(ctx, nil, notes, treePath, []string{path}) 86 if err != nil { 87 log.Error("Unable to get the commit for the path %q. Error: %v", treePath, err) 88 return err 89 } 90 note.Commit = lastCommits[path] 91 92 return nil 93 }