code.gitea.io/gitea@v1.19.3/modules/git/notes_nogogit.go (about)

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