2013-03-05 13:53:04 -06:00
|
|
|
package git
|
|
|
|
|
|
|
|
/*
|
|
|
|
#include <git2.h>
|
2015-01-04 11:05:11 -06:00
|
|
|
#include <git2/sys/openssl.h>
|
2013-03-05 13:53:04 -06:00
|
|
|
*/
|
|
|
|
import "C"
|
|
|
|
import (
|
2013-04-26 14:12:27 -05:00
|
|
|
"bytes"
|
2014-03-18 21:39:34 -05:00
|
|
|
"encoding/hex"
|
2013-05-21 08:14:26 -05:00
|
|
|
"errors"
|
2013-09-18 02:23:47 -05:00
|
|
|
"runtime"
|
2013-05-14 14:34:07 -05:00
|
|
|
"strings"
|
2014-02-26 12:41:20 -06:00
|
|
|
"unsafe"
|
2013-03-05 13:53:04 -06:00
|
|
|
)
|
|
|
|
|
2014-04-02 12:31:48 -05:00
|
|
|
type ErrorClass int
|
|
|
|
|
2013-03-05 13:53:04 -06:00
|
|
|
const (
|
2020-12-05 09:23:44 -06:00
|
|
|
ErrorClassNone ErrorClass = C.GIT_ERROR_NONE
|
|
|
|
ErrorClassNoMemory ErrorClass = C.GIT_ERROR_NOMEMORY
|
|
|
|
ErrorClassOS ErrorClass = C.GIT_ERROR_OS
|
|
|
|
ErrorClassInvalid ErrorClass = C.GIT_ERROR_INVALID
|
|
|
|
ErrorClassReference ErrorClass = C.GIT_ERROR_REFERENCE
|
|
|
|
ErrorClassZlib ErrorClass = C.GIT_ERROR_ZLIB
|
|
|
|
ErrorClassRepository ErrorClass = C.GIT_ERROR_REPOSITORY
|
|
|
|
ErrorClassConfig ErrorClass = C.GIT_ERROR_CONFIG
|
|
|
|
ErrorClassRegex ErrorClass = C.GIT_ERROR_REGEX
|
|
|
|
ErrorClassOdb ErrorClass = C.GIT_ERROR_ODB
|
|
|
|
ErrorClassIndex ErrorClass = C.GIT_ERROR_INDEX
|
|
|
|
ErrorClassObject ErrorClass = C.GIT_ERROR_OBJECT
|
|
|
|
ErrorClassNet ErrorClass = C.GIT_ERROR_NET
|
|
|
|
ErrorClassTag ErrorClass = C.GIT_ERROR_TAG
|
|
|
|
ErrorClassTree ErrorClass = C.GIT_ERROR_TREE
|
|
|
|
ErrorClassIndexer ErrorClass = C.GIT_ERROR_INDEXER
|
|
|
|
ErrorClassSSL ErrorClass = C.GIT_ERROR_SSL
|
|
|
|
ErrorClassSubmodule ErrorClass = C.GIT_ERROR_SUBMODULE
|
|
|
|
ErrorClassThread ErrorClass = C.GIT_ERROR_THREAD
|
|
|
|
ErrorClassStash ErrorClass = C.GIT_ERROR_STASH
|
|
|
|
ErrorClassCheckout ErrorClass = C.GIT_ERROR_CHECKOUT
|
|
|
|
ErrorClassFetchHead ErrorClass = C.GIT_ERROR_FETCHHEAD
|
|
|
|
ErrorClassMerge ErrorClass = C.GIT_ERROR_MERGE
|
|
|
|
ErrorClassSSH ErrorClass = C.GIT_ERROR_SSH
|
|
|
|
ErrorClassFilter ErrorClass = C.GIT_ERROR_FILTER
|
|
|
|
ErrorClassRevert ErrorClass = C.GIT_ERROR_REVERT
|
|
|
|
ErrorClassCallback ErrorClass = C.GIT_ERROR_CALLBACK
|
|
|
|
ErrorClassRebase ErrorClass = C.GIT_ERROR_REBASE
|
|
|
|
ErrorClassPatch ErrorClass = C.GIT_ERROR_PATCH
|
2013-03-05 13:53:04 -06:00
|
|
|
)
|
|
|
|
|
2014-04-02 12:31:48 -05:00
|
|
|
type ErrorCode int
|
|
|
|
|
|
|
|
const (
|
2020-12-05 09:23:44 -06:00
|
|
|
// ErrorCodeOK indicates that the operation completed successfully.
|
|
|
|
ErrorCodeOK ErrorCode = C.GIT_OK
|
|
|
|
|
|
|
|
// ErrorCodeGeneric represents a generic error.
|
|
|
|
ErrorCodeGeneric ErrorCode = C.GIT_ERROR
|
|
|
|
// ErrorCodeNotFound represents that the requested object could not be found
|
|
|
|
ErrorCodeNotFound ErrorCode = C.GIT_ENOTFOUND
|
|
|
|
// ErrorCodeExists represents that the object exists preventing operation.
|
|
|
|
ErrorCodeExists ErrorCode = C.GIT_EEXISTS
|
|
|
|
// ErrorCodeAmbiguous represents that more than one object matches.
|
|
|
|
ErrorCodeAmbiguous ErrorCode = C.GIT_EAMBIGUOUS
|
|
|
|
// ErrorCodeBuffs represents that the output buffer is too short to hold data.
|
|
|
|
ErrorCodeBuffs ErrorCode = C.GIT_EBUFS
|
|
|
|
|
|
|
|
// ErrorCodeUser is a special error that is never generated by libgit2
|
2014-04-04 02:14:31 -05:00
|
|
|
// code. You can return it from a callback (e.g to stop an iteration)
|
|
|
|
// to know that it was generated by the callback and not by libgit2.
|
2020-12-05 09:23:44 -06:00
|
|
|
ErrorCodeUser ErrorCode = C.GIT_EUSER
|
|
|
|
|
|
|
|
// ErrorCodeBareRepo represents that the operation not allowed on bare repository
|
|
|
|
ErrorCodeBareRepo ErrorCode = C.GIT_EBAREREPO
|
|
|
|
// ErrorCodeUnbornBranch represents that HEAD refers to branch with no commits.
|
|
|
|
ErrorCodeUnbornBranch ErrorCode = C.GIT_EUNBORNBRANCH
|
|
|
|
// ErrorCodeUnmerged represents that a merge in progress prevented operation.
|
|
|
|
ErrorCodeUnmerged ErrorCode = C.GIT_EUNMERGED
|
|
|
|
// ErrorCodeNonFastForward represents that the reference was not fast-forwardable.
|
|
|
|
ErrorCodeNonFastForward ErrorCode = C.GIT_ENONFASTFORWARD
|
|
|
|
// ErrorCodeInvalidSpec represents that the name/ref spec was not in a valid format.
|
|
|
|
ErrorCodeInvalidSpec ErrorCode = C.GIT_EINVALIDSPEC
|
|
|
|
// ErrorCodeConflict represents that checkout conflicts prevented operation.
|
|
|
|
ErrorCodeConflict ErrorCode = C.GIT_ECONFLICT
|
|
|
|
// ErrorCodeLocked represents that lock file prevented operation.
|
|
|
|
ErrorCodeLocked ErrorCode = C.GIT_ELOCKED
|
|
|
|
// ErrorCodeModified represents that the reference value does not match expected.
|
|
|
|
ErrorCodeModified ErrorCode = C.GIT_EMODIFIED
|
|
|
|
// ErrorCodeAuth represents that the authentication failed.
|
|
|
|
ErrorCodeAuth ErrorCode = C.GIT_EAUTH
|
|
|
|
// ErrorCodeCertificate represents that the server certificate is invalid.
|
|
|
|
ErrorCodeCertificate ErrorCode = C.GIT_ECERTIFICATE
|
|
|
|
// ErrorCodeApplied represents that the patch/merge has already been applied.
|
|
|
|
ErrorCodeApplied ErrorCode = C.GIT_EAPPLIED
|
|
|
|
// ErrorCodePeel represents that the requested peel operation is not possible.
|
|
|
|
ErrorCodePeel ErrorCode = C.GIT_EPEEL
|
|
|
|
// ErrorCodeEOF represents an unexpected EOF.
|
|
|
|
ErrorCodeEOF ErrorCode = C.GIT_EEOF
|
|
|
|
// ErrorCodeInvalid represents an invalid operation or input.
|
|
|
|
ErrorCodeInvalid ErrorCode = C.GIT_EINVALID
|
|
|
|
// ErrorCodeUIncommitted represents that uncommitted changes in index prevented operation.
|
|
|
|
ErrorCodeUncommitted ErrorCode = C.GIT_EUNCOMMITTED
|
|
|
|
// ErrorCodeDirectory represents that the operation is not valid for a directory.
|
|
|
|
ErrorCodeDirectory ErrorCode = C.GIT_EDIRECTORY
|
|
|
|
// ErrorCodeMergeConflict represents that a merge conflict exists and cannot continue.
|
|
|
|
ErrorCodeMergeConflict ErrorCode = C.GIT_EMERGECONFLICT
|
|
|
|
|
|
|
|
// ErrorCodePassthrough represents that a user-configured callback refused to act.
|
|
|
|
ErrorCodePassthrough ErrorCode = C.GIT_PASSTHROUGH
|
|
|
|
// ErrorCodeIterOver signals end of iteration with iterator.
|
|
|
|
ErrorCodeIterOver ErrorCode = C.GIT_ITEROVER
|
|
|
|
// ErrorCodeRetry is an internal-only error code.
|
|
|
|
ErrorCodeRetry ErrorCode = C.GIT_RETRY
|
|
|
|
// ErrorCodeMismatch represents a hashsum mismatch in object.
|
|
|
|
ErrorCodeMismatch ErrorCode = C.GIT_EMISMATCH
|
|
|
|
// ErrorCodeIndexDirty represents that unsaved changes in the index would be overwritten.
|
|
|
|
ErrorCodeIndexDirty ErrorCode = C.GIT_EINDEXDIRTY
|
|
|
|
// ErrorCodeApplyFail represents that a patch application failed.
|
|
|
|
ErrorCodeApplyFail ErrorCode = C.GIT_EAPPLYFAIL
|
2013-05-21 08:14:26 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2014-04-26 13:51:21 -05:00
|
|
|
ErrInvalid = errors.New("Invalid state for operation")
|
2013-05-21 08:14:26 -05:00
|
|
|
)
|
|
|
|
|
2015-04-21 06:14:22 -05:00
|
|
|
var pointerHandles *HandleList
|
|
|
|
|
2013-03-05 13:53:04 -06:00
|
|
|
func init() {
|
2020-09-29 17:42:20 -05:00
|
|
|
initLibGit2()
|
|
|
|
}
|
|
|
|
|
|
|
|
func initLibGit2() {
|
2015-04-21 06:14:22 -05:00
|
|
|
pointerHandles = NewHandleList()
|
|
|
|
|
2014-12-02 18:50:37 -06:00
|
|
|
C.git_libgit2_init()
|
|
|
|
|
2016-10-31 15:12:03 -05:00
|
|
|
// Due to the multithreaded nature of Go and its interaction with
|
|
|
|
// calling C functions, we cannot work with a library that was not built
|
|
|
|
// with multi-threading support. The most likely outcome is a segfault
|
|
|
|
// or panic at an incomprehensible time, so let's make it easy by
|
|
|
|
// panicking right here.
|
|
|
|
if Features()&FeatureThreads == 0 {
|
|
|
|
panic("libgit2 was not built with threading support")
|
|
|
|
}
|
|
|
|
|
2014-12-02 18:50:37 -06:00
|
|
|
// This is not something we should be doing, as we may be
|
|
|
|
// stomping all over someone else's setup. The user should do
|
|
|
|
// this themselves or use some binding/wrapper which does it
|
|
|
|
// in such a way that they can be sure they're the only ones
|
|
|
|
// setting it up.
|
|
|
|
C.git_openssl_set_locking()
|
2013-03-05 13:53:04 -06:00
|
|
|
}
|
|
|
|
|
2020-06-21 08:44:06 -05:00
|
|
|
// Shutdown frees all the resources acquired by libgit2. Make sure no
|
|
|
|
// references to any git2go objects are live before calling this.
|
|
|
|
// After this is called, invoking any function from this library will result in
|
|
|
|
// undefined behavior, so make sure this is called carefully.
|
|
|
|
func Shutdown() {
|
|
|
|
pointerHandles.Clear()
|
|
|
|
|
|
|
|
C.git_libgit2_shutdown()
|
|
|
|
}
|
|
|
|
|
2020-09-29 17:42:20 -05:00
|
|
|
// ReInit reinitializes the global state, this is useful if the effective user
|
|
|
|
// id has changed and you want to update the stored search paths for gitconfig
|
|
|
|
// files. This function frees any references to objects, so it should be called
|
|
|
|
// before any other functions are called.
|
|
|
|
func ReInit() {
|
|
|
|
Shutdown()
|
|
|
|
initLibGit2()
|
|
|
|
}
|
|
|
|
|
2014-03-18 21:11:41 -05:00
|
|
|
// Oid represents the id for a Git object.
|
2014-03-17 22:54:40 -05:00
|
|
|
type Oid [20]byte
|
2013-03-05 13:53:04 -06:00
|
|
|
|
|
|
|
func newOidFromC(coid *C.git_oid) *Oid {
|
2013-03-08 09:03:49 -06:00
|
|
|
if coid == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-03-05 13:53:04 -06:00
|
|
|
oid := new(Oid)
|
2014-03-17 22:54:40 -05:00
|
|
|
copy(oid[0:20], C.GoBytes(unsafe.Pointer(coid), 20))
|
2013-03-05 13:53:04 -06:00
|
|
|
return oid
|
|
|
|
}
|
|
|
|
|
2014-03-17 23:04:26 -05:00
|
|
|
func NewOidFromBytes(b []byte) *Oid {
|
2013-03-05 13:53:04 -06:00
|
|
|
oid := new(Oid)
|
2014-03-17 22:54:40 -05:00
|
|
|
copy(oid[0:20], b[0:20])
|
2013-03-05 13:53:04 -06:00
|
|
|
return oid
|
|
|
|
}
|
|
|
|
|
|
|
|
func (oid *Oid) toC() *C.git_oid {
|
2014-03-17 22:54:40 -05:00
|
|
|
return (*C.git_oid)(unsafe.Pointer(oid))
|
2013-03-05 13:53:04 -06:00
|
|
|
}
|
|
|
|
|
2014-03-17 23:04:26 -05:00
|
|
|
func NewOid(s string) (*Oid, error) {
|
2014-03-18 21:39:34 -05:00
|
|
|
if len(s) > C.GIT_OID_HEXSZ {
|
|
|
|
return nil, errors.New("string is too long for oid")
|
|
|
|
}
|
2013-03-05 13:53:04 -06:00
|
|
|
|
2014-03-18 21:39:34 -05:00
|
|
|
o := new(Oid)
|
2013-09-18 02:23:47 -05:00
|
|
|
|
2014-03-18 21:39:34 -05:00
|
|
|
slice, error := hex.DecodeString(s)
|
|
|
|
if error != nil {
|
|
|
|
return nil, error
|
2013-03-05 13:53:04 -06:00
|
|
|
}
|
|
|
|
|
2014-04-02 12:31:48 -05:00
|
|
|
if len(slice) != 20 {
|
2020-12-05 09:23:44 -06:00
|
|
|
return nil, &GitError{"Invalid Oid", ErrorClassNone, ErrGeneric}
|
2014-04-02 12:31:48 -05:00
|
|
|
}
|
|
|
|
|
2014-03-18 21:39:34 -05:00
|
|
|
copy(o[:], slice[:20])
|
2013-03-05 13:53:04 -06:00
|
|
|
return o, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (oid *Oid) String() string {
|
2014-03-18 21:39:34 -05:00
|
|
|
return hex.EncodeToString(oid[:])
|
2013-03-05 13:53:04 -06:00
|
|
|
}
|
|
|
|
|
2013-04-26 14:12:27 -05:00
|
|
|
func (oid *Oid) Cmp(oid2 *Oid) int {
|
2014-03-17 22:54:40 -05:00
|
|
|
return bytes.Compare(oid[:], oid2[:])
|
2013-04-26 14:12:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (oid *Oid) Copy() *Oid {
|
2018-02-15 12:01:14 -06:00
|
|
|
ret := *oid
|
|
|
|
return &ret
|
2013-04-26 14:12:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (oid *Oid) Equal(oid2 *Oid) bool {
|
2018-02-15 12:01:14 -06:00
|
|
|
return *oid == *oid2
|
2013-04-26 14:12:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (oid *Oid) IsZero() bool {
|
2018-02-15 12:01:14 -06:00
|
|
|
return *oid == Oid{}
|
2013-04-26 14:12:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (oid *Oid) NCmp(oid2 *Oid, n uint) int {
|
2014-03-17 22:54:40 -05:00
|
|
|
return bytes.Compare(oid[:n], oid2[:n])
|
2013-04-26 14:12:27 -05:00
|
|
|
}
|
|
|
|
|
2013-05-21 16:03:11 -05:00
|
|
|
func ShortenOids(ids []*Oid, minlen int) (int, error) {
|
|
|
|
shorten := C.git_oid_shorten_new(C.size_t(minlen))
|
|
|
|
if shorten == nil {
|
|
|
|
panic("Out of memory")
|
|
|
|
}
|
|
|
|
defer C.git_oid_shorten_free(shorten)
|
|
|
|
|
|
|
|
var ret C.int
|
2013-09-18 02:23:47 -05:00
|
|
|
|
|
|
|
runtime.LockOSThread()
|
|
|
|
defer runtime.UnlockOSThread()
|
|
|
|
|
2013-05-21 16:03:11 -05:00
|
|
|
for _, id := range ids {
|
|
|
|
buf := make([]byte, 41)
|
|
|
|
C.git_oid_fmt((*C.char)(unsafe.Pointer(&buf[0])), id.toC())
|
|
|
|
buf[40] = 0
|
|
|
|
ret = C.git_oid_shorten_add(shorten, (*C.char)(unsafe.Pointer(&buf[0])))
|
|
|
|
if ret < 0 {
|
2013-07-07 09:43:44 -05:00
|
|
|
return int(ret), MakeGitError(ret)
|
2013-05-21 16:03:11 -05:00
|
|
|
}
|
|
|
|
}
|
2017-07-08 04:38:19 -05:00
|
|
|
runtime.KeepAlive(ids)
|
2013-05-21 16:03:11 -05:00
|
|
|
return int(ret), nil
|
|
|
|
}
|
|
|
|
|
2013-03-05 13:53:04 -06:00
|
|
|
type GitError struct {
|
2014-04-02 12:31:48 -05:00
|
|
|
Message string
|
|
|
|
Class ErrorClass
|
|
|
|
Code ErrorCode
|
2013-03-05 13:53:04 -06:00
|
|
|
}
|
|
|
|
|
2014-01-29 17:01:26 -06:00
|
|
|
func (e GitError) Error() string {
|
2013-03-05 13:53:04 -06:00
|
|
|
return e.Message
|
|
|
|
}
|
|
|
|
|
2014-04-02 12:31:48 -05:00
|
|
|
func IsErrorClass(err error, c ErrorClass) bool {
|
|
|
|
if err == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if gitError, ok := err.(*GitError); ok {
|
|
|
|
return gitError.Class == c
|
|
|
|
}
|
|
|
|
return false
|
2013-07-07 09:43:44 -05:00
|
|
|
}
|
|
|
|
|
2014-04-02 12:31:48 -05:00
|
|
|
func IsErrorCode(err error, c ErrorCode) bool {
|
|
|
|
if err == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if gitError, ok := err.(*GitError); ok {
|
|
|
|
return gitError.Code == c
|
|
|
|
}
|
|
|
|
return false
|
2013-07-07 09:43:44 -05:00
|
|
|
}
|
|
|
|
|
2020-12-05 09:23:44 -06:00
|
|
|
func MakeGitError(c C.int) error {
|
2014-04-02 12:31:48 -05:00
|
|
|
var errMessage string
|
|
|
|
var errClass ErrorClass
|
2020-12-05 09:23:44 -06:00
|
|
|
errorCode := ErrorCode(c)
|
|
|
|
if errorCode != ErrorCodeIterOver {
|
|
|
|
err := C.git_error_last()
|
2014-04-02 12:31:48 -05:00
|
|
|
if err != nil {
|
|
|
|
errMessage = C.GoString(err.message)
|
|
|
|
errClass = ErrorClass(err.klass)
|
|
|
|
} else {
|
2020-12-05 09:23:44 -06:00
|
|
|
errClass = ErrorClassInvalid
|
2014-04-02 12:31:48 -05:00
|
|
|
}
|
2013-05-21 04:51:31 -05:00
|
|
|
}
|
2020-12-05 09:23:44 -06:00
|
|
|
if errMessage == "" {
|
|
|
|
errMessage = errorCode.String()
|
|
|
|
}
|
|
|
|
return &GitError{errMessage, errClass, errorCode}
|
2013-03-05 13:53:04 -06:00
|
|
|
}
|
|
|
|
|
2014-03-24 18:12:32 -05:00
|
|
|
func MakeGitError2(err int) error {
|
|
|
|
return MakeGitError(C.int(err))
|
|
|
|
}
|
|
|
|
|
2013-03-05 13:53:04 -06:00
|
|
|
func cbool(b bool) C.int {
|
2014-01-29 17:01:26 -06:00
|
|
|
if b {
|
2013-03-05 13:53:04 -06:00
|
|
|
return C.int(1)
|
|
|
|
}
|
|
|
|
return C.int(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
func ucbool(b bool) C.uint {
|
2014-01-29 17:01:26 -06:00
|
|
|
if b {
|
2013-03-05 13:53:04 -06:00
|
|
|
return C.uint(1)
|
|
|
|
}
|
|
|
|
return C.uint(0)
|
|
|
|
}
|
2013-05-14 14:34:07 -05:00
|
|
|
|
2020-12-05 15:13:59 -06:00
|
|
|
func setCallbackError(errorMessage **C.char, err error) C.int {
|
|
|
|
if err != nil {
|
|
|
|
*errorMessage = C.CString(err.Error())
|
|
|
|
if gitError, ok := err.(*GitError); ok {
|
|
|
|
return C.int(gitError.Code)
|
|
|
|
}
|
|
|
|
return C.int(ErrorCodeUser)
|
|
|
|
}
|
|
|
|
return C.int(ErrorCodeOK)
|
|
|
|
}
|
|
|
|
|
2013-05-14 14:34:07 -05:00
|
|
|
func Discover(start string, across_fs bool, ceiling_dirs []string) (string, error) {
|
|
|
|
ceildirs := C.CString(strings.Join(ceiling_dirs, string(C.GIT_PATH_LIST_SEPARATOR)))
|
|
|
|
defer C.free(unsafe.Pointer(ceildirs))
|
|
|
|
|
|
|
|
cstart := C.CString(start)
|
|
|
|
defer C.free(unsafe.Pointer(cstart))
|
|
|
|
|
2014-02-23 08:31:22 -06:00
|
|
|
var buf C.git_buf
|
2018-08-08 04:51:51 -05:00
|
|
|
defer C.git_buf_dispose(&buf)
|
2013-05-14 14:34:07 -05:00
|
|
|
|
2013-09-18 02:23:47 -05:00
|
|
|
runtime.LockOSThread()
|
|
|
|
defer runtime.UnlockOSThread()
|
|
|
|
|
2014-02-23 08:31:22 -06:00
|
|
|
ret := C.git_repository_discover(&buf, cstart, cbool(across_fs), ceildirs)
|
|
|
|
if ret < 0 {
|
2013-07-07 09:43:44 -05:00
|
|
|
return "", MakeGitError(ret)
|
2013-05-14 14:34:07 -05:00
|
|
|
}
|
|
|
|
|
2014-02-23 08:31:22 -06:00
|
|
|
return C.GoString(buf.ptr), nil
|
2013-05-14 14:34:07 -05:00
|
|
|
}
|