Make all non-user-creatable structures non-comparable (#802) #805

Merged
lhchavez merged 1 commits from cherry-pick-802-release-0.27 into release-0.27 2021-09-05 16:06:59 -05:00
32 changed files with 63 additions and 8 deletions

View File

@ -85,6 +85,7 @@ func (v *Repository) BlameFile(path string, opts *BlameOptions) (*Blame, error)
} }
type Blame struct { type Blame struct {
doNotCompare
ptr *C.git_blame ptr *C.git_blame
} }

View File

@ -15,6 +15,7 @@ import (
) )
type Blob struct { type Blob struct {
doNotCompare
Object Object
cast_ptr *C.git_blob cast_ptr *C.git_blob
} }
@ -96,6 +97,7 @@ func (repo *Repository) CreateFromStream(hintPath string) (*BlobWriteStream, err
} }
type BlobWriteStream struct { type BlobWriteStream struct {
doNotCompare
ptr *C.git_writestream ptr *C.git_writestream
repo *Repository repo *Repository
} }

View File

@ -19,6 +19,7 @@ const (
) )
type Branch struct { type Branch struct {
doNotCompare
*Reference *Reference
} }
@ -27,6 +28,7 @@ func (r *Reference) Branch() *Branch {
} }
type BranchIterator struct { type BranchIterator struct {
doNotCompare
ptr *C.git_branch_iterator ptr *C.git_branch_iterator
repo *Repository repo *Repository
} }

View File

@ -22,6 +22,7 @@ const (
// Commit // Commit
type Commit struct { type Commit struct {
doNotCompare
Object Object
cast_ptr *C.git_commit cast_ptr *C.git_commit
} }

View File

@ -52,6 +52,7 @@ func newConfigEntryFromC(centry *C.git_config_entry) *ConfigEntry {
} }
type Config struct { type Config struct {
doNotCompare
ptr *C.git_config ptr *C.git_config
} }
@ -361,6 +362,7 @@ func OpenOndisk(parent *Config, path string) (*Config, error) {
} }
type ConfigIterator struct { type ConfigIterator struct {
doNotCompare
ptr *C.git_config_iterator ptr *C.git_config_iterator
cfg *Config cfg *Config
} }

View File

