// Copyright 2019 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

// +build !gogit

package git

import (
	"context"
	"io/ioutil"
	"strings"

	"code.gitea.io/gitea/modules/log"
)

// GetNote retrieves the git-notes data for a given commit.
func GetNote(ctx context.Context, repo *Repository, commitID string, note *Note) error {
	log.Trace("Searching for git note corresponding to the commit %q in the repository %q", commitID, repo.Path)
	notes, err := repo.GetCommit(NotesRef)
	if err != nil {
		log.Error("Unable to get commit from ref %q. Error: %v", NotesRef, err)
		return err
	}

	path := ""

	tree := &notes.Tree
	log.Trace("Found tree with ID %q while searching for git note corresponding to the commit %q", tree.ID, commitID)

	var entry *TreeEntry
	originalCommitID := commitID
	for len(commitID) > 2 {
		entry, err = tree.GetTreeEntryByPath(commitID)
		if err == nil {
			path += commitID
			break
		}
		if IsErrNotExist(err) {
			tree, err = tree.SubTree(commitID[0:2])
			path += commitID[0:2] + "/"
			commitID = commitID[2:]
		}
		if err != nil {
			log.Error("Unable to find git note corresponding to the commit %q. Error: %v", originalCommitID, err)
			return err
		}
	}

	blob := entry.Blob()
	dataRc, err := blob.DataAsync()
	if err != nil {
		log.Error("Unable to read blob with ID %q. Error: %v", blob.ID, err)
		return err
	}
	closed := false
	defer func() {
		if !closed {
			_ = dataRc.Close()
		}
	}()
	d, err := ioutil.ReadAll(dataRc)
	if err != nil {
		log.Error("Unable to read blob with ID %q. Error: %v", blob.ID, err)
		return err
	}
	_ = dataRc.Close()
	closed = true
	note.Message = d

	treePath := ""
	if idx := strings.LastIndex(path, "/"); idx > -1 {
		treePath = path[:idx]
		path = path[idx+1:]
	}

	lastCommits, err := GetLastCommitForPaths(ctx, notes, treePath, []string{path})
	if err != nil {
		log.Error("Unable to get the commit for the path %q. Error: %v", treePath, err)
		return err
	}
	note.Commit = lastCommits[path]

	return nil
}
ble>
<table class='tabs'><tr><td>
<a href='/gitea.git/about/'>about</a><a href='/gitea.git/'>summary</a><a href='/gitea.git/refs/?id=47289344e2ede03b7a74208aad3b92c8bca979c3'>refs</a><a href='/gitea.git/log/.revive.toml'>log</a><a class='active' href='/gitea.git/tree/.revive.toml?id=47289344e2ede03b7a74208aad3b92c8bca979c3'>tree</a><a href='/gitea.git/commit/.revive.toml?id=47289344e2ede03b7a74208aad3b92c8bca979c3'>commit</a><a href='/gitea.git/diff/.revive.toml?id=47289344e2ede03b7a74208aad3b92c8bca979c3'>diff</a><a href='/gitea.git/stats/.revive.toml'>stats</a></td><td class='form'><form class='right' method='get' action='/gitea.git/log/.revive.toml'>
<input type='hidden' name='id' value='47289344e2ede03b7a74208aad3b92c8bca979c3'/><select name='qt'>
<option value='grep'>log msg</option>
<option value='author'>author</option>
<option value='committer'>committer</option>
<option value='range'>range</option>
</select>
<input class='txt' type='search' size='10' name='q' value=''/>
<input type='submit' value='search'/>
</form>
</td></tr></table>
<div class='path'>path: <a href='/gitea.git/tree/?id=47289344e2ede03b7a74208aad3b92c8bca979c3'>root</a>/<a href='/gitea.git/tree/.revive.toml?id=47289344e2ede03b7a74208aad3b92c8bca979c3'>.revive.toml</a></div><div class='content'>blob: 74743d5ef4d4fa1f6454615b357306134bd910b5 (<a href='/gitea.git/plain/.revive.toml?id=47289344e2ede03b7a74208aad3b92c8bca979c3'>plain</a>)
<table summary='blob content' class='blob'>
<tr><td class='linenumbers'><pre><a id='n1' href='#n1'>1</a>
<a id='n2' href='#n2'>2</a>
<a id='n3' href='#n3'>3</a>
<a id='n4' href='#n4'>4</a>
<a id='n5' href='#n5'>5</a>
<a id='n6' href='#n6'>6</a>
<a id='n7' href='#n7'>7</a>
<a id='n8' href='#n8'>8</a>
<a id='n9' href='#n9'>9</a>
<a id='n10' href='#n10'>10</a>
<a id='n11' href='#n11'>11</a>
<a id='n12' href='#n12'>12</a>
<a id='n13' href='#n13'>13</a>
<a id='n14' href='#n14'>14</a>
<a id='n15' href='#n15'>15</a>
<a id='n16' href='#n16'>16</a>
<a id='n17' href='#n17'>17</a>
<a id='n18' href='#n18'>18</a>
<a id='n19' href='#n19'>19</a>
<a id='n20' href='#n20'>20</a>
<a id='n21' href='#n21'>21</a>
<a id='n22' href='#n22'>22</a>
<a id='n23' href='#n23'>23</a>
<a id='n24' href='#n24'>24</a>
<a id='n25' href='#n25'>25</a>
</pre></td>
<td class='lines'><pre><code>