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;
}
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);
}
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;
}
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);
}
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);
}
}
}
Aggregations