List of usage examples for org.eclipse.jgit.lib Repository close
@Override public void close()
Decrement the use count, and maybe close resources.
From source file:com.spotify.docker.Utils.java
License:Apache License
public static String getGitCommitId() throws GitAPIException, DockerException, IOException, MojoExecutionException { final FileRepositoryBuilder builder = new FileRepositoryBuilder(); builder.readEnvironment(); // scan environment GIT_* variables builder.findGitDir(); // scan up the file system tree if (builder.getGitDir() == null) { throw new MojoExecutionException("Cannot tag with git commit ID because directory not a git repo"); }//from w w w . j a va2 s . c o m final StringBuilder result = new StringBuilder(); final Repository repo = builder.build(); try { // get the first 7 characters of the latest commit final ObjectId head = repo.resolve("HEAD"); result.append(head.getName().substring(0, 7)); final Git git = new Git(repo); // append first git tag we find for (Ref gitTag : git.tagList().call()) { if (gitTag.getObjectId().equals(head)) { // name is refs/tag/name, so get substring after last slash final String name = gitTag.getName(); result.append("."); result.append(name.substring(name.lastIndexOf('/') + 1)); break; } } // append '.DIRTY' if any files have been modified final Status status = git.status().call(); if (status.hasUncommittedChanges()) { result.append(".DIRTY"); } } finally { repo.close(); } return result.length() == 0 ? null : result.toString(); }
From source file:com.surevine.gateway.scm.git.jgit.JGitGitFacade.java
License:Open Source License
@Override public boolean fetch(final LocalRepoBean repoBean, final String remoteName) throws GitException { String remoteToPull = (remoteName != null) ? remoteName : "origin"; FetchResult result;/*from w w w .j av a 2s . c o m*/ try { FileRepositoryBuilder builder = new FileRepositoryBuilder(); Repository repository = builder.setGitDir(repoBean.getGitConfigDirectory().toFile()).findGitDir() .build(); Git git = new org.eclipse.jgit.api.Git(repository); FetchCommand fetchCommand = git.fetch(); fetchCommand.setRemote(remoteToPull); fetchCommand.setRefSpecs(new RefSpec("+refs/heads/*:refs/heads/*")); result = fetchCommand.call(); repository.close(); } catch (GitAPIException | IOException e) { LOGGER.error(e); throw new GitException(e); } boolean hadUpdates = !result.getTrackingRefUpdates().isEmpty(); return hadUpdates; }
From source file:com.surevine.gateway.scm.git.jgit.JGitGitFacade.java
License:Open Source License
@Override public void tag(final LocalRepoBean repoBean, final String tag) throws GitException { try {/*from ww w . j av a 2s. c o m*/ FileRepositoryBuilder builder = new FileRepositoryBuilder(); Repository repository = builder.setGitDir(repoBean.getGitConfigDirectory().toFile()).findGitDir() .build(); Git git = new org.eclipse.jgit.api.Git(repository); TagCommand tagCommand = git.tag(); tagCommand.setName(tag); tagCommand.call(); repository.close(); } catch (GitAPIException | IOException e) { LOGGER.error(e); throw new GitException(e); } }
From source file:com.surevine.gateway.scm.git.jgit.JGitGitFacade.java
License:Open Source License
@Override public Path bundle(final LocalRepoBean repoBean) throws GitException { OutputStream outputStream = null; try {//w w w .ja va2 s .com FileRepositoryBuilder builder = new FileRepositoryBuilder(); Repository repository = builder.setGitDir(repoBean.getGitConfigDirectory().toFile()).findGitDir() .build(); BundleWriter bundleWriter = new BundleWriter(repository); String fileName = StringUtil.cleanStringForFilePath(repoBean.getProjectKey() + "_" + repoBean.getSlug()) + ".bundle"; Path outputPath = Paths.get(PropertyUtil.getTempDir(), fileName); outputStream = Files.newOutputStream(outputPath); Map<String, Ref> refMap = repository.getAllRefs(); for (Ref ref : refMap.values()) { bundleWriter.include(ref); } bundleWriter.writeBundle(NullProgressMonitor.INSTANCE, outputStream); outputStream.close(); repository.close(); return outputPath; } catch (IOException e) { throw new GitException(e); } finally { if (outputStream != null) { try { outputStream.close(); } catch (IOException ioe) { LOGGER.error(ioe); } } } }
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 {/*from www .j a v a2 s. c o 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.TestUtility.java
License:Open Source License
public static LocalRepoBean createTestRepoMultipleBranches() throws Exception { final String projectKey = "test_" + UUID.randomUUID().toString(); final String repoSlug = "testRepo"; final String remoteURL = "ssh://fake_url"; final Path repoPath = Paths.get(PropertyUtil.getGitDir(), "local_scm", projectKey, repoSlug); Files.createDirectories(repoPath); final Repository repo = new FileRepository(repoPath.resolve(".git").toFile()); repo.create();/*w ww . j a v a 2 s . c om*/ final StoredConfig config = repo.getConfig(); config.setString("remote", "origin", "url", remoteURL); config.save(); final LocalRepoBean repoBean = new LocalRepoBean(); repoBean.setProjectKey(projectKey); repoBean.setSlug(repoSlug); repoBean.setLocalBare(false); final Git git = new Git(repo); // add some files to some branches for (int i = 0; i < 3; i++) { final String filename = "newfile" + i + ".txt"; Files.write(repoPath.resolve(filename), Arrays.asList("Hello World"), StandardCharsets.UTF_8, StandardOpenOption.CREATE, StandardOpenOption.APPEND); git.add().addFilepattern(filename).call(); git.commit().setMessage("Added " + filename).call(); } git.checkout().setName("branch1").setCreateBranch(true).call(); for (int i = 0; i < 3; i++) { final String filename = "branch1" + i + ".txt"; Files.write(repoPath.resolve(filename), Arrays.asList("Hello World"), StandardCharsets.UTF_8, StandardOpenOption.CREATE, StandardOpenOption.APPEND); git.add().addFilepattern(filename).call(); git.commit().setMessage("Added " + filename).call(); } git.checkout().setName("master").call(); git.checkout().setName("branch2").setCreateBranch(true).call(); for (int i = 0; i < 3; i++) { final String filename = "branch2" + i + ".txt"; Files.write(repoPath.resolve(filename), Arrays.asList("Hello World"), StandardCharsets.UTF_8, StandardOpenOption.CREATE, StandardOpenOption.APPEND); git.add().addFilepattern(filename).call(); git.commit().setMessage("Added " + filename).call(); } repo.close(); return repoBean; }
From source file:com.surevine.gateway.scm.git.jgit.TestUtility.java
License:Open Source License
public static LocalRepoBean createTestRepo() throws Exception { final String projectKey = "test_" + UUID.randomUUID().toString(); final String repoSlug = "testRepo"; final String remoteURL = "ssh://fake_url"; final Path repoPath = Paths.get(PropertyUtil.getGitDir(), "local_scm", projectKey, repoSlug); Files.createDirectories(repoPath); final Repository repo = new FileRepository(repoPath.resolve(".git").toFile()); repo.create();// w w w . j a va 2s . c o m final StoredConfig config = repo.getConfig(); config.setString("remote", "origin", "url", remoteURL); config.save(); final LocalRepoBean repoBean = new LocalRepoBean(); repoBean.setProjectKey(projectKey); repoBean.setSlug(repoSlug); repoBean.setLocalBare(false); repoBean.setSourcePartner("partner"); final Git git = new Git(repo); for (int i = 0; i < 3; i++) { final String filename = "newfile" + i + ".txt"; Files.write(repoPath.resolve(filename), Arrays.asList("Hello World"), StandardCharsets.UTF_8, StandardOpenOption.CREATE, StandardOpenOption.APPEND); git.add().addFilepattern(filename).call(); git.commit().setMessage("Added " + filename).call(); } git.checkout().setName("master").call(); repo.close(); return repoBean; }
From source file:com.tactfactory.harmony.utils.GitUtils.java
License:Open Source License
/** * Add a submodule to .gitmodules file.// ww w.jav a 2 s. com * @param repositoryPath Absolute path of the repository * @param submodulePath Absolute path of the submodule repository * @param submoduleUrl Url of the submodule * @throws IOException */ public static void addSubmodule(String repositoryPath, String submodulePath, String submoduleUrl) throws IOException { // Get main repository RepositoryBuilder repoBuilder = new RepositoryBuilder(); Repository repository = repoBuilder.setWorkTree(new File(repositoryPath)) .setGitDir(new File(repositoryPath + "/.git")).readEnvironment().findGitDir().build(); // Get submodule relative path String path = TactFileUtils.absoluteToRelativePath(submodulePath, repositoryPath); path = path.substring(0, path.length() - 1); // Update .gitmodules file try { FileBasedConfig modulesConfig = new FileBasedConfig( new File(repository.getWorkTree(), Constants.DOT_GIT_MODULES), repository.getFS()); modulesConfig.load(); modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path, ConfigConstants.CONFIG_KEY_PATH, path); modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path, ConfigConstants.CONFIG_KEY_URL, submoduleUrl); modulesConfig.save(); } catch (ConfigInvalidException e) { ConsoleUtils.displayError(e); } repository.close(); }
From source file:com.tasktop.c2c.server.scm.service.GitServiceBean.java
License:Open Source License
private void visitAllCommitsAfter(Date maxDate, CommitVisitor visitor) { Set<ObjectId> visited = new java.util.HashSet<ObjectId>(); for (Repository repo : repositoryProvider.getAllRepositories()) { visitAllCommitsAfter(repo, maxDate, visitor, visited); repo.close(); }/*from w w w. j a v a2 s . c om*/ }
From source file:com.tasktop.c2c.server.scm.service.GitServiceBean.java
License:Open Source License
@Secured({ Role.Observer, Role.User }) @Override/*from w w w.ja va 2 s. c o m*/ public List<Commit> getLog(Region region) { List<Commit> result = new ArrayList<Commit>(); Set<ObjectId> visited = new HashSet<ObjectId>(); for (Repository repo : repositoryProvider.getAllRepositories()) { result.addAll(getLog(repo, region, visited)); repo.close(); } Collections.sort(result, new Comparator<Commit>() { @Override public int compare(Commit o1, Commit o2) { return o2.getDate().compareTo(o1.getDate()); } }); QueryUtil.applyRegionToList(result, region); return result; }