summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--checkout.go2
-rw-r--r--config.go2
-rw-r--r--config_test.go2
-rw-r--r--git.go14
-rw-r--r--merge.go24
-rw-r--r--odb.go30
-rw-r--r--odb_test.go49
-rw-r--r--signature.go2
8 files changed, 89 insertions, 36 deletions
diff --git a/checkout.go b/checkout.go
index db3118f..2b12058 100644
--- a/checkout.go
+++ b/checkout.go
@@ -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
diff --git a/config.go b/config.go
index 8dfe0af..7260089 100644
--- a/config.go
+++ b/config.go
@@ -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))
diff --git a/config_test.go b/config_test.go
index 196d4ad..398236e 100644
--- a/config_test.go
+++ b/config_test.go
@@ -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
}
diff --git a/git.go b/git.go
index a1c07f6..968d404 100644
--- a/git.go
+++ b/git.go
@@ -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 {
diff --git a/merge.go b/merge.go
index adc521a..bc672ce 100644
--- a/merge.go
+++ b/merge.go
@@ -344,9 +344,29 @@ type MergeFileFlags int
const (
MergeFileDefault MergeFileFlags = C.GIT_MERGE_FILE_DEFAULT
- MergeFileStyleMerge MergeFileFlags = C.GIT_MERGE_FILE_STYLE_MERGE
- MergeFileStyleDiff MergeFileFlags = C.GIT_MERGE_FILE_STYLE_DIFF3
+ // 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 {
diff --git a/odb.go b/odb.go
index f236fc4..7ae108a 100644
--- a/odb.go
+++ b/odb.go
@@ -8,6 +8,7 @@ extern void _go_git_odb_backend_free(git_odb_backend *backend);
*/
import "C"
import (
+ "io"
"reflect"
"runtime"
"unsafe"
@@ -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_otype(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_otype(otype))
runtime.KeepAlive(data)
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)
diff --git a/odb_test.go b/odb_test.go
index 3d22fc9..46acdba 100644
--- a/odb_test.go
+++ b/odb_test.go
@@ -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)
@@ -26,13 +28,27 @@ func TestOdbReadHeader(t *testing.T) {
if err != nil {
t.Fatalf("ReadHeader: %v", err)
}
-
+
if sz != uint64(len(data)) {
t.Errorf("ReadHeader got size %d, want %d", sz, len(data))
}
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,14 +105,16 @@ committer John Doe <[email protected]> 1390682018 +0000
Initial commit.`
- oid, error := odb.Hash([]byte(str), ObjectCommit)
- checkFatal(t, error)
+ for _, data := range [][]byte{[]byte(str), doublePointerBytes()} {
+ oid, error := odb.Hash(data, ObjectCommit)
+ checkFatal(t, error)
- coid, error := odb.Write([]byte(str), ObjectCommit)
- checkFatal(t, error)
+ coid, error := odb.Write(data, ObjectCommit)
+ checkFatal(t, error)
- if oid.Cmp(coid) != 0 {
- t.Fatal("Hash and write Oids are different")
+ if oid.Cmp(coid) != 0 {
+ t.Fatal("Hash and write Oids are different")
+ }
}
}
diff --git a/signature.go b/signature.go
index 16964d2..220fe57 100644
--- a/signature.go
+++ b/signature.go
@@ -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