8000 Add support for reading git notes by JakobDev · Pull Request #928 · go-git/go-git · GitHub
[go: up one dir, main page]

Skip to content

Add support for reading git notes #928

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Add support for reading git notes
  • Loading branch information
JakobDev committed Nov 16, 2023
commit 1d6fc434b3fd65f37120119615be9fbd54f96270
12 changes: 6 additions & 6 deletions COMPATIBILITY.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,12 @@ compatibility status with go-git.

## Advanced

| Feature | Sub-feature | Status | Notes | Examples |
| ---------- | ----------- | ----------- | ----- | -------- |
| `notes` | | | | |
| `replace` | | ❌ | | |
| `worktree` | | ❌ | | |
| `annotate` | | (see blame) | | |
| Feature | Sub-feature | Status | Notes | Examples |
| ---------- | ----------- | ----------- | ------------- | -------- |
| `notes` | | | Only reading | |
| `replace` | | ❌ | | |
| `worktree` | | ❌ | | |
| `annotate` | | (see blame) | | |

## GPG

Expand Down
46 changes: 46 additions & 0 deletions repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -1720,6 +1720,52 @@ func (r *Repository) resolveHashPrefix(hashStr string) []plumbing.Hash {
return hashes
}

// GetNote retuns the note for the given commit. returns a empty string, if the commit has no note.
func (r *Repository) GetNote(commit *object.Commit) (string, error) {
notes, err := r.Notes()
if err != nil {
return "", err
}

noteRef, err := notes.Next()
if err != nil {
if errors.Is(err, io.EOF) {
return "", nil
}
return "", err
}

noteCommit, err := r.CommitObject(noteRef.Hash())
if err != nil {
return "", err
}

tree, err := noteCommit.Tree()
if err != nil {
return "", err
}

fi, err := tree.File(commit.Hash.String())
if err != nil {
if errors.Is(err, object.ErrFileNotFound) {
return "", nil
}
return "", err
}

reader, err := fi.Blob.Reader()
if err != nil {
return "", err
}

by, err := io.ReadAll(reader)
if err != nil {
return "", err
}

return strings.TrimSpace(string(by)), nil
}

type RepackConfig struct {
// UseRefDeltas configures whether packfile encoder will use reference deltas.
// By default OFSDeltaObject is used.
Expand Down
17 changes: 17 additions & 0 deletions repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3333,3 +3333,20 @@ func BenchmarkPlainClone(b *testing.B) {
clone(b)
}
}

func (s *RepositorySuite) TestGetNoteNotExisting(c *C) {
r, _ := Init(memory.NewStorage(), nil)
err := r.clone(context.Background(), &CloneOptions{URL: s.GetBasicLocalRepositoryURL()})
c.Assert(err, IsNil)

head, err := r.Head()
c.Assert(err, IsNil)

commit, err := r.CommitObject(head.Hash())
c.Assert(err, IsNil)

note, err := r.GetNote(commit)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need a test case that also includes notes in.

You can create a sample repository with 1-2 commits with some notes. Then you submit its pack as a PR to https://github.com/go-git/go-git-fixtures.

That would enable you to refer to that pack via ByTag as we have in other parts of the code base.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that we got your changes merged in the other repo, we just need to update the go-git-fixtures version to the latest.

c.Assert(err, IsNil)

c.Check(note, Equals, "")
}
0