Skip to content
This repository was archived by the owner on Feb 9, 2021. It is now read-only.

Commit f656d83

Browse files
committed
BZ-1198662/1192459: improvements on git sync mechanism
1 parent 7141e33 commit f656d83

File tree

3 files changed

+82
-34
lines changed

3 files changed

+82
-34
lines changed

uberfire-nio2-backport/uberfire-nio2-impls/uberfire-nio2-jgit/src/main/java/org/uberfire/java/nio/fs/jgit/JGitFileSystemProvider.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
import org.eclipse.jgit.api.Git;
5050
import org.eclipse.jgit.api.ListBranchCommand;
5151
import org.eclipse.jgit.api.ListBranchCommand.ListMode;
52+
import org.eclipse.jgit.api.errors.InvalidRemoteException;
5253
import org.eclipse.jgit.diff.DiffEntry;
5354
import org.eclipse.jgit.errors.RepositoryNotFoundException;
5455
import org.eclipse.jgit.internal.storage.file.WindowCache;

uberfire-nio2-backport/uberfire-nio2-impls/uberfire-nio2-jgit/src/main/java/org/uberfire/java/nio/fs/jgit/util/JGitUtil.java

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -270,20 +270,44 @@ public static void syncRepository( final Git git,
270270
specs.add( new RefSpec( "+refs/notes/*:refs/notes/*" ) );
271271

272272
try {
273-
final FetchResult result = git.fetch()
273+
git.fetch()
274274
.setCredentialsProvider( credentialsProvider )
275275
.setRefSpecs( specs )
276276
.setRemote( origin )
277277
.call();
278278

279279
final List<Ref> branches = git.branchList().setListMode( ListBranchCommand.ListMode.ALL ).call();
280280

281+
final Set<String> remoteBranches = new HashSet<String>();
282+
final Set<String> localBranches = new HashSet<String>();
283+
281284
for ( final Ref branch : branches ) {
282285
final String branchName = branch.getName().substring( branch.getName().lastIndexOf( "/" ) + 1 );
286+
if ( branch.getName().startsWith( "refs/remotes" ) ) {
287+
remoteBranches.add( branchName );
288+
} else {
289+
localBranches.add( branchName );
290+
}
291+
}
292+
293+
for ( final String localBranch : localBranches ) {
294+
if ( remoteBranches.contains( localBranch ) ) {
295+
git.branchCreate()
296+
.setName( localBranch )
297+
.setUpstreamMode( CreateBranchCommand.SetupUpstreamMode.SET_UPSTREAM )
298+
.setStartPoint( "upstream/" + localBranch )
299+
.setForce( true )
300+
.call();
301+
}
302+
}
303+
304+
remoteBranches.removeAll( localBranches );
305+
306+
for ( final String branch : remoteBranches ) {
283307
git.branchCreate()
284-
.setName( branchName )
308+
.setName( branch )
285309
.setUpstreamMode( CreateBranchCommand.SetupUpstreamMode.SET_UPSTREAM )
286-
.setStartPoint( "upstream/" + branchName )
310+
.setStartPoint( "upstream/" + branch )
287311
.setForce( true )
288312
.call();
289313
}

uberfire-nio2-backport/uberfire-nio2-impls/uberfire-nio2-jgit/src/test/java/org/uberfire/java/nio/fs/jgit/JGitFileSystemProviderTest.java

Lines changed: 54 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,19 @@
1616

1717
package org.uberfire.java.nio.fs.jgit;
1818

19-
import static org.fest.assertions.api.Assertions.*;
20-
import static org.uberfire.java.nio.file.StandardDeleteOption.*;
21-
import static org.uberfire.java.nio.fs.jgit.util.JGitUtil.*;
22-
2319
import java.io.File;
2420
import java.io.IOException;
2521
import java.io.InputStream;
2622
import java.io.OutputStream;
2723
import java.net.URI;
2824
import java.text.SimpleDateFormat;
25+
import java.util.ArrayList;
2926
import java.util.HashMap;
27+
import java.util.HashSet;
3028
import java.util.List;
3129
import java.util.Map;
3230
import java.util.Scanner;
31+
import java.util.Set;
3332

3433
import org.eclipse.jgit.api.Git;
3534
import org.eclipse.jgit.lib.ObjectId;
@@ -56,7 +55,11 @@
5655
import org.uberfire.java.nio.file.attribute.BasicFileAttributes;
5756
import org.uberfire.java.nio.file.attribute.FileTime;
5857
import org.uberfire.java.nio.fs.jgit.util.JGitUtil;
59-
import org.uberfire.java.nio.fs.jgit.util.JGitUtil.PathType;
58+
import org.uberfire.java.nio.fs.jgit.util.JGitUtil.*;
59+
60+
import static org.fest.assertions.api.Assertions.*;
61+
import static org.uberfire.java.nio.file.StandardDeleteOption.*;
62+
import static org.uberfire.java.nio.fs.jgit.util.JGitUtil.*;
6063

6164
public class JGitFileSystemProviderTest extends AbstractTestInfra {
6265

@@ -211,7 +214,6 @@ public void testNewFileSystemClone() throws IOException {
211214
}
212215

213216
@Test
214-
@Ignore(value = "Can't push to git using git protocol, just ssh")
215217
public void testNewFileSystemCloneAndPush() throws IOException {
216218

217219
final URI originRepo = URI.create( "git://my-simple-test-origin-repo" );
@@ -243,22 +245,6 @@ public void testNewFileSystemCloneAndPush() throws IOException {
243245
put( "fileXXXXX.txt", tempFile( "temp" ) );
244246
}} );
245247

246-
provider.getFileSystem( URI.create( "git://my-repo?push=git://localhost:9418/my-simple-test-origin-repo&force" ) );
247-
248-
assertThat( fs ).isNotNull();
249-
250-
assertThat( fs.getRootDirectories() ).hasSize( 2 );
251-
252-
for ( final Path root : fs.getRootDirectories() ) {
253-
if ( root.toAbsolutePath().toUri().toString().contains( "upstream" ) ) {
254-
assertThat( provider.newDirectoryStream( root, null ) ).isNotEmpty().hasSize( 2 );
255-
} else if ( root.toAbsolutePath().toUri().toString().contains( "origin" ) ) {
256-
assertThat( provider.newDirectoryStream( root, null ) ).isNotEmpty().hasSize( 1 );
257-
} else {
258-
assertThat( provider.newDirectoryStream( root, null ) ).isNotEmpty().hasSize( 2 );
259-
}
260-
}
261-
262248
final URI newRepo2 = URI.create( "git://my-repo2" );
263249

264250
final Map<String, Object> env2 = new HashMap<String, Object>() {{
@@ -268,19 +254,56 @@ public void testNewFileSystemCloneAndPush() throws IOException {
268254

269255
final FileSystem fs2 = provider.newFileSystem( newRepo2, env2 );
270256

271-
provider.getFileSystem( URI.create( "git://my-repo?sync=git://localhost:9418/my-simple-test-origin-repo&force" ) );
257+
commit( origin.gitRepo(), "user-branch", "user1", "user1@example.com", "commitx", null, null, false, new HashMap<String, File>() {{
258+
put( "file1UserBranch.txt", tempFile( "tempX" ) );
259+
}} );
272260

273-
assertThat( fs2.getRootDirectories() ).hasSize( 2 );
261+
provider.getFileSystem( URI.create( "git://my-repo2?sync=git://localhost:9418/my-simple-test-origin-repo&force" ) );
274262

263+
assertThat( fs2.getRootDirectories() ).hasSize( 5 );
264+
265+
final List<String> rootURIs1 = new ArrayList<String>() {{
266+
add( "git://master@my-repo2/" );
267+
add( "git://user-branch@my-repo2/" );
268+
add( "git://origin/master@my-repo2/" );
269+
add( "git://upstream/master@my-repo2/" );
270+
add( "git://upstream/user-branch@my-repo2/" );
271+
}};
272+
273+
final List<String> rootURIs2 = new ArrayList<String>() {{
274+
add( "git://master@my-repo2/" );
275+
add( "git://user-branch@my-repo2/" );
276+
add( "git://user-branch-2@my-repo2/" );
277+
add( "git://origin/master@my-repo2/" );
278+
add( "git://upstream/master@my-repo2/" );
279+
add( "git://upstream/user-branch@my-repo2/" );
280+
add( "git://upstream/user-branch-2@my-repo2/" );
281+
}};
282+
283+
final Set<String> rootURIs = new HashSet<String>();
275284
for ( final Path root : fs2.getRootDirectories() ) {
276-
if ( root.toAbsolutePath().toUri().toString().contains( "upstream" ) ) {
277-
assertThat( provider.newDirectoryStream( root, null ) ).isNotEmpty().hasSize( 2 );
278-
} else if ( root.toAbsolutePath().toUri().toString().contains( "origin" ) ) {
279-
assertThat( provider.newDirectoryStream( root, null ) ).isNotEmpty().hasSize( 2 );
280-
} else {
281-
assertThat( provider.newDirectoryStream( root, null ) ).isNotEmpty().hasSize( 2 );
282-
}
285+
rootURIs.add( root.toUri().toString() );
283286
}
287+
288+
rootURIs.removeAll( rootURIs1 );
289+
290+
assertThat( rootURIs ).isEmpty();
291+
292+
commit( origin.gitRepo(), "user-branch-2", "user1", "user1@example.com", "commitx", null, null, false, new HashMap<String, File>() {{
293+
put( "file2UserBranch.txt", tempFile( "tempX" ) );
294+
}} );
295+
296+
provider.getFileSystem( URI.create( "git://my-repo2?sync=git://localhost:9418/my-simple-test-origin-repo&force" ) );
297+
298+
assertThat( fs2.getRootDirectories() ).hasSize( 7 );
299+
300+
for ( final Path root : fs2.getRootDirectories() ) {
301+
rootURIs.add( root.toUri().toString() );
302+
}
303+
304+
rootURIs.removeAll( rootURIs2 );
305+
306+
assertThat( rootURIs ).isEmpty();
284307
}
285308

286309
@Test

0 commit comments

Comments
 (0)