Skip to content

Commit 686500e

Browse files
committed
Merge pull request #429 from josharian/cherrypick-commit
cherrypick: wrap git_cherrypick_commit (cherry picked from commit 45097a8)
1 parent d843782 commit 686500e

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

cherrypick.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,19 @@ func (v *Repository) Cherrypick(commit *Commit, opts CherrypickOptions) error {
7373
}
7474
return nil
7575
}
76+
77+
func (r *Repository) CherrypickCommit(pick, our *Commit, opts CherrypickOptions) (*Index, error) {
78+
runtime.LockOSThread()
79+
defer runtime.UnlockOSThread()
80+
81+
cOpts := opts.MergeOpts.toC()
82+
83+
var ptr *C.git_index
84+
ret := C.git_cherrypick_commit(&ptr, r.ptr, pick.cast_ptr, our.cast_ptr, C.uint(opts.Mainline), cOpts)
85+
runtime.KeepAlive(pick)
86+
runtime.KeepAlive(our)
87+
if ret < 0 {
88+
return nil, MakeGitError(ret)
89+
}
90+
return newIndexFromC(ptr, r), nil
91+
}

cherrypick_test.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,57 @@ func TestCherrypick(t *testing.T) {
8484
t.Fatal("Incorrect repository state: ", state)
8585
}
8686
}
87+
88+
func TestCherrypickCommit(t *testing.T) {
89+
t.Parallel()
90+
repo := createTestRepo(t)
91+
defer cleanupTestRepo(t, repo)
92+
93+
c1, _ := seedTestRepo(t, repo)
94+
c2, _ := updateReadme(t, repo, content)
95+
96+
commit1, err := repo.LookupCommit(c1)
97+
if err != nil {
98+
t.Fatal(err)
99+
}
100+
commit2, err := repo.LookupCommit(c2)
101+
if err != nil {
102+
t.Fatal(err)
103+
}
104+
105+
checkout(t, repo, commit1)
106+
107+
if got := readReadme(t, repo); got == content {
108+
t.Fatalf("README = %q, want %q", got, content)
109+
}
110+
111+
opts, err := DefaultCherrypickOptions()
112+
if err != nil {
113+
t.Fatal(err)
114+
}
115+
116+
idx, err := repo.CherrypickCommit(commit2, commit1, opts)
117+
if err != nil {
118+
t.Fatal(err)
119+
}
120+
defer idx.Free()
121+
122+
// The file is only updated in the index, not in the working directory.
123+
if got := readReadme(t, repo); got == content {
124+
t.Errorf("README = %q, want %q", got, content)
125+
}
126+
if got := repo.State(); got != RepositoryStateNone {
127+
t.Errorf("repo.State() = %v, want %v", got, RepositoryStateCherrypick)
128+
}
129+
130+
if got := idx.EntryCount(); got != 1 {
131+
t.Fatalf("idx.EntryCount() = %v, want %v", got, 1)
132+
}
133+
entry, err := idx.EntryByIndex(0)
134+
if err != nil {
135+
t.Fatal(err)
136+
}
137+
if entry.Path != "README" {
138+
t.Errorf("entry.Path = %v, want %v", entry.Path, "README")
139+
}
140+
}

0 commit comments

Comments
 (0)