Skip to content

Commit e1e888d

Browse files
author
Denis
committed
branch creating and merging fixes, test updates
1 parent a7e1f85 commit e1e888d

File tree

2 files changed

+81
-34
lines changed

2 files changed

+81
-34
lines changed

src/main/java/com/projectkaiser/scm/vcs/GitVCS.java

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616
import org.apache.commons.io.IOUtils;
1717
import org.apache.commons.logging.Log;
18-
import org.eclipse.jgit.api.CreateBranchCommand;
1918
import org.eclipse.jgit.api.Git;
2019
import org.eclipse.jgit.api.MergeResult;
2120
import org.eclipse.jgit.api.ResetCommand.ResetType;
@@ -59,11 +58,16 @@ public void createBranch(String srcBranchName, String newBranchName, String comm
5958

6059
try (Git git = getLocalGit(workspace)) {
6160

61+
git
62+
.checkout()
63+
.setCreateBranch(true)
64+
.setStartPoint("origin/" + srcBranchName)
65+
.setName(srcBranchName)
66+
.call(); // switch to master
67+
6268
git
6369
.branchCreate()
6470
.setName(newBranchName)
65-
.setUpstreamMode(CreateBranchCommand.SetupUpstreamMode.SET_UPSTREAM)
66-
.setStartPoint("origin/" + srcBranchName)
6771
.call();
6872

6973
RefSpec refSpec = new RefSpec().setSourceDestination(newBranchName,
@@ -73,6 +77,12 @@ public void createBranch(String srcBranchName, String newBranchName, String comm
7377
.setRefSpecs(refSpec)
7478
.setCredentialsProvider(credentials)
7579
.call();
80+
81+
git
82+
.branchDelete()
83+
.setBranchNames(newBranchName)
84+
.call();
85+
7686
}
7787
} finally {
7888
workspace.unlock();
@@ -187,13 +197,13 @@ public PKVCSMergeResult merge(String sourceBranchUrl, String destBranchUrl, Stri
187197

188198
PKVCSMergeResult res = new PKVCSMergeResult();
189199

190-
res.setIsSuccess(!mr.getMergeStatus().equals(MergeResult.MergeStatus.CONFLICTING) &&
200+
res.setSuccess(!mr.getMergeStatus().equals(MergeResult.MergeStatus.CONFLICTING) &&
191201
!mr.getMergeStatus().equals(MergeResult.MergeStatus.FAILED) &&
192202
!mr.getMergeStatus().equals(MergeResult.MergeStatus.ABORTED) &&
193203
!mr.getMergeStatus().equals(MergeResult.MergeStatus.NOT_SUPPORTED));
194204

195205

196-
if (!res.getIsSuccess()) {
206+
if (!res.getSuccess()) {
197207
res.getConflictingFiles().addAll(mr.getConflicts().keySet());
198208
try {
199209
git

src/test/java/com/projectkaiser/scm/vcs/GitVCSTest.java

Lines changed: 66 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
package com.projectkaiser.scm.vcs;
22

33

4-
import static org.junit.Assert.assertEquals;
5-
import static org.junit.Assert.assertTrue;
6-
import static org.junit.Assert.fail;
4+
import static org.junit.Assert.*;
75

86
import java.io.File;
7+
import java.io.FileWriter;
98
import java.io.IOException;
109
import java.io.PrintWriter;
1110
import java.util.UUID;
1211
import java.util.function.Consumer;
1312

13+
import org.eclipse.jgit.api.CreateBranchCommand;
1414
import org.eclipse.jgit.api.Git;
1515
import org.eclipse.jgit.api.errors.GitAPIException;
1616
import org.eclipse.jgit.api.errors.NoFilepatternException;
@@ -24,13 +24,16 @@
2424
import org.kohsuke.github.GitHub;
2525

2626
import com.projectkaiser.scm.vcs.api.IVCS;
27+
import com.projectkaiser.scm.vcs.api.PKVCSMergeResult;
2728
import com.projectkaiser.scm.vcs.api.VCSWorkspace;
2829
import com.projectkaiser.scm.vcs.api.exceptions.EVCSBranchExists;
2930

3031
public class GitVCSTest {
3132

32-
private static final String FILE1_ADDED_COMMIT_MESSAGE = "file1 added";
33-
private static final String FILE2_ADDED_COMMIT_MESSAGE = "file2 added";
33+
private static final String FILE1_ADDED_COMMIT_MESSAGE = "test-master added";
34+
private static final String FILE2_ADDED_COMMIT_MESSAGE = "test-branch added";
35+
private static final String FILE1_CHANGED_COMMIT_MESSAGE = "test-master changed";
36+
private static final String FILE2_CHANGED_COMMIT_MESSAGE = "test-branch changed";
3437
private static final String WORKSPACE_DIR = System.getProperty("java.io.tmpdir") + "pk-vcs-workspaces";
3538
private static final String REPO_NAME = "pk-vcs-git-testrepo";
3639
private static final String GITHUB_USER = System.getProperty("PK_VCS_TEST_GITHUB_USER") == null ?
@@ -51,6 +54,10 @@ public class GitVCSTest {
5154
private static final String PROXY_PASS = getJvmProperty("https.proxyPassword");
5255
private static final String LINE_1 = "line 1";
5356
private static final String LINE_2 = "line 2";
57+
private static final String LINE_3 = "line 3";
58+
private static final String LINE_4 = "line 4";
59+
private static final String FILE1_NAME = "test-master.txt";
60+
private static final String FILE2_NAME = "test-branch.txt";
5461

5562
private IVCS vcs;
5663
private GitHub github;
@@ -185,76 +192,106 @@ public void testGetSetFileContent() throws NoFilepatternException, GitAPIExcepti
185192
}
186193

187194
@Test
188-
public void testGitMerge() throws IOException, NoFilepatternException, GitAPIException, InterruptedException {
195+
public void testGitMergeConflict() throws IOException, NoFilepatternException, GitAPIException, InterruptedException {
196+
repo.createContent(LINE_1.getBytes(), FILE1_ADDED_COMMIT_MESSAGE, FILE1_NAME, SRC_BRANCH);
189197
vcs.createBranch(SRC_BRANCH, NEW_BRANCH, CREATED_DST_BRANCH_COMMIT_MESSAGE);
190198
VCSWorkspace w = VCSWorkspace.getLockedWorkspace(gitVCS.getRepoFolder());
191199
try {
192200
try (Git git = gitVCS.getLocalGit(w)) {
193201

194202
git
195203
.checkout()
196-
.setCreateBranch(true)
197-
.setStartPoint("refs/remotes/origin/" + SRC_BRANCH)
204+
.setCreateBranch(false)
198205
.setName(SRC_BRANCH)
199206
.call(); // switch to master
200207

201-
File file1 = new File(w.getFolder(), "file1.txt");
202-
file1.createNewFile();
203-
204208
git
205-
.add()
206-
.addFilepattern("file1.txt")
209+
.pull()
210+
.setCredentialsProvider(gitVCS.getCredentials())
207211
.call();
208212

213+
File file = new File(w.getFolder(), FILE1_NAME);
214+
FileWriter writer = new FileWriter(file, false);
215+
writer.write(LINE_3);
216+
writer.close();
217+
209218
git
210219
.commit()
211-
.setMessage(FILE1_ADDED_COMMIT_MESSAGE)
220+
.setAll(true)
221+
.setMessage(FILE1_CHANGED_COMMIT_MESSAGE)
212222
.call();
213223

214224
git
215-
216225
.checkout()
217-
.setCreateBranch(false)
218-
.setName(NEW_BRANCH)
219-
.call(); // switch to new-branch
226+
.setCreateBranch(true)
227+
.setStartPoint("origin/" + NEW_BRANCH)
228+
.setUpstreamMode(CreateBranchCommand.SetupUpstreamMode.TRACK)
229+
.setName(NEW_BRANCH)
230+
.call(); // switch to new-branch and track origin/new-branch
231+
// note: local new-branch was deleted at gitVCS.createBranch().
232+
// If not delete then new-branch will exist without tracking origin/new-branch
220233

221-
File file2 = new File(w.getFolder(), "file2.txt");
222-
file2.createNewFile();
234+
file = new File(w.getFolder(), FILE1_NAME);
235+
writer = new FileWriter(file, false);
236+
writer.write(LINE_4);
237+
writer.close();
223238

224239
git
225-
.add()
226-
.addFilepattern("file2.txt")
240+
.commit()
241+
.setAll(true)
242+
.setMessage(FILE2_CHANGED_COMMIT_MESSAGE)
227243
.call();
228244

229245
git
230-
.commit()
231-
.setMessage(FILE2_ADDED_COMMIT_MESSAGE)
232-
.call();
246+
.checkout()
247+
.setCreateBranch(false)
248+
.setName(SRC_BRANCH)
249+
.call(); // switch to master
233250

234251
git
235252
.push()
236253
.setPushAll()
237254
.setRemote("origin")
238255
.setCredentialsProvider(gitVCS.getCredentials())
239256
.call();
257+
258+
259+
PKVCSMergeResult res = vcs.merge(NEW_BRANCH, SRC_BRANCH, MERGE_COMMIT_MESSAGE);
260+
261+
assertFalse(res.getSuccess());
262+
assertTrue(res.getConflictingFiles().size() == 1);
263+
assertTrue(res.getConflictingFiles().contains(file.getName()));
264+
}
265+
} finally {
266+
w.unlock();
267+
}
268+
}
269+
270+
@Test
271+
public void testGitMerge() throws IOException, NoFilepatternException, GitAPIException, InterruptedException {
272+
vcs.createBranch(SRC_BRANCH, NEW_BRANCH, CREATED_DST_BRANCH_COMMIT_MESSAGE);
273+
VCSWorkspace w = VCSWorkspace.getLockedWorkspace(gitVCS.getRepoFolder());
274+
try {
275+
try (Git git = gitVCS.getLocalGit(w)) {
240276

241277
git
242278
.checkout()
243279
.setCreateBranch(false)
244-
.setForce(false)
245280
.setName(SRC_BRANCH)
246281
.call(); // switch to master
247282

248-
vcs.merge(NEW_BRANCH, SRC_BRANCH, MERGE_COMMIT_MESSAGE);
283+
repo.createContent(LINE_1.getBytes(), FILE1_ADDED_COMMIT_MESSAGE, FILE1_NAME, SRC_BRANCH);
284+
repo.createContent(LINE_2.getBytes(), FILE2_ADDED_COMMIT_MESSAGE, FILE2_NAME, NEW_BRANCH);
249285

286+
vcs.merge(NEW_BRANCH, SRC_BRANCH, MERGE_COMMIT_MESSAGE);
250287

251288
git
252289
.pull()
253290
.setCredentialsProvider(gitVCS.getCredentials())
254291
.call();
255292

256-
assertTrue(file1.exists());
257-
assertTrue(file2.exists());
293+
assertTrue(new File(w.getFolder(), FILE2_NAME).exists());
294+
assertTrue(new File(w.getFolder(), FILE1_NAME).exists());
258295

259296
Iterable<RevCommit> commits = git
260297
.log()

0 commit comments

Comments
 (0)