From b1a9de8037e4260dcd840db38e2e16473cdddcb6 Mon Sep 17 00:00:00 2001 From: ezwiebel Date: Sun, 7 Aug 2016 14:40:59 +1000 Subject: [PATCH 01/10] Initial rebase wrapper version --- rebase.go | 152 ++++++++++++++++++++++++++++++++++++++++++++ rebase_test.go | 167 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 319 insertions(+) create mode 100644 rebase.go create mode 100644 rebase_test.go diff --git a/rebase.go b/rebase.go new file mode 100644 index 0000000..3e7da53 --- /dev/null +++ b/rebase.go @@ -0,0 +1,152 @@ +package git + +/* +#include +*/ +import "C" +import ( + "errors" + "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 rebaseOperationFromC(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{} + +// Rebase object wrapper for C pointer +type Rebase struct { + ptr *C.git_rebase +} + +//RebaseInit initializes a rebase operation to rebase the changes in branch relative to upstream onto another branch. +func (r *Repository) RebaseInit(branch *AnnotatedCommit, upstream *AnnotatedCommit, onto *AnnotatedCommit, opts *RebaseOptions) (*Rebase, error) { + runtime.LockOSThread() + defer runtime.UnlockOSThread() + + //TODO : use real rebase_options + if opts != nil { + return nil, errors.New("RebaseOptions Not implemented yet") + } + + 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, nil) + if err < 0 { + return nil, MakeGitError(err) + } + + return newRebaseFromC(ptr), nil +} + +// 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 GIT_REBASE_OPERATION_EXEC) +// 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 rebaseOperationFromC(ptr), nil +} + +// Commit commits the current patch. +// You must have resolved any conflicts that were introduced during the patch application from the git_rebase_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 + } + + 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 +} + +//Free frees the Rebase object and underlying git_rebase C pointer. +func (rebase *Rebase) Free() { + runtime.SetFinalizer(rebase, nil) + C.git_reference_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..d4b4251 --- /dev/null +++ b/rebase_test.go @@ -0,0 +1,167 @@ +package git + +import ( + "testing" + "time" +) + +func createBranch(repo *Repository, branch string) error { + head, err := repo.Head() + if err != nil { + return err + } + commit, err := repo.LookupCommit(head.Target()) + if err != nil { + return err + } + _, 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 commitSomething(repo *Repository, something string) (*Oid, error) { + head, err := repo.Head() + if err != nil { + return nil, err + } + + headCommit, err := repo.LookupCommit(head.Target()) + if err != nil { + return nil, err + } + + index, err := NewIndex() + if err != nil { + return nil, err + } + defer index.Free() + + blobOID, err := repo.CreateBlobFromBuffer([]byte("fou")) + 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 + } + + if err != nil { + return nil, err + } + commit, err := repo.CreateCommit("HEAD", signature(), signature(), "Test rebase onto, 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 { + head, err := repo.Head() + if err != nil { + return false + } + headCommit, err := repo.LookupCommit(head.Target()) + if err != nil { + return false + } + headTree, err := headCommit.Tree() + if err != nil { + return false + } + _, err = headTree.EntryByPath(file) + + return err == nil +} + +func TestRebaseOnto(t *testing.T) { + repo := createTestRepo(t) + defer cleanupTestRepo(t, repo) + + fileInMaster := "something" + fileInEmile := "something else" + + // Seed master + seedTestRepo(t, repo) + + // Create a new branch from master + err := createBranch(repo, "emile") + checkFatal(t, err) + + // Create a commit in master + _, err = commitSomething(repo, fileInMaster) + checkFatal(t, err) + + // Switch to this emile + err = repo.SetHead("refs/heads/emile") + checkFatal(t, err) + + // Check master commit is not in emile branch + if entryExists(repo, fileInMaster) { + t.Fatal("something entry should not exist in emile branch") + } + + // Create a commit in emile + _, err = commitSomething(repo, fileInEmile) + checkFatal(t, err) + + // Rebase onto master + master, err := repo.LookupBranch("master", BranchLocal) + branch, err := repo.AnnotatedCommitFromRef(master.Reference) + checkFatal(t, err) + + rebase, err := repo.RebaseInit(nil, nil, branch, nil) + checkFatal(t, err) + defer rebase.Free() + + operation, err := rebase.Next() + checkFatal(t, err) + + commit, err := repo.LookupCommit(operation.ID) + checkFatal(t, err) + + err = rebase.Commit(operation.ID, signature(), signature(), commit.Message()) + checkFatal(t, err) + + rebase.Finish() + + // Check master commit is now also in emile branch + if !entryExists(repo, fileInMaster) { + t.Fatal("something entry should now exist in emile branch") + } +} From a62a8c3b92eeb14055d30608ac7d3243bcfba701 Mon Sep 17 00:00:00 2001 From: ezwiebel Date: Sun, 7 Aug 2016 16:33:06 +1000 Subject: [PATCH 02/10] Add operation OperationCount() service and enrich UTs --- rebase.go | 15 +++ rebase_test.go | 251 ++++++++++++++++++++++++++++++++++++------------- 2 files changed, 201 insertions(+), 65 deletions(-) diff --git a/rebase.go b/rebase.go index 3e7da53..fb3d612 100644 --- a/rebase.go +++ b/rebase.go @@ -139,6 +139,11 @@ func (rebase *Rebase) Finish() error { return 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)) +} + //Free frees the Rebase object and underlying git_rebase C pointer. func (rebase *Rebase) Free() { runtime.SetFinalizer(rebase, nil) @@ -150,3 +155,13 @@ func newRebaseFromC(ptr *C.git_rebase) *Rebase { runtime.SetFinalizer(rebase, (*Rebase).Free) return rebase } + +/* TODO -- Add last wrapper services and manage rebase_options + +int git_rebase_abort(git_rebase *rebase); +int git_rebase_init_options(git_rebase_options *opts, unsigned int version); +int git_rebase_open(git_rebase **out, git_repository *repo, const git_rebase_options *opts); +git_rebase_operation * git_rebase_operation_byindex(git_rebase *rebase, size_t idx); +size_t git_rebase_operation_current(git_rebase *rebase); + +*/ diff --git a/rebase_test.go b/rebase_test.go index d4b4251..7106e7f 100644 --- a/rebase_test.go +++ b/rebase_test.go @@ -1,19 +1,142 @@ package git import ( + "errors" + "strconv" "testing" "time" ) +// Tests + +func TestRebaseNoConflicts(t *testing.T) { + // TEST DATA + + // Inputs + branchName := "emile" + masterCommit := "something" + emileCommits := []string{ + "fou", + "barre", + "ouich", + } + + // Outputs + expectedHistory := []string{ + "Test rebase onto, Baby! " + emileCommits[2], + "Test rebase onto, Baby! " + emileCommits[1], + "Test rebase onto, Baby! " + emileCommits[0], + "Test rebase onto, Baby! " + masterCommit, + "This is a commit\n", + } + + // TEST + repo := createTestRepo(t) + seedTestRepo(t, repo) + + // Setup a repo with 2 branches and a different tree + err := setupRepoForRebase(repo, masterCommit, branchName) + checkFatal(t, err) + defer cleanupTestRepo(t, repo) + + // Create several commits in emile + for _, commit := range emileCommits { + _, err = commitSomething(repo, commit, commit) + checkFatal(t, err) + } + + // Rebase onto master + err = performRebaseOnto(repo, "master") + checkFatal(t, err) + + // 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) error { + master, err := repo.LookupBranch(branch, BranchLocal) + if err != nil { + return err + } + defer master.Free() + + onto, err := repo.AnnotatedCommitFromRef(master.Reference) + if err != nil { + return err + } + defer onto.Free() + + rebase, err := repo.RebaseInit(nil, nil, onto, nil) + if err != nil { + return err + } + defer rebase.Free() + + opCount := int(rebase.OperationCount()) + + for op := 0; op < opCount; op++ { + operation, err := rebase.Next() + if err != nil { + return err + } + + commit, err := repo.LookupCommit(operation.ID) + if err != nil { + return err + } + defer commit.Free() + + err = rebase.Commit(operation.ID, signature(), signature(), commit.Message()) + if err != nil { + return err + } + } + + err = rebase.Finish() + if err != nil { + return err + } + + return nil +} + func createBranch(repo *Repository, branch string) error { - head, err := repo.Head() - if err != nil { - return err - } - commit, err := repo.LookupCommit(head.Target()) + commit, err := headCommit(repo) if err != nil { return err } + defer commit.Free() _, err = repo.CreateBranch(branch, commit, false) if err != nil { return err @@ -30,24 +153,50 @@ func signature() *Signature { } } -func commitSomething(repo *Repository, something string) (*Oid, error) { +func headCommit(repo *Repository) (*Commit, error) { head, err := repo.Head() if err != nil { return nil, err } + defer head.Free() - headCommit, err := repo.LookupCommit(head.Target()) + 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("fou")) + blobOID, err := repo.CreateBlobFromBuffer([]byte(content)) if err != nil { return nil, err } @@ -71,6 +220,7 @@ func commitSomething(repo *Repository, something string) (*Oid, error) { if err != nil { return nil, err } + defer newTree.Free() if err != nil { return nil, err @@ -92,76 +242,47 @@ func commitSomething(repo *Repository, something string) (*Oid, error) { } func entryExists(repo *Repository, file string) bool { - head, err := repo.Head() - if err != nil { - return false - } - headCommit, err := repo.LookupCommit(head.Target()) - if err != nil { - return false - } - headTree, err := headCommit.Tree() + headTree, err := headTree(repo) if err != nil { return false } + defer headTree.Free() + _, err = headTree.EntryByPath(file) return err == nil } -func TestRebaseOnto(t *testing.T) { - repo := createTestRepo(t) - defer cleanupTestRepo(t, repo) +func commitMsgsList(repo *Repository) ([]string, error) { + head, err := headCommit(repo) + if err != nil { + return nil, err + } + defer head.Free() - fileInMaster := "something" - fileInEmile := "something else" + var commits []string - // Seed master - seedTestRepo(t, repo) + parent := head.Parent(0) + defer parent.Free() + commits = append(commits, head.Message(), parent.Message()) - // Create a new branch from master - err := createBranch(repo, "emile") - checkFatal(t, err) - - // Create a commit in master - _, err = commitSomething(repo, fileInMaster) - checkFatal(t, err) - - // Switch to this emile - err = repo.SetHead("refs/heads/emile") - checkFatal(t, err) - - // Check master commit is not in emile branch - if entryExists(repo, fileInMaster) { - t.Fatal("something entry should not exist in emile branch") + for parent.ParentCount() != 0 { + parent = parent.Parent(0) + defer parent.Free() + commits = append(commits, parent.Message()) } - // Create a commit in emile - _, err = commitSomething(repo, fileInEmile) - checkFatal(t, err) + return commits, nil +} - // Rebase onto master - master, err := repo.LookupBranch("master", BranchLocal) - branch, err := repo.AnnotatedCommitFromRef(master.Reference) - checkFatal(t, err) - - rebase, err := repo.RebaseInit(nil, nil, branch, nil) - checkFatal(t, err) - defer rebase.Free() - - operation, err := rebase.Next() - checkFatal(t, err) - - commit, err := repo.LookupCommit(operation.ID) - checkFatal(t, err) - - err = rebase.Commit(operation.ID, signature(), signature(), commit.Message()) - checkFatal(t, err) - - rebase.Finish() - - // Check master commit is now also in emile branch - if !entryExists(repo, fileInMaster) { - t.Fatal("something entry should now exist in emile branch") +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]) + } } } From e1f09497409bb8dea59d8312f1049267cee37544 Mon Sep 17 00:00:00 2001 From: ezwiebel Date: Sun, 7 Aug 2016 17:00:14 +1000 Subject: [PATCH 03/10] Add Abort() service to wrapper --- rebase.go | 10 +++++- rebase_test.go | 91 +++++++++++++++++++++++++++++++++++++++----------- 2 files changed, 80 insertions(+), 21 deletions(-) diff --git a/rebase.go b/rebase.go index fb3d612..ee409df 100644 --- a/rebase.go +++ b/rebase.go @@ -50,6 +50,15 @@ type Rebase struct { ptr *C.git_rebase } +// 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 { + err := C.git_rebase_abort(rebase.ptr) + if err < 0 { + return MakeGitError(err) + } + return nil +} + //RebaseInit initializes a rebase operation to rebase the changes in branch relative to upstream onto another branch. func (r *Repository) RebaseInit(branch *AnnotatedCommit, upstream *AnnotatedCommit, onto *AnnotatedCommit, opts *RebaseOptions) (*Rebase, error) { runtime.LockOSThread() @@ -158,7 +167,6 @@ func newRebaseFromC(ptr *C.git_rebase) *Rebase { /* TODO -- Add last wrapper services and manage rebase_options -int git_rebase_abort(git_rebase *rebase); int git_rebase_init_options(git_rebase_options *opts, unsigned int version); int git_rebase_open(git_rebase **out, git_repository *repo, const git_rebase_options *opts); git_rebase_operation * git_rebase_operation_byindex(git_rebase *rebase, size_t idx); diff --git a/rebase_test.go b/rebase_test.go index 7106e7f..dc5aeb3 100644 --- a/rebase_test.go +++ b/rebase_test.go @@ -9,6 +9,58 @@ import ( // 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) + seedTestRepo(t, repo) + + // Setup a repo with 2 branches and a different tree + err := setupRepoForRebase(repo, masterCommit, branchName) + checkFatal(t, err) + defer cleanupTestRepo(t, repo) + + // 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 @@ -23,10 +75,10 @@ func TestRebaseNoConflicts(t *testing.T) { // Outputs expectedHistory := []string{ - "Test rebase onto, Baby! " + emileCommits[2], - "Test rebase onto, Baby! " + emileCommits[1], - "Test rebase onto, Baby! " + emileCommits[0], - "Test rebase onto, Baby! " + masterCommit, + "Test rebase, Baby! " + emileCommits[2], + "Test rebase, Baby! " + emileCommits[1], + "Test rebase, Baby! " + emileCommits[0], + "Test rebase, Baby! " + masterCommit, "This is a commit\n", } @@ -46,7 +98,12 @@ func TestRebaseNoConflicts(t *testing.T) { } // Rebase onto master - err = performRebaseOnto(repo, "master") + rebase, err := performRebaseOnto(repo, "master") + checkFatal(t, err) + defer rebase.Free() + + // Finish the rebase properly + err = rebase.Finish() checkFatal(t, err) // Check history is in correct order @@ -84,51 +141,45 @@ func setupRepoForRebase(repo *Repository, masterCommit, branchName string) error return nil } -func performRebaseOnto(repo *Repository, branch string) error { +func performRebaseOnto(repo *Repository, branch string) (*Rebase, error) { master, err := repo.LookupBranch(branch, BranchLocal) if err != nil { - return err + return nil, err } defer master.Free() onto, err := repo.AnnotatedCommitFromRef(master.Reference) if err != nil { - return err + return nil, err } defer onto.Free() rebase, err := repo.RebaseInit(nil, nil, onto, nil) if err != nil { - return err + return nil, err } - defer rebase.Free() opCount := int(rebase.OperationCount()) for op := 0; op < opCount; op++ { operation, err := rebase.Next() if err != nil { - return err + return nil, err } commit, err := repo.LookupCommit(operation.ID) if err != nil { - return err + return nil, err } defer commit.Free() err = rebase.Commit(operation.ID, signature(), signature(), commit.Message()) if err != nil { - return err + return nil, err } } - err = rebase.Finish() - if err != nil { - return err - } - - return nil + return rebase, nil } func createBranch(repo *Repository, branch string) error { @@ -225,7 +276,7 @@ func commitSomething(repo *Repository, something, content string) (*Oid, error) if err != nil { return nil, err } - commit, err := repo.CreateCommit("HEAD", signature(), signature(), "Test rebase onto, Baby! "+something, newTree, headCommit) + commit, err := repo.CreateCommit("HEAD", signature(), signature(), "Test rebase, Baby! "+something, newTree, headCommit) if err != nil { return nil, err } From 193b21398b76d602daabfef4d2462531c9aec410 Mon Sep 17 00:00:00 2001 From: ezwiebel Date: Sun, 7 Aug 2016 17:31:42 +1000 Subject: [PATCH 04/10] Add CurrentOperationIndex() and OperationAt(index uint) services to wrapper --- rebase.go | 47 ++++++++++++++++++++++++++++++----------------- rebase_test.go | 26 +++++++++++++++++++++++--- 2 files changed, 53 insertions(+), 20 deletions(-) diff --git a/rebase.go b/rebase.go index ee409df..2218295 100644 --- a/rebase.go +++ b/rebase.go @@ -33,7 +33,7 @@ type RebaseOperation struct { Exec string } -func rebaseOperationFromC(c *C.git_rebase_operation) *RebaseOperation { +func newRebaseOperationFromC(c *C.git_rebase_operation) *RebaseOperation { operation := &RebaseOperation{} operation.Type = RebaseOperationType(c._type) operation.ID = newOidFromC(&c.id) @@ -50,15 +50,6 @@ type Rebase struct { ptr *C.git_rebase } -// 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 { - err := C.git_rebase_abort(rebase.ptr) - if err < 0 { - return MakeGitError(err) - } - return nil -} - //RebaseInit initializes a rebase operation to rebase the changes in branch relative to upstream onto another branch. func (r *Repository) RebaseInit(branch *AnnotatedCommit, upstream *AnnotatedCommit, onto *AnnotatedCommit, opts *RebaseOptions) (*Rebase, error) { runtime.LockOSThread() @@ -66,7 +57,7 @@ func (r *Repository) RebaseInit(branch *AnnotatedCommit, upstream *AnnotatedComm //TODO : use real rebase_options if opts != nil { - return nil, errors.New("RebaseOptions Not implemented yet") + return nil, errors.New("RebaseOptions Not implemented yet, use nil for default opts") } if branch == nil { @@ -90,6 +81,23 @@ func (r *Repository) RebaseInit(branch *AnnotatedCommit, upstream *AnnotatedComm 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. +// If the first operation has not yet been applied then this returns -1 (C.GIT_REBASE_NO_OPERATION). +func (rebase *Rebase) CurrentOperationIndex() int { + return int(C.git_rebase_operation_current(rebase.ptr)) +} + +// 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 GIT_REBASE_OPERATION_EXEC) // then the patch will be applied and the index and working directory will be updated with the changes. @@ -104,7 +112,7 @@ func (rebase *Rebase) Next() (*RebaseOperation, error) { return nil, MakeGitError(err) } - return rebaseOperationFromC(ptr), nil + return newRebaseOperationFromC(ptr), nil } // Commit commits the current patch. @@ -148,9 +156,16 @@ func (rebase *Rebase) Finish() error { return 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)) +// 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 and underlying git_rebase C pointer. @@ -169,7 +184,5 @@ func newRebaseFromC(ptr *C.git_rebase) *Rebase { int git_rebase_init_options(git_rebase_options *opts, unsigned int version); int git_rebase_open(git_rebase **out, git_repository *repo, const git_rebase_options *opts); -git_rebase_operation * git_rebase_operation_byindex(git_rebase *rebase, size_t idx); -size_t git_rebase_operation_current(git_rebase *rebase); */ diff --git a/rebase_test.go b/rebase_test.go index dc5aeb3..e97a1a7 100644 --- a/rebase_test.go +++ b/rebase_test.go @@ -29,12 +29,12 @@ func TestRebaseAbort(t *testing.T) { // 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) - defer cleanupTestRepo(t, repo) // Create several commits in emile for _, commit := range emileCommits { @@ -84,12 +84,12 @@ func TestRebaseNoConflicts(t *testing.T) { // 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) - defer cleanupTestRepo(t, repo) // Create several commits in emile for _, commit := range emileCommits { @@ -154,25 +154,41 @@ func performRebaseOnto(repo *Repository, branch string) (*Rebase, error) { } defer onto.Free() + // Init rebase rebase, err := repo.RebaseInit(nil, nil, onto, nil) if err != nil { return nil, err } - opCount := int(rebase.OperationCount()) + // Check no operation has been started yet + if rebase.CurrentOperationIndex() != -1 { // -1 == GIT_REBASE_NO_OPERATION + 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 + if rebase.CurrentOperationIndex() != 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 @@ -182,6 +198,10 @@ func performRebaseOnto(repo *Repository, branch string) (*Rebase, error) { 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 { From b2d71f4fbc4bf6d0d1e5b29e38270f4760db3f04 Mon Sep 17 00:00:00 2001 From: ezwiebel Date: Mon, 8 Aug 2016 10:49:40 +1000 Subject: [PATCH 05/10] Fix Free() service in Rebase wrapper --- rebase.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rebase.go b/rebase.go index 2218295..ab112c0 100644 --- a/rebase.go +++ b/rebase.go @@ -171,7 +171,7 @@ func (rebase *Rebase) Abort() error { //Free frees the Rebase object and underlying git_rebase C pointer. func (rebase *Rebase) Free() { runtime.SetFinalizer(rebase, nil) - C.git_reference_free(rebase.ptr) + C.git_rebase_free(rebase.ptr) } func newRebaseFromC(ptr *C.git_rebase) *Rebase { From e00b0831aaefeceab320c8d2fdc23fffcca58168 Mon Sep 17 00:00:00 2001 From: ezwiebel Date: Tue, 6 Sep 2016 14:15:10 +1000 Subject: [PATCH 06/10] Add RebaseOpen() service to wrapper --- rebase.go | 20 +++++++++++++++++++- rebase_test.go | 16 +++++++++++++++- 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/rebase.go b/rebase.go index ab112c0..a51b7ce 100644 --- a/rebase.go +++ b/rebase.go @@ -81,6 +81,25 @@ func (r *Repository) RebaseInit(branch *AnnotatedCommit, upstream *AnnotatedComm return newRebaseFromC(ptr), nil } +//RebaseOpen opens an existing rebase that was previously started by either an invocation of git_rebase_init or by another client. +func (r *Repository) RebaseOpen(opts *RebaseOptions) (*Rebase, error) { + runtime.LockOSThread() + defer runtime.UnlockOSThread() + + //TODO : use real rebase_options + if opts != nil { + return nil, errors.New("RebaseOptions Not implemented yet, use nil for default opts") + } + + var ptr *C.git_rebase + err := C.git_rebase_open(&ptr, r.ptr, nil) + 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)) @@ -183,6 +202,5 @@ func newRebaseFromC(ptr *C.git_rebase) *Rebase { /* TODO -- Add last wrapper services and manage rebase_options int git_rebase_init_options(git_rebase_options *opts, unsigned int version); -int git_rebase_open(git_rebase **out, git_repository *repo, const git_rebase_options *opts); */ diff --git a/rebase_test.go b/rebase_test.go index e97a1a7..745297e 100644 --- a/rebase_test.go +++ b/rebase_test.go @@ -87,8 +87,14 @@ func TestRebaseNoConflicts(t *testing.T) { defer cleanupTestRepo(t, repo) seedTestRepo(t, repo) + // Try to open existing rebase + oRebase, err := repo.RebaseOpen(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) + err = setupRepoForRebase(repo, masterCommit, branchName) checkFatal(t, err) // Create several commits in emile @@ -102,6 +108,14 @@ func TestRebaseNoConflicts(t *testing.T) { checkFatal(t, err) defer rebase.Free() + // Open existing rebase + oRebase, err = repo.RebaseOpen(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) From adc3a4bd89723785b5f04dd14691472300a03bdb Mon Sep 17 00:00:00 2001 From: ezwiebel Date: Tue, 13 Sep 2016 16:03:16 +1000 Subject: [PATCH 07/10] Add DefaultRebaseOptions() [git_rebase_init_options(GIT_REBASE_OPTIONS_VERSION)] service to wrapper --- rebase.go | 69 +++++++++++++++++++++++++++++++++++--------------- rebase_test.go | 23 +++++++++++++++++ 2 files changed, 72 insertions(+), 20 deletions(-) diff --git a/rebase.go b/rebase.go index a51b7ce..464840b 100644 --- a/rebase.go +++ b/rebase.go @@ -5,7 +5,6 @@ package git */ import "C" import ( - "errors" "runtime" "unsafe" ) @@ -43,7 +42,53 @@ func newRebaseOperationFromC(c *C.git_rebase_operation) *RebaseOperation { } // RebaseOptions are used to tell the rebase machinery how to operate -type RebaseOptions struct{} +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: C.CString(ro.RewriteNotesRef), + merge_options: *ro.MergeOptions.toC(), + checkout_options: *ro.CheckoutOptions.toC(), + } +} // Rebase object wrapper for C pointer type Rebase struct { @@ -55,11 +100,6 @@ func (r *Repository) RebaseInit(branch *AnnotatedCommit, upstream *AnnotatedComm runtime.LockOSThread() defer runtime.UnlockOSThread() - //TODO : use real rebase_options - if opts != nil { - return nil, errors.New("RebaseOptions Not implemented yet, use nil for default opts") - } - if branch == nil { branch = &AnnotatedCommit{ptr: nil} } @@ -73,7 +113,7 @@ func (r *Repository) RebaseInit(branch *AnnotatedCommit, upstream *AnnotatedComm } var ptr *C.git_rebase - err := C.git_rebase_init(&ptr, r.ptr, branch.ptr, upstream.ptr, onto.ptr, nil) + err := C.git_rebase_init(&ptr, r.ptr, branch.ptr, upstream.ptr, onto.ptr, opts.toC()) if err < 0 { return nil, MakeGitError(err) } @@ -86,13 +126,8 @@ func (r *Repository) RebaseOpen(opts *RebaseOptions) (*Rebase, error) { runtime.LockOSThread() defer runtime.UnlockOSThread() - //TODO : use real rebase_options - if opts != nil { - return nil, errors.New("RebaseOptions Not implemented yet, use nil for default opts") - } - var ptr *C.git_rebase - err := C.git_rebase_open(&ptr, r.ptr, nil) + err := C.git_rebase_open(&ptr, r.ptr, opts.toC()) if err < 0 { return nil, MakeGitError(err) } @@ -198,9 +233,3 @@ func newRebaseFromC(ptr *C.git_rebase) *Rebase { runtime.SetFinalizer(rebase, (*Rebase).Free) return rebase } - -/* TODO -- Add last wrapper services and manage rebase_options - -int git_rebase_init_options(git_rebase_options *opts, unsigned int version); - -*/ diff --git a/rebase_test.go b/rebase_test.go index 745297e..f76baed 100644 --- a/rebase_test.go +++ b/rebase_test.go @@ -9,6 +9,29 @@ import ( // Tests +func TestDefaultRebaseOptions(t *testing.T) { + opts, err := DefaultRebaseOptions() + checkFatal(t, err) + + if opts.Version != 1 { + t.Error("Expected opts Version to equal 1, got ", opts.Version) + } + if opts.Quiet != 0 { + t.Error("Expected opts Quiet to equal 1, got ", opts.Quiet) + } + if opts.InMemory != 0 { + t.Error("Expected opts InMemory to equal 1, got ", opts.InMemory) + } + if opts.RewriteNotesRef != "" { + t.Error("Expected opts RewriteNotesRef to equal 1, got ", opts.RewriteNotesRef) + } + + copts := opts.toC() + if copts == nil { + t.Error("Copts should not be nil") + } +} + func TestRebaseAbort(t *testing.T) { // TEST DATA From 03e10c56391b1d7ca7e2c4e59417500bea2bd242 Mon Sep 17 00:00:00 2001 From: ezwiebel Date: Wed, 14 Sep 2016 14:42:58 +1000 Subject: [PATCH 08/10] Fix reference bug introduced with RebaseOptions implementation --- rebase.go | 9 ++++++++- rebase_test.go | 6 ++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/rebase.go b/rebase.go index 464840b..f28c7f6 100644 --- a/rebase.go +++ b/rebase.go @@ -84,12 +84,19 @@ func (ro *RebaseOptions) toC() *C.git_rebase_options { version: C.uint(ro.Version), quiet: C.int(ro.Quiet), inmemory: C.int(ro.InMemory), - rewrite_notes_ref: C.CString(ro.RewriteNotesRef), + rewrite_notes_ref: rewriteNotesRefToC(ro.RewriteNotesRef), merge_options: *ro.MergeOptions.toC(), checkout_options: *ro.CheckoutOptions.toC(), } } +func rewriteNotesRefToC(ref string) *C.char { + if ref == "" { + return nil + } + return C.CString(ref) +} + // Rebase object wrapper for C pointer type Rebase struct { ptr *C.git_rebase diff --git a/rebase_test.go b/rebase_test.go index f76baed..38d6868 100644 --- a/rebase_test.go +++ b/rebase_test.go @@ -143,6 +143,12 @@ func TestRebaseNoConflicts(t *testing.T) { err = rebase.Finish() checkFatal(t, err) + // Check no more rebase is in progress + oRebase, err = repo.RebaseOpen(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) From a671e67ee884c0c69b6216ac0bff01dfde4dd622 Mon Sep 17 00:00:00 2001 From: ezwiebel Date: Tue, 1 Nov 2016 10:57:23 +1100 Subject: [PATCH 09/10] Took @carlosmn PR review into account --- git.go | 1 + rebase.go | 35 ++++++++++++++++++++--------------- rebase_test.go | 20 +++++++++++--------- 3 files changed, 32 insertions(+), 24 deletions(-) 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/rebase.go b/rebase.go index f28c7f6..8384d68 100644 --- a/rebase.go +++ b/rebase.go @@ -28,14 +28,14 @@ const ( // RebaseOperation describes a single instruction/operation to be performed during the rebase. type RebaseOperation struct { Type RebaseOperationType - ID *Oid + 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.Id = newOidFromC(&c.id) operation.Exec = C.GoString(c.exec) return operation @@ -84,26 +84,26 @@ func (ro *RebaseOptions) toC() *C.git_rebase_options { version: C.uint(ro.Version), quiet: C.int(ro.Quiet), inmemory: C.int(ro.InMemory), - rewrite_notes_ref: rewriteNotesRefToC(ro.RewriteNotesRef), + rewrite_notes_ref: mapEmptyStringToNull(ro.RewriteNotesRef), merge_options: *ro.MergeOptions.toC(), checkout_options: *ro.CheckoutOptions.toC(), } } -func rewriteNotesRefToC(ref string) *C.char { +func mapEmptyStringToNull(ref string) *C.char { if ref == "" { return nil } return C.CString(ref) } -// Rebase object wrapper for C pointer +// Rebase is the struct representing a Rebase object. type Rebase struct { ptr *C.git_rebase } -//RebaseInit initializes a rebase operation to rebase the changes in branch relative to upstream onto another branch. -func (r *Repository) RebaseInit(branch *AnnotatedCommit, upstream *AnnotatedCommit, onto *AnnotatedCommit, opts *RebaseOptions) (*Rebase, error) { +// 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() @@ -128,8 +128,8 @@ func (r *Repository) RebaseInit(branch *AnnotatedCommit, upstream *AnnotatedComm return newRebaseFromC(ptr), nil } -//RebaseOpen opens an existing rebase that was previously started by either an invocation of git_rebase_init or by another client. -func (r *Repository) RebaseOpen(opts *RebaseOptions) (*Rebase, error) { +// 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() @@ -149,9 +149,13 @@ func (rebase *Rebase) OperationAt(index uint) *RebaseOperation { } // CurrentOperationIndex gets the index of the rebase operation that is currently being applied. -// If the first operation has not yet been applied then this returns -1 (C.GIT_REBASE_NO_OPERATION). -func (rebase *Rebase) CurrentOperationIndex() int { - return int(C.git_rebase_operation_current(rebase.ptr)) +// Returns an error if no rebase operation is currently applied. +func (rebase *Rebase) CurrentOperationIndex() (uint, error) { + 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. @@ -160,7 +164,7 @@ func (rebase *Rebase) OperationCount() uint { } // 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 GIT_REBASE_OPERATION_EXEC) +// 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) { @@ -177,7 +181,7 @@ func (rebase *Rebase) Next() (*RebaseOperation, error) { } // Commit commits the current patch. -// You must have resolved any conflicts that were introduced during the patch application from the git_rebase_next invocation. +// 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() @@ -192,6 +196,7 @@ func (rebase *Rebase) Commit(ID *Oid, author, committer *Signature, message stri if err != nil { return err } + defer C.git_signature_free(committerSig) cmsg := C.CString(message) defer C.free(unsafe.Pointer(cmsg)) @@ -229,7 +234,7 @@ func (rebase *Rebase) Abort() error { return nil } -//Free frees the Rebase object and underlying git_rebase C pointer. +// Free frees the Rebase object. func (rebase *Rebase) Free() { runtime.SetFinalizer(rebase, nil) C.git_rebase_free(rebase.ptr) diff --git a/rebase_test.go b/rebase_test.go index 38d6868..9c8e0c2 100644 --- a/rebase_test.go +++ b/rebase_test.go @@ -111,7 +111,7 @@ func TestRebaseNoConflicts(t *testing.T) { seedTestRepo(t, repo) // Try to open existing rebase - oRebase, err := repo.RebaseOpen(nil) + oRebase, err := repo.OpenRebase(nil) if err == nil { t.Fatal("Did not expect to find a rebase in progress") } @@ -132,7 +132,7 @@ func TestRebaseNoConflicts(t *testing.T) { defer rebase.Free() // Open existing rebase - oRebase, err = repo.RebaseOpen(nil) + oRebase, err = repo.OpenRebase(nil) checkFatal(t, err) defer oRebase.Free() if oRebase == nil { @@ -144,7 +144,7 @@ func TestRebaseNoConflicts(t *testing.T) { checkFatal(t, err) // Check no more rebase is in progress - oRebase, err = repo.RebaseOpen(nil) + oRebase, err = repo.OpenRebase(nil) if err == nil { t.Fatal("Did not expect to find a rebase in progress") } @@ -198,13 +198,14 @@ func performRebaseOnto(repo *Repository, branch string) (*Rebase, error) { defer onto.Free() // Init rebase - rebase, err := repo.RebaseInit(nil, nil, onto, nil) + rebase, err := repo.InitRebase(nil, nil, onto, nil) if err != nil { return nil, err } // Check no operation has been started yet - if rebase.CurrentOperationIndex() != -1 { // -1 == GIT_REBASE_NO_OPERATION + rebaseOperationIndex, err := rebase.CurrentOperationIndex() + if err == nil { return nil, errors.New("No operation should have been started yet") } @@ -217,7 +218,8 @@ func performRebaseOnto(repo *Repository, branch string) (*Rebase, error) { } // Check operation index is correct - if rebase.CurrentOperationIndex() != op { + rebaseOperationIndex, err = rebase.CurrentOperationIndex() + if int(rebaseOperationIndex) != op { return nil, errors.New("Bad operation index") } if !operationsAreEqual(rebase.OperationAt(uint(op)), operation) { @@ -225,14 +227,14 @@ func performRebaseOnto(repo *Repository, branch string) (*Rebase, error) { } // Get current rebase operation created commit - commit, err := repo.LookupCommit(operation.ID) + 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()) + err = rebase.Commit(operation.Id, signature(), signature(), commit.Message()) if err != nil { return nil, err } @@ -242,7 +244,7 @@ func performRebaseOnto(repo *Repository, branch string) (*Rebase, error) { } func operationsAreEqual(l, r *RebaseOperation) bool { - return l.Exec == r.Exec && l.Type == r.Type && l.ID.String() == r.ID.String() + return l.Exec == r.Exec && l.Type == r.Type && l.Id.String() == r.Id.String() } func createBranch(repo *Repository, branch string) error { From 6118c9ba37f494cbb892aa686416280ae6ee6593 Mon Sep 17 00:00:00 2001 From: ezwiebel Date: Wed, 23 Nov 2016 17:10:59 +1100 Subject: [PATCH 10/10] LockOSThread in CurrentOperationIndex for git error creation --- rebase.go | 5 +++++ rebase_test.go | 23 ----------------------- 2 files changed, 5 insertions(+), 23 deletions(-) diff --git a/rebase.go b/rebase.go index 8384d68..8553e25 100644 --- a/rebase.go +++ b/rebase.go @@ -145,16 +145,21 @@ func (r *Repository) OpenRebase(opts *RebaseOptions) (*Rebase, error) { // 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 } diff --git a/rebase_test.go b/rebase_test.go index 9c8e0c2..fb88a4e 100644 --- a/rebase_test.go +++ b/rebase_test.go @@ -9,29 +9,6 @@ import ( // Tests -func TestDefaultRebaseOptions(t *testing.T) { - opts, err := DefaultRebaseOptions() - checkFatal(t, err) - - if opts.Version != 1 { - t.Error("Expected opts Version to equal 1, got ", opts.Version) - } - if opts.Quiet != 0 { - t.Error("Expected opts Quiet to equal 1, got ", opts.Quiet) - } - if opts.InMemory != 0 { - t.Error("Expected opts InMemory to equal 1, got ", opts.InMemory) - } - if opts.RewriteNotesRef != "" { - t.Error("Expected opts RewriteNotesRef to equal 1, got ", opts.RewriteNotesRef) - } - - copts := opts.toC() - if copts == nil { - t.Error("Copts should not be nil") - } -} - func TestRebaseAbort(t *testing.T) { // TEST DATA