11package 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
86import java .io .File ;
7+ import java .io .FileWriter ;
98import java .io .IOException ;
109import java .io .PrintWriter ;
1110import java .util .UUID ;
1211import java .util .function .Consumer ;
1312
13+ import org .eclipse .jgit .api .CreateBranchCommand ;
1414import org .eclipse .jgit .api .Git ;
1515import org .eclipse .jgit .api .errors .GitAPIException ;
1616import org .eclipse .jgit .api .errors .NoFilepatternException ;
2424import org .kohsuke .github .GitHub ;
2525
2626import com .projectkaiser .scm .vcs .api .IVCS ;
27+ import com .projectkaiser .scm .vcs .api .PKVCSMergeResult ;
2728import com .projectkaiser .scm .vcs .api .VCSWorkspace ;
2829import com .projectkaiser .scm .vcs .api .exceptions .EVCSBranchExists ;
2930
3031public 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