Search in sources :

Example 1 with RepositoryBuilder

use of org.eclipse.jgit.lib.RepositoryBuilder in project sonarqube by SonarSource.

the class Jenkins method getJenkinsGitPrSha1.

private String getJenkinsGitPrSha1() {
    String gitBranch = system.envVariable("GIT_BRANCH");
    if (StringUtils.isBlank(gitBranch)) {
        return null;
    }
    Path baseDir = inputProject.getBaseDir();
    RepositoryBuilder builder = new RepositoryBuilder().findGitDir(baseDir.toFile()).setMustExist(true);
    if (builder.getGitDir() == null) {
        return null;
    }
    String refName = "refs/remotes/origin/" + gitBranch;
    try (Repository repo = builder.build()) {
        Ref ref = repo.exactRef(refName);
        if (ref != null) {
            return ref.getObjectId().getName();
        }
    } catch (Exception e) {
        LOG.debug("Couldn't find git sha1 in '{}': {}", refName, e.getMessage());
    }
    return null;
}
Also used : Path(java.nio.file.Path) Repository(org.eclipse.jgit.lib.Repository) Ref(org.eclipse.jgit.lib.Ref) RepositoryBuilder(org.eclipse.jgit.lib.RepositoryBuilder)

Example 2 with RepositoryBuilder

use of org.eclipse.jgit.lib.RepositoryBuilder in project sonarqube by SonarSource.

the class GitScmProviderTest method branchChangedLines_given2NestedSubmodulesWithChangesInTheBottomSubmodule_detectChanges.

@Test
public void branchChangedLines_given2NestedSubmodulesWithChangesInTheBottomSubmodule_detectChanges() throws IOException, GitAPIException {
    Git gitForRepo2, gitForRepo3;
    Path worktreeForRepo2, worktreeForRepo3;
    worktreeForRepo2 = temp.newFolder().toPath();
    gitForRepo2 = createRepository(worktreeForRepo2);
    worktreeForRepo3 = temp.newFolder().toPath();
    gitForRepo3 = createRepository(worktreeForRepo3);
    createAndCommitFile("sub2.js", gitForRepo3, worktreeForRepo3);
    addSubmodule(gitForRepo2, "sub2", worktreeForRepo3.toUri().toString());
    addSubmodule(git, "sub1", worktreeForRepo2.toUri().toString());
    File mainFolderWithAllSubmodules = temp.newFolder();
    Git.cloneRepository().setURI(worktree.toUri().toString()).setDirectory(mainFolderWithAllSubmodules).setCloneSubmodules(true).call();
    Path submodule2Path = mainFolderWithAllSubmodules.toPath().resolve("sub1/sub2");
    Repository submodule2 = new RepositoryBuilder().findGitDir(submodule2Path.toFile()).build();
    Git gitForSubmodule2 = new Git(submodule2);
    gitForSubmodule2.branchCreate().setName("develop").call();
    gitForSubmodule2.checkout().setName("develop").call();
    Path submodule2File = mainFolderWithAllSubmodules.toPath().resolve("sub1/sub2/sub2.js");
    Files.write(submodule2File, randomizedContent("sub2.js", 3).getBytes(), StandardOpenOption.APPEND);
    gitForSubmodule2.add().addFilepattern("sub2.js").call();
    gitForSubmodule2.commit().setAuthor("joe", "joe@example.com").setMessage("important change").call();
    Map<Path, Set<Integer>> changedLines = newScmProvider().branchChangedLines("master", submodule2Path, Set.of(submodule2File));
    assertThat(changedLines).hasSize(1);
    assertThat(changedLines.entrySet().iterator().next().getValue()).containsOnly(4, 5, 6);
}
Also used : Path(java.nio.file.Path) Repository(org.eclipse.jgit.lib.Repository) Git(org.eclipse.jgit.api.Git) Set(java.util.Set) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) Collections.emptySet(java.util.Collections.emptySet) FileRepositoryBuilder(org.eclipse.jgit.storage.file.FileRepositoryBuilder) RepositoryBuilder(org.eclipse.jgit.lib.RepositoryBuilder) File(java.io.File) Test(org.junit.Test)

Example 3 with RepositoryBuilder

use of org.eclipse.jgit.lib.RepositoryBuilder in project egit by eclipse.

the class LocalRepositoryTestCase method createLocalTestRepository.

protected Repository createLocalTestRepository(String repoName) throws IOException {
    File gitDir = new File(new File(testDirectory, repoName), Constants.DOT_GIT);
    Repository myRepository = new RepositoryBuilder().setGitDir(gitDir).build();
    myRepository.create();
    return myRepository;
}
Also used : Repository(org.eclipse.jgit.lib.Repository) FileRepositoryBuilder(org.eclipse.jgit.storage.file.FileRepositoryBuilder) RepositoryBuilder(org.eclipse.jgit.lib.RepositoryBuilder) IFile(org.eclipse.core.resources.IFile) File(java.io.File)

