1
1
package com .projectkaiser .scm .vcs ;
2
2
3
3
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 .*;
7
5
8
6
import java .io .File ;
7
+ import java .io .FileWriter ;
9
8
import java .io .IOException ;
10
9
import java .io .PrintWriter ;
11
10
import java .util .UUID ;
12
11
import java .util .function .Consumer ;
13
12
13
+ import org .eclipse .jgit .api .CreateBranchCommand ;
14
14
import org .eclipse .jgit .api .Git ;
15
15
import org .eclipse .jgit .api .errors .GitAPIException ;
16
16
import org .eclipse .jgit .api .errors .NoFilepatternException ;
24
24
import org .kohsuke .github .GitHub ;
25
25
26
26
import com .projectkaiser .scm .vcs .api .IVCS ;
27
+ import com .projectkaiser .scm .vcs .api .PKVCSMergeResult ;
27
28
import com .projectkaiser .scm .vcs .api .VCSWorkspace ;
28
29
import com .projectkaiser .scm .vcs .api .exceptions .EVCSBranchExists ;
29
30
30
31
public class GitVCSTest {
31
32
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" ;
34
37
private static final String WORKSPACE_DIR = System .getProperty ("java.io.tmpdir" ) + "pk-vcs-workspaces" ;
35
38
private static final String REPO_NAME = "pk-vcs-git-testrepo" ;
36
39
private static final String GITHUB_USER = System .getProperty ("PK_VCS_TEST_GITHUB_USER" ) == null ?
@@ -51,6 +54,10 @@ public class GitVCSTest {
51
54
private static final String PROXY_PASS = getJvmProperty ("https.proxyPassword" );
52
55
private static final String LINE_1 = "line 1" ;
53
56
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" ;
54
61
55
62
private IVCS vcs ;
56
63
private GitHub github ;
@@ -185,76 +192,106 @@ public void testGetSetFileContent() throws NoFilepatternException, GitAPIExcepti
185
192
}
186
193
187
194
@ 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 );
189
197
vcs .createBranch (SRC_BRANCH , NEW_BRANCH , CREATED_DST_BRANCH_COMMIT_MESSAGE );
190
198
VCSWorkspace w = VCSWorkspace .getLockedWorkspace (gitVCS .getRepoFolder ());
191
199
try {
192
200
try (Git git = gitVCS .getLocalGit (w )) {
193
201
194
202
git
195
203
.checkout ()
196
- .setCreateBranch (true )
197
- .setStartPoint ("refs/remotes/origin/" + SRC_BRANCH )
204
+ .setCreateBranch (false )
198
205
.setName (SRC_BRANCH )
199
206
.call (); // switch to master
200
207
201
- File file1 = new File (w .getFolder (), "file1.txt" );
202
- file1 .createNewFile ();
203
-
204
208
git
205
- .add ()
206
- .addFilepattern ( "file1.txt" )
209
+ .pull ()
210
+ .setCredentialsProvider ( gitVCS . getCredentials () )
207
211
.call ();
208
212
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
+
209
218
git
210
219
.commit ()
211
- .setMessage (FILE1_ADDED_COMMIT_MESSAGE )
220
+ .setAll (true )
221
+ .setMessage (FILE1_CHANGED_COMMIT_MESSAGE )
212
222
.call ();
213
223
214
224
git
215
-
216
225
.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
220
233
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 ();
223
238
224
239
git
225
- .add ()
226
- .addFilepattern ("file2.txt" )
240
+ .commit ()
241
+ .setAll (true )
242
+ .setMessage (FILE2_CHANGED_COMMIT_MESSAGE )
227
243
.call ();
228
244
229
245
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
233
250
234
251
git
235
252
.push ()
236
253
.setPushAll ()
237
254
.setRemote ("origin" )
238
255
.setCredentialsProvider (gitVCS .getCredentials ())
239
256
.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 )) {
240
276
241
277
git
242
278
.checkout ()
243
279
.setCreateBranch (false )
244
- .setForce (false )
245
280
.setName (SRC_BRANCH )
246
281
.call (); // switch to master
247
282
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 );
249
285
286
+ vcs .merge (NEW_BRANCH , SRC_BRANCH , MERGE_COMMIT_MESSAGE );
250
287
251
288
git
252
289
.pull ()
253
290
.setCredentialsProvider (gitVCS .getCredentials ())
254
291
.call ();
255
292
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 ());
258
295
259
296
Iterable <RevCommit > commits = git
260
297
.log ()
0 commit comments