public void cleanup() { FileUtils.deleteQuietly(path); JGitFileSystemProvider gitFsProvider = (JGitFileSystemProvider) FileSystemProviders.resolveProvider(URI.create("git://whatever")); gitFsProvider.shutdown(); FileUtils.deleteQuietly(gitFsProvider.getGitRepoContainerDir()); }
private void copyDirectory(final JGitPathImpl source, final JGitPathImpl target) { 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())); } }
private void deleteResource(final JGitPathImpl path, final DeleteOption... options) { delete(path, buildCommitInfo("delete {" + path.getPath() + "}", Arrays.asList(options))); }
private void cleanupParentDir(File gitDir) throws java.io.IOException { final File parentDir = gitDir.getParentFile(); if (parentDir.isDirectory() && parentDirIsEmpty(parentDir) && !parentDir.equals(getGitRepoContainerDir())) { FileUtils.delete(parentDir, FileUtils.RECURSIVE | FileUtils.RETRY); } }
final FileSystem fs = provider.newFileSystem(newRepo, new HashMap<String, Object>() {{ put(JGitFileSystemProviderConfiguration.GIT_ENV_KEY_INIT, final Path _path = provider.getPath(URI.create("git://user_branch@outstream-test-repo/some/path/myfile.txt")); provider.setAttribute(_path, FileSystemState.FILE_SYSTEM_STATE_ATTR, FileSystemState.BATCH); final Path path = provider.getPath(URI.create("git://user_branch@outstream-test-repo/some/path/myfile.txt")); final OutputStream outStream = provider.newOutputStream(path); assertThat(outStream).isNotNull(); outStream.write(("my cool content" + z).getBytes()); final Path path2 = provider.getPath(URI.create("git://error_branch@outstream-test-repo/some/path/myfile.txt")); final OutputStream outStream2 = provider.newOutputStream(path2); assertThat(outStream2).isNotNull(); outStream2.write(("bad content" + z).getBytes()); provider.setAttribute(_path, FileSystemState.FILE_SYSTEM_STATE_ATTR, FileSystemState.NORMAL); final Path path = provider.getPath(URI.create("git://error_branch@outstream-test-repo/some/path/myfile.txt")); final JGitVersionAttributeView attrs = provider.getFileAttributeView(path.getRoot(), JGitVersionAttributeView.class);
@Test public void testCopyFilesAcrossRepositories() throws IOException { final URI newRepo1 = URI.create("git://copyasset-test-repo1"); provider.newFileSystem(newRepo1, EMPTY_ENV); final URI newRepo2 = URI.create("git://copyasset-test-repo2"); provider.newFileSystem(newRepo2, EMPTY_ENV); final Path path = provider.getPath(URI.create("git://master@copyasset-test-repo1/myfile1.txt")); { final OutputStream outStream = provider.newOutputStream(path); outStream.write("my cool content".getBytes()); outStream.close(); } final Path target = provider.getPath(URI.create("git://master@copyasset-test-repo2/myfile1.txt")); provider.copy(path, target); final DirectoryStream<Path> stream = provider.newDirectoryStream(provider.getPath(URI.create("git://master@copyasset-test-repo2/")), null); for (Path path1 : stream) { System.out.println("content: " + path1.toUri()); } assertThat(stream).isNotNull().hasSize(1); }
@Test public void testDeleteBranch() throws IOException { final URI newRepo = URI.create("git://delete-branch-test-repo"); provider.newFileSystem(newRepo, EMPTY_ENV); final Path path = provider.getPath(URI.create("git://user_branch@delete-branch-test-repo/path/to/myfile.txt")); final OutputStream outStream = provider.newOutputStream(path); assertThat(outStream).isNotNull(); outStream.write("my cool content".getBytes()); outStream.close(); provider.newInputStream(path).close(); provider.delete(provider.getPath(URI.create("git://user_branch@delete-branch-test-repo"))); try { provider.delete(provider.getPath(URI.create("git://user_branch@delete-branch-test-repo"))); failBecauseExceptionWasNotThrown(NoSuchFileException.class); } catch (NoSuchFileException ignored) { } try { provider.delete(provider.getPath(URI.create("git://some_user_branch@delete-branch-test-repo"))); failBecauseExceptionWasNotThrown(NoSuchFileException.class); } catch (NoSuchFileException ignored) { } }
@Test public void testDeleteNonEmptyDirectory() throws IOException { final URI newRepo = URI.create("git://delete-non-empty-test-repo"); provider.newFileSystem(newRepo, EMPTY_ENV); final Path dir = provider.getPath(URI.create("git://master@delete-non-empty-test-repo/other/path")); final Path _root = provider.getPath(URI.create("git://master@delete-non-empty-test-repo/myfile1.txt")); final OutputStream outRootStream = provider.newOutputStream(_root); outRootStream.write("my cool content".getBytes()); outRootStream.close(); final Path path = provider.getPath(URI.create("git://master@delete-non-empty-test-repo/other/path/myfile1.txt")); final OutputStream outStream = provider.newOutputStream(path); outStream.write("my cool content".getBytes()); outStream.close(); final Path path2 = provider.getPath(URI.create("git://master@delete-non-empty-test-repo/other/path/myfile2.txt")); final OutputStream outStream2 = provider.newOutputStream(path2); outStream2.write("my cool content".getBytes()); outStream2.close(); final Path path3 = provider.getPath(URI.create("git://master@delete-non-empty-test-repo/other/path/myfile3.txt")); final OutputStream outStream3 = provider.newOutputStream(path3); outStream3.write("my cool content".getBytes());
@Test public void testDeleteBranchIfExists() throws IOException { final URI newRepo = URI.create("git://deletebranchifexists1-test-repo"); provider.newFileSystem(newRepo, EMPTY_ENV); final Path path = provider.getPath(URI.create("git://user_branch@deletebranchifexists1-test-repo/path/to/myfile.txt")); final OutputStream outStream = provider.newOutputStream(path); assertThat(outStream).isNotNull(); outStream.write("my cool content".getBytes()); outStream.close(); provider.newInputStream(path).close(); assertThat(provider.deleteIfExists(provider.getPath(URI.create("git://user_branch@deletebranchifexists1-test-repo")))).isTrue(); assertThat(provider.deleteIfExists(provider.getPath(URI.create("git://not_user_branch@deletebranchifexists1-test-repo")))).isFalse(); assertThat(provider.deleteIfExists(provider.getPath(URI.create("git://user_branch@deletebranchifexists1-test-repo")))).isFalse(); }
@Test public void testNewFileSystemWithSubfolder() { final URI newRepo = URI.create("git://test/sub-repo-name"); final FileSystem fs = provider.newFileSystem(newRepo, EMPTY_ENV); assertThat(fs).isNotNull(); final DirectoryStream<Path> stream = provider.newDirectoryStream(provider.getPath(newRepo), null); assertThat(stream).isEmpty(); }
@Test public void testPathNonBranchRooted() throws IOException, GitAPIException { final JGitFileSystemProvider fsProvider = mock(JGitFileSystemProvider.class); when(fsProvider.isDefault()).thenReturn(false); when(fsProvider.getScheme()).thenReturn("git"); final Git git = setupGit(); final JGitFileSystemImpl fileSystem = new JGitFileSystemImpl(fsProvider, null, git, createFSLock(git), "my-repo", CredentialsProvider.getDefault(), null, null); final Path path = fileSystem.getPath("/path/to/some/place.txt"); AssertionsForClassTypes.assertThat(path).isNotNull(); assertThat(path.isAbsolute()).isTrue(); assertThat(path.toString()).isEqualTo("/path/to/some/place.txt"); assertThat(path.toUri().toString()).isEqualTo("git://master@my-repo/path/to/some/place.txt"); assertThat(path.getNameCount()).isEqualTo(4); assertThat(path.getName(0).toString()).isNotNull().isEqualTo("path"); assertThat(path.getRoot().toString()).isNotNull().isEqualTo("/"); }
@Test public void testCreateDirectory() { 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 PathInfo result = path.getFileSystem().getGit().getPathInfo(path.getRefTree(), path.getPath()); assertThat(result.getPathType()).isEqualTo(PathType.NOT_FOUND); provider.createDirectory(path); final PathInfo resultAfter = path.getFileSystem().getGit().getPathInfo(path.getRefTree(), path.getPath()); assertThat(resultAfter.getPathType()).isEqualTo(PathType.DIRECTORY); final Path gitkeepPath = path.resolve(".gitkeep"); assertThat(provider.exists(gitkeepPath)).isEqualTo(true); try { provider.createDirectory(path); failBecauseExceptionWasNotThrown(FileAlreadyExistsException.class); } catch (FileAlreadyExistsException ignored) { } }
@Override public void setAttribute(final Path path, final String attribute, final Object value, final LinkOption... options) throws UnsupportedOperationException, IllegalArgumentException, ClassCastException, IOException, SecurityException { checkNotNull("path", path); checkNotEmpty("attributes", attribute); final String[] s = split(attribute); if (s[0].length() == 0) { throw new IllegalArgumentException(attribute); } final FlexibleFileAttributeView view = getFileAttributeView(toPathImpl(path), s[0], options); if (view == null) { throw new UnsupportedOperationException("View '" + s[0] + "' not available"); } view.setAttribute(s[1], value); }
@Test(expected = NoSuchFileException.class) public void testInputStream3() throws IOException { final File parentFolder = createTempDirectory(); final File gitFolder = new File(parentFolder, "mytest.git"); final Git origin = new CreateRepository(gitFolder).execute().get(); new 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")); }}).execute(); final URI newRepo = URI.create("git://xxinputstream-test-repo"); final Map<String, Object> env = new HashMap<String, Object>() {{ put(JGitFileSystemProviderConfiguration.GIT_ENV_KEY_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); }
@Override public Path getPath(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(); } try { return JGitPathImpl.create(fileSystem, URIUtil.decode(extractPath(uri)), extractHost(uri), false); } catch (final URIException e) { return null; } }
@Override public Path getPath(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); if (LOG.isDebugEnabled()) { LOG.debug("Accessing uri " + uri.toString()); } Path path; JGitFileSystem fileSystem = deepLookupFSFrom(uri); String branch = extractBranchFrom(uri); String host = buildHostFrom(fileSystem, branch); String pathStr = buildPathFrom(uri, host); path = JGitPathImpl.create(fileSystem, pathStr, host, false); return path; }
this.lockAndSquash(path, (SquashOption) value); return; firePostponedBatchEvents(fileSystem); postCommitHook(fileSystem); final String[] s = split(attribute); if (s[0].length() == 0) { throw new IllegalArgumentException(attribute); final ExtendedAttributeView view = getFileAttributeView(toPathImpl(path), s[0], options);
@Test public void testDeleteShouldRemoveEmptyParentDir() throws IOException { final URI doraRepo = URI.create("git://parentDir/dora-repo"); FileSystem doraFS = provider.newFileSystem(doraRepo, EMPTY_ENV); final File doraRepoDir = ((JGitFileSystemProxy) doraFS).getGit().getRepository().getDirectory(); final File parentDir = doraRepoDir.getParentFile(); final File gitProviderDir = provider.getGitRepoContainerDir(); final URI doraRepo1 = URI.create("git://parentDir/dora-repo1"); FileSystem doraFS1 = provider.newFileSystem(doraRepo1, EMPTY_ENV); final File dora1RepoDir = ((JGitFileSystemProxy) doraFS1).getGit().getRepository().getDirectory(); final File parentDir1 = doraRepoDir.getParentFile(); assertEquals(parentDir, parentDir1); provider.delete(doraFS.getPath(null)); assertFalse(doraRepoDir.exists()); assertTrue(parentDir.exists()); assertTrue(gitProviderDir.exists()); provider.delete(doraFS1.getPath(null)); assertFalse(dora1RepoDir.exists()); assertFalse(parentDir1.exists()); assertTrue(gitProviderDir.exists()); }
target.getPath()); if (!isRoot(target) && targetResult.getPathType() != NOT_FOUND) { if (!contains(options, StandardCopyOption.REPLACE_EXISTING)) { throw new FileAlreadyExistsException(target.toString()); copy(source, target, options); delete(source); } else { final Map<JGitPathImpl, JGitPathImpl> fromTo = new HashMap<>(); if (sourceResult.getPathType() == DIRECTORY) { fromTo.putAll(mapDirectoryContent(source, target, options)); moveFiles(source, target, fromTo,
@Test public void testCreateANewDirectoryWithMigrationEnv() { final Map<String, ?> envMigrate = new HashMap<String, Object>() {{ put("init", Boolean.TRUE); put("migrate-from", URI.create("git://old")); }}; String newPath = "git://test/old"; final URI newUri = URI.create(newPath); provider.newFileSystem(newUri, envMigrate); provider.getFileSystem(newUri); assertThat(new File(provider.getGitRepoContainerDir(), "test/old" + ".git")).exists(); assertThat(provider.getFileSystem(newUri)).isNotNull(); } }