Merge remote-tracking branch 'upstream/master' into mempack

This commit is contained in:
lhchavez 2019-01-08 02:01:44 +00:00
commit b609c04b68
28 changed files with 204 additions and 83 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/static-build/

View File

@ -208,7 +208,7 @@ func (repo *Repository) RemoteName(canonicalBranchName string) (string, error) {
if ret < 0 {
return "", MakeGitError(ret)
}
defer C.git_buf_free(&nameBuf)
defer C.git_buf_dispose(&nameBuf)
return C.GoString(nameBuf.ptr), nil
}
@ -256,7 +256,7 @@ func (repo *Repository) UpstreamName(canonicalBranchName string) (string, error)
if ret < 0 {
return "", MakeGitError(ret)
}
defer C.git_buf_free(&nameBuf)
defer C.git_buf_dispose(&nameBuf)
return C.GoString(nameBuf.ptr), nil
}

View File

@ -35,7 +35,7 @@ const (
CheckoutDontUpdateIndex CheckoutStrategy = C.GIT_CHECKOUT_DONT_UPDATE_INDEX // Normally checkout updates index entries as it goes; this stops that
CheckoutNoRefresh CheckoutStrategy = C.GIT_CHECKOUT_NO_REFRESH // Don't refresh index/config/etc before doing checkout
CheckoutSkipUnmerged CheckoutStrategy = C.GIT_CHECKOUT_SKIP_UNMERGED // Allow checkout to skip unmerged files
CheckoutUserOurs CheckoutStrategy = C.GIT_CHECKOUT_USE_OURS // For unmerged files, checkout stage 2 from index
CheckoutUseOurs CheckoutStrategy = C.GIT_CHECKOUT_USE_OURS // For unmerged files, checkout stage 2 from index
CheckoutUseTheirs CheckoutStrategy = C.GIT_CHECKOUT_USE_THEIRS // For unmerged files, checkout stage 3 from index
CheckoutDisablePathspecMatch CheckoutStrategy = C.GIT_CHECKOUT_DISABLE_PATHSPEC_MATCH // Treat pathspec as simple list of exact match file paths
CheckoutSkipLockedDirectories CheckoutStrategy = C.GIT_CHECKOUT_SKIP_LOCKED_DIRECTORIES // Ignore directories in use, they will be left empty

View File

@ -37,10 +37,10 @@ func (c *Commit) RawMessage() string {
func (c *Commit) ExtractSignature() (string, string, error) {
var c_signed C.git_buf
defer C.git_buf_free(&c_signed)
defer C.git_buf_dispose(&c_signed)
var c_signature C.git_buf
defer C.git_buf_free(&c_signature)
defer C.git_buf_dispose(&c_signature)
oid := c.Id()
repo := C.git_commit_owner(c.cast_ptr)

View File

@ -134,7 +134,7 @@ func (c *Config) LookupString(name string) (string, error) {
if ret < 0 {
return "", MakeGitError(ret)
}
defer C.git_buf_free(&valBuf)
defer C.git_buf_dispose(&valBuf)
return C.GoString(valBuf.ptr), nil
}
@ -344,7 +344,7 @@ func (c *Config) OpenLevel(parent *Config, level ConfigLevel) (*Config, error) {
}
// OpenOndisk creates a new config instance containing a single on-disk file
func OpenOndisk(parent *Config, path string) (*Config, error) {
func OpenOndisk(path string) (*Config, error) {
cpath := C.CString(path)
defer C.free(unsafe.Pointer(cpath))
@ -390,7 +390,7 @@ func (iter *ConfigIterator) Free() {
func ConfigFindGlobal() (string, error) {
var buf C.git_buf
defer C.git_buf_free(&buf)
defer C.git_buf_dispose(&buf)
runtime.LockOSThread()
defer runtime.UnlockOSThread()
@ -405,7 +405,7 @@ func ConfigFindGlobal() (string, error) {
func ConfigFindSystem() (string, error) {
var buf C.git_buf
defer C.git_buf_free(&buf)
defer C.git_buf_dispose(&buf)
runtime.LockOSThread()
defer runtime.UnlockOSThread()
@ -420,7 +420,7 @@ func ConfigFindSystem() (string, error) {
func ConfigFindXDG() (string, error) {
var buf C.git_buf
defer C.git_buf_free(&buf)
defer C.git_buf_dispose(&buf)
runtime.LockOSThread()
defer runtime.UnlockOSThread()
@ -438,7 +438,7 @@ func ConfigFindXDG() (string, error) {
// Look for the file in %PROGRAMDATA%\Git\config used by portable git.
func ConfigFindProgramdata() (string, error) {
var buf C.git_buf
defer C.git_buf_free(&buf)
defer C.git_buf_dispose(&buf)
runtime.LockOSThread()
defer runtime.UnlockOSThread()

View File

@ -13,7 +13,7 @@ func setupConfig() (*Config, error) {
err error
)
c, err = OpenOndisk(nil, tempConfig)
c, err = OpenOndisk(tempConfig)
if err != nil {
return nil, err
}

View File

@ -212,7 +212,7 @@ func (result *DescribeResult) Format(opts *DescribeFormatOptions) (string, error
if ecode < 0 {
return "", MakeGitError(ecode)
}
defer C.git_buf_free(&resultBuf)
defer C.git_buf_dispose(&resultBuf)
return C.GoString(resultBuf.ptr), nil
}

View File

@ -246,7 +246,7 @@ const (
func (stats *DiffStats) String(format DiffStatsFormat,
width uint) (string, error) {
buf := C.git_buf{}
defer C.git_buf_free(&buf)
defer C.git_buf_dispose(&buf)
runtime.LockOSThread()
defer runtime.UnlockOSThread()

16
git.go
View File

@ -189,22 +189,16 @@ func (oid *Oid) Cmp(oid2 *Oid) int {
}
func (oid *Oid) Copy() *Oid {
ret := new(Oid)
copy(ret[:], oid[:])
return ret
ret := *oid
return &ret
}
func (oid *Oid) Equal(oid2 *Oid) bool {
return bytes.Equal(oid[:], oid2[:])
return *oid == *oid2
}
func (oid *Oid) IsZero() bool {
for _, a := range oid {
if a != 0 {
return false
}
}
return true
return *oid == Oid{}
}
func (oid *Oid) NCmp(oid2 *Oid, n uint) int {
@ -309,7 +303,7 @@ func Discover(start string, across_fs bool, ceiling_dirs []string) (string, erro
defer C.free(unsafe.Pointer(cstart))
var buf C.git_buf
defer C.git_buf_free(&buf)
defer C.git_buf_dispose(&buf)
runtime.LockOSThread()
defer runtime.UnlockOSThread()

View File

@ -3,10 +3,9 @@
package git
/*
#cgo CFLAGS: -I${SRCDIR}/vendor/libgit2/include
#cgo LDFLAGS: -L${SRCDIR}/vendor/libgit2/build/ -lgit2
#cgo windows LDFLAGS: -lwinhttp
#cgo !windows pkg-config: --static ${SRCDIR}/vendor/libgit2/build/libgit2.pc
#cgo windows CFLAGS: -I${SRCDIR}/static-build/install/include/
#cgo windows LDFLAGS: -L${SRCDIR}/static-build/install/lib/ -lgit2 -lwinhttp
#cgo !windows pkg-config: --static ${SRCDIR}/static-build/install/lib/pkgconfig/libgit2.pc
#include <git2.h>
#if LIBGIT2_VER_MAJOR != 0 || LIBGIT2_VER_MINOR != 27

1
go.mod Normal file
View File

@ -0,0 +1 @@
module github.com/libgit2/git2go

View File

@ -145,6 +145,20 @@ func (v *Index) Path() string {
return ret
}
// Clear clears the index object in memory; changes must be explicitly
// written to disk for them to take effect persistently
func (v *Index) Clear() error {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
err := C.git_index_clear(v.ptr)
runtime.KeepAlive(v)
if err < 0 {
return MakeGitError(err)
}
return nil
}
// Add adds or replaces the given entry to the index, making a copy of
// the data
func (v *Index) Add(entry *IndexEntry) error {

View File

@ -3,6 +3,7 @@ package git
import (
"io/ioutil"
"os"
"path"
"runtime"
"testing"
)
@ -59,14 +60,29 @@ func TestIndexWriteTreeTo(t *testing.T) {
repo := createTestRepo(t)
defer cleanupTestRepo(t, repo)
repo2 := createTestRepo(t)
defer cleanupTestRepo(t, repo2)
idx, err := NewIndex()
checkFatal(t, err)
idx, err := repo.Index()
odb, err := repo.Odb()
checkFatal(t, err)
err = idx.AddByPath("README")
content, err := ioutil.ReadFile(path.Join(repo.Workdir(), "README"))
checkFatal(t, err)
treeId, err := idx.WriteTreeTo(repo2)
id, err := odb.Write(content, ObjectBlob)
checkFatal(t, err)
err = idx.Add(&IndexEntry{
Mode: FilemodeBlob,
Uid: 0,
Gid: 0,
Size: uint32(len(content)),
Id: id,
Path: "README",
})
checkFatal(t, err)
treeId, err := idx.WriteTreeTo(repo)
checkFatal(t, err)
if treeId.String() != "b7119b11e8ef7a1a5a34d3ac87f5b075228ac81e" {

View File

@ -344,9 +344,29 @@ type MergeFileFlags int
const (
MergeFileDefault MergeFileFlags = C.GIT_MERGE_FILE_DEFAULT
// Create standard conflicted merge files
MergeFileStyleMerge MergeFileFlags = C.GIT_MERGE_FILE_STYLE_MERGE
// Create diff3-style files
MergeFileStyleDiff MergeFileFlags = C.GIT_MERGE_FILE_STYLE_DIFF3
// Condense non-alphanumeric regions for simplified diff file
MergeFileStyleSimplifyAlnum MergeFileFlags = C.GIT_MERGE_FILE_SIMPLIFY_ALNUM
// Ignore all whitespace
MergeFileIgnoreWhitespace MergeFileFlags = C.GIT_MERGE_FILE_IGNORE_WHITESPACE
// Ignore changes in amount of whitespace
MergeFileIgnoreWhitespaceChange MergeFileFlags = C.GIT_MERGE_FILE_IGNORE_WHITESPACE_CHANGE
// Ignore whitespace at end of line
MergeFileIgnoreWhitespaceEOL MergeFileFlags = C.GIT_MERGE_FILE_IGNORE_WHITESPACE_EOL
// Use the "patience diff" algorithm
MergeFileDiffPatience MergeFileFlags = C.GIT_MERGE_FILE_DIFF_PATIENCE
// Take extra time to find minimal diff
MergeFileDiffMinimal MergeFileFlags = C.GIT_MERGE_FILE_DIFF_MINIMAL
)
type MergeFileOptions struct {

View File

@ -132,7 +132,7 @@ func (c *NoteCollection) DefaultRef() (string, error) {
}
ret := C.GoString(buf.ptr)
C.git_buf_free(&buf)
C.git_buf_dispose(&buf)
return ret, nil
}

View File

@ -13,12 +13,12 @@ import (
type ObjectType int
const (
ObjectAny ObjectType = C.GIT_OBJ_ANY
ObjectBad ObjectType = C.GIT_OBJ_BAD
ObjectCommit ObjectType = C.GIT_OBJ_COMMIT
ObjectTree ObjectType = C.GIT_OBJ_TREE
ObjectBlob ObjectType = C.GIT_OBJ_BLOB
ObjectTag ObjectType = C.GIT_OBJ_TAG
ObjectAny ObjectType = C.GIT_OBJECT_ANY
ObjectBad ObjectType = C.GIT_OBJECT_BAD
ObjectCommit ObjectType = C.GIT_OBJECT_COMMIT
ObjectTree ObjectType = C.GIT_OBJECT_TREE
ObjectBlob ObjectType = C.GIT_OBJECT_BLOB
ObjectTag ObjectType = C.GIT_OBJECT_TAG
)
type Object struct {
@ -67,7 +67,7 @@ func (o *Object) ShortId() (string, error) {
if ecode < 0 {
return "", MakeGitError(ecode)
}
defer C.git_buf_free(&resultBuf)
defer C.git_buf_dispose(&resultBuf)
return C.GoString(resultBuf.ptr), nil
}
@ -217,7 +217,7 @@ func (o *Object) Peel(t ObjectType) (*Object, error) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
err := C.git_object_peel(&cobj, o.ptr, C.git_otype(t))
err := C.git_object_peel(&cobj, o.ptr, C.git_object_t(t))
runtime.KeepAlive(o)
if err < 0 {
return nil, MakeGitError(err)

38
odb.go
View File

@ -8,6 +8,7 @@ extern void _go_git_odb_backend_free(git_odb_backend *backend);
*/
import "C"
import (
"io"
"reflect"
"runtime"
"unsafe"
@ -60,12 +61,12 @@ func (v *Odb) ReadHeader(oid *Oid) (uint64, ObjectType, error) {
defer runtime.UnlockOSThread()
var sz C.size_t
var cotype C.git_otype
var cotype C.git_object_t
ret := C.git_odb_read_header(&sz, &cotype, v.ptr, oid.toC())
runtime.KeepAlive(v)
if ret < 0 {
return 0, C.GIT_OBJ_BAD, MakeGitError(ret)
return 0, ObjectBad, MakeGitError(ret)
}
return uint64(sz), ObjectType(cotype), nil
@ -80,15 +81,19 @@ func (v *Odb) Exists(oid *Oid) bool {
func (v *Odb) Write(data []byte, otype ObjectType) (oid *Oid, err error) {
oid = new(Oid)
var cptr unsafe.Pointer
if len(data) > 0 {
cptr = unsafe.Pointer(&data[0])
}
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_odb_write(oid.toC(), v.ptr, cptr, C.size_t(len(data)), C.git_otype(otype))
var size C.size_t
if len(data) > 0 {
size = C.size_t(len(data))
} else {
data = []byte{0}
size = C.size_t(0)
}
ret := C.git_odb_write(oid.toC(), v.ptr, unsafe.Pointer(&data[0]), size, C.git_object_t(otype))
runtime.KeepAlive(v)
if ret < 0 {
return nil, MakeGitError(ret)
@ -164,13 +169,19 @@ func (v *Odb) ForEach(callback OdbForEachCallback) error {
// Hash determines the object-ID (sha1) of a data buffer.
func (v *Odb) Hash(data []byte, otype ObjectType) (oid *Oid, err error) {
oid = new(Oid)
header := (*reflect.SliceHeader)(unsafe.Pointer(&data))
ptr := unsafe.Pointer(header.Data)
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_odb_hash(oid.toC(), ptr, C.size_t(header.Len), C.git_otype(otype))
var size C.size_t
if len(data) > 0 {
size = C.size_t(len(data))
} else {
data = []byte{0}
size = C.size_t(0)
}
ret := C.git_odb_hash(oid.toC(), unsafe.Pointer(&data[0]), size, C.git_object_t(otype))
runtime.KeepAlive(data)
if ret < 0 {
return nil, MakeGitError(ret)
@ -182,7 +193,7 @@ func (v *Odb) Hash(data []byte, otype ObjectType) (oid *Oid, err error) {
// contents of the object.
func (v *Odb) NewReadStream(id *Oid) (*OdbReadStream, error) {
stream := new(OdbReadStream)
var ctype C.git_otype
var ctype C.git_object_t
var csize C.size_t
runtime.LockOSThread()
@ -210,7 +221,7 @@ func (v *Odb) NewWriteStream(size int64, otype ObjectType) (*OdbWriteStream, err
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_odb_open_wstream(&stream.ptr, v.ptr, C.git_off_t(size), C.git_otype(otype))
ret := C.git_odb_open_wstream(&stream.ptr, v.ptr, C.git_off_t(size), C.git_object_t(otype))
runtime.KeepAlive(v)
if ret < 0 {
return nil, MakeGitError(ret)
@ -287,6 +298,9 @@ func (stream *OdbReadStream) Read(data []byte) (int, error) {
if ret < 0 {
return 0, MakeGitError(ret)
}
if ret == 0 {
return 0, io.EOF
}
header.Len = int(ret)

View File

@ -1,12 +1,14 @@
package git
import (
"bytes"
"errors"
"io"
"io/ioutil"
"testing"
)
func TestOdbReadHeader(t *testing.T) {
func TestOdbRead(t *testing.T) {
t.Parallel()
repo := createTestRepo(t)
defer cleanupTestRepo(t, repo)
@ -33,6 +35,20 @@ func TestOdbReadHeader(t *testing.T) {
if typ != ObjectBlob {
t.Errorf("ReadHeader got object type %s", typ)
}
obj, err := odb.Read(id)
if err != nil {
t.Fatalf("Read: %v", err)
}
if !bytes.Equal(obj.Data(), data) {
t.Errorf("Read got wrong data")
}
if sz := obj.Len(); sz != uint64(len(data)) {
t.Errorf("Read got size %d, want %d", sz, len(data))
}
if typ := obj.Type(); typ != ObjectBlob {
t.Errorf("Read got object type %s", typ)
}
}
func TestOdbStream(t *testing.T) {
@ -47,22 +63,29 @@ func TestOdbStream(t *testing.T) {
str := "hello, world!"
stream, error := odb.NewWriteStream(int64(len(str)), ObjectBlob)
writeStream, error := odb.NewWriteStream(int64(len(str)), ObjectBlob)
checkFatal(t, error)
n, error := io.WriteString(stream, str)
n, error := io.WriteString(writeStream, str)
checkFatal(t, error)
if n != len(str) {
t.Fatalf("Bad write length %v != %v", n, len(str))
}
error = stream.Close()
error = writeStream.Close()
checkFatal(t, error)
expectedId, error := NewOid("30f51a3fba5274d53522d0f19748456974647b4f")
checkFatal(t, error)
if stream.Id.Cmp(expectedId) != 0 {
if writeStream.Id.Cmp(expectedId) != 0 {
t.Fatal("Wrong data written")
}
readStream, error := odb.NewReadStream(&writeStream.Id)
checkFatal(t, error)
data, error := ioutil.ReadAll(readStream)
if str != string(data) {
t.Fatalf("Wrong data read %v != %v", str, string(data))
}
}
func TestOdbHash(t *testing.T) {
@ -82,16 +105,18 @@ committer John Doe <john@doe.com> 1390682018 +0000
Initial commit.`
oid, error := odb.Hash([]byte(str), ObjectCommit)
for _, data := range [][]byte{[]byte(str), doublePointerBytes()} {
oid, error := odb.Hash(data, ObjectCommit)
checkFatal(t, error)
coid, error := odb.Write([]byte(str), ObjectCommit)
coid, error := odb.Write(data, ObjectCommit)
checkFatal(t, error)
if oid.Cmp(coid) != 0 {
t.Fatal("Hash and write Oids are different")
}
}
}
func TestOdbForeach(t *testing.T) {
t.Parallel()

View File

@ -85,6 +85,19 @@ func (pb *Packbuilder) InsertTree(id *Oid) error {
return nil
}
func (pb *Packbuilder) InsertWalk(walk *RevWalk) error {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_packbuilder_insert_walk(pb.ptr, walk.ptr)
runtime.KeepAlive(pb)
runtime.KeepAlive(walk)
if ret != 0 {
return MakeGitError(ret)
}
return nil
}
func (pb *Packbuilder) ObjectCount() uint32 {
ret := uint32(C.git_packbuilder_object_count(pb.ptr))
runtime.KeepAlive(pb)

View File

@ -51,7 +51,7 @@ func (patch *Patch) String() (string, error) {
if ecode < 0 {
return "", MakeGitError(ecode)
}
defer C.git_buf_free(&buf)
defer C.git_buf_dispose(&buf)
return C.GoString(buf.ptr), nil
}

View File

@ -6,6 +6,7 @@ package git
import "C"
import (
"errors"
"fmt"
"runtime"
"unsafe"
)
@ -16,6 +17,8 @@ type RebaseOperationType uint
const (
// RebaseOperationPick The given commit is to be cherry-picked. The client should commit the changes and continue if there are no conflicts.
RebaseOperationPick RebaseOperationType = C.GIT_REBASE_OPERATION_PICK
// RebaseOperationReword The given commit is to be cherry-picked, but the client should prompt the user to provide an updated commit message.
RebaseOperationReword RebaseOperationType = C.GIT_REBASE_OPERATION_REWORD
// RebaseOperationEdit The given commit is to be cherry-picked, but the client should stop to allow the user to edit the changes before committing them.
RebaseOperationEdit RebaseOperationType = C.GIT_REBASE_OPERATION_EDIT
// RebaseOperationSquash The given commit is to be squashed into the previous commit. The commit message will be merged with the previous message.
@ -26,6 +29,24 @@ const (
RebaseOperationExec RebaseOperationType = C.GIT_REBASE_OPERATION_EXEC
)
func (t RebaseOperationType) String() string {
switch t {
case RebaseOperationPick:
return "pick"
case RebaseOperationReword:
return "reword"
case RebaseOperationEdit:
return "edit"
case RebaseOperationSquash:
return "squash"
case RebaseOperationFixup:
return "fixup"
case RebaseOperationExec:
return "exec"
}
return fmt.Sprintf("RebaseOperationType(%d)", t)
}
// Special value indicating that there is no currently active operation
var RebaseNoOperation uint = ^uint(0)

View File

@ -284,7 +284,7 @@ func (v *Reference) Peel(t ObjectType) (*Object, error) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
err := C.git_reference_peel(&cobj, v.ptr, C.git_otype(t))
err := C.git_reference_peel(&cobj, v.ptr, C.git_object_t(t))
runtime.KeepAlive(v)
if err < 0 {
return nil, MakeGitError(err)

View File

@ -174,7 +174,7 @@ func (v *Repository) lookupType(id *Oid, t ObjectType) (*Object, error) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_object_lookup(&ptr, v.ptr, id.toC(), C.git_otype(t))
ret := C.git_object_lookup(&ptr, v.ptr, id.toC(), C.git_object_t(t))
runtime.KeepAlive(id)
if ret < 0 {
return nil, MakeGitError(ret)

View File

@ -8,6 +8,8 @@ import (
func TestResetToCommit(t *testing.T) {
t.Parallel()
repo := createTestRepo(t)
defer cleanupTestRepo(t, repo)
seedTestRepo(t, repo)
// create commit to reset to
commitId, _ := updateReadme(t, repo, "testing reset")

View File

@ -2,18 +2,19 @@
set -ex
VENDORED_PATH=vendor/libgit2
ROOT="$(cd "$0/../.." && echo "${PWD}")"
BUILD_PATH="${ROOT}/static-build"
VENDORED_PATH="${ROOT}/vendor/libgit2"
cd $VENDORED_PATH &&
mkdir -p install/lib &&
mkdir -p build &&
cd build &&
mkdir -p "${BUILD_PATH}/build" "${BUILD_PATH}/install/lib"
cd "${BUILD_PATH}/build" &&
cmake -DTHREADSAFE=ON \
-DBUILD_CLAR=OFF \
-DBUILD_SHARED_LIBS=OFF \
-DCMAKE_C_FLAGS=-fPIC \
-DCMAKE_BUILD_TYPE="RelWithDebInfo" \
-DCMAKE_INSTALL_PREFIX=../install \
.. &&
-DCMAKE_INSTALL_PREFIX="${BUILD_PATH}/install" \
"${VENDORED_PATH}" &&
cmake --build .
cmake --build . --target install

View File

@ -31,7 +31,7 @@ import (
func SearchPath(level ConfigLevel) (string, error) {
var buf C.git_buf
defer C.git_buf_free(&buf)
defer C.git_buf_dispose(&buf)
runtime.LockOSThread()
defer runtime.UnlockOSThread()

View File

@ -26,7 +26,7 @@ func newSignatureFromC(sig *C.git_signature) *Signature {
}
}
// the offset in mintes, which is what git wants
// Offset returns the time zone offset of v.When in minutes, which is what git wants.
func (v *Signature) Offset() int {
_, offset := v.When.Zone()
return offset / 60

2
vendor/libgit2 vendored

@ -1 +1 @@
Subproject commit 7f6c1ce9760c6c397e94d4eba506faeb95452702
Subproject commit fba70a9d5f1fa433968a3dfd51e3153c8eebe834