Example 4 with RepositoryBuilder

use of org.eclipse.jgit.lib.RepositoryBuilder in project egit by eclipse.

the class SampleTestRepository method createRepository.

private TestRepository<Repository> createRepository() throws Exception {
    String gitdirName = "test" + System.currentTimeMillis() + Constants.DOT_GIT;
    File gitdir = new File(trash, gitdirName).getCanonicalFile();
    Repository db = new RepositoryBuilder().setGitDir(gitdir).build();
    assertFalse(gitdir.exists());
    db.create(true);
    return new TestRepository<>(db);
}
Also used : TestRepository(org.eclipse.jgit.junit.TestRepository) Repository(org.eclipse.jgit.lib.Repository) TestRepository(org.eclipse.jgit.junit.TestRepository) RepositoryBuilder(org.eclipse.jgit.lib.RepositoryBuilder) File(java.io.File)

Example 5 with RepositoryBuilder

use of org.eclipse.jgit.lib.RepositoryBuilder in project egit by eclipse.

the class ExistingOrNewPage method fillTreeItemWithGitDirectory.

private void fillTreeItemWithGitDirectory(RepositoryMapping m, TreeItem treeItem, IPath gitDir, boolean isAlternative) {
    if (m.getGitDir() == null)
        treeItem.setText(2, UIText.ExistingOrNewPage_SymbolicValueEmptyMapping);
    else {
        IPath relativePath = new Path(m.getGitDir());
        if (isAlternative) {
            IPath withoutLastSegment = relativePath.removeLastSegments(1);
            IPath path;
            if (withoutLastSegment.isEmpty())
                // $NON-NLS-1$
                path = Path.fromPortableString(".");
            else
                path = withoutLastSegment;
            treeItem.setText(0, path.toString());
        }
        treeItem.setText(2, relativePath.toOSString());
        try {
            IProject project = m.getContainer().getProject();
            Repository repo = new RepositoryBuilder().setGitDir(gitDir.toFile()).build();
            File workTree = repo.getWorkTree();
            IPath workTreePath = Path.fromOSString(workTree.getAbsolutePath());
            if (workTreePath.isPrefixOf(project.getLocation())) {
                IPath makeRelativeTo = project.getLocation().makeRelativeTo(workTreePath);
                String repoRelativePath = makeRelativeTo.append("/.project").toPortableString();
                ObjectId headCommitId = repo.resolve(Constants.HEAD);
                if (headCommitId != null) {
                    // Not an empty repo
                    try (RevWalk revWalk = new RevWalk(repo)) {
                        RevCommit headCommit = revWalk.parseCommit(headCommitId);
                        RevTree headTree = headCommit.getTree();
                        TreeWalk projectInRepo = TreeWalk.forPath(repo, repoRelativePath, headTree);
                        if (projectInRepo != null) {
                            // the .project file is tracked by this repo
                            treeItem.setChecked(true);
                        }
                    }
                }
            }
            repo.close();
        } catch (IOException e1) {
            Activator.logError(UIText.ExistingOrNewPage_FailedToDetectRepositoryMessage, e1);
        }
    }
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) IPath(org.eclipse.core.runtime.IPath) FileRepositoryBuilder(org.eclipse.jgit.storage.file.FileRepositoryBuilder) RepositoryBuilder(org.eclipse.jgit.lib.RepositoryBuilder) ObjectId(org.eclipse.jgit.lib.ObjectId) IOException(java.io.IOException) RevWalk(org.eclipse.jgit.revwalk.RevWalk) IProject(org.eclipse.core.resources.IProject) Repository(org.eclipse.jgit.lib.Repository) File(java.io.File) TreeWalk(org.eclipse.jgit.treewalk.TreeWalk) RevTree(org.eclipse.jgit.revwalk.RevTree) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Aggregations

Repository (org.eclipse.jgit.lib.Repository)5 RepositoryBuilder (org.eclipse.jgit.lib.RepositoryBuilder)5 File (java.io.File)4 FileRepositoryBuilder (org.eclipse.jgit.storage.file.FileRepositoryBuilder)3 Path (java.nio.file.Path)2 IOException (java.io.IOException)1 Collections.emptySet (java.util.Collections.emptySet)1 HashSet (java.util.HashSet)1 LinkedHashSet (java.util.LinkedHashSet)1 Set (java.util.Set)1 IFile (org.eclipse.core.resources.IFile)1 IProject (org.eclipse.core.resources.IProject)1 IPath (org.eclipse.core.runtime.IPath)1 Path (org.eclipse.core.runtime.Path)1 Git (org.eclipse.jgit.api.Git)1 TestRepository (org.eclipse.jgit.junit.TestRepository)1 ObjectId (org.eclipse.jgit.lib.ObjectId)1 Ref (org.eclipse.jgit.lib.Ref)1 RevCommit (org.eclipse.jgit.revwalk.RevCommit)1 RevTree (org.eclipse.jgit.revwalk.RevTree)1