Skip to content

Commit b98b0e7

Browse files
committed
rebase: correct the return values for CurrentOperationIndex
We were incorectly reporting `C.GIT_REBASE_NO_OPERATION` as an error code when it is none. We should instead return it as the value. The compiler doesn't seem to actually look at the sizes so instead we must recreate the value ourselves with `^uint(0)`. The error return is kept for API compatibility but should go away eventually.
1 parent ce65c23 commit b98b0e7

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

rebase.go

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package git
55
*/
66
import "C"
77
import (
8+
"errors"
89
"runtime"
910
"unsafe"
1011
)
@@ -25,6 +26,12 @@ const (
2526
RebaseOperationExec RebaseOperationType = C.GIT_REBASE_OPERATION_EXEC
2627
)
2728

29+
// Special value indicating that there is no currently active operation
30+
var RebaseNoOperation uint = ^uint(0)
31+
32+
// Error returned if there is no current rebase operation
33+
var ErrRebaseNoOperation = errors.New("o current rebase operation")
34+
2835
// RebaseOperation describes a single instruction/operation to be performed during the rebase.
2936
type RebaseOperation struct {
3037
Type RebaseOperationType
@@ -154,18 +161,21 @@ func (rebase *Rebase) OperationAt(index uint) *RebaseOperation {
154161
return newRebaseOperationFromC(operation)
155162
}
156163

157-
// CurrentOperationIndex gets the index of the rebase operation that is currently being applied.
158-
// Returns an error if no rebase operation is currently applied.
164+
// CurrentOperationIndex gets the index of the rebase operation that is
165+
// currently being applied. There is also an error returned for API
166+
// compatibility.
159167
func (rebase *Rebase) CurrentOperationIndex() (uint, error) {
160168
runtime.LockOSThread()
161169
defer runtime.UnlockOSThread()
162170

163-
operationIndex := int(C.git_rebase_operation_current(rebase.ptr))
164-
if operationIndex == C.GIT_REBASE_NO_OPERATION {
165-
return 0, MakeGitError(C.GIT_REBASE_NO_OPERATION)
171+
var err error
172+
operationIndex := uint(C.git_rebase_operation_current(rebase.ptr))
173+
runtime.KeepAlive(rebase)
174+
if operationIndex == RebaseNoOperation {
175+
err = ErrRebaseNoOperation
166176
}
167177

168-
return uint(operationIndex), nil
178+
return uint(operationIndex), err
169179
}
170180

171181
// OperationCount gets the count of rebase operations that are to be applied.

rebase_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ func performRebaseOnto(repo *Repository, branch string) (*Rebase, error) {
182182

183183
// Check no operation has been started yet
184184
rebaseOperationIndex, err := rebase.CurrentOperationIndex()
185-
if err == nil {
185+
if rebaseOperationIndex != RebaseNoOperation && err != ErrRebaseNoOperation {
186186
return nil, errors.New("No operation should have been started yet")
187187
}
188188

0 commit comments

Comments
 (0)