diff --git a/.travis.yml b/.travis.yml index 79ad168..f796389 100644 --- a/.travis.yml +++ b/.travis.yml @@ -10,8 +10,6 @@ go: - 1.3 - 1.4 - 1.5 - - 1.6 - - 1.7 - tip matrix: diff --git a/blame_test.go b/blame_test.go index ec96af7..a2a4d38 100644 --- a/blame_test.go +++ b/blame_test.go @@ -6,7 +6,6 @@ import ( ) func TestBlame(t *testing.T) { - t.Parallel() repo := createTestRepo(t) defer cleanupTestRepo(t, repo) diff --git a/blob.go b/blob.go index 8b3e94f..1a86e60 100644 --- a/blob.go +++ b/blob.go @@ -37,24 +37,15 @@ func (repo *Repository) CreateBlobFromBuffer(data []byte) (*Oid, error) { defer runtime.UnlockOSThread() var id C.git_oid - var size C.size_t + var ptr unsafe.Pointer - // Go 1.6 added some increased checking of passing pointer to - // C, but its check depends on its expectations of waht we - // pass to the C function, so unless we take the address of - // its contents at the call site itself, it can fail when - // 'data' is a slice of a slice. - // - // When we're given an empty slice, create a dummy one where 0 - // isn't out of bounds. if len(data) > 0 { - size = C.size_t(len(data)) + ptr = unsafe.Pointer(&data[0]) } else { - data = []byte{0} - size = C.size_t(0) + ptr = unsafe.Pointer(nil) } - ecode := C.git_blob_create_frombuffer(&id, repo.ptr, unsafe.Pointer(&data[0]), size) + ecode := C.git_blob_create_frombuffer(&id, repo.ptr, ptr, C.size_t(len(data))) if ecode < 0 { return nil, MakeGitError(ecode) } diff --git a/blob_test.go b/blob_test.go index 815ab3d..2b5ec4f 100644 --- a/blob_test.go +++ b/blob_test.go @@ -1,23 +1,10 @@ package git import ( - "bytes" "testing" ) -type bufWrapper struct { - buf [64]byte - pointer []byte -} - -func doublePointerBytes() []byte { - o := &bufWrapper{} - o.pointer = o.buf[0:10] - return o.pointer[0:1] -} - func TestCreateBlobFromBuffer(t *testing.T) { - t.Parallel() repo := createTestRepo(t) defer cleanupTestRepo(t, repo) @@ -27,16 +14,4 @@ func TestCreateBlobFromBuffer(t *testing.T) { if id.String() != "e69de29bb2d1d6434b8b29ae775ad8c2e48c5391" { t.Fatal("Empty buffer did not deliver empty blob id") } - - for _, data := range []([]byte){[]byte("hello there"), doublePointerBytes()} { - id, err = repo.CreateBlobFromBuffer(data) - checkFatal(t, err) - - blob, err := repo.LookupBlob(id) - checkFatal(t, err) - if !bytes.Equal(blob.Contents(), data) { - t.Fatal("Loaded bytes don't match original bytes:", - blob.Contents(), "!=", data) - } - } } diff --git a/branch.go b/branch.go index a869054..df72dba 100644 --- a/branch.go +++ b/branch.go @@ -13,7 +13,6 @@ import ( type BranchType uint const ( - BranchAll BranchType = C.GIT_BRANCH_ALL BranchLocal BranchType = C.GIT_BRANCH_LOCAL BranchRemote BranchType = C.GIT_BRANCH_REMOTE ) diff --git a/branch_test.go b/branch_test.go index 01a2e28..a0834a8 100644 --- a/branch_test.go +++ b/branch_test.go @@ -5,7 +5,6 @@ import ( ) func TestBranchIterator(t *testing.T) { - t.Parallel() repo := createTestRepo(t) defer cleanupTestRepo(t, repo) @@ -28,7 +27,6 @@ func TestBranchIterator(t *testing.T) { } func TestBranchIteratorEach(t *testing.T) { - t.Parallel() repo := createTestRepo(t) defer cleanupTestRepo(t, repo) diff --git a/cherrypick_test.go b/cherrypick_test.go index bfa5ca8..a3246bd 100644 --- a/cherrypick_test.go +++ b/cherrypick_test.go @@ -33,7 +33,6 @@ func readReadme(t *testing.T, repo *Repository) string { } func TestCherrypick(t *testing.T) { - t.Parallel() repo := createTestRepo(t) defer cleanupTestRepo(t, repo) diff --git a/clone_test.go b/clone_test.go index 24c3a09..a6bbf94 100644 --- a/clone_test.go +++ b/clone_test.go @@ -10,7 +10,6 @@ const ( ) func TestClone(t *testing.T) { - t.Parallel() repo := createTestRepo(t) defer cleanupTestRepo(t, repo) @@ -36,7 +35,6 @@ func TestClone(t *testing.T) { } func TestCloneWithCallback(t *testing.T) { - t.Parallel() testPayload := 0 repo := createTestRepo(t) diff --git a/config_test.go b/config_test.go index f31e73e..fea8d8a 100644 --- a/config_test.go +++ b/config_test.go @@ -88,7 +88,6 @@ var tests = []TestRunner{ } func TestConfigLookups(t *testing.T) { - t.Parallel() var ( err error c *Config diff --git a/describe_test.go b/describe_test.go index f0a45f4..25af107 100644 --- a/describe_test.go +++ b/describe_test.go @@ -8,7 +8,6 @@ import ( ) func TestDescribeCommit(t *testing.T) { - t.Parallel() repo := createTestRepo(t) defer cleanupTestRepo(t, repo) diff --git a/diff.go b/diff.go index 8ae0512..9a7ac78 100644 --- a/diff.go +++ b/diff.go @@ -217,33 +217,6 @@ func (stats *DiffStats) FilesChanged() int { return int(C.git_diff_stats_files_changed(stats.ptr)) } -type DiffStatsFormat int - -const ( - DiffStatsNone DiffStatsFormat = C.GIT_DIFF_STATS_NONE - DiffStatsFull DiffStatsFormat = C.GIT_DIFF_STATS_FULL - DiffStatsShort DiffStatsFormat = C.GIT_DIFF_STATS_SHORT - DiffStatsNumber DiffStatsFormat = C.GIT_DIFF_STATS_NUMBER - DiffStatsIncludeSummary DiffStatsFormat = C.GIT_DIFF_STATS_INCLUDE_SUMMARY -) - -func (stats *DiffStats) String(format DiffStatsFormat, - width uint) (string, error) { - buf := C.git_buf{} - defer C.git_buf_free(&buf) - - runtime.LockOSThread() - defer runtime.UnlockOSThread() - - ret := C.git_diff_stats_to_buf(&buf, - stats.ptr, C.git_diff_stats_format_t(format), C.size_t(width)) - if ret < 0 { - return "", MakeGitError(ret) - } - - return C.GoString(buf.ptr), nil -} - func (diff *Diff) Stats() (*DiffStats, error) { stats := new(DiffStats) diff --git a/diff_test.go b/diff_test.go index 6fbad51..850ed8e 100644 --- a/diff_test.go +++ b/diff_test.go @@ -7,7 +7,6 @@ import ( ) func TestFindSimilar(t *testing.T) { - t.Parallel() repo := createTestRepo(t) defer cleanupTestRepo(t, repo) @@ -62,7 +61,7 @@ func TestFindSimilar(t *testing.T) { } func TestDiffTreeToTree(t *testing.T) { - t.Parallel() + repo := createTestRepo(t) defer cleanupTestRepo(t, repo) @@ -190,7 +189,6 @@ func createTestTrees(t *testing.T, repo *Repository) (originalTree *Tree, newTre } func TestDiffBlobs(t *testing.T) { - t.Parallel() repo := createTestRepo(t) defer cleanupTestRepo(t, repo) diff --git a/git.go b/git.go index ed891e6..e272389 100644 --- a/git.go +++ b/git.go @@ -50,6 +50,7 @@ const ( ErrClassFilter ErrorClass = C.GITERR_FILTER ErrClassRevert ErrorClass = C.GITERR_REVERT ErrClassCallback ErrorClass = C.GITERR_CALLBACK + ErrClassRebase ErrorClass = C.GITERR_REBASE ) type ErrorCode int diff --git a/git_test.go b/git_test.go index bdb6837..58caf71 100644 --- a/git_test.go +++ b/git_test.go @@ -109,7 +109,6 @@ func updateReadme(t *testing.T, repo *Repository, content string) (*Oid, *Oid) { } func TestOidZero(t *testing.T) { - t.Parallel() var zeroId Oid if !zeroId.IsZero() { @@ -118,7 +117,6 @@ func TestOidZero(t *testing.T) { } func TestEmptyOid(t *testing.T) { - t.Parallel() _, err := NewOid("") if err == nil || !IsErrorCode(err, ErrGeneric) { t.Fatal("Should have returned invalid error") diff --git a/index_test.go b/index_test.go index 600b8b1..5f6b375 100644 --- a/index_test.go +++ b/index_test.go @@ -8,7 +8,6 @@ import ( ) func TestCreateRepoAndStage(t *testing.T) { - t.Parallel() repo := createTestRepo(t) defer cleanupTestRepo(t, repo) @@ -25,7 +24,6 @@ func TestCreateRepoAndStage(t *testing.T) { } func TestIndexReadTree(t *testing.T) { - t.Parallel() repo := createTestRepo(t) defer cleanupTestRepo(t, repo) @@ -55,7 +53,6 @@ func TestIndexReadTree(t *testing.T) { } func TestIndexWriteTreeTo(t *testing.T) { - t.Parallel() repo := createTestRepo(t) defer cleanupTestRepo(t, repo) @@ -75,7 +72,6 @@ func TestIndexWriteTreeTo(t *testing.T) { } func TestIndexAddAndWriteTreeTo(t *testing.T) { - t.Parallel() repo := createTestRepo(t) defer cleanupTestRepo(t, repo) @@ -110,7 +106,6 @@ func TestIndexAddAndWriteTreeTo(t *testing.T) { } func TestIndexAddAllNoCallback(t *testing.T) { - t.Parallel() repo := createTestRepo(t) defer cleanupTestRepo(t, repo) @@ -132,7 +127,6 @@ func TestIndexAddAllNoCallback(t *testing.T) { } func TestIndexAddAllCallback(t *testing.T) { - t.Parallel() repo := createTestRepo(t) defer cleanupTestRepo(t, repo) @@ -161,7 +155,6 @@ func TestIndexAddAllCallback(t *testing.T) { } func TestIndexOpen(t *testing.T) { - t.Parallel() repo := createTestRepo(t) defer cleanupTestRepo(t, repo) diff --git a/merge_test.go b/merge_test.go index f2c84bc..8059727 100644 --- a/merge_test.go +++ b/merge_test.go @@ -6,7 +6,6 @@ import ( ) func TestMergeWithSelf(t *testing.T) { - t.Parallel() repo := createTestRepo(t) defer cleanupTestRepo(t, repo) @@ -25,7 +24,6 @@ func TestMergeWithSelf(t *testing.T) { } func TestMergeAnalysisWithSelf(t *testing.T) { - t.Parallel() repo := createTestRepo(t) defer cleanupTestRepo(t, repo) @@ -48,7 +46,6 @@ func TestMergeAnalysisWithSelf(t *testing.T) { } func TestMergeSameFile(t *testing.T) { - t.Parallel() file := MergeFileInput{ Path: "test", Mode: 33188, @@ -71,7 +68,6 @@ func TestMergeSameFile(t *testing.T) { } func TestMergeTreesWithoutAncestor(t *testing.T) { - t.Parallel() repo := createTestRepo(t) defer cleanupTestRepo(t, repo) @@ -129,7 +125,6 @@ func appendCommit(t *testing.T, repo *Repository) (*Oid, *Oid) { } func TestMergeBase(t *testing.T) { - t.Parallel() repo := createTestRepo(t) defer cleanupTestRepo(t, repo) diff --git a/note_test.go b/note_test.go index 9f64eb8..27e04be 100644 --- a/note_test.go +++ b/note_test.go @@ -8,7 +8,6 @@ import ( ) func TestCreateNote(t *testing.T) { - t.Parallel() repo := createTestRepo(t) defer cleanupTestRepo(t, repo) @@ -28,7 +27,6 @@ func TestCreateNote(t *testing.T) { } func TestNoteIterator(t *testing.T) { - t.Parallel() repo := createTestRepo(t) defer cleanupTestRepo(t, repo) @@ -65,7 +63,6 @@ func TestNoteIterator(t *testing.T) { } func TestRemoveNote(t *testing.T) { - t.Parallel() repo := createTestRepo(t) defer cleanupTestRepo(t, repo) @@ -89,7 +86,6 @@ func TestRemoveNote(t *testing.T) { } func TestDefaultNoteRef(t *testing.T) { - t.Parallel() repo := createTestRepo(t) defer cleanupTestRepo(t, repo) diff --git a/object_test.go b/object_test.go index cb262de..2ae2a6a 100644 --- a/object_test.go +++ b/object_test.go @@ -5,7 +5,6 @@ import ( ) func TestObjectPoymorphism(t *testing.T) { - t.Parallel() repo := createTestRepo(t) defer cleanupTestRepo(t, repo) @@ -89,7 +88,6 @@ func checkOwner(t *testing.T, repo *Repository, obj Object) { } func TestObjectOwner(t *testing.T) { - t.Parallel() repo := createTestRepo(t) defer cleanupTestRepo(t, repo) @@ -106,7 +104,6 @@ func TestObjectOwner(t *testing.T) { } func TestObjectPeel(t *testing.T) { - t.Parallel() repo := createTestRepo(t) defer cleanupTestRepo(t, repo) diff --git a/odb_test.go b/odb_test.go index 3d22fc9..dfd2ad0 100644 --- a/odb_test.go +++ b/odb_test.go @@ -7,7 +7,6 @@ import ( ) func TestOdbReadHeader(t *testing.T) { - t.Parallel() repo := createTestRepo(t) defer cleanupTestRepo(t, repo) @@ -36,7 +35,6 @@ func TestOdbReadHeader(t *testing.T) { } func TestOdbStream(t *testing.T) { - t.Parallel() repo := createTestRepo(t) defer cleanupTestRepo(t, repo) @@ -66,7 +64,7 @@ func TestOdbStream(t *testing.T) { } func TestOdbHash(t *testing.T) { - t.Parallel() + repo := createTestRepo(t) defer cleanupTestRepo(t, repo) @@ -94,7 +92,6 @@ Initial commit.` } func TestOdbForeach(t *testing.T) { - t.Parallel() repo := createTestRepo(t) defer cleanupTestRepo(t, repo) diff --git a/patch_test.go b/patch_test.go index 291c705..2d52fb4 100644 --- a/patch_test.go +++ b/patch_test.go @@ -6,7 +6,6 @@ import ( ) func TestPatch(t *testing.T) { - t.Parallel() repo := createTestRepo(t) defer cleanupTestRepo(t, repo) diff --git a/push_test.go b/push_test.go index f372882..8f6e806 100644 --- a/push_test.go +++ b/push_test.go @@ -5,7 +5,6 @@ import ( ) func TestRemotePush(t *testing.T) { - t.Parallel() repo := createBareTestRepo(t) defer cleanupTestRepo(t, repo) diff --git a/rebase.go b/rebase.go new file mode 100644 index 0000000..8553e25 --- /dev/null +++ b/rebase.go @@ -0,0 +1,252 @@ +package git + +/* +#include +*/ +import "C" +import ( + "runtime" + "unsafe" +) + +// RebaseOperationType is the type of rebase operation +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 + // 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. + RebaseOperationSquash RebaseOperationType = C.GIT_REBASE_OPERATION_SQUASH + // RebaseOperationFixup No commit will be cherry-picked. The client should run the given command and (if successful) continue. + RebaseOperationFixup RebaseOperationType = C.GIT_REBASE_OPERATION_FIXUP + // RebaseOperationExec No commit will be cherry-picked. The client should run the given command and (if successful) continue. + RebaseOperationExec RebaseOperationType = C.GIT_REBASE_OPERATION_EXEC +) + +// RebaseOperation describes a single instruction/operation to be performed during the rebase. +type RebaseOperation struct { + Type RebaseOperationType + Id *Oid + Exec string +} + +func newRebaseOperationFromC(c *C.git_rebase_operation) *RebaseOperation { + operation := &RebaseOperation{} + operation.Type = RebaseOperationType(c._type) + operation.Id = newOidFromC(&c.id) + operation.Exec = C.GoString(c.exec) + + return operation +} + +// RebaseOptions are used to tell the rebase machinery how to operate +type RebaseOptions struct { + Version uint + Quiet int + InMemory int + RewriteNotesRef string + MergeOptions MergeOptions + CheckoutOptions CheckoutOpts +} + +// DefaultRebaseOptions returns a RebaseOptions with default values. +func DefaultRebaseOptions() (RebaseOptions, error) { + opts := C.git_rebase_options{} + + runtime.LockOSThread() + defer runtime.UnlockOSThread() + + ecode := C.git_rebase_init_options(&opts, C.GIT_REBASE_OPTIONS_VERSION) + if ecode < 0 { + return RebaseOptions{}, MakeGitError(ecode) + } + return rebaseOptionsFromC(&opts), nil +} + +func rebaseOptionsFromC(opts *C.git_rebase_options) RebaseOptions { + return RebaseOptions{ + Version: uint(opts.version), + Quiet: int(opts.quiet), + InMemory: int(opts.inmemory), + RewriteNotesRef: C.GoString(opts.rewrite_notes_ref), + MergeOptions: mergeOptionsFromC(&opts.merge_options), + CheckoutOptions: checkoutOptionsFromC(&opts.checkout_options), + } +} + +func (ro *RebaseOptions) toC() *C.git_rebase_options { + if ro == nil { + return nil + } + return &C.git_rebase_options{ + version: C.uint(ro.Version), + quiet: C.int(ro.Quiet), + inmemory: C.int(ro.InMemory), + rewrite_notes_ref: mapEmptyStringToNull(ro.RewriteNotesRef), + merge_options: *ro.MergeOptions.toC(), + checkout_options: *ro.CheckoutOptions.toC(), + } +} + +func mapEmptyStringToNull(ref string) *C.char { + if ref == "" { + return nil + } + return C.CString(ref) +} + +// Rebase is the struct representing a Rebase object. +type Rebase struct { + ptr *C.git_rebase +} + +// InitRebase initializes a rebase operation to rebase the changes in branch relative to upstream onto another branch. +func (r *Repository) InitRebase(branch *AnnotatedCommit, upstream *AnnotatedCommit, onto *AnnotatedCommit, opts *RebaseOptions) (*Rebase, error) { + runtime.LockOSThread() + defer runtime.UnlockOSThread() + + if branch == nil { + branch = &AnnotatedCommit{ptr: nil} + } + + if upstream == nil { + upstream = &AnnotatedCommit{ptr: nil} + } + + if onto == nil { + onto = &AnnotatedCommit{ptr: nil} + } + + var ptr *C.git_rebase + err := C.git_rebase_init(&ptr, r.ptr, branch.ptr, upstream.ptr, onto.ptr, opts.toC()) + if err < 0 { + return nil, MakeGitError(err) + } + + return newRebaseFromC(ptr), nil +} + +// OpenRebase opens an existing rebase that was previously started by either an invocation of InitRebase or by another client. +func (r *Repository) OpenRebase(opts *RebaseOptions) (*Rebase, error) { + runtime.LockOSThread() + defer runtime.UnlockOSThread() + + var ptr *C.git_rebase + err := C.git_rebase_open(&ptr, r.ptr, opts.toC()) + if err < 0 { + return nil, MakeGitError(err) + } + + return newRebaseFromC(ptr), nil +} + +// OperationAt gets the rebase operation specified by the given index. +func (rebase *Rebase) OperationAt(index uint) *RebaseOperation { + operation := C.git_rebase_operation_byindex(rebase.ptr, C.size_t(index)) + + return newRebaseOperationFromC(operation) +} + +// CurrentOperationIndex gets the index of the rebase operation that is currently being applied. +// Returns an error if no rebase operation is currently applied. +func (rebase *Rebase) CurrentOperationIndex() (uint, error) { + runtime.LockOSThread() + defer runtime.UnlockOSThread() + + operationIndex := int(C.git_rebase_operation_current(rebase.ptr)) + if operationIndex == C.GIT_REBASE_NO_OPERATION { + return 0, MakeGitError(C.GIT_REBASE_NO_OPERATION) + } + + return uint(operationIndex), nil +} + +// OperationCount gets the count of rebase operations that are to be applied. +func (rebase *Rebase) OperationCount() uint { + return uint(C.git_rebase_operation_entrycount(rebase.ptr)) +} + +// Next performs the next rebase operation and returns the information about it. +// If the operation is one that applies a patch (which is any operation except RebaseOperationExec) +// then the patch will be applied and the index and working directory will be updated with the changes. +// If there are conflicts, you will need to address those before committing the changes. +func (rebase *Rebase) Next() (*RebaseOperation, error) { + runtime.LockOSThread() + defer runtime.UnlockOSThread() + + var ptr *C.git_rebase_operation + err := C.git_rebase_next(&ptr, rebase.ptr) + if err < 0 { + return nil, MakeGitError(err) + } + + return newRebaseOperationFromC(ptr), nil +} + +// Commit commits the current patch. +// You must have resolved any conflicts that were introduced during the patch application from the Next() invocation. +func (rebase *Rebase) Commit(ID *Oid, author, committer *Signature, message string) error { + runtime.LockOSThread() + defer runtime.UnlockOSThread() + + 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) + + cmsg := C.CString(message) + defer C.free(unsafe.Pointer(cmsg)) + + cerr := C.git_rebase_commit(ID.toC(), rebase.ptr, authorSig, committerSig, nil, cmsg) + if cerr < 0 { + return MakeGitError(cerr) + } + + return nil +} + +// Finish finishes a rebase that is currently in progress once all patches have been applied. +func (rebase *Rebase) Finish() error { + runtime.LockOSThread() + defer runtime.UnlockOSThread() + + err := C.git_rebase_finish(rebase.ptr, nil) + if err < 0 { + return MakeGitError(err) + } + + return nil +} + +// Abort aborts a rebase that is currently in progress, resetting the repository and working directory to their state before rebase began. +func (rebase *Rebase) Abort() error { + runtime.LockOSThread() + defer runtime.UnlockOSThread() + + err := C.git_rebase_abort(rebase.ptr) + if err < 0 { + return MakeGitError(err) + } + return nil +} + +// Free frees the Rebase object. +func (rebase *Rebase) Free() { + runtime.SetFinalizer(rebase, nil) + C.git_rebase_free(rebase.ptr) +} + +func newRebaseFromC(ptr *C.git_rebase) *Rebase { + rebase := &Rebase{ptr: ptr} + runtime.SetFinalizer(rebase, (*Rebase).Free) + return rebase +} diff --git a/rebase_test.go b/rebase_test.go new file mode 100644 index 0000000..fb88a4e --- /dev/null +++ b/rebase_test.go @@ -0,0 +1,381 @@ +package git + +import ( + "errors" + "strconv" + "testing" + "time" +) + +// Tests + +func TestRebaseAbort(t *testing.T) { + // TEST DATA + + // Inputs + branchName := "emile" + masterCommit := "something" + emileCommits := []string{ + "fou", + "barre", + } + + // Outputs + expectedHistory := []string{ + "Test rebase, Baby! " + emileCommits[1], + "Test rebase, Baby! " + emileCommits[0], + "This is a commit\n", + } + + // TEST + repo := createTestRepo(t) + defer cleanupTestRepo(t, repo) + seedTestRepo(t, repo) + + // Setup a repo with 2 branches and a different tree + err := setupRepoForRebase(repo, masterCommit, branchName) + checkFatal(t, err) + + // Create several commits in emile + for _, commit := range emileCommits { + _, err = commitSomething(repo, commit, commit) + checkFatal(t, err) + } + + // Check history + actualHistory, err := commitMsgsList(repo) + checkFatal(t, err) + assertStringList(t, expectedHistory, actualHistory) + + // Rebase onto master + rebase, err := performRebaseOnto(repo, "master") + checkFatal(t, err) + defer rebase.Free() + + // Abort rebase + rebase.Abort() + + // Check history is still the same + actualHistory, err = commitMsgsList(repo) + checkFatal(t, err) + assertStringList(t, expectedHistory, actualHistory) +} + +func TestRebaseNoConflicts(t *testing.T) { + // TEST DATA + + // Inputs + branchName := "emile" + masterCommit := "something" + emileCommits := []string{ + "fou", + "barre", + "ouich", + } + + // Outputs + expectedHistory := []string{ + "Test rebase, Baby! " + emileCommits[2], + "Test rebase, Baby! " + emileCommits[1], + "Test rebase, Baby! " + emileCommits[0], + "Test rebase, Baby! " + masterCommit, + "This is a commit\n", + } + + // TEST + repo := createTestRepo(t) + defer cleanupTestRepo(t, repo) + seedTestRepo(t, repo) + + // Try to open existing rebase + oRebase, err := repo.OpenRebase(nil) + if err == nil { + t.Fatal("Did not expect to find a rebase in progress") + } + + // Setup a repo with 2 branches and a different tree + err = setupRepoForRebase(repo, masterCommit, branchName) + checkFatal(t, err) + + // Create several commits in emile + for _, commit := range emileCommits { + _, err = commitSomething(repo, commit, commit) + checkFatal(t, err) + } + + // Rebase onto master + rebase, err := performRebaseOnto(repo, "master") + checkFatal(t, err) + defer rebase.Free() + + // Open existing rebase + oRebase, err = repo.OpenRebase(nil) + checkFatal(t, err) + defer oRebase.Free() + if oRebase == nil { + t.Fatal("Expected to find an existing rebase in progress") + } + + // Finish the rebase properly + err = rebase.Finish() + checkFatal(t, err) + + // Check no more rebase is in progress + oRebase, err = repo.OpenRebase(nil) + if err == nil { + t.Fatal("Did not expect to find a rebase in progress") + } + + // Check history is in correct order + actualHistory, err := commitMsgsList(repo) + checkFatal(t, err) + assertStringList(t, expectedHistory, actualHistory) + +} + +// Utils +func setupRepoForRebase(repo *Repository, masterCommit, branchName string) error { + // Create a new branch from master + err := createBranch(repo, branchName) + if err != nil { + return err + } + + // Create a commit in master + _, err = commitSomething(repo, masterCommit, masterCommit) + if err != nil { + return err + } + + // Switch to emile + err = repo.SetHead("refs/heads/" + branchName) + if err != nil { + return err + } + + // Check master commit is not in emile branch + if entryExists(repo, masterCommit) { + return errors.New(masterCommit + " entry should not exist in " + branchName + " branch.") + } + + return nil +} + +func performRebaseOnto(repo *Repository, branch string) (*Rebase, error) { + master, err := repo.LookupBranch(branch, BranchLocal) + if err != nil { + return nil, err + } + defer master.Free() + + onto, err := repo.AnnotatedCommitFromRef(master.Reference) + if err != nil { + return nil, err + } + defer onto.Free() + + // Init rebase + rebase, err := repo.InitRebase(nil, nil, onto, nil) + if err != nil { + return nil, err + } + + // Check no operation has been started yet + rebaseOperationIndex, err := rebase.CurrentOperationIndex() + if err == nil { + return nil, errors.New("No operation should have been started yet") + } + + // Iterate in rebase operations regarding operation count + opCount := int(rebase.OperationCount()) + for op := 0; op < opCount; op++ { + operation, err := rebase.Next() + if err != nil { + return nil, err + } + + // Check operation index is correct + rebaseOperationIndex, err = rebase.CurrentOperationIndex() + if int(rebaseOperationIndex) != op { + return nil, errors.New("Bad operation index") + } + if !operationsAreEqual(rebase.OperationAt(uint(op)), operation) { + return nil, errors.New("Rebase operations should be equal") + } + + // Get current rebase operation created commit + commit, err := repo.LookupCommit(operation.Id) + if err != nil { + return nil, err + } + defer commit.Free() + + // Apply commit + err = rebase.Commit(operation.Id, signature(), signature(), commit.Message()) + if err != nil { + return nil, err + } + } + + return rebase, nil +} + +func operationsAreEqual(l, r *RebaseOperation) bool { + return l.Exec == r.Exec && l.Type == r.Type && l.Id.String() == r.Id.String() +} + +func createBranch(repo *Repository, branch string) error { + commit, err := headCommit(repo) + if err != nil { + return err + } + defer commit.Free() + _, err = repo.CreateBranch(branch, commit, false) + if err != nil { + return err + } + + return nil +} + +func signature() *Signature { + return &Signature{ + Name: "Emile", + Email: "emile@emile.com", + When: time.Now(), + } +} + +func headCommit(repo *Repository) (*Commit, error) { + head, err := repo.Head() + if err != nil { + return nil, err + } + defer head.Free() + + commit, err := repo.LookupCommit(head.Target()) + if err != nil { + return nil, err + } + + return commit, nil +} + +func headTree(repo *Repository) (*Tree, error) { + headCommit, err := headCommit(repo) + if err != nil { + return nil, err + } + defer headCommit.Free() + + tree, err := headCommit.Tree() + if err != nil { + return nil, err + } + + return tree, nil +} + +func commitSomething(repo *Repository, something, content string) (*Oid, error) { + headCommit, err := headCommit(repo) + if err != nil { + return nil, err + } + defer headCommit.Free() + + index, err := NewIndex() + if err != nil { + return nil, err + } + defer index.Free() + + blobOID, err := repo.CreateBlobFromBuffer([]byte(content)) + if err != nil { + return nil, err + } + + entry := &IndexEntry{ + Mode: FilemodeBlob, + Id: blobOID, + Path: something, + } + + if err := index.Add(entry); err != nil { + return nil, err + } + + newTreeOID, err := index.WriteTreeTo(repo) + if err != nil { + return nil, err + } + + newTree, err := repo.LookupTree(newTreeOID) + if err != nil { + return nil, err + } + defer newTree.Free() + + if err != nil { + return nil, err + } + commit, err := repo.CreateCommit("HEAD", signature(), signature(), "Test rebase, Baby! "+something, newTree, headCommit) + if err != nil { + return nil, err + } + + opts := &CheckoutOpts{ + Strategy: CheckoutRemoveUntracked | CheckoutForce, + } + err = repo.CheckoutIndex(index, opts) + if err != nil { + return nil, err + } + + return commit, nil +} + +func entryExists(repo *Repository, file string) bool { + headTree, err := headTree(repo) + if err != nil { + return false + } + defer headTree.Free() + + _, err = headTree.EntryByPath(file) + + return err == nil +} + +func commitMsgsList(repo *Repository) ([]string, error) { + head, err := headCommit(repo) + if err != nil { + return nil, err + } + defer head.Free() + + var commits []string + + parent := head.Parent(0) + defer parent.Free() + commits = append(commits, head.Message(), parent.Message()) + + for parent.ParentCount() != 0 { + parent = parent.Parent(0) + defer parent.Free() + commits = append(commits, parent.Message()) + } + + return commits, nil +} + +func assertStringList(t *testing.T, expected, actual []string) { + if len(expected) != len(actual) { + t.Fatal("Lists are not the same size, expected " + strconv.Itoa(len(expected)) + + ", got " + strconv.Itoa(len(actual))) + } + for index, element := range expected { + if element != actual[index] { + t.Error("Expected element " + strconv.Itoa(index) + " to be " + element + ", got " + actual[index]) + } + } +} diff --git a/reference_test.go b/reference_test.go index b6721e1..761daf8 100644 --- a/reference_test.go +++ b/reference_test.go @@ -8,7 +8,6 @@ import ( ) func TestRefModification(t *testing.T) { - t.Parallel() repo := createTestRepo(t) defer cleanupTestRepo(t, repo) @@ -54,7 +53,6 @@ func TestRefModification(t *testing.T) { } func TestReferenceIterator(t *testing.T) { - t.Parallel() repo := createTestRepo(t) defer cleanupTestRepo(t, repo) @@ -133,7 +131,6 @@ func TestReferenceIterator(t *testing.T) { } func TestReferenceOwner(t *testing.T) { - t.Parallel() repo := createTestRepo(t) defer cleanupTestRepo(t, repo) @@ -153,7 +150,6 @@ func TestReferenceOwner(t *testing.T) { } func TestUtil(t *testing.T) { - t.Parallel() repo := createTestRepo(t) defer cleanupTestRepo(t, repo) @@ -181,7 +177,6 @@ func TestUtil(t *testing.T) { } func TestIsNote(t *testing.T) { - t.Parallel() repo := createTestRepo(t) defer cleanupTestRepo(t, repo) @@ -215,7 +210,6 @@ func TestIsNote(t *testing.T) { } func TestReferenceIsValidName(t *testing.T) { - t.Parallel() if !ReferenceIsValidName("HEAD") { t.Errorf("HEAD should be a valid reference name") } diff --git a/remote_test.go b/remote_test.go index 3d00640..978b803 100644 --- a/remote_test.go +++ b/remote_test.go @@ -6,7 +6,6 @@ import ( ) func TestListRemotes(t *testing.T) { - t.Parallel() repo := createTestRepo(t) defer cleanupTestRepo(t, repo) @@ -34,7 +33,6 @@ func assertHostname(cert *Certificate, valid bool, hostname string, t *testing.T } func TestCertificateCheck(t *testing.T) { - t.Parallel() repo := createTestRepo(t) defer cleanupTestRepo(t, repo) @@ -54,7 +52,6 @@ func TestCertificateCheck(t *testing.T) { } func TestRemoteConnect(t *testing.T) { - t.Parallel() repo := createTestRepo(t) defer cleanupTestRepo(t, repo) @@ -66,7 +63,6 @@ func TestRemoteConnect(t *testing.T) { } func TestRemoteLs(t *testing.T) { - t.Parallel() repo := createTestRepo(t) defer cleanupTestRepo(t, repo) @@ -85,7 +81,6 @@ func TestRemoteLs(t *testing.T) { } func TestRemoteLsFiltering(t *testing.T) { - t.Parallel() repo := createTestRepo(t) defer cleanupTestRepo(t, repo) @@ -112,7 +107,6 @@ func TestRemoteLsFiltering(t *testing.T) { } func TestRemotePruneRefs(t *testing.T) { - t.Parallel() repo := createTestRepo(t) defer cleanupTestRepo(t, repo) @@ -135,7 +129,6 @@ func TestRemotePruneRefs(t *testing.T) { } func TestRemotePrune(t *testing.T) { - t.Parallel() remoteRepo := createTestRepo(t) defer cleanupTestRepo(t, remoteRepo) diff --git a/reset_test.go b/reset_test.go index 45777e4..ec578bd 100644 --- a/reset_test.go +++ b/reset_test.go @@ -6,7 +6,6 @@ import ( ) func TestResetToCommit(t *testing.T) { - t.Parallel() repo := createTestRepo(t) seedTestRepo(t, repo) // create commit to reset to diff --git a/revparse_test.go b/revparse_test.go index 2835434..75e9ffd 100644 --- a/revparse_test.go +++ b/revparse_test.go @@ -5,7 +5,6 @@ import ( ) func TestRevparse(t *testing.T) { - t.Parallel() repo := createTestRepo(t) defer cleanupTestRepo(t, repo) @@ -18,7 +17,6 @@ func TestRevparse(t *testing.T) { } func TestRevparseSingle(t *testing.T) { - t.Parallel() repo := createTestRepo(t) defer cleanupTestRepo(t, repo) @@ -31,7 +29,6 @@ func TestRevparseSingle(t *testing.T) { } func TestRevparseExt(t *testing.T) { - t.Parallel() repo := createTestRepo(t) defer cleanupTestRepo(t, repo) diff --git a/status.go b/status.go index e68e6e9..068a474 100644 --- a/status.go +++ b/status.go @@ -25,7 +25,6 @@ const ( StatusWtTypeChange Status = C.GIT_STATUS_WT_TYPECHANGE StatusWtRenamed Status = C.GIT_STATUS_WT_RENAMED StatusIgnored Status = C.GIT_STATUS_IGNORED - StatusConflicted Status = C.GIT_STATUS_CONFLICTED ) type StatusEntry struct { diff --git a/status_test.go b/status_test.go index 17ed94f..5b97b00 100644 --- a/status_test.go +++ b/status_test.go @@ -7,7 +7,6 @@ import ( ) func TestStatusFile(t *testing.T) { - t.Parallel() repo := createTestRepo(t) defer cleanupTestRepo(t, repo) @@ -28,7 +27,6 @@ func TestStatusFile(t *testing.T) { } func TestStatusList(t *testing.T) { - t.Parallel() repo := createTestRepo(t) defer cleanupTestRepo(t, repo) diff --git a/submodule_test.go b/submodule_test.go index fa2e98c..43c890a 100644 --- a/submodule_test.go +++ b/submodule_test.go @@ -5,7 +5,6 @@ import ( ) func TestSubmoduleForeach(t *testing.T) { - t.Parallel() repo := createTestRepo(t) defer cleanupTestRepo(t, repo) diff --git a/tag_test.go b/tag_test.go index 3404923..2fdfe00 100644 --- a/tag_test.go +++ b/tag_test.go @@ -7,7 +7,6 @@ import ( ) func TestCreateTag(t *testing.T) { - t.Parallel() repo := createTestRepo(t) defer cleanupTestRepo(t, repo) @@ -27,7 +26,6 @@ func TestCreateTag(t *testing.T) { } func TestCreateTagLightweight(t *testing.T) { - t.Parallel() repo := createTestRepo(t) defer cleanupTestRepo(t, repo) @@ -52,7 +50,6 @@ func TestCreateTagLightweight(t *testing.T) { } func TestListTags(t *testing.T) { - t.Parallel() repo := createTestRepo(t) defer cleanupTestRepo(t, repo) @@ -82,7 +79,6 @@ func TestListTags(t *testing.T) { } func TestListTagsWithMatch(t *testing.T) { - t.Parallel() repo := createTestRepo(t) defer cleanupTestRepo(t, repo) @@ -120,7 +116,6 @@ func TestListTagsWithMatch(t *testing.T) { } func TestTagForeach(t *testing.T) { - t.Parallel() repo := createTestRepo(t) defer cleanupTestRepo(t, repo) diff --git a/tree_test.go b/tree_test.go index f5b6822..fae395a 100644 --- a/tree_test.go +++ b/tree_test.go @@ -3,7 +3,6 @@ package git import "testing" func TestTreeEntryById(t *testing.T) { - t.Parallel() repo := createTestRepo(t) defer cleanupTestRepo(t, repo) @@ -23,7 +22,6 @@ func TestTreeEntryById(t *testing.T) { } func TestTreeBuilderInsert(t *testing.T) { - t.Parallel() repo := createTestRepo(t) defer cleanupTestRepo(t, repo)