github.com/gitbundle/modules@v0.0.0-20231025071548-85b91c5c3b01/git/notes_gogit.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  
    14  	"github.com/gitbundle/modules/log"
    15  
    16  	"github.com/go-git/go-git/v5/plumbing/object"
    17  )
    18  
    19  // GetNote retrieves the git-notes data for a given commit.
    20  // FIXME: Add LastCommitCache support
    21  func GetNote(ctx context.Context, repo *Repository, commitID string, note *Note) error {
    22  	log.Trace("Searching for git note corresponding to the commit %q in the repository %q", commitID, repo.Path)
    23  	notes, err := repo.GetCommit(NotesRef)
    24  	if err != nil {
    25  		if IsErrNotExist(err) {
    26  			return err
    27  		}
    28  		log.Error("Unable to get commit from ref %q. Error: %v", NotesRef, err)
    29  		return err
    30  	}
    31  
    32  	remainingCommitID := commitID
    33  	path := ""
    34  	currentTree := notes.Tree.gogitTree
    35  	log.Trace("Found tree with ID %q while searching for git note corresponding to the commit %q", currentTree.Entries[0].Name, commitID)
    36  	var file *object.File
    37  	for len(remainingCommitID) > 2 {
    38  		file, err = currentTree.File(remainingCommitID)
    39  		if err == nil {
    40  			path += remainingCommitID
    41  			break
    42  		}
    43  		if err == object.ErrFileNotFound {
    44  			currentTree, err = currentTree.Tree(remainingCommitID[0:2])
    45  			path += remainingCommitID[0:2] + "/"
    46  			remainingCommitID = remainingCommitID[2:]
    47  		}
    48  		if err != nil {
    49  			if err == object.ErrDirectoryNotFound {
    50  				return ErrNotExist{ID: remainingCommitID, RelPath: path}
    51  			}
    52  			log.Error("Unable to find git note corresponding to the commit %q. Error: %v", commitID, err)
    53  			return err
    54  		}
    55  	}
    56  
    57  	blob := file.Blob
    58  	dataRc, err := blob.Reader()
    59  	if err != nil {
    60  		log.Error("Unable to read blob with ID %q. Error: %v", blob.ID, err)
    61  		return err
    62  	}
    63  
    64  	defer dataRc.Close()
    65  	d, err := io.ReadAll(dataRc)
    66  	if err != nil {
    67  		log.Error("Unable to read blob with ID %q. Error: %v", blob.ID, err)
    68  		return err
    69  	}
    70  	note.Message = d
    71  
    72  	commitNodeIndex, commitGraphFile := repo.CommitNodeIndex()
    73  	if commitGraphFile != nil {
    74  		defer commitGraphFile.Close()
    75  	}
    76  
    77  	commitNode, err := commitNodeIndex.Get(notes.ID)
    78  	if err != nil {
    79  		return err
    80  	}
    81  
    82  	lastCommits, err := GetLastCommitForPaths(ctx, nil, commitNode, "", []string{path})
    83  	if err != nil {
    84  		log.Error("Unable to get the commit for the path %q. Error: %v", path, err)
    85  		return err
    86  	}
    87  	note.Commit = convertCommit(lastCommits[path])
    88  
    89  	return nil
    90  }