public static void delete( final JGitPathImpl path, final String sessionId, final String name, final String email, final String message, final TimeZone timeZone, final Date when, final boolean amend ) { commit( path, sessionId, name, email, message, timeZone, when, amend, new HashMap<String, File>() {{ put( path.getPath(), null ); }} ); }
@Override public VersionAttributes readAttributes() throws IOException { if ( attrs == null ) { attrs = JGitUtil.buildVersionAttributes( path.getFileSystem(), path.getRefTree(), path.getPath() ); } return attrs; }
private JGitPathImpl composePath( final JGitPathImpl directory, final JGitPathImpl fileName, final CopyOption... options ) { if ( directory.getPath().endsWith( "/" ) ) { return toPathImpl( getPath( URI.create( directory.toUri().toString() + fileName.toString( false ) ) ) ); } return toPathImpl( getPath( URI.create( directory.toUri().toString() + "/" + fileName.toString( false ) ) ) ); }
@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() ); }
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; }
@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() ); } }
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 ); }
final Pair<PathType, ObjectId> result = checkPath( gPath.getFileSystem().gitRepo(), gPath.getRefTree(), gPath.getPath() );
@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 ); }
final Pair<PathType, ObjectId> result = checkPath( gPath.getFileSystem().gitRepo(), gPath.getRefTree(), gPath.getPath() );
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 ); }
void deleteResource( final JGitPathImpl path, final DeleteOption... options ) { String sessionId = null; String name = null; String email = null; String message = "delete {" + path.getPath() + "}"; TimeZone timeZone = null; Date when = null; if ( options != null && options.length > 0 ) { for ( final DeleteOption option : options ) { if ( option instanceof CommentedOption ) { final CommentedOption op = (CommentedOption) option; sessionId = op.getSessionId(); name = op.getName(); email = op.getEmail(); message = op.getMessage(); timeZone = op.getTimeZone(); when = op.getWhen(); break; } } } JGitUtil.delete( path, sessionId, name, email, message, timeZone, when, amend() ); checkAmend(); }
@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 ) { } }