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

Commit

Permalink
BZ-1198662/1192459: improvements on git sync mechanism
Browse files Browse the repository at this point in the history
  • Loading branch information
porcelli committed Mar 10, 2015
1 parent 7141e33 commit f656d83
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 34 deletions.
Expand Up @@ -49,6 +49,7 @@
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.ListBranchCommand;
import org.eclipse.jgit.api.ListBranchCommand.ListMode;
import org.eclipse.jgit.api.errors.InvalidRemoteException;
import org.eclipse.jgit.diff.DiffEntry;
import org.eclipse.jgit.errors.RepositoryNotFoundException;
import org.eclipse.jgit.internal.storage.file.WindowCache;
Expand Down
Expand Up @@ -270,20 +270,44 @@ public static void syncRepository( final Git git,
specs.add( new RefSpec( "+refs/notes/*:refs/notes/*" ) );

try {
final FetchResult result = git.fetch()
git.fetch()
.setCredentialsProvider( credentialsProvider )
.setRefSpecs( specs )
.setRemote( origin )
.call();

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

final Set<String> remoteBranches = new HashSet<String>();
final Set<String> localBranches = new HashSet<String>();

for ( final Ref branch : branches ) {
final String branchName = branch.getName().substring( branch.getName().lastIndexOf( "/" ) + 1 );
if ( branch.getName().startsWith( "refs/remotes" ) ) {
remoteBranches.add( branchName );
} else {
localBranches.add( branchName );
}
}

for ( final String localBranch : localBranches ) {
if ( remoteBranches.contains( localBranch ) ) {
git.branchCreate()
.setName( localBranch )
.setUpstreamMode( CreateBranchCommand.SetupUpstreamMode.SET_UPSTREAM )
.setStartPoint( "upstream/" + localBranch )
.setForce( true )
.call();
}
}

remoteBranches.removeAll( localBranches );

for ( final String branch : remoteBranches ) {
git.branchCreate()
.setName( branchName )
.setName( branch )
.setUpstreamMode( CreateBranchCommand.SetupUpstreamMode.SET_UPSTREAM )
.setStartPoint( "upstream/" + branchName )
.setStartPoint( "upstream/" + branch )
.setForce( true )
.call();
}
Expand Down
Expand Up @@ -16,20 +16,19 @@

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

import static org.fest.assertions.api.Assertions.*;
import static org.uberfire.java.nio.file.StandardDeleteOption.*;
import static org.uberfire.java.nio.fs.jgit.util.JGitUtil.*;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;

import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.lib.ObjectId;
Expand All @@ -56,7 +55,11 @@
import org.uberfire.java.nio.file.attribute.BasicFileAttributes;
import org.uberfire.java.nio.file.attribute.FileTime;
import org.uberfire.java.nio.fs.jgit.util.JGitUtil;
import org.uberfire.java.nio.fs.jgit.util.JGitUtil.PathType;
import org.uberfire.java.nio.fs.jgit.util.JGitUtil.*;

import static org.fest.assertions.api.Assertions.*;
import static org.uberfire.java.nio.file.StandardDeleteOption.*;
import static org.uberfire.java.nio.fs.jgit.util.JGitUtil.*;

public class JGitFileSystemProviderTest extends AbstractTestInfra {

Expand Down Expand Up @@ -211,7 +214,6 @@ public void testNewFileSystemClone() throws IOException {
}

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

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

provider.getFileSystem( URI.create( "git://my-repo?push=git://localhost:9418/my-simple-test-origin-repo&force" ) );

assertThat( fs ).isNotNull();

assertThat( fs.getRootDirectories() ).hasSize( 2 );

for ( final Path root : fs.getRootDirectories() ) {
if ( root.toAbsolutePath().toUri().toString().contains( "upstream" ) ) {
assertThat( provider.newDirectoryStream( root, null ) ).isNotEmpty().hasSize( 2 );
} else if ( root.toAbsolutePath().toUri().toString().contains( "origin" ) ) {
assertThat( provider.newDirectoryStream( root, null ) ).isNotEmpty().hasSize( 1 );
} else {
assertThat( provider.newDirectoryStream( root, null ) ).isNotEmpty().hasSize( 2 );
}
}

final URI newRepo2 = URI.create( "git://my-repo2" );

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

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

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

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

assertThat( fs2.getRootDirectories() ).hasSize( 5 );

final List<String> rootURIs1 = new ArrayList<String>() {{
add( "git://master@my-repo2/" );
add( "git://user-branch@my-repo2/" );
add( "git://origin/master@my-repo2/" );
add( "git://upstream/master@my-repo2/" );
add( "git://upstream/user-branch@my-repo2/" );
}};

final List<String> rootURIs2 = new ArrayList<String>() {{
add( "git://master@my-repo2/" );
add( "git://user-branch@my-repo2/" );
add( "git://user-branch-2@my-repo2/" );
add( "git://origin/master@my-repo2/" );
add( "git://upstream/master@my-repo2/" );
add( "git://upstream/user-branch@my-repo2/" );
add( "git://upstream/user-branch-2@my-repo2/" );
}};

final Set<String> rootURIs = new HashSet<String>();
for ( final Path root : fs2.getRootDirectories() ) {
if ( root.toAbsolutePath().toUri().toString().contains( "upstream" ) ) {
assertThat( provider.newDirectoryStream( root, null ) ).isNotEmpty().hasSize( 2 );
} else if ( root.toAbsolutePath().toUri().toString().contains( "origin" ) ) {
assertThat( provider.newDirectoryStream( root, null ) ).isNotEmpty().hasSize( 2 );
} else {
assertThat( provider.newDirectoryStream( root, null ) ).isNotEmpty().hasSize( 2 );
}
rootURIs.add( root.toUri().toString() );
}

rootURIs.removeAll( rootURIs1 );

assertThat( rootURIs ).isEmpty();

commit( origin.gitRepo(), "user-branch-2", "user1", "user1@example.com", "commitx", null, null, false, new HashMap<String, File>() {{
put( "file2UserBranch.txt", tempFile( "tempX" ) );
}} );

provider.getFileSystem( URI.create( "git://my-repo2?sync=git://localhost:9418/my-simple-test-origin-repo&force" ) );

assertThat( fs2.getRootDirectories() ).hasSize( 7 );

for ( final Path root : fs2.getRootDirectories() ) {
rootURIs.add( root.toUri().toString() );
}

rootURIs.removeAll( rootURIs2 );

assertThat( rootURIs ).isEmpty();
}

@Test
Expand Down

0 comments on commit f656d83

Please sign in to comment.