use of org.eclipse.jgit.lib.ObjectId in project camel by apache.
the class GitProducer method doCherryPick.
protected void doCherryPick(Exchange exchange, String operation) throws Exception {
CherryPickResult result = null;
String commitId = null;
try {
if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(GitConstants.GIT_COMMIT_ID))) {
commitId = exchange.getIn().getHeader(GitConstants.GIT_COMMIT_ID, String.class);
} else {
throw new IllegalArgumentException("Commit id must be specified to execute " + operation);
}
RevWalk walk = new RevWalk(repo);
ObjectId id = repo.resolve(commitId);
RevCommit commit = walk.parseCommit(id);
walk.dispose();
if (ObjectHelper.isNotEmpty(endpoint.getBranchName())) {
git.checkout().setCreateBranch(false).setName(endpoint.getBranchName()).call();
}
result = git.cherryPick().include(commit).call();
} catch (Exception e) {
LOG.error("There was an error in Git " + operation + " operation");
throw e;
}
updateExchange(exchange, result);
}
use of org.eclipse.jgit.lib.ObjectId in project checkstyle by checkstyle.
the class CommitValidationTest method resolveRevCommitsPair.
private static RevCommitsPair resolveRevCommitsPair(Repository repo) {
RevCommitsPair revCommitIteratorPair;
try (RevWalk revWalk = new RevWalk(repo)) {
final Iterator<RevCommit> first;
final Iterator<RevCommit> second;
final ObjectId headId = repo.resolve(Constants.HEAD);
final RevCommit headCommit = revWalk.parseCommit(headId);
if (isMergeCommit(headCommit)) {
final RevCommit firstParent = headCommit.getParent(0);
final RevCommit secondParent = headCommit.getParent(1);
try (Git git = new Git(repo)) {
first = git.log().add(firstParent).call().iterator();
second = git.log().add(secondParent).call().iterator();
}
} else {
try (Git git = new Git(repo)) {
first = git.log().call().iterator();
}
second = Collections.emptyIterator();
}
revCommitIteratorPair = new RevCommitsPair(new OmitMergeCommitsIterator(first), new OmitMergeCommitsIterator(second));
} catch (GitAPIException | IOException ex) {
revCommitIteratorPair = new RevCommitsPair();
}
return revCommitIteratorPair;
}
use of org.eclipse.jgit.lib.ObjectId in project che by eclipse.
the class JGitConnection method merge.
@Override
public MergeResult merge(String commit) throws GitException {
org.eclipse.jgit.api.MergeResult jGitMergeResult;
MergeResult.MergeStatus status;
try {
Ref ref = repository.findRef(commit);
if (ref == null) {
throw new GitException("Invalid reference to commit for merge " + commit);
}
// Shorten local branch names by removing '/refs/heads/' from the beginning
String name = ref.getName();
if (name.startsWith(Constants.R_HEADS)) {
name = name.substring(Constants.R_HEADS.length());
}
jGitMergeResult = getGit().merge().include(name, ref.getObjectId()).call();
} catch (CheckoutConflictException exception) {
jGitMergeResult = new org.eclipse.jgit.api.MergeResult(exception.getConflictingPaths());
} catch (IOException | GitAPIException exception) {
throw new GitException(exception.getMessage(), exception);
}
switch(jGitMergeResult.getMergeStatus()) {
case ALREADY_UP_TO_DATE:
status = MergeResult.MergeStatus.ALREADY_UP_TO_DATE;
break;
case CONFLICTING:
status = MergeResult.MergeStatus.CONFLICTING;
break;
case FAILED:
status = MergeResult.MergeStatus.FAILED;
break;
case FAST_FORWARD:
status = MergeResult.MergeStatus.FAST_FORWARD;
break;
case MERGED:
status = MergeResult.MergeStatus.MERGED;
break;
case NOT_SUPPORTED:
status = MergeResult.MergeStatus.NOT_SUPPORTED;
break;
case CHECKOUT_CONFLICT:
status = MergeResult.MergeStatus.CONFLICTING;
break;
default:
throw new IllegalStateException("Unknown merge status " + jGitMergeResult.getMergeStatus());
}
ObjectId[] jGitMergedCommits = jGitMergeResult.getMergedCommits();
List<String> mergedCommits = new ArrayList<>();
if (jGitMergedCommits != null) {
for (ObjectId jGitMergedCommit : jGitMergedCommits) {
mergedCommits.add(jGitMergedCommit.getName());
}
}
List<String> conflicts;
if (org.eclipse.jgit.api.MergeResult.MergeStatus.CHECKOUT_CONFLICT.equals(jGitMergeResult.getMergeStatus())) {
conflicts = jGitMergeResult.getCheckoutConflicts();
} else {
Map<String, int[][]> jGitConflicts = jGitMergeResult.getConflicts();
conflicts = jGitConflicts != null ? new ArrayList<>(jGitConflicts.keySet()) : Collections.emptyList();
}
Map<String, ResolveMerger.MergeFailureReason> jGitFailing = jGitMergeResult.getFailingPaths();
ObjectId newHead = jGitMergeResult.getNewHead();
return newDto(MergeResult.class).withFailed(jGitFailing != null ? new ArrayList<>(jGitFailing.keySet()) : Collections.emptyList()).withNewHead(newHead != null ? newHead.getName() : null).withMergeStatus(status).withConflicts(conflicts).withMergedCommits(mergedCommits);
}
use of org.eclipse.jgit.lib.ObjectId in project che by eclipse.
the class JGitConnection method setRevisionRange.
private void setRevisionRange(LogCommand logCommand, LogParams params) throws IOException {
if (params != null && logCommand != null) {
String revisionRangeSince = params.getRevisionRangeSince();
String revisionRangeUntil = params.getRevisionRangeUntil();
if (revisionRangeSince != null && revisionRangeUntil != null) {
ObjectId since = repository.resolve(revisionRangeSince);
ObjectId until = repository.resolve(revisionRangeUntil);
logCommand.addRange(since, until);
}
}
}
use of org.eclipse.jgit.lib.ObjectId in project che by eclipse.
the class JGitDiffPage method commitToWorkingTree.
/**
* Show changes between specified revision and working tree.
*
* @param commitId
* id of commit
* @param formatter
* diff formatter
* @return list of diff entries
* @throws IOException
* if any i/o errors occurs
*/
private List<DiffEntry> commitToWorkingTree(String commitId, DiffFormatter formatter) throws IOException {
ObjectId commitA = repository.resolve(commitId);
if (commitA == null) {
File heads = new File(repository.getWorkTree().getPath() + "/.git/refs/heads");
if (heads.exists() && heads.list().length == 0) {
return Collections.emptyList();
}
throw new IllegalArgumentException("Invalid commit id " + commitId);
}
RevTree treeA;
try (RevWalk revWalkA = new RevWalk(repository)) {
treeA = revWalkA.parseTree(commitA);
}
List<DiffEntry> diff;
try (ObjectReader reader = repository.newObjectReader()) {
CanonicalTreeParser iterA = new CanonicalTreeParser();
iterA.reset(reader, treeA);
FileTreeIterator iterB = new FileTreeIterator(repository);
// Seems bug in DiffFormatter when work with working. Disable detect
// renames by formatter and do it later.
formatter.setDetectRenames(false);
diff = formatter.scan(iterA, iterB);
if (!params.isNoRenames()) {
// Detect renames.
RenameDetector renameDetector = createRenameDetector();
ContentSource.Pair sourcePairReader = new ContentSource.Pair(ContentSource.create(reader), ContentSource.create(iterB));
renameDetector.addAll(diff);
diff = renameDetector.compute(sourcePairReader, NullProgressMonitor.INSTANCE);
}
}
return diff;
}
Aggregations