Skip to content

Commit bdf3ed4

Browse files
rebase: Add wrapper for git_rebase_inmemory_index() (#900) (#903)
* rebase: Fix missing initialization of the repo pointer While the `Rebase` structure has a pointer to the repository the rebase is creatde in, this pointer isn't ever initialized. Fix this. * rebase: Add wrapper for `git_rebase_inmemory_index()` Add a new wrapper for `git_rebase_inmemory_index()`, which can be used to retrieve the index for an in-memory rebase. Co-authored-by: Patrick Steinhardt <[email protected]> (cherry picked from commit e7d1b2b) Co-authored-by: Patrick Steinhardt <[email protected]>
1 parent 45c4957 commit bdf3ed4

File tree

2 files changed

+99
-4
lines changed

2 files changed

+99
-4
lines changed

rebase.go

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ func (r *Repository) InitRebase(branch *AnnotatedCommit, upstream *AnnotatedComm
181181
return nil, MakeGitError(ret)
182182
}
183183

184-
return newRebaseFromC(ptr, cOpts), nil
184+
return newRebaseFromC(ptr, r, cOpts), nil
185185
}
186186

187187
// OpenRebase opens an existing rebase that was previously started by either an invocation of InitRebase or by another client.
@@ -203,7 +203,7 @@ func (r *Repository) OpenRebase(opts *RebaseOptions) (*Rebase, error) {
203203
return nil, MakeGitError(ret)
204204
}
205205

206-
return newRebaseFromC(ptr, cOpts), nil
206+
return newRebaseFromC(ptr, r, cOpts), nil
207207
}
208208

209209
// OperationAt gets the rebase operation specified by the given index.
@@ -255,6 +255,27 @@ func (rebase *Rebase) Next() (*RebaseOperation, error) {
255255
return newRebaseOperationFromC(ptr), nil
256256
}
257257

258+
// InmemoryIndex gets the index produced by the last operation, which is the
259+
// result of `Next()` and which will be committed by the next invocation of
260+
// `Commit()`. This is useful for resolving conflicts in an in-memory rebase
261+
// before committing them.
262+
//
263+
// This is only applicable for in-memory rebases; for rebases within a working
264+
// directory, the changes were applied to the repository's index.
265+
func (rebase *Rebase) InmemoryIndex() (*Index, error) {
266+
runtime.LockOSThread()
267+
defer runtime.UnlockOSThread()
268+
269+
var ptr *C.git_index
270+
err := C.git_rebase_inmemory_index(&ptr, rebase.ptr)
271+
runtime.KeepAlive(rebase)
272+
if err < 0 {
273+
return nil, MakeGitError(err)
274+
}
275+
276+
return newIndexFromC(ptr, rebase.r), nil
277+
}
278+
258279
// Commit commits the current patch.
259280
// You must have resolved any conflicts that were introduced during the patch application from the Next() invocation.
260281
func (rebase *Rebase) Commit(ID *Oid, author, committer *Signature, message string) error {
@@ -320,8 +341,8 @@ func (r *Rebase) Free() {
320341
freeRebaseOptions(r.options)
321342
}
322343

323-
func newRebaseFromC(ptr *C.git_rebase, opts *C.git_rebase_options) *Rebase {
324-
rebase := &Rebase{ptr: ptr, options: opts}
344+
func newRebaseFromC(ptr *C.git_rebase, repo *Repository, opts *C.git_rebase_options) *Rebase {
345+
rebase := &Rebase{ptr: ptr, r: repo, options: opts}
325346
runtime.SetFinalizer(rebase, (*Rebase).Free)
326347
return rebase
327348
}

rebase_test.go

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,80 @@ import (
99

1010
// Tests
1111

12+
func TestRebaseInMemoryWithConflict(t *testing.T) {
13+
repo := createTestRepo(t)
14+
defer cleanupTestRepo(t, repo)
15+
seedTestRepo(t, repo)
16+
17+
// Create two branches with common history, where both modify "common-file"
18+
// in a conflicting way.
19+
_, err := commitSomething(repo, "common-file", "a\nb\nc\n", commitOptions{})
20+
checkFatal(t, err)
21+
checkFatal(t, createBranch(repo, "branch-a"))
22+
checkFatal(t, createBranch(repo, "branch-b"))
23+
24+
checkFatal(t, repo.SetHead("refs/heads/branch-a"))
25+
_, err = commitSomething(repo, "common-file", "1\nb\nc\n", commitOptions{})
26+
checkFatal(t, err)
27+
28+
checkFatal(t, repo.SetHead("refs/heads/branch-b"))
29+
_, err = commitSomething(repo, "common-file", "x\nb\nc\n", commitOptions{})
30+
checkFatal(t, err)
31+
32+
branchA, err := repo.LookupBranch("branch-a", BranchLocal)
33+
checkFatal(t, err)
34+
onto, err := repo.AnnotatedCommitFromRef(branchA.Reference)
35+
checkFatal(t, err)
36+
37+
// We then rebase "branch-b" onto "branch-a" in-memory, which should result
38+
// in a conflict.
39+
rebase, err := repo.InitRebase(nil, nil, onto, &RebaseOptions{InMemory: 1})
40+
checkFatal(t, err)
41+
42+
_, err = rebase.Next()
43+
checkFatal(t, err)
44+
45+
index, err := rebase.InmemoryIndex()
46+
checkFatal(t, err)
47+
48+
// We simply resolve the conflict and commit the rebase.
49+
if !index.HasConflicts() {
50+
t.Fatal("expected index to have conflicts")
51+
}
52+
53+
conflict, err := index.Conflict("common-file")
54+
checkFatal(t, err)
55+
56+
resolvedBlobID, err := repo.CreateBlobFromBuffer([]byte("resolved contents"))
57+
checkFatal(t, err)
58+
59+
resolvedEntry := *conflict.Our
60+
resolvedEntry.Id = resolvedBlobID
61+
checkFatal(t, index.Add(&resolvedEntry))
62+
checkFatal(t, index.RemoveConflict("common-file"))
63+
64+
var commitID Oid
65+
checkFatal(t, rebase.Commit(&commitID, signature(), signature(), "rebased message"))
66+
checkFatal(t, rebase.Finish())
67+
68+
// And then assert that we can look up the new merge commit, and that the
69+
// "common-file" has the expected contents.
70+
commit, err := repo.LookupCommit(&commitID)
71+
checkFatal(t, err)
72+
if commit.Message() != "rebased message" {
73+
t.Fatalf("unexpected commit message %q", commit.Message())
74+
}
75+
76+
tree, err := commit.Tree()
77+
checkFatal(t, err)
78+
79+
blob, err := repo.LookupBlob(tree.EntryByName("common-file").Id)
80+
checkFatal(t, err)
81+
if string(blob.Contents()) != "resolved contents" {
82+
t.Fatalf("unexpected resolved blob contents %q", string(blob.Contents()))
83+
}
84+
}
85+
1286
func TestRebaseAbort(t *testing.T) {
1387
// TEST DATA
1488

0 commit comments

Comments
 (0)