@ -23,6 +23,7 @@ const (
) )
type Cred struct { type Cred struct {
doNotCompare
ptr *C.git_cred ptr *C.git_cred
} }
@ -38,7 +39,7 @@ func (o *Cred) Type() CredType {
} }
func credFromC(ptr *C.git_cred) *Cred { func credFromC(ptr *C.git_cred) *Cred {
return &Cred{ptr} return &Cred{ptr: ptr}
} }
func NewCredUserpassPlaintext(username string, password string) (int, Cred) { func NewCredUserpassPlaintext(username string, password string) (int, Cred) {

View File

@ -177,7 +177,7 @@ type SubmoduleCbk = SubmoduleCallback
// Deprecated: SubmoduleVisitor is not used. // Deprecated: SubmoduleVisitor is not used.
func SubmoduleVisitor(csub unsafe.Pointer, name *C.char, handle unsafe.Pointer) C.int { func SubmoduleVisitor(csub unsafe.Pointer, name *C.char, handle unsafe.Pointer) C.int {
sub := &Submodule{(*C.git_submodule)(csub), nil} sub := &Submodule{ptr: (*C.git_submodule)(csub)}
callback, ok := pointerHandles.Get(handle).(SubmoduleCallback) callback, ok := pointerHandles.Get(handle).(SubmoduleCallback)
if !ok { if !ok {

View File

@ -176,6 +176,7 @@ func (repo *Repository) DescribeWorkdir(opts *DescribeOptions) (*DescribeResult,
// //
// Use Format() to get a string out of it. // Use Format() to get a string out of it.
type DescribeResult struct { type DescribeResult struct {
doNotCompare
ptr *C.git_describe_result ptr *C.git_describe_result
} }

View File

@ -131,6 +131,7 @@ func diffLineFromC(line *C.git_diff_line) DiffLine {
} }
type Diff struct { type Diff struct {
doNotCompare
ptr *C.git_diff ptr *C.git_diff
repo *Repository repo *Repository
runFinalizer bool runFinalizer bool
@ -218,6 +219,7 @@ func (diff *Diff) FindSimilar(opts *DiffFindOptions) error {
} }
type DiffStats struct { type DiffStats struct {
doNotCompare
ptr *C.git_diff_stats ptr *C.git_diff_stats
} }

4
git.go
View File

@ -119,6 +119,10 @@ var (
ErrInvalid = errors.New("Invalid state for operation") ErrInvalid = errors.New("Invalid state for operation")
) )
// doNotCompare is an idiomatic way of making structs non-comparable to avoid
// future field additions to make them non-comparable.
type doNotCompare [0]func()
var pointerHandles *HandleList var pointerHandles *HandleList
func init() { func init() {

View File

@ -11,6 +11,7 @@ import (
) )
type HandleList struct { type HandleList struct {
doNotCompare
sync.RWMutex sync.RWMutex
// stores the Go pointers // stores the Go pointers
handles map[unsafe.Pointer]interface{} handles map[unsafe.Pointer]interface{}

View File

@ -52,6 +52,7 @@ const (
) )
type Index struct { type Index struct {
doNotCompare
ptr *C.git_index ptr *C.git_index
repo *Repository repo *Repository
} }
@ -108,7 +109,7 @@ func freeCIndexEntry(entry *C.git_index_entry) {
} }
func newIndexFromC(ptr *C.git_index, repo *Repository) *Index { func newIndexFromC(ptr *C.git_index, repo *Repository) *Index {
idx := &Index{ptr, repo} idx := &Index{ptr: ptr, repo: repo}
runtime.SetFinalizer(idx, (*Index).Free) runtime.SetFinalizer(idx, (*Index).Free)
return idx return idx
} }
@ -616,6 +617,7 @@ func (v *Index) RemoveConflict(path string) error {
} }
type IndexConflictIterator struct { type IndexConflictIterator struct {
doNotCompare
ptr *C.git_index_conflict_iterator ptr *C.git_index_conflict_iterator
index *Index index *Index
} }

View File

@ -19,6 +19,7 @@ import (
// Indexer can post-process packfiles and create an .idx file for efficient // Indexer can post-process packfiles and create an .idx file for efficient
// lookup. // lookup.
type Indexer struct { type Indexer struct {
doNotCompare
ptr *C.git_indexer ptr *C.git_indexer
stats C.git_transfer_progress stats C.git_transfer_progress
ccallbacks C.git_remote_callbacks ccallbacks C.git_remote_callbacks

View File

@ -18,6 +18,7 @@ import (
// Mempack is a custom ODB backend that permits packing object in-memory. // Mempack is a custom ODB backend that permits packing object in-memory.
type Mempack struct { type Mempack struct {
doNotCompare
ptr *C.git_odb_backend ptr *C.git_odb_backend
} }

View File

@ -17,6 +17,7 @@ import (
) )
type AnnotatedCommit struct { type AnnotatedCommit struct {
doNotCompare
ptr *C.git_annotated_commit ptr *C.git_annotated_commit
r *Repository r *Repository
} }
@ -426,11 +427,12 @@ func (r *Repository) MergeBaseOctopus(oids []*Oid) (*Oid, error) {
} }
type MergeFileResult struct { type MergeFileResult struct {
doNotCompare
ptr *C.git_merge_file_result
Automergeable bool Automergeable bool
Path string Path string
Mode uint Mode uint
Contents []byte Contents []byte
ptr *C.git_merge_file_result
} }
func newMergeFileResultFromC(c *C.git_merge_file_result) *MergeFileResult { func newMergeFileResultFromC(c *C.git_merge_file_result) *MergeFileResult {

View File

@ -13,6 +13,7 @@ import (
// This object represents the possible operations which can be // This object represents the possible operations which can be
// performed on the collection of notes for a repository. // performed on the collection of notes for a repository.
type NoteCollection struct { type NoteCollection struct {
doNotCompare
repo *Repository repo *Repository
} }
@ -139,6 +140,7 @@ func (c *NoteCollection) DefaultRef() (string, error) {
// Note // Note
type Note struct { type Note struct {
doNotCompare
ptr *C.git_note ptr *C.git_note
r *Repository r *Repository
} }
@ -189,6 +191,7 @@ func (n *Note) Message() string {
// NoteIterator // NoteIterator
type NoteIterator struct { type NoteIterator struct {
doNotCompare
ptr *C.git_note_iterator ptr *C.git_note_iterator
r *Repository r *Repository
} }

View File

@ -22,6 +22,7 @@ const (
) )
type Object struct { type Object struct {
doNotCompare
ptr *C.git_object ptr *C.git_object
repo *Repository repo *Repository
} }

8
odb.go
View File

@ -22,10 +22,12 @@ import (
) )
type Odb struct { type Odb struct {
doNotCompare
ptr *C.git_odb ptr *C.git_odb
} }
type OdbBackend struct { type OdbBackend struct {
doNotCompare
ptr *C.git_odb_backend ptr *C.git_odb_backend
} }
@ -45,7 +47,7 @@ func NewOdb() (odb *Odb, err error) {
} }
func NewOdbBackendFromC(ptr unsafe.Pointer) (backend *OdbBackend) { func NewOdbBackendFromC(ptr unsafe.Pointer) (backend *OdbBackend) {
backend = &OdbBackend{(*C.git_odb_backend)(ptr)} backend = &OdbBackend{ptr: (*C.git_odb_backend)(ptr)}
return backend return backend
} }
@ -313,6 +315,7 @@ func (v *OdbBackend) Free() {
} }
type OdbObject struct { type OdbObject struct {
doNotCompare
ptr *C.git_odb_object ptr *C.git_odb_object
} }
@ -356,6 +359,7 @@ func (object *OdbObject) Data() (data []byte) {
} }
type OdbReadStream struct { type OdbReadStream struct {
doNotCompare
ptr *C.git_odb_stream ptr *C.git_odb_stream
Size uint64 Size uint64
Type ObjectType Type ObjectType
@ -396,6 +400,7 @@ func (stream *OdbReadStream) Free() {
} }
type OdbWriteStream struct { type OdbWriteStream struct {
doNotCompare
ptr *C.git_odb_stream ptr *C.git_odb_stream
Id Oid Id Oid
} }
@ -440,6 +445,7 @@ func (stream *OdbWriteStream) Free() {
// OdbWritepack is a stream to write a packfile to the ODB. // OdbWritepack is a stream to write a packfile to the ODB.
type OdbWritepack struct { type OdbWritepack struct {
doNotCompare
ptr *C.git_odb_writepack ptr *C.git_odb_writepack
stats C.git_transfer_progress stats C.git_transfer_progress
ccallbacks C.git_remote_callbacks ccallbacks C.git_remote_callbacks

View File

@ -16,6 +16,7 @@ import (
) )
type Packbuilder struct { type Packbuilder struct {
doNotCompare
ptr *C.git_packbuilder ptr *C.git_packbuilder
r *Repository r *Repository
} }

View File

@ -10,6 +10,7 @@ import (
) )
type Patch struct { type Patch struct {
doNotCompare
ptr *C.git_patch ptr *C.git_patch
} }

View File

@ -142,6 +142,7 @@ func mapEmptyStringToNull(ref string) *C.char {
// Rebase is the struct representing a Rebase object. // Rebase is the struct representing a Rebase object.
type Rebase struct { type Rebase struct {
doNotCompare
ptr *C.git_rebase ptr *C.git_rebase
r *Repository r *Repository
options *C.git_rebase_options options *C.git_rebase_options

View File

@ -13,11 +13,13 @@ import (
) )
type Refdb struct { type Refdb struct {
doNotCompare
ptr *C.git_refdb ptr *C.git_refdb
r *Repository r *Repository
} }
type RefdbBackend struct { type RefdbBackend struct {
doNotCompare
ptr *C.git_refdb_backend ptr *C.git_refdb_backend
} }
@ -38,7 +40,7 @@ func (v *Repository) NewRefdb() (refdb *Refdb, err error) {
} }
func NewRefdbBackendFromC(ptr unsafe.Pointer) (backend *RefdbBackend) { func NewRefdbBackendFromC(ptr unsafe.Pointer) (backend *RefdbBackend) {
backend = &RefdbBackend{(*C.git_refdb_backend)(ptr)} backend = &RefdbBackend{ptr: (*C.git_refdb_backend)(ptr)}
return backend return backend
} }

View File

@ -17,11 +17,13 @@ const (
) )
type Reference struct { type Reference struct {
doNotCompare
ptr *C.git_reference ptr *C.git_reference
repo *Repository repo *Repository
} }
type ReferenceCollection struct { type ReferenceCollection struct {
doNotCompare
repo *Repository repo *Repository
} }
@ -363,11 +365,13 @@ func (v *Reference) Free() {
} }
type ReferenceIterator struct { type ReferenceIterator struct {
doNotCompare
ptr *C.git_reference_iterator ptr *C.git_reference_iterator
repo *Repository repo *Repository
} }
type ReferenceNameIterator struct { type ReferenceNameIterator struct {
doNotCompare
*ReferenceIterator *ReferenceIterator
} }
@ -422,7 +426,7 @@ func (repo *Repository) NewReferenceIteratorGlob(glob string) (*ReferenceIterato
} }
func (i *ReferenceIterator) Names() *ReferenceNameIterator { func (i *ReferenceIterator) Names() *ReferenceNameIterator {
return &ReferenceNameIterator{i} return &ReferenceNameIterator{ReferenceIterator: i}
} }
// NextName retrieves the next reference name. If the iteration is over, // NextName retrieves the next reference name. If the iteration is over,

View File

@ -150,6 +150,7 @@ type ProxyOptions struct {
} }
type Remote struct { type Remote struct {
doNotCompare
ptr *C.git_remote ptr *C.git_remote
callbacks RemoteCallbacks callbacks RemoteCallbacks
repo *Repository repo *Repository
@ -498,6 +499,7 @@ func (r *Remote) Free() {
} }
type RemoteCollection struct { type RemoteCollection struct {
doNotCompare
repo *Repository repo *Repository
} }

View File

@ -14,6 +14,7 @@ import (
// Repository // Repository
type Repository struct { type Repository struct {
doNotCompare
ptr *C.git_repository ptr *C.git_repository
// Remotes represents the collection of remotes and can be // Remotes represents the collection of remotes and can be
// used to add, remove and configure remotes for this // used to add, remove and configure remotes for this

View File

@ -20,6 +20,7 @@ const (
) )
type Revspec struct { type Revspec struct {
doNotCompare
to *Object to *Object
from *Object from *Object
flags RevparseFlag flags RevparseFlag

View File

@ -35,6 +35,7 @@ const (
// StashCollection represents the possible operations that can be // StashCollection represents the possible operations that can be
// performed on the collection of stashes for a repository. // performed on the collection of stashes for a repository.
type StashCollection struct { type StashCollection struct {
doNotCompare
repo *Repository repo *Repository
} }

View File

@ -55,6 +55,7 @@ func statusEntryFromC(statusEntry *C.git_status_entry) StatusEntry {
} }
type StatusList struct { type StatusList struct {
doNotCompare
ptr *C.git_status_list ptr *C.git_status_list
r *Repository r *Repository
} }

View File

@ -21,6 +21,7 @@ type SubmoduleUpdateOptions struct {
// Submodule // Submodule
type Submodule struct { type Submodule struct {
doNotCompare
ptr *C.git_submodule ptr *C.git_submodule
r *Repository r *Repository
} }
@ -82,6 +83,7 @@ const (
) )
type SubmoduleCollection struct { type SubmoduleCollection struct {
doNotCompare
repo *Repository repo *Repository
} }
@ -117,7 +119,7 @@ type submoduleCallbackData struct {
//export submoduleCallback //export submoduleCallback
func submoduleCallback(csub unsafe.Pointer, name *C.char, handle unsafe.Pointer) C.int { func submoduleCallback(csub unsafe.Pointer, name *C.char, handle unsafe.Pointer) C.int {
sub := &Submodule{(*C.git_submodule)(csub), nil} sub := &Submodule{ptr: (*C.git_submodule)(csub)}
data, ok := pointerHandles.Get(handle).(submoduleCallbackData) data, ok := pointerHandles.Get(handle).(submoduleCallbackData)
if !ok { if !ok {

2
tag.go
View File

@ -13,6 +13,7 @@ import (
// Tag // Tag
type Tag struct { type Tag struct {
doNotCompare
Object Object
cast_ptr *C.git_tag cast_ptr *C.git_tag
} }
@ -64,6 +65,7 @@ func (t *Tag) TargetType() ObjectType {
} }
type TagsCollection struct { type TagsCollection struct {
doNotCompare
repo *Repository repo *Repository
} }

View File

@ -24,6 +24,7 @@ const (
) )
type Tree struct { type Tree struct {
doNotCompare
Object Object
cast_ptr *C.git_tree cast_ptr *C.git_tree
} }
@ -167,6 +168,7 @@ func (t *Tree) Walk(callback TreeWalkCallback) error {
} }
type TreeBuilder struct { type TreeBuilder struct {
doNotCompare
ptr *C.git_treebuilder ptr *C.git_treebuilder
repo *Repository repo *Repository
} }

View File

@ -22,6 +22,7 @@ const (
) )
type RevWalk struct { type RevWalk struct {
doNotCompare
ptr *C.git_revwalk ptr *C.git_revwalk
repo *Repository repo *Repository
} }