@Override public RemoteFile readFileWithMode(String uri, String ref, String path) throws GitAPIException, IOException { File dir = FileUtils.createTempDir("jgit_", ".git", null); //$NON-NLS-1$ //$NON-NLS-2$ try (Git git = Git.cloneRepository().setBare(true).setDirectory(dir) .setURI(uri).call()) { Repository repo = git.getRepository(); ObjectId refCommitId = sha1(uri, ref); if (refCommitId == null) { throw new InvalidRefNameException(MessageFormat .format(JGitText.get().refNotResolved, ref)); } RevCommit commit = repo.parseCommit(refCommitId); TreeWalk tw = TreeWalk.forPath(repo, path, commit.getTree()); // TODO(ifrade): Cope better with big files (e.g. using // InputStream instead of byte[]) return new RemoteFile( tw.getObjectReader().open(tw.getObjectId(0)) .getCachedBytes(Integer.MAX_VALUE), tw.getFileMode(0)); } finally { FileUtils.delete(dir, FileUtils.RECURSIVE); } } }
public Git cloneInto(File dir, boolean isBare) throws GitAPIException { Preconditions.checkNotNull(dir); Preconditions.checkArgument(dir.exists(),"dir does not exist: "+dir.getAbsolutePath()); CloneCommand cc = Git.cloneRepository().setURI(url).setBare(isBare) .setDirectory(dir).setCloneAllBranches(true); if ((!Strings.isNullOrEmpty(username)) || (!Strings.isNullOrEmpty(password))) { cc = cc.setCredentialsProvider(new UsernamePasswordCredentialsProvider( Strings.nullToEmpty(username), Strings .nullToEmpty(password))); } Git git = cc.call(); return git; }
static private Git cloneRepository(final String name) { return Git.cloneRepository() .setBare( true ) .setCloneAllBranches( true ) .setURI( METHOD + BASE + name + ".git" ) .setDirectory( new File( LOCAL_STORE, name ) ) .call(); }
/** * Clone a git repository. * * @param uri Where from? * @param dest Where to? * @param bare Bare (metadata-only) or full? * @throws GitAPIException */ private void cloneGitRepository(String uri, Path dest, boolean bare) throws GitAPIException { File directory = new File(dest.toString()); if (directory.exists()) throw new RepoDrillerException("Error, destination " + dest.toString() + " already exists"); log.info("Cloning Remote Repository " + uri + " into " + this.path); Git.cloneRepository() .setURI(uri) .setBare(bare) .setDirectory(directory) .setCloneAllBranches(true) .setNoCheckout(false) .call(); }
private void cloneRepository(Repository repo, String forkedPluginKey) { Git git = new Git(repo); git.cloneRepository() .setURI(repo.getDirectory().toURI().toString()) .setDirectory(new File(repositoriesDir, forkedPluginKey)) .setBare(false) .call(); executor.forKey(forkedPluginKey, SYNC_EVENT, new Operation<Repository, Void>() { public Void operateOn(Repository repo) throws Exception { // do nothing, we just want to force a sync return null; } }); log.info("Git repository {} cloned to {}", repo.getWorkTree().getName(), forkedPluginKey); }
public static Git tempClone(final JGitFileSystem fs, final String uuid) { try { File tempRepoPlace; int i = 0; while (true) { tempRepoPlace = new File(TEMP + uuid + "_" + i, fs.getGit().getRepository().getDirectory().getName().replaceFirst("\\.git", "")); if (tempRepoPlace.exists()) { i++; } else { break; } } return Git.cloneRepository() .setURI(fs.getGit().getRepository().getDirectory().toURI().toString()) .setDirectory(tempRepoPlace) .setBare(false) .setCloneAllBranches(true) .call(); } catch (GitAPIException e) { throw new RuntimeException(e); } } }
@Override public void execute() throws BuildException { log("Cloning repository " + uri); CloneCommand clone = Git.cloneRepository(); try { clone.setURI(uri).setDirectory(destination).setBranch(branch).setBare(bare); clone.call().getRepository().close(); } catch (Exception e) { log("Could not clone repository: " + e, e, Project.MSG_ERR); throw new BuildException("Could not clone repository: " + e.getMessage(), e); } } }
public synchronized void ensureLocalClone() throws IOException { GitRepository gitRepository = getGitRepository(); try { if (git != null) { return; } File dir = Files.createTempDir(); logger.info("cloning {} into {}", gitRepository.getUrl(), dir); CloneCommand cc = Git.cloneRepository() .setURI(gitRepository.getUrl()).setBare(true) .setDirectory(dir).setCloneAllBranches(true); if ((!Strings.isNullOrEmpty(gitRepository.getUsername())) || (!Strings.isNullOrEmpty(gitRepository.getPassword()))) { cc = cc.setCredentialsProvider(new UsernamePasswordCredentialsProvider( Strings.nullToEmpty(gitRepository.getUsername()), Strings.nullToEmpty(gitRepository.getPassword()))); } git = cc.call(); repo = git.getRepository(); } catch (GitAPIException e) { throw new IOException(e); } }
public static Git cloneRepository(final File repoFolder, final String fromURI, final CredentialsProvider credentialsProvider) { if (!repoFolder.getName().endsWith(DOT_GIT_EXT)) { throw new RuntimeException("Invalid name"); } try { final File gitDir = RepositoryCache.FileKey.resolve(repoFolder, DETECTED); final Repository repository; final Git git; if (gitDir != null && gitDir.exists()) { repository = new FileRepository(gitDir); git = new Git(repository); } else { git = Git.cloneRepository() .setBare(true) .setCloneAllBranches(true) .setURI(fromURI) .setDirectory(repoFolder) .setCredentialsProvider(credentialsProvider) .call(); repository = git.getRepository(); } fetchRepository(git, credentialsProvider); repository.close(); return git; } catch (final Exception ex) { throw new RuntimeException(ex); } }
protected Object doInBackground(String... params) { final AuthData authData = AuthData.getInstance(context); final ConnectionType connection = SshSessionFactory.getConnectionType(context); File localPath = new File(context.getFilesDir() + "/" + GIT_DIR); FileUtils.deleteFile(localPath); CloneCommand cloneCommand = Git.cloneRepository(); String url = getUrl(context); if (connection != ConnectionType.kHttp) cloneCommand.setTransportConfigCallback(new CustomTransportConfigCallback(context)); System.setProperty("user.home", context.getFilesDir().getAbsolutePath() ); try { cloneCommand .setURI(url) .setDirectory(localPath) .setCredentialsProvider(new CredentialsProviderAllowHost(authData.getUser(), authData.getPassword())) .setTransportConfigCallback(new CustomTransportConfigCallback(context)) .setBare(false) .call(); } catch (Exception e) { e.printStackTrace(); return e; } return null; }
private Git getWorkingFileRepository() throws GitAPIException { // Check for existing repo if (config.getWorkingFileRepository().getObjectDatabase().exists()) { return Git.wrap(config.getWorkingFileRepository()); } // Clone new repo return config.configure(pushRepository.clone(Git.cloneRepository())).setBare(true).call(); } }
@Test public void testRemoteRoot() throws IOException, GitAPIException { final JGitFileSystemProvider fsProvider = mock(JGitFileSystemProvider.class); final File tempDir = createTempDirectory(); final Git git = new GitImpl(GitImpl._cloneRepository().setNoCheckout(false).setBare(true).setCloneAllBranches(true).setURI(setupGit().getRepository().getDirectory().toString()).setDirectory(tempDir).call()); final JGitFileSystemImpl fileSystem = new JGitFileSystemImpl(fsProvider, null, git, createFSLock(git), "my-repo", CredentialsProvider.getDefault(), null, null); assertThat(fileSystem.isReadOnly()).isFalse(); assertThat(fileSystem.getSeparator()).isEqualTo("/"); assertThat(fileSystem.getName()).isEqualTo("my-repo"); assertThat(fileSystem.getRootDirectories()).hasSize(1); final Path root = fileSystem.getRootDirectories().iterator().next(); assertThat(root.toString()).isEqualTo("/"); assertThat(root.getRoot().toString()).isEqualTo("/"); }
@Test public void testRemoteRoot() throws IOException, GitAPIException { final JGitFileSystemProvider fsProvider = mock(JGitFileSystemProvider.class); final File tempDir = createTempDirectory(); final Git git = new GitImpl(GitImpl._cloneRepository().setNoCheckout(false).setBare(true).setCloneAllBranches(true).setURI(setupGit().getRepository().getDirectory().toString()).setDirectory(tempDir).call()); final JGitFileSystemImpl fileSystem = new JGitFileSystemImpl(fsProvider, null, git, createFSLock(git), "my-repo", CredentialsProvider.getDefault(), null, null); assertThat(fileSystem.isReadOnly()).isFalse(); assertThat(fileSystem.getSeparator()).isEqualTo("/"); assertThat(fileSystem.getName()).isEqualTo("my-repo"); assertThat(fileSystem.getRootDirectories()).hasSize(1); final Path root = fileSystem.getRootDirectories().iterator().next(); assertThat(root.toString()).isEqualTo("/"); assertThat(root.getRoot().toString()).isEqualTo("/"); }
public byte[] readFile(String uri, String ref, String path) throws GitAPIException, IOException { File dir = FileUtils.createTempDir("jgit_", ".git", null); //$NON-NLS-1$ //$NON-NLS-2$ Repository repo = Git .cloneRepository() .setBare(true) .setDirectory(dir) .setURI(uri) .call() .getRepository(); try { return readFileFromRepo(repo, ref, path); } finally { repo.close(); FileUtils.delete(dir, FileUtils.RECURSIVE); } }
public static void cloneRepo(String uri, File cloneTo) { Git result = null; try { result = Git.cloneRepository().setURI(uri).setDirectory(cloneTo) .setBranch(MASTER_BRANCH).setBare(false).setRemote(REMOTE_NAME) .setNoCheckout(false).setCloneAllBranches(false).setCloneSubmodules(false).call(); } catch (InvalidRemoteException e) { e.printStackTrace(); } catch (TransportException e) { e.printStackTrace(); } catch (GitAPIException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } finally { if (result != null) { result.close(); } } }
public static void cloneRepo(String uri, File cloneTo, String branch) { Git result = null; try { result = Git.cloneRepository().setURI(uri).setDirectory(cloneTo) .setBranch(branch).setBare(false).setRemote(REMOTE_NAME) .setNoCheckout(false).setCloneAllBranches(false).setCloneSubmodules(false).call(); } catch (InvalidRemoteException e) { e.printStackTrace(); } catch (TransportException e) { e.printStackTrace(); } catch (GitAPIException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } finally { if (result != null) { result.close(); } } }
@Override protected Git getGitRepo() throws Exception { if (stagingDirectory.exists() && new File(stagingDirectory, "HEAD").exists()) { Git git = Git.open(stagingDirectory); git.fetch().setRemote(getRemoteUrl(null, null)).setProgressMonitor(new TextProgressMonitor()).setRefSpecs(new RefSpec("refs/heads/master")).call(); } else { Git.cloneRepository() .setURI(getRemoteUrl(null, null)) .setProgressMonitor(new TextProgressMonitor()) .setDirectory(stagingDirectory) .setNoCheckout(true) .setBare(true) .call(); } Repository r = null; RepositoryBuilder b = new RepositoryBuilder().setGitDir(stagingDirectory).setWorkTree(sourceDirectory); r = b.build(); final Git git = Git.wrap(r); return git; }
public static void cloneRepo(String uri, File cloneTo, String branch, GitUser user) { Git result = null; try { result = Git.cloneRepository().setURI(uri).setDirectory(cloneTo) .setBranch(branch).setBare(false).setRemote(REMOTE_NAME) .setNoCheckout(false).setCloneAllBranches(false).setCloneSubmodules(false) .setCredentialsProvider(new UsernamePasswordCredentialsProvider(user.getUsername(), user.getPassword())).call(); } catch (InvalidRemoteException e) { e.printStackTrace(); } catch (TransportException e) { e.printStackTrace(); } catch (GitAPIException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } finally { if (result != null) { result.close(); } } }
@Override public RemoteFile readFileWithMode(String uri, String ref, String path) throws GitAPIException, IOException { File dir = FileUtils.createTempDir("jgit_", ".git", null); //$NON-NLS-1$ //$NON-NLS-2$ try (Git git = Git.cloneRepository().setBare(true).setDirectory(dir) .setURI(uri).call()) { Repository repo = git.getRepository(); ObjectId refCommitId = sha1(uri, ref); if (refCommitId == null) { throw new InvalidRefNameException(MessageFormat .format(JGitText.get().refNotResolved, ref)); } RevCommit commit = repo.parseCommit(refCommitId); TreeWalk tw = TreeWalk.forPath(repo, path, commit.getTree()); // TODO(ifrade): Cope better with big files (e.g. using // InputStream instead of byte[]) return new RemoteFile( tw.getObjectReader().open(tw.getObjectId(0)) .getCachedBytes(Integer.MAX_VALUE), tw.getFileMode(0)); } finally { FileUtils.delete(dir, FileUtils.RECURSIVE); } } }
@Test public void testRemoteRoot() throws IOException, GitAPIException { final JGitFileSystemProvider fsProvider = mock( JGitFileSystemProvider.class ); final File tempDir = createTempDirectory(); final Git git = Git.cloneRepository().setNoCheckout( false ).setBare( true ).setCloneAllBranches( true ).setURI( setupGit().getRepository().getDirectory().toString() ).setDirectory( tempDir ).call(); final JGitFileSystem fileSystem = new JGitFileSystem( fsProvider, null, git, "my-repo", CredentialsProvider.getDefault() ); assertThat( fileSystem.isReadOnly() ).isFalse(); assertThat( fileSystem.getSeparator() ).isEqualTo( "/" ); assertThat( fileSystem.getName() ).isEqualTo( "my-repo" ); assertThat( fileSystem.getRootDirectories() ).hasSize( 1 ); final Path root = fileSystem.getRootDirectories().iterator().next(); assertThat( root.toString() ).isEqualTo( "/" ); assertThat( root.getRoot().toString() ).isEqualTo( "/" ); }