public JGitFileSystemProvider() { fullHostName = DAEMON_ENABLED ? DAEMON_HOST + ":" + DAEMON_PORT : null; final String[] repos = FILE_REPOSITORIES_ROOT.list( new FilenameFilter() { @Override public boolean accept( final File dir, String name ) { return name.endsWith( DOT_GIT_EXT ); } } ); if ( repos != null ) { for ( final String repo : repos ) { final File repoDir = new File( FILE_REPOSITORIES_ROOT, repo ); if ( repoDir.isDirectory() ) { final String name = repoDir.getName().substring( 0, repoDir.getName().indexOf( DOT_GIT_EXT ) ); final JGitFileSystem fs = new JGitFileSystem( this, fullHostName, newRepository( repoDir, true ), name, ALL, buildCredential( null ) ); fileSystems.put( name, fs ); repoIndex.put( fs.gitRepo().getRepository(), fs ); } } } if ( DAEMON_ENABLED ) { buildAndStartDaemon(); } else { daemonService = null; } }
} else { credential = buildCredential( null ); git = newRepository( repoDest, bare );
@Test public void testInputStream() throws IOException { final File parentFolder = createTempDirectory(); final File gitFolder = new File( parentFolder, "mytest.git" ); final Git origin = JGitUtil.newRepository( gitFolder, true ); commit( origin, "master", "user", "user@example.com", "commit message", null, null, false, new HashMap<String, File>() {{ put( "myfile.txt", tempFile( "temp\n.origin\n.content" ) ); }} ); final URI newRepo = URI.create( "git://inputstream-test-repo" ); final Map<String, Object> env = new HashMap<String, Object>() {{ put( JGitFileSystemProvider.GIT_DEFAULT_REMOTE_NAME, origin.getRepository().getDirectory().toString() ); }}; final FileSystem fs = PROVIDER.newFileSystem( newRepo, env ); assertThat( fs ).isNotNull(); final Path path = PROVIDER.getPath( URI.create( "git://origin/master@inputstream-test-repo/myfile.txt" ) ); final InputStream inputStream = PROVIDER.newInputStream( path ); assertThat( inputStream ).isNotNull(); final String content = new Scanner( inputStream ).useDelimiter( "\\A" ).next(); inputStream.close(); assertThat( content ).isNotNull().isEqualTo( "temp\n.origin\n.content" ); }
@Test public void testNewOutputStreamWithJGitOp() throws Exception { final File parentFolder = createTempDirectory(); final File gitFolder = new File( parentFolder, "mytest.git" ); final Git origin = JGitUtil.newRepository( gitFolder, true ); commit( origin, "master", "user", "user@example.com", "commit message", null, null, false, new HashMap<String, File>() {{ put( "myfile.txt", tempFile( "temp\n.origin\n.content" ) ); }} ); commit( origin, "user_branch", "user", "user@example.com", "commit message", null, null, false, new HashMap<String, File>() {{ put( "path/to/some/file/myfile.txt", tempFile( "some\n.content\nhere" ) ); }} ); final URI newRepo = URI.create( "git://outstreamwithop-test-repo" ); final Map<String, Object> env = new HashMap<String, Object>() {{ put( JGitFileSystemProvider.GIT_DEFAULT_REMOTE_NAME, origin.getRepository().getDirectory().toString() ); }}; final FileSystem fs = PROVIDER.newFileSystem( newRepo, env ); assertThat( fs ).isNotNull(); final SimpleDateFormat formatter = new SimpleDateFormat( "dd/MM/yyyy" ); final CommentedOption op = new CommentedOption( "User Tester", "user.tester@example.com", "omg, is it the end?", formatter.parse( "31/12/2012" ) ); final Path path = PROVIDER.getPath( URI.create( "git://user_branch@outstreamwithop-test-repo/some/path/myfile.txt" ) ); final OutputStream outStream = PROVIDER.newOutputStream( path, op ); assertThat( outStream ).isNotNull(); outStream.write( "my cool content".getBytes() ); outStream.close(); final InputStream inStream = PROVIDER.newInputStream( path ); final String content = new Scanner( inStream ).useDelimiter( "\\A" ).next(); inStream.close(); assertThat( content ).isNotNull().isEqualTo( "my cool content" ); }
@Test(expected = NoSuchFileException.class) public void testInputStream3() throws IOException { final File parentFolder = createTempDirectory(); final File gitFolder = new File( parentFolder, "mytest.git" ); final Git origin = JGitUtil.newRepository( gitFolder, true ); commit( origin, "master", "user", "user@example.com", "commit message", null, null, false, new HashMap<String, File>() {{ put( "path/to/file/myfile.txt", tempFile( "temp\n.origin\n.content" ) ); }} ); final URI newRepo = URI.create( "git://xxinputstream-test-repo" ); final Map<String, Object> env = new HashMap<String, Object>() {{ put( JGitFileSystemProvider.GIT_DEFAULT_REMOTE_NAME, origin.getRepository().getDirectory().toString() ); }}; final FileSystem fs = PROVIDER.newFileSystem( newRepo, env ); assertThat( fs ).isNotNull(); final Path path = PROVIDER.getPath( URI.create( "git://origin/master@xxinputstream-test-repo/path/to" ) ); PROVIDER.newInputStream( path ); }
@Test(expected = NoSuchFileException.class) public void testInputStreamNoSuchFile() throws IOException { final File parentFolder = createTempDirectory(); final File gitFolder = new File( parentFolder, "mytest.git" ); final Git origin = JGitUtil.newRepository( gitFolder, true ); commit( origin, "master", "user1", "user1@example.com", "commitx", null, null, false, new HashMap<String, File>() {{ put( "file.txt", tempFile( "temp.origin.content.2" ) ); }} ); final URI newRepo = URI.create( "git://inputstream-not-exists-test-repo" ); final Map<String, Object> env = new HashMap<String, Object>() {{ put( JGitFileSystemProvider.GIT_DEFAULT_REMOTE_NAME, origin.getRepository().getDirectory().toString() ); }}; final FileSystem fs = PROVIDER.newFileSystem( newRepo, env ); assertThat( fs ).isNotNull(); final Path path = PROVIDER.getPath( URI.create( "git://origin/master@inputstream-not-exists-test-repo/temp.txt" ) ); PROVIDER.newInputStream( path ); }
@Test public void testNewRepo() throws IOException { final File parentFolder = createTempDirectory(); final File gitFolder = new File( parentFolder, "mytest.git" ); final Git git = JGitUtil.newRepository( gitFolder, true ); assertThat( git ).isNotNull(); assertThat( branchList( git ).size() ).isEqualTo( 0 ); commit( git, "master", "name", "name@example.com", "commit", null, null, false, new HashMap<String, File>() {{ put( "file.txt", tempFile( "temp" ) ); }} ); assertThat( branchList( git ).size() ).isEqualTo( 1 ); }
@Test public void testInputStream2() throws IOException { final File parentFolder = createTempDirectory(); final File gitFolder = new File( parentFolder, "mytest.git" ); final Git origin = JGitUtil.newRepository( gitFolder, true ); commit( origin, "master", "user", "user@example.com", "commit message", null, null, false, new HashMap<String, File>() {{ put( "path/to/file/myfile.txt", tempFile( "temp\n.origin\n.content" ) ); }} ); final URI newRepo = URI.create( "git://xinputstream-test-repo" ); final Map<String, Object> env = new HashMap<String, Object>() {{ put( JGitFileSystemProvider.GIT_DEFAULT_REMOTE_NAME, origin.getRepository().getDirectory().toString() ); }}; final FileSystem fs = PROVIDER.newFileSystem( newRepo, env ); assertThat( fs ).isNotNull(); final Path path = PROVIDER.getPath( URI.create( "git://origin/master@xinputstream-test-repo/path/to/file/myfile.txt" ) ); final InputStream inputStream = PROVIDER.newInputStream( path ); assertThat( inputStream ).isNotNull(); final String content = new Scanner( inputStream ).useDelimiter( "\\A" ).next(); inputStream.close(); assertThat( content ).isNotNull().isEqualTo( "temp\n.origin\n.content" ); }
final File gitFolder = new File( parentFolder, "mytest.git" ); final Git origin = JGitUtil.newRepository( gitFolder, true );
@Test public void testAmend() throws IOException { final File parentFolder = createTempDirectory(); System.out.println("COOL!:" + parentFolder.toString()); final File gitFolder = new File( parentFolder, "myxxxtest.git" ); final Git origin = JGitUtil.newRepository( gitFolder, true ); commit( origin, "master", "name", "name@example.com", "commit!", null, null, false, new HashMap<String, File>() {{ put( "path/to/file2.txt", tempFile( "tempwdf sdf asdf asd2222" ) ); }} ); commit( origin, "master", "name", "name@example.com", "commit!", null, null, true, new HashMap<String, File>() {{ put( "path/to/file3.txt", tempFile( "temp2x d dasdf asdf 222" ) ); }} ); final File gitClonedFolder = new File( parentFolder, "myclone.git" ); final Git git = cloneRepository( gitClonedFolder, origin.getRepository().getDirectory().toString(), true, CredentialsProvider.getDefault() ); assertThat( JGitUtil.checkPath( git, "master", "pathx/" ).getK1() ).isEqualTo( NOT_FOUND ); assertThat( JGitUtil.checkPath( git, "master", "path/to/file2.txt" ).getK1() ).isEqualTo( FILE ); assertThat( JGitUtil.checkPath( git, "master", "path/to" ).getK1() ).isEqualTo( DIRECTORY ); }
@Test public void testClone() throws IOException { final File parentFolder = createTempDirectory(); final File gitFolder = new File( parentFolder, "mytest.git" ); final Git origin = JGitUtil.newRepository( gitFolder, true ); commit( origin, "user_branch", "name", "name@example.com", "commit!", null, null, false, new HashMap<String, File>() {{ put( "file2.txt", tempFile( "temp2222" ) ); }} ); commit( origin, "master", "name", "name@example.com", "commit", null, null, false, new HashMap<String, File>() {{ put( "file.txt", tempFile( "temp" ) ); }} ); commit( origin, "master", "name", "name@example.com", "commit", null, null, false, new HashMap<String, File>() {{ put( "file3.txt", tempFile( "temp3" ) ); }} ); final File gitClonedFolder = new File( parentFolder, "myclone.git" ); final Git git = cloneRepository( gitClonedFolder, origin.getRepository().getDirectory().toString(), true, CredentialsProvider.getDefault() ); assertThat( git ).isNotNull(); assertThat( branchList( git, ALL ) ).hasSize( 4 ); assertThat( branchList( git, ALL ).get( 0 ).getName() ).isEqualTo( "refs/heads/master" ); assertThat( branchList( git, ALL ).get( 1 ).getName() ).isEqualTo( "refs/heads/user_branch" ); assertThat( branchList( git, ALL ).get( 2 ).getName() ).isEqualTo( "refs/remotes/origin/master" ); assertThat( branchList( git, ALL ).get( 3 ).getName() ).isEqualTo( "refs/remotes/origin/user_branch" ); }
@Test public void testPathResolve() throws IOException { final File parentFolder = createTempDirectory(); final File gitFolder = new File( parentFolder, "mytest.git" ); final Git origin = JGitUtil.newRepository( gitFolder, true ); commit( origin, "user_branch", "name", "name@example.com", "commit!", null, null, false, new HashMap<String, File>() {{ put( "path/to/file2.txt", tempFile( "temp2222" ) ); }} ); commit( origin, "user_branch", "name", "name@example.com", "commit!", null, null, false, new HashMap<String, File>() {{ put( "path/to/file3.txt", tempFile( "temp2222" ) ); }} ); final File gitClonedFolder = new File( parentFolder, "myclone.git" ); final Git git = cloneRepository( gitClonedFolder, origin.getRepository().getDirectory().toString(), true, CredentialsProvider.getDefault() ); assertThat( JGitUtil.checkPath( git, "user_branch", "pathx/" ).getK1() ).isEqualTo( NOT_FOUND ); assertThat( JGitUtil.checkPath( git, "user_branch", "path/to/file2.txt" ).getK1() ).isEqualTo( FILE ); assertThat( JGitUtil.checkPath( git, "user_branch", "path/to" ).getK1() ).isEqualTo( DIRECTORY ); }