List of usage examples for org.eclipse.jgit.storage.file FileRepositoryBuilder FileRepositoryBuilder
FileRepositoryBuilder
From source file:com.surevine.gateway.scm.git.jgit.JGitGitFacade.java
License:Open Source License
@Override public boolean repoAlreadyCloned(final LocalRepoBean repoBean) throws GitException { boolean alreadyCloned = false; // if the enclosing directory exists then examine the repository to check it's the right one if (Files.exists(repoBean.getRepoDirectory())) { try {// w ww . j a va 2 s.co m // load the repository into jgit FileRepositoryBuilder builder = new FileRepositoryBuilder(); Repository repository = builder.setGitDir(repoBean.getGitConfigDirectory().toFile()).findGitDir() .build(); // examine the repository configuration and confirm whether it has a remote named "origin" // that points to the clone URL in the argument repo information. If it does the repo has // already been cloned. Config storedConfig = repository.getConfig(); String originURL = storedConfig.getString("remote", "origin", "url"); alreadyCloned = originURL != null && repoBean.getCloneSourceURI() != null && originURL.equals(repoBean.getCloneSourceURI()); repository.close(); } catch (IOException e) { LOGGER.error(e); throw new GitException(e); } } return alreadyCloned; }
From source file:com.surevine.gateway.scm.git.jgit.JGitGitFacade.java
License:Open Source License
@Override public boolean isRepoEmpty(LocalRepoBean repoBean) throws GitException { FileRepositoryBuilder builder = new FileRepositoryBuilder(); Repository repository = null;/*from w ww.ja va2 s .com*/ try { repository = builder.setGitDir(repoBean.getRepoDirectory().toFile()).findGitDir().build(); return repository == null || repository.getRef("HEAD") == null || repository.getRef("HEAD").getObjectId() == null; } catch (IOException e) { throw new GitException("Error detecting whether repository is empty.", e); } }
From source file:com.surevine.gateway.scm.git.jgit.JGitGitFacadeTest.java
License:Open Source License
@Test public void testBundleMultipleBranches() throws Exception { LocalRepoBean repoWithBranches = TestUtility.createTestRepoMultipleBranches(); Path bundlePath = underTest.bundle(repoWithBranches); assertTrue(Files.exists(bundlePath)); assertTrue(Files.isRegularFile(bundlePath)); LocalRepoBean fromBundle = new LocalRepoBean(); fromBundle.setCloneSourceURI(bundlePath.toString()); fromBundle.setProjectKey(repoWithBranches.getProjectKey()); fromBundle.setSlug(repoWithBranches.getSlug() + "_from_bundle"); fromBundle.setLocalBare(true);/*from w w w . j a v a 2 s. c om*/ underTest.clone(fromBundle); FileRepositoryBuilder builder = new FileRepositoryBuilder(); Repository originalRepo = builder.setGitDir(repoWithBranches.getGitConfigDirectory().toFile()).findGitDir() .build(); Git original = new Git(originalRepo); List<Ref> originalBranches = original.branchList().call(); Repository fromBundleRepo = builder.setGitDir(fromBundle.getGitConfigDirectory().toFile()).findGitDir() .build(); Git fromBundleGit = new Git(fromBundleRepo); List<Ref> fromBundleBraches = fromBundleGit.branchList().call(); int matches = 0; for (Ref branchRef : originalBranches) { for (Ref fromBundleRef : fromBundleBraches) { if (branchRef.getName().equals(fromBundleRef.getName())) { matches++; continue; } } } assertEquals(matches, originalBranches.size()); TestUtility.destroyTestRepo(fromBundle); TestUtility.destroyTestRepo(repoWithBranches); Files.deleteIfExists(bundlePath); }
From source file:com.surevine.gateway.scm.git.jgit.JGitGitFacadeTest.java
License:Open Source License
@Test public void testFetch() throws Exception { LocalRepoBean sourceRepoBean = TestUtility.createTestRepo(); String sourceDirectory = sourceRepoBean.getRepoDirectory().toString(); LocalRepoBean targetRepoBean = new LocalRepoBean(); targetRepoBean.setLocalBare(false);/*from w w w .j a v a 2s.c om*/ targetRepoBean.setProjectKey(sourceRepoBean.getProjectKey()); targetRepoBean.setSlug(sourceRepoBean.getSlug() + "_clone"); targetRepoBean.setCloneSourceURI(sourceDirectory); underTest.clone(targetRepoBean); underTest.addRemote(targetRepoBean, "source_repo", sourceDirectory); Map<String, String> remotes = underTest.getRemotes(targetRepoBean); assertTrue(remotes.containsKey("source_repo") && remotes.containsValue(sourceDirectory)); assertFalse(underTest.fetch(targetRepoBean, "source_repo")); FileRepositoryBuilder builder = new FileRepositoryBuilder(); Repository sourceRepository = builder.setGitDir(sourceRepoBean.getGitConfigDirectory().toFile()) .findGitDir().build(); String filename = "should_be_in_both.txt"; Files.write(sourceRepoBean.getRepoDirectory().resolve(filename), Arrays.asList("Hello World"), StandardCharsets.UTF_8, StandardOpenOption.CREATE, StandardOpenOption.APPEND); Git git = new Git(sourceRepository); git.add().addFilepattern(filename).call(); git.commit().setMessage("Added " + filename).call(); assertTrue(underTest.fetch(targetRepoBean, "source_repo")); TestUtility.destroyTestRepo(sourceRepoBean); TestUtility.destroyTestRepo(targetRepoBean); }
From source file:com.tasktop.c2c.server.scm.service.FetchWorkerThread.java
License:Open Source License
private void doMirrorFetch(String projectId, File mirroredRepo) { try {//from w ww. j a va 2 s.c o m // Set the home directory. This is used for configuration and ssh keys FS fs = FS.detect(); fs.setUserHome(new File(repositoryProvider.getBaseDir(), projectId)); FileRepositoryBuilder builder = new FileRepositoryBuilder().setGitDir(mirroredRepo).setFS(fs).setup(); Git git = new Git(new FileRepository(builder)); git.fetch().setRefSpecs(new RefSpec("refs/heads/*:refs/heads/*")).setThin(true).call(); } catch (Exception e) { logger.warn("Caught exception durring fetch", e); } }
From source file:com.technophobia.substeps.mojo.runner.SubstepsRunnerMojoTest.java
License:Open Source License
@Test public void testGitStuff() { FileRepositoryBuilder builder = new FileRepositoryBuilder(); try {/*from w w w . j av a 2 s .c o m*/ Repository repository = builder.setGitDir(new File("/home/ian/projects/github/substeps-framework/.git")) .readEnvironment() // scan environment GIT_* variables .findGitDir() // scan up the file system tree .build(); Git git = new Git(repository); String branchName = git.getRepository().getBranch(); System.out.println("get branch: " + branchName); if (branchName != null) { System.setProperty("SUBSTEPS_CURRENT_BRANCHNAME", branchName); } Config cfg = Configuration.INSTANCE.getConfig(); // System.out.println(cfg.getString("user.name")); System.out.println(cfg.getString("substeps.current.branchname")); } catch (Exception e) { System.out.println("Exception trying to get hold of the current branch: " + e.getMessage()); } }
From source file:com.telefonica.euro_iaas.sdc.puppetwrapper.services.impl.GitCloneServiceImpl.java
License:Apache License
public void download(String url, String moduleName) throws ModuleDownloaderException { // prepare a new folder for the cloned repository File localPath = new File(modulesCodeDownloadPath + moduleName); localPath.delete();/*w w w . j a va2s.co m*/ try { FileUtils.deleteDirectory(localPath); } catch (IOException e) { throw new ModuleDownloaderException(e); } // then clone logger.debug("Cloning from " + url + " to " + localPath); try { Git.cloneRepository().setURI(url).setDirectory(localPath).call(); } catch (InvalidRemoteException e) { throw new ModuleDownloaderException(e); } catch (TransportException e) { throw new ModuleDownloaderException(e); } catch (GitAPIException e) { throw new ModuleDownloaderException(e); } // now open the created repository FileRepositoryBuilder builder = new FileRepositoryBuilder(); Repository repository; try { repository = builder.setGitDir(localPath).readEnvironment() // scan environment GIT_* variables .findGitDir() // scan up the file system tree .build(); } catch (IOException e) { throw new ModuleDownloaderException(e); } logger.debug("Having repository: " + repository.getDirectory()); repository.close(); }
From source file:com.thoughtworks.go.service.ConfigRepository.java
License:Apache License
@Autowired public ConfigRepository(SystemEnvironment systemEnvironment) throws IOException { this.systemEnvironment = systemEnvironment; workingDir = this.systemEnvironment.getConfigRepoDir(); File configRepoDir = new File(workingDir, ".git"); gitRepo = new FileRepositoryBuilder().setGitDir(configRepoDir).build(); gitRepo.getConfig().setInt("gc", null, "auto", 0); git = new Git(gitRepo); }
From source file:com.unboundid.buildtools.repositoryinfo.RepositoryInfo.java
License:Open Source License
/** * Obtains information about the git repository from which the LDAP SDK source * was obtained./*w w w . j a va 2 s . c om*/ * * @throws Exception If a problem is encountered while attempting to obtain * information about the git repository. */ private void getGitInfo() throws Exception { final Repository r = new FileRepositoryBuilder().readEnvironment().findGitDir(baseDir).build(); final String repoURL = r.getConfig().getString("remote", "origin", "url"); final String repoRevision = r.resolve("HEAD").getName(); final String canonicalWorkspacePath = baseDir.getCanonicalPath(); final String canonicalRepositoryBaseDir = r.getDirectory().getParentFile().getCanonicalPath(); String repoPath = canonicalWorkspacePath.substring(canonicalRepositoryBaseDir.length()); if (!repoPath.startsWith("/")) { repoPath = '/' + repoPath; } getProject().setProperty(repositoryTypePropertyName, "git"); getProject().setProperty(repositoryURLPropertyName, repoURL); getProject().setProperty(repositoryPathPropertyName, repoPath); getProject().setProperty(repositoryRevisionPropertyName, repoRevision); getProject().setProperty(repositoryRevisionNumberPropertyName, "-1"); }
From source file:com.verigreen.collector.common.EmailSender.java
License:Apache License
public String getCommitMessage(String commitId) { String commitMessage = null;// w w w.ja v a 2 s . c om try { FileRepositoryBuilder builder = new FileRepositoryBuilder(); String repoPath = VerigreenNeededLogic.properties.getProperty("git.repositoryLocation"); Repository repo = builder.setGitDir(new File(repoPath)).setMustExist(true).build(); Git git = new Git(repo); Iterable<RevCommit> log = git.log().call(); Iterator<RevCommit> iterator = log.iterator(); while (commitMessage == null && iterator.hasNext()) { RevCommit rev = iterator.next(); String commit = rev.getName().substring(0, 7); if (commit.equals(commitId)) { commitMessage = rev.getFullMessage(); } } } catch (Exception e) { e.printStackTrace(); } return commitMessage; }