Create a NotesCollection for managing notes
As with the others, move these methods into their own namespace.
This commit is contained in:
parent
01a2d8d38d
commit
70c95a7655
121
note.go
121
note.go
|
@ -10,6 +10,127 @@ import (
|
|||
"unsafe"
|
||||
)
|
||||
|
||||
// This object represents the possible operations which can be
|
||||
// performed on the collection of notes for a repository.
|
||||
type NoteCollection struct {
|
||||
repo *Repository
|
||||
}
|
||||
|
||||
// Create adds a note for an object
|
||||
func (c *NoteCollection) Create(
|
||||
ref string, author, committer *Signature, id *Oid,
|
||||
note string, force bool) (*Oid, error) {
|
||||
|
||||
oid := new(Oid)
|
||||
|
||||
var cref *C.char
|
||||
if ref == "" {
|
||||
cref = nil
|
||||
} else {
|
||||
cref = C.CString(ref)
|
||||
defer C.free(unsafe.Pointer(cref))
|
||||
}
|
||||
|
||||
authorSig, err := author.toC()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer C.git_signature_free(authorSig)
|
||||
|
||||
committerSig, err := committer.toC()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer C.git_signature_free(committerSig)
|
||||
|
||||
cnote := C.CString(note)
|
||||
defer C.free(unsafe.Pointer(cnote))
|
||||
|
||||
runtime.LockOSThread()
|
||||
defer runtime.UnlockOSThread()
|
||||
|
||||
ret := C.git_note_create(
|
||||
oid.toC(), c.repo.ptr, cref, authorSig,
|
||||
committerSig, id.toC(), cnote, cbool(force))
|
||||
|
||||
if ret < 0 {
|
||||
return nil, MakeGitError(ret)
|
||||
}
|
||||
return oid, nil
|
||||
}
|
||||
|
||||
// Read reads the note for an object
|
||||
func (c *NoteCollection) Read(ref string, id *Oid) (*Note, error) {
|
||||
var cref *C.char
|
||||
if ref == "" {
|
||||
cref = nil
|
||||
} else {
|
||||
cref = C.CString(ref)
|
||||
defer C.free(unsafe.Pointer(cref))
|
||||
}
|
||||
|
||||
note := new(Note)
|
||||
|
||||
runtime.LockOSThread()
|
||||
defer runtime.UnlockOSThread()
|
||||
|
||||
if ret := C.git_note_read(¬e.ptr, c.repo.ptr, cref, id.toC()); ret < 0 {
|
||||
return nil, MakeGitError(ret)
|
||||
}
|
||||
|
||||
runtime.SetFinalizer(note, (*Note).Free)
|
||||
return note, nil
|
||||
}
|
||||
|
||||
// Remove removes the note for an object
|
||||
func (c *NoteCollection) Remove(ref string, author, committer *Signature, id *Oid) error {
|
||||
var cref *C.char
|
||||
if ref == "" {
|
||||
cref = nil
|
||||
} else {
|
||||
cref = C.CString(ref)
|
||||
defer C.free(unsafe.Pointer(cref))
|
||||
}
|
||||
|
||||
authorSig, err := author.toC()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer C.git_signature_free(authorSig)
|
||||
|
||||
committerSig, err := committer.toC()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer C.git_signature_free(committerSig)
|
||||
|
||||
runtime.LockOSThread()
|
||||
defer runtime.UnlockOSThread()
|
||||
|
||||
ret := C.git_note_remove(c.repo.ptr, cref, authorSig, committerSig, id.toC())
|
||||
if ret < 0 {
|
||||
return MakeGitError(ret)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// DefaultRef returns the default notes reference for a repository
|
||||
func (c *NoteCollection) DefaultRef() (string, error) {
|
||||
buf := C.git_buf{}
|
||||
|
||||
runtime.LockOSThread()
|
||||
defer runtime.UnlockOSThread()
|
||||
|
||||
if ret := C.git_note_default_ref(&buf, c.repo.ptr); ret < 0 {
|
||||
return "", MakeGitError(ret)
|
||||
}
|
||||
|
||||
ret := C.GoString(buf.ptr)
|
||||
C.git_buf_free(&buf)
|
||||
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
// Note
|
||||
type Note struct {
|
||||
ptr *C.git_note
|
||||
|
|
14
note_test.go
14
note_test.go
|
@ -53,7 +53,7 @@ func TestNoteIterator(t *testing.T) {
|
|||
break
|
||||
}
|
||||
|
||||
note, err := repo.ReadNote("", commitId)
|
||||
note, err := repo.Notes.Read("", commitId)
|
||||
checkFatal(t, err)
|
||||
|
||||
if !reflect.DeepEqual(note.Id(), noteId) {
|
||||
|
@ -73,13 +73,13 @@ func TestRemoveNote(t *testing.T) {
|
|||
|
||||
note, _ := createTestNote(t, repo, commit)
|
||||
|
||||
_, err = repo.ReadNote("", commit.Id())
|
||||
_, err = repo.Notes.Read("", commit.Id())
|
||||
checkFatal(t, err)
|
||||
|
||||
err = repo.RemoveNote("", note.Author(), note.Committer(), commitId)
|
||||
err = repo.Notes.Remove("", note.Author(), note.Committer(), commitId)
|
||||
checkFatal(t, err)
|
||||
|
||||
_, err = repo.ReadNote("", commit.Id())
|
||||
_, err = repo.Notes.Read("", commit.Id())
|
||||
if err == nil {
|
||||
t.Fatal("note remove failed")
|
||||
}
|
||||
|
@ -89,7 +89,7 @@ func TestDefaultNoteRef(t *testing.T) {
|
|||
repo := createTestRepo(t)
|
||||
defer cleanupTestRepo(t, repo)
|
||||
|
||||
ref, err := repo.DefaultNoteRef()
|
||||
ref, err := repo.Notes.DefaultRef()
|
||||
checkFatal(t, err)
|
||||
|
||||
compareStrings(t, "refs/notes/commits", ref)
|
||||
|
@ -103,10 +103,10 @@ func createTestNote(t *testing.T, repo *Repository, commit *Commit) (*Note, *Oid
|
|||
When: time.Date(2015, 01, 05, 13, 0, 0, 0, loc),
|
||||
}
|
||||
|
||||
noteId, err := repo.CreateNote("", sig, sig, commit.Id(), "I am a note\n", false)
|
||||
noteId, err := repo.Notes.Create("", sig, sig, commit.Id(), "I am a note\n", false)
|
||||
checkFatal(t, err)
|
||||
|
||||
note, err := repo.ReadNote("", commit.Id())
|
||||
note, err := repo.Notes.Read("", commit.Id())
|
||||
checkFatal(t, err)
|
||||
|
||||
return note, noteId
|
||||
|
|
119
repository.go
119
repository.go
|
@ -24,12 +24,16 @@ type Repository struct {
|
|||
// References represents the collection of references and can
|
||||
// be used to create, remove or update refernces for this repository.
|
||||
References ReferenceCollection
|
||||
// Notes represents the collection of notes and can be used to
|
||||
// read, write and delete notes from this repository.
|
||||
Notes NoteCollection
|
||||
}
|
||||
|
||||
func initRepositoryObject(repo *Repository) {
|
||||
repo.Remotes.repo = repo
|
||||
repo.Submodules.repo = repo
|
||||
repo.References.repo = repo
|
||||
repo.Notes.repo = repo
|
||||
runtime.SetFinalizer(repo, (*Repository).Free)
|
||||
}
|
||||
|
||||
|
@ -414,121 +418,6 @@ func (v *Repository) TreeBuilderFromTree(tree *Tree) (*TreeBuilder, error) {
|
|||
return bld, nil
|
||||
}
|
||||
|
||||
// CreateNote adds a note for an object
|
||||
func (v *Repository) CreateNote(
|
||||
ref string, author, committer *Signature, id *Oid,
|
||||
note string, force bool) (*Oid, error) {
|
||||
|
||||
oid := new(Oid)
|
||||
|
||||
var cref *C.char
|
||||
if ref == "" {
|
||||
cref = nil
|
||||
} else {
|
||||
cref = C.CString(ref)
|
||||
defer C.free(unsafe.Pointer(cref))
|
||||
}
|
||||
|
||||
authorSig, err := author.toC()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer C.git_signature_free(authorSig)
|
||||
|
||||
committerSig, err := committer.toC()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer C.git_signature_free(committerSig)
|
||||
|
||||
cnote := C.CString(note)
|
||||
defer C.free(unsafe.Pointer(cnote))
|
||||
|
||||
runtime.LockOSThread()
|
||||
defer runtime.UnlockOSThread()
|
||||
|
||||
ret := C.git_note_create(
|
||||
oid.toC(), v.ptr, cref, authorSig,
|
||||
committerSig, id.toC(), cnote, cbool(force))
|
||||
|
||||
if ret < 0 {
|
||||
return nil, MakeGitError(ret)
|
||||
}
|
||||
return oid, nil
|
||||
}
|
||||
|
||||
// ReadNote reads the note for an object
|
||||
func (v *Repository) ReadNote(ref string, id *Oid) (*Note, error) {
|
||||
var cref *C.char
|
||||
if ref == "" {
|
||||
cref = nil
|
||||
} else {
|
||||
cref = C.CString(ref)
|
||||
defer C.free(unsafe.Pointer(cref))
|
||||
}
|
||||
|
||||
note := new(Note)
|
||||
|
||||
runtime.LockOSThread()
|
||||
defer runtime.UnlockOSThread()
|
||||
|
||||
if ret := C.git_note_read(¬e.ptr, v.ptr, cref, id.toC()); ret < 0 {
|
||||
return nil, MakeGitError(ret)
|
||||
}
|
||||
|
||||
runtime.SetFinalizer(note, (*Note).Free)
|
||||
return note, nil
|
||||
}
|
||||
|
||||
// RemoveNote removes the note for an object
|
||||
func (v *Repository) RemoveNote(ref string, author, committer *Signature, id *Oid) error {
|
||||
var cref *C.char
|
||||
if ref == "" {
|
||||
cref = nil
|
||||
} else {
|
||||
cref = C.CString(ref)
|
||||
defer C.free(unsafe.Pointer(cref))
|
||||
}
|
||||
|
||||
authorSig, err := author.toC()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer C.git_signature_free(authorSig)
|
||||
|
||||
committerSig, err := committer.toC()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer C.git_signature_free(committerSig)
|
||||
|
||||
runtime.LockOSThread()
|
||||
defer runtime.UnlockOSThread()
|
||||
|
||||
ret := C.git_note_remove(v.ptr, cref, authorSig, committerSig, id.toC())
|
||||
if ret < 0 {
|
||||
return MakeGitError(ret)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// DefaultNoteRef returns the default notes reference for a repository
|
||||
func (v *Repository) DefaultNoteRef() (string, error) {
|
||||
buf := C.git_buf{}
|
||||
|
||||
runtime.LockOSThread()
|
||||
defer runtime.UnlockOSThread()
|
||||
|
||||
if ret := C.git_note_default_ref(&buf, v.ptr); ret < 0 {
|
||||
return "", MakeGitError(ret)
|
||||
}
|
||||
|
||||
ret := C.GoString(buf.ptr)
|
||||
C.git_buf_free(&buf)
|
||||
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
type RepositoryState int
|
||||
|
||||
const (
|
||||
|
|
Loading…
Reference in New Issue