@Override public Repository open( final DaemonClient client, final String name ) throws RepositoryNotFoundException, ServiceNotAuthorizedException, ServiceNotEnabledException, ServiceMayNotContinueException { final JGitFileSystem fs = fileSystems.get( name ); if ( fs == null ) { throw new RepositoryNotFoundException( name ); } return fs.gitRepo().getRepository(); } }
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; } }
private boolean existsBranch( final JGitPathImpl path ) { return hasBranch( path.getFileSystem().gitRepo(), path.getRefTree() ); }
private boolean deleteRepo( final FileSystem fileSystem ) { final File gitDir = ( (JGitFileSystem) fileSystem ).gitRepo().getRepository().getDirectory(); fileSystem.close(); try { FileUtils.delete( gitDir, FileUtils.RECURSIVE ); closedFileSystems.remove( fileSystem ); fileSystems.remove( ( (JGitFileSystem) fileSystem ).id() ); return true; } catch ( java.io.IOException e ) { throw new IOException( e ); } }
private void createBranch( final JGitPathImpl source, final JGitPathImpl target ) { JGitUtil.createBranch( source.getFileSystem().gitRepo(), source.getRefTree(), target.getRefTree() ); }
@Override public FileSystem getFileSystem( final URI uri ) throws IllegalArgumentException, FileSystemNotFoundException, SecurityException { checkNotNull( "uri", uri ); checkCondition( "uri scheme not supported", uri.getScheme().equals( getScheme() ) || uri.getScheme().equals( "default" ) ); checkURI( "uri", uri ); final JGitFileSystem fileSystem = fileSystems.get( extractRepoName( uri ) ); if ( fileSystem == null ) { throw new FileSystemNotFoundException( "No filesystem for uri (" + uri + ") found." ); } if ( hasSyncFlag( uri ) ) { try { final String treeRef = "master"; final ObjectId oldHead = JGitUtil.getTreeRefObjectId( fileSystem.gitRepo().getRepository(), treeRef ); final Map<String, String> params = getQueryParams( uri ); syncRepository( fileSystem.gitRepo(), fileSystem.getCredential(), params.get( "sync" ), hasForceFlag( uri ) ); final ObjectId newHead = JGitUtil.getTreeRefObjectId( fileSystem.gitRepo().getRepository(), treeRef ); notifyDiffs( fileSystem, treeRef, "<system>", "<system>", oldHead, newHead ); } catch ( final Exception ex ) { throw new IOException( ex ); } } return fileSystem; }
@Override public FileStore getFileStore( final Path path ) throws IOException, SecurityException { checkNotNull( "path", path ); return new JGitFileStore( toPathImpl( path ).getFileSystem().gitRepo().getRepository() ); }
@Override public void createDirectory( final Path path, final FileAttribute<?>... attrs ) throws UnsupportedOperationException, FileAlreadyExistsException, IOException, SecurityException { checkNotNull( "path", path ); final JGitPathImpl gPath = toPathImpl( path ); final Pair<PathType, ObjectId> result = checkPath( gPath.getFileSystem().gitRepo(), gPath.getRefTree(), gPath.getPath() ); if ( !result.getK1().equals( NOT_FOUND ) ) { throw new FileAlreadyExistsException( path.toString() ); } try { final OutputStream outputStream = newOutputStream( path.resolve( ".gitignore" ) ); outputStream.write( "# empty\n".getBytes() ); outputStream.close(); } catch ( final Exception e ) { throw new IOException( e ); } }
@Override public InputStream newInputStream( final Path path, final OpenOption... options ) throws IllegalArgumentException, UnsupportedOperationException, NoSuchFileException, IOException, SecurityException { checkNotNull( "path", path ); final JGitPathImpl gPath = toPathImpl( path ); return resolveInputStream( gPath.getFileSystem().gitRepo(), gPath.getRefTree(), gPath.getPath() ); }
@Override public void checkAccess( final Path path, final AccessMode... modes ) throws UnsupportedOperationException, NoSuchFileException, AccessDeniedException, IOException, SecurityException { checkNotNull( "path", path ); final JGitPathImpl gPath = toPathImpl( path ); final Pair<PathType, ObjectId> result = checkPath( gPath.getFileSystem().gitRepo(), gPath.getRefTree(), gPath.getPath() ); if ( result.getK1().equals( NOT_FOUND ) ) { throw new NoSuchFileException( path.toString() ); } }
@Override public boolean isSameFile( final Path pathA, final Path pathB ) throws IOException, SecurityException { checkNotNull( "pathA", pathA ); checkNotNull( "pathB", pathB ); final JGitPathImpl gPathA = toPathImpl( pathA ); final JGitPathImpl gPathB = toPathImpl( pathB ); final Pair<PathType, ObjectId> resultA = checkPath( gPathA.getFileSystem().gitRepo(), gPathA.getRefTree(), gPathA.getPath() ); final Pair<PathType, ObjectId> resultB = checkPath( gPathB.getFileSystem().gitRepo(), gPathB.getRefTree(), gPathB.getPath() ); if ( resultA.getK1() == PathType.FILE && resultA.getK2().equals( resultB.getK2() ) ) { return true; } return pathA.equals( pathB ); }
public boolean deleteAssetIfExists( final JGitPathImpl path, final DeleteOption... options ) { final Pair<PathType, ObjectId> result = checkPath( path.getFileSystem().gitRepo(), path.getRefTree(), path.getPath() ); if ( result.getK1().equals( PathType.DIRECTORY ) ) { if ( deleteNonEmptyDirectory( options ) ) { deleteResource( path, options ); return true; } final List<JGitPathInfo> content = listPathContent( path.getFileSystem().gitRepo(), path.getRefTree(), path.getPath() ); if ( content.size() == 1 && content.get( 0 ).getPath().equals( path.getPath().substring( 1 ) + "/.gitignore" ) ) { delete( path.resolve( ".gitignore" ) ); return true; } throw new DirectoryNotEmptyException( path.toString() ); } if ( result.getK1().equals( NOT_FOUND ) ) { return false; } deleteResource( path, options ); return true; }
public void deleteAsset( final JGitPathImpl path, final DeleteOption... options ) { final Pair<PathType, ObjectId> result = checkPath( path.getFileSystem().gitRepo(), path.getRefTree(), path.getPath() ); if ( result.getK1().equals( PathType.DIRECTORY ) ) { if ( deleteNonEmptyDirectory( options ) ) { deleteResource( path, options ); return; } final List<JGitPathInfo> content = listPathContent( path.getFileSystem().gitRepo(), path.getRefTree(), path.getPath() ); if ( content.size() == 1 && content.get( 0 ).getPath().equals( path.getPath().substring( 1 ) + "/.gitignore" ) ) { delete( path.resolve( ".gitignore" ) ); deleteResource( path, options ); return; } throw new DirectoryNotEmptyException( path.toString() ); } if ( result.getK1().equals( NOT_FOUND ) ) { throw new NoSuchFileException( path.toString() ); } deleteResource( path, options ); }
private void copyDirectory( final JGitPathImpl source, final JGitPathImpl target, final CopyOption... options ) { final List<JGitPathImpl> directories = new ArrayList<JGitPathImpl>(); for ( final Path path : newDirectoryStream( source, null ) ) { final JGitPathImpl gPath = toPathImpl( path ); final Pair<PathType, ObjectId> pathResult = checkPath( gPath.getFileSystem().gitRepo(), gPath.getRefTree(), gPath.getPath() ); if ( pathResult.getK1() == DIRECTORY ) { directories.add( gPath ); continue; } final JGitPathImpl gTarget = composePath( target, (JGitPathImpl) gPath.getFileName() ); copyFile( gPath, gTarget ); } for ( final JGitPathImpl directory : directories ) { createDirectory( composePath( target, (JGitPathImpl) directory.getFileName() ) ); } }
@Override public <A extends BasicFileAttributes> A readAttributes( final Path path, final Class<A> type, final LinkOption... options ) throws NoSuchFileException, UnsupportedOperationException, IOException, SecurityException { checkNotNull( "path", path ); checkNotNull( "type", type ); final JGitPathImpl gPath = toPathImpl( path ); final Pair<PathType, ObjectId> pathResult = checkPath( gPath.getFileSystem().gitRepo(), gPath.getRefTree(), gPath.getPath() ); if ( pathResult.getK1().equals( NOT_FOUND ) ) { throw new NoSuchFileException( path.toString() ); } if ( type == BasicFileAttributesImpl.class || type == BasicFileAttributes.class || type == VersionAttributes.class ) { final JGitVersionAttributeView view = getFileAttributeView( path, JGitVersionAttributeView.class, options ); return (A) view.readAttributes(); } return null; }
@Override public <V extends FileAttributeView> V getFileAttributeView( final Path path, final Class<V> type, final LinkOption... options ) throws NoSuchFileException { checkNotNull( "path", path ); checkNotNull( "type", type ); final JGitPathImpl gPath = toPathImpl( path ); final Pair<PathType, ObjectId> pathResult = checkPath( gPath.getFileSystem().gitRepo(), gPath.getRefTree(), gPath.getPath() ); if ( pathResult.getK1().equals( NOT_FOUND ) ) { throw new NoSuchFileException( path.toString() ); } final V resultView = gPath.getAttrView( type ); if ( resultView == null && ( type == BasicFileAttributeView.class || type == VersionAttributeView.class || type == JGitVersionAttributeView.class ) ) { final V newView = (V) new JGitVersionAttributeView( gPath ); gPath.addAttrView( newView ); return newView; } return resultView; }
private void copyAsset( final JGitPathImpl source, final JGitPathImpl target, final CopyOption... options ) { final Pair<PathType, ObjectId> sourceResult = checkPath( source.getFileSystem().gitRepo(), source.getRefTree(), source.getPath() ); final Pair<PathType, ObjectId> targetResult = checkPath( target.getFileSystem().gitRepo(), target.getRefTree(), target.getPath() ); if ( !isRoot( target ) && targetResult.getK1() != NOT_FOUND ) { if ( !contains( options, StandardCopyOption.REPLACE_EXISTING ) ) { throw new FileAlreadyExistsException( target.toString() ); } } if ( sourceResult.getK1() == NOT_FOUND ) { throw new NoSuchFileException( target.toString() ); } if ( sourceResult.getK1() == DIRECTORY ) { copyDirectory( source, target, options ); return; } copyFile( source, target, options ); }
@Test public void testCreateDirectory() throws Exception { final URI newRepo = URI.create( "git://xcreatedir-test-repo" ); PROVIDER.newFileSystem( newRepo, EMPTY_ENV ); final JGitPathImpl path = (JGitPathImpl) PROVIDER.getPath( URI.create( "git://master@xcreatedir-test-repo/some/path/to/" ) ); final Pair<PathType, ObjectId> result = JGitUtil.checkPath( path.getFileSystem().gitRepo(), path.getRefTree(), path.getPath() ); assertThat( result.getK1() ).isEqualTo( PathType.NOT_FOUND ); PROVIDER.createDirectory( path ); final Pair<PathType, ObjectId> resultAfter = JGitUtil.checkPath( path.getFileSystem().gitRepo(), path.getRefTree(), path.getPath() ); assertThat( resultAfter.getK1() ).isEqualTo( PathType.DIRECTORY ); try { PROVIDER.createDirectory( path ); failBecauseExceptionWasNotThrown( FileAlreadyExistsException.class ); } catch ( FileAlreadyExistsException e ) { } }