summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--rebase.go47
-rw-r--r--rebase_test.go26
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 {