Skip to content

Commit a671e67

Browse files
committed
Took @carlosmn PR review into account
1 parent 03e10c5 commit a671e67

File tree

3 files changed

+32
-24
lines changed

3 files changed

+32
-24
lines changed

git.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ const (
5050
ErrClassFilter ErrorClass = C.GITERR_FILTER
5151
ErrClassRevert ErrorClass = C.GITERR_REVERT
5252
ErrClassCallback ErrorClass = C.GITERR_CALLBACK
53+
ErrClassRebase ErrorClass = C.GITERR_REBASE
5354
)
5455

5556
type ErrorCode int

rebase.go

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,14 @@ const (
2828
// RebaseOperation describes a single instruction/operation to be performed during the rebase.
2929
type RebaseOperation struct {
3030
Type RebaseOperationType
31-
ID *Oid
31+
Id *Oid
3232
Exec string
3333
}
3434

3535
func newRebaseOperationFromC(c *C.git_rebase_operation) *RebaseOperation {
3636
operation := &RebaseOperation{}
3737
operation.Type = RebaseOperationType(c._type)
38-
operation.ID = newOidFromC(&c.id)
38+
operation.Id = newOidFromC(&c.id)
3939
operation.Exec = C.GoString(c.exec)
4040

4141
return operation
@@ -84,26 +84,26 @@ func (ro *RebaseOptions) toC() *C.git_rebase_options {
8484
version: C.uint(ro.Version),
8585
quiet: C.int(ro.Quiet),
8686
inmemory: C.int(ro.InMemory),
87-
rewrite_notes_ref: rewriteNotesRefToC(ro.RewriteNotesRef),
87+
rewrite_notes_ref: mapEmptyStringToNull(ro.RewriteNotesRef),
8888
merge_options: *ro.MergeOptions.toC(),
8989
checkout_options: *ro.CheckoutOptions.toC(),
9090
}
9191
}
9292

93-
func rewriteNotesRefToC(ref string) *C.char {
93+
func mapEmptyStringToNull(ref string) *C.char {
9494
if ref == "" {
9595
return nil
9696
}
9797
return C.CString(ref)
9898
}
9999

100-
// Rebase object wrapper for C pointer
100+
// Rebase is the struct representing a Rebase object.
101101
type Rebase struct {
102102
ptr *C.git_rebase
103103
}
104104

105-
//RebaseInit initializes a rebase operation to rebase the changes in branch relative to upstream onto another branch.
106-
func (r *Repository) RebaseInit(branch *AnnotatedCommit, upstream *AnnotatedCommit, onto *AnnotatedCommit, opts *RebaseOptions) (*Rebase, error) {
105+
// InitRebase initializes a rebase operation to rebase the changes in branch relative to upstream onto another branch.
106+
func (r *Repository) InitRebase(branch *AnnotatedCommit, upstream *AnnotatedCommit, onto *AnnotatedCommit, opts *RebaseOptions) (*Rebase, error) {
107107
runtime.LockOSThread()
108108
defer runtime.UnlockOSThread()
109109

@@ -128,8 +128,8 @@ func (r *Repository) RebaseInit(branch *AnnotatedCommit, upstream *AnnotatedComm
128128
return newRebaseFromC(ptr), nil
129129
}
130130

131-
//RebaseOpen opens an existing rebase that was previously started by either an invocation of git_rebase_init or by another client.
132-
func (r *Repository) RebaseOpen(opts *RebaseOptions) (*Rebase, error) {
131+
// OpenRebase opens an existing rebase that was previously started by either an invocation of InitRebase or by another client.
132+
func (r *Repository) OpenRebase(opts *RebaseOptions) (*Rebase, error) {
133133
runtime.LockOSThread()
134134
defer runtime.UnlockOSThread()
135135

@@ -149,9 +149,13 @@ func (rebase *Rebase) OperationAt(index uint) *RebaseOperation {
149149
}
150150

151151
// CurrentOperationIndex gets the index of the rebase operation that is currently being applied.
152-
// If the first operation has not yet been applied then this returns -1 (C.GIT_REBASE_NO_OPERATION).
153-
func (rebase *Rebase) CurrentOperationIndex() int {
154-
return int(C.git_rebase_operation_current(rebase.ptr))
152+
// Returns an error if no rebase operation is currently applied.
153+
func (rebase *Rebase) CurrentOperationIndex() (uint, error) {
154+
operationIndex := int(C.git_rebase_operation_current(rebase.ptr))
155+
if operationIndex == C.GIT_REBASE_NO_OPERATION {
156+
return 0, MakeGitError(C.GIT_REBASE_NO_OPERATION)
157+
}
158+
return uint(operationIndex), nil
155159
}
156160

157161
// OperationCount gets the count of rebase operations that are to be applied.
@@ -160,7 +164,7 @@ func (rebase *Rebase) OperationCount() uint {
160164
}
161165

162166
// Next performs the next rebase operation and returns the information about it.
163-
// If the operation is one that applies a patch (which is any operation except GIT_REBASE_OPERATION_EXEC)
167+
// If the operation is one that applies a patch (which is any operation except RebaseOperationExec)
164168
// then the patch will be applied and the index and working directory will be updated with the changes.
165169
// If there are conflicts, you will need to address those before committing the changes.
166170
func (rebase *Rebase) Next() (*RebaseOperation, error) {
@@ -177,7 +181,7 @@ func (rebase *Rebase) Next() (*RebaseOperation, error) {
177181
}
178182

179183
// Commit commits the current patch.
180-
// You must have resolved any conflicts that were introduced during the patch application from the git_rebase_next invocation.
184+
// You must have resolved any conflicts that were introduced during the patch application from the Next() invocation.
181185
func (rebase *Rebase) Commit(ID *Oid, author, committer *Signature, message string) error {
182186
runtime.LockOSThread()
183187
defer runtime.UnlockOSThread()
@@ -192,6 +196,7 @@ func (rebase *Rebase) Commit(ID *Oid, author, committer *Signature, message stri
192196
if err != nil {
193197
return err
194198
}
199+
defer C.git_signature_free(committerSig)
195200

196201
cmsg := C.CString(message)
197202
defer C.free(unsafe.Pointer(cmsg))
@@ -229,7 +234,7 @@ func (rebase *Rebase) Abort() error {
229234
return nil
230235
}
231236

232-
//Free frees the Rebase object and underlying git_rebase C pointer.
237+
// Free frees the Rebase object.
233238
func (rebase *Rebase) Free() {
234239
runtime.SetFinalizer(rebase, nil)
235240
C.git_rebase_free(rebase.ptr)

rebase_test.go

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ func TestRebaseNoConflicts(t *testing.T) {
111111
seedTestRepo(t, repo)
112112

113113
// Try to open existing rebase
114-
oRebase, err := repo.RebaseOpen(nil)
114+
oRebase, err := repo.OpenRebase(nil)
115115
if err == nil {
116116
t.Fatal("Did not expect to find a rebase in progress")
117117
}
@@ -132,7 +132,7 @@ func TestRebaseNoConflicts(t *testing.T) {
132132
defer rebase.Free()
133133

134134
// Open existing rebase
135-
oRebase, err = repo.RebaseOpen(nil)
135+
oRebase, err = repo.OpenRebase(nil)
136136
checkFatal(t, err)
137137
defer oRebase.Free()
138138
if oRebase == nil {
@@ -144,7 +144,7 @@ func TestRebaseNoConflicts(t *testing.T) {
144144
checkFatal(t, err)
145145

146146
// Check no more rebase is in progress
147-
oRebase, err = repo.RebaseOpen(nil)
147+
oRebase, err = repo.OpenRebase(nil)
148148
if err == nil {
149149
t.Fatal("Did not expect to find a rebase in progress")
150150
}
@@ -198,13 +198,14 @@ func performRebaseOnto(repo *Repository, branch string) (*Rebase, error) {
198198
defer onto.Free()
199199

200200
// Init rebase
201-
rebase, err := repo.RebaseInit(nil, nil, onto, nil)
201+
rebase, err := repo.InitRebase(nil, nil, onto, nil)
202202
if err != nil {
203203
return nil, err
204204
}
205205

206206
// Check no operation has been started yet
207-
if rebase.CurrentOperationIndex() != -1 { // -1 == GIT_REBASE_NO_OPERATION
207+
rebaseOperationIndex, err := rebase.CurrentOperationIndex()
208+
if err == nil {
208209
return nil, errors.New("No operation should have been started yet")
209210
}
210211

@@ -217,22 +218,23 @@ func performRebaseOnto(repo *Repository, branch string) (*Rebase, error) {
217218
}
218219

219220
// Check operation index is correct
220-
if rebase.CurrentOperationIndex() != op {
221+
rebaseOperationIndex, err = rebase.CurrentOperationIndex()
222+
if int(rebaseOperationIndex) != op {
221223
return nil, errors.New("Bad operation index")
222224
}
223225
if !operationsAreEqual(rebase.OperationAt(uint(op)), operation) {
224226
return nil, errors.New("Rebase operations should be equal")
225227
}
226228

227229
// Get current rebase operation created commit
228-
commit, err := repo.LookupCommit(operation.ID)
230+
commit, err := repo.LookupCommit(operation.Id)
229231
if err != nil {
230232
return nil, err
231233
}
232234
defer commit.Free()
233235

234236
// Apply commit
235-
err = rebase.Commit(operation.ID, signature(), signature(), commit.Message())
237+
err = rebase.Commit(operation.Id, signature(), signature(), commit.Message())
236238
if err != nil {
237239
return nil, err
238240
}
@@ -242,7 +244,7 @@ func performRebaseOnto(repo *Repository, branch string) (*Rebase, error) {
242244
}
243245

244246
func operationsAreEqual(l, r *RebaseOperation) bool {
245-
return l.Exec == r.Exec && l.Type == r.Type && l.ID.String() == r.ID.String()
247+
return l.Exec == r.Exec && l.Type == r.Type && l.Id.String() == r.Id.String()
246248
}
247249

248250
func createBranch(repo *Repository, branch string) error {

0 commit comments

Comments
 (0)