List of usage examples for org.eclipse.jgit.lib AbbreviatedObjectId prefixCompare
public final int prefixCompare(AnyObjectId other)
From source file:com.microsoft.gittf.core.util.CommitWalker.java
License:Open Source License
/** * Get the list of commit deltas that represents the commits between a * source commit and a target commit. The method skips walking parents that * are ignored.//from www . j a v a2 s. com * * @param repository * the git repository * @param sourceCommitID * the source commit id * @param targetCommitID * the target commit * @param ignoreCommitIDs * the list of commits to ignore * @return * @throws Exception * throws an exception if the path between to commits is ambigous */ public static List<CommitDelta> getCommitList(final Repository repository, final ObjectId sourceCommitID, final ObjectId targetCommitID, final AbbreviatedObjectId[] ignoreCommitIDs) throws Exception { Check.notNull(repository, "repository"); //$NON-NLS-1$ Check.notNull(targetCommitID, "targetCommitID"); //$NON-NLS-1$ List<CommitDelta> commitList = new ArrayList<CommitDelta>(); RevWalk walker = new RevWalk(repository); try { final RevCommit headCommit = walker.lookupCommit(targetCommitID); walker.parseHeaders(headCommit); RevCommit currentCommit = headCommit; /* * Walk backwards from the destination commit searching for the * starting commit. */ while (currentCommit != null && !currentCommit.getId().equals(sourceCommitID)) { final RevCommit[] parents = currentCommit.getParents(); RevCommit fromCommit = null; final RevCommit toCommit = currentCommit; /* The repository's initial commit. */ if (parents == null || parents.length == 0) { /* * Sanity check: make sure we are in an unbridged * repository. */ if (sourceCommitID != null) { throw new Exception(Messages.formatString("CheckinHeadCommitTask.LatestNotInTreeFormat", //$NON-NLS-1$ ObjectIdUtil.abbreviate(repository, sourceCommitID))); } fromCommit = null; } else if (parents.length == 1) { for (AbbreviatedObjectId ignore : ignoreCommitIDs) { if (ignore.prefixCompare(parents[0].getId()) == 0) { throw new Exception(Messages.formatString( "CheckinHeadCommitTask.CommitHasOneParentThatIsSquashedFormat", //$NON-NLS-1$ ObjectIdUtil.abbreviate(repository, currentCommit), ObjectIdUtil.abbreviate(repository, parents[0].getId()))); } } fromCommit = parents[0]; walker.parseHeaders(fromCommit); } else { RevCommit possibleFrom = null; /* * See if one of the parents is our destination - if so, * we'll simply use it. (This commit is the result of a * merge with the latest changeset on the TFS server. We * won't preserve history before the merge.) */ for (RevCommit parent : currentCommit.getParents()) { if (parent.getId().equals(sourceCommitID)) { possibleFrom = parent; break; } } if (possibleFrom == null) { /* * See if all the parents but one have been squashed - * if so, we can follow the non-squashed parent for its * history. */ for (RevCommit parent : currentCommit.getParents()) { boolean parentIgnored = false; for (AbbreviatedObjectId ignore : ignoreCommitIDs) { if (ignore.prefixCompare(parent.getId()) == 0) { parentIgnored = true; break; } } /* This parent is squashed, continue */ if (parentIgnored) { continue; } /* * This is a possible (non-squashed) avenue to * follow, mark it as such and investigate other * parents. */ else if (possibleFrom == null) { possibleFrom = parent; } /* * We have two non-squashed parents. We cannot * reconcile history. */ else { throw new Exception( Messages.formatString("CheckinHeadCommitTask.NonLinearHistoryFormat", //$NON-NLS-1$ ObjectIdUtil.abbreviate(repository, currentCommit.getId()))); } } } /* All our parents were squashed! */ if (possibleFrom == null) { throw new Exception(Messages.formatString("CheckinHeadCommitTask.AllParentsSquashedFormat", //$NON-NLS-1$ ObjectIdUtil.abbreviate(repository, currentCommit.getId()))); } /* We only had one non-squashed parent */ else { fromCommit = possibleFrom; walker.parseHeaders(possibleFrom); } } commitList.add(new CommitDelta(fromCommit, toCommit)); currentCommit = fromCommit; } } finally { if (walker != null) { walker.dispose(); } } Collections.reverse(commitList); return commitList; }
From source file:org.eclipse.egit.core.op.SquashCommitsOperation.java
License:Open Source License
@Override public void execute(IProgressMonitor m) throws CoreException { IWorkspaceRunnable action = new IWorkspaceRunnable() { @Override/*from www . j av a 2s .c o m*/ public void run(IProgressMonitor pm) throws CoreException { SubMonitor progress = SubMonitor.convert(pm, 2); progress.subTask(MessageFormat.format(CoreText.SquashCommitsOperation_squashing, Integer.valueOf(commits.size()))); InteractiveHandler handler = new InteractiveHandler() { @Override public void prepareSteps(List<RebaseTodoLine> steps) { RevCommit firstCommit = commits.get(0); for (RebaseTodoLine step : steps) { if (isRelevant(step.getCommit())) { try { if (step.getCommit().prefixCompare(firstCommit) == 0) step.setAction(RebaseTodoLine.Action.PICK); else step.setAction(RebaseTodoLine.Action.SQUASH); } catch (IllegalTodoFileModification e) { // shouldn't happen } } } } private boolean isRelevant(AbbreviatedObjectId id) { for (RevCommit commit : commits) { if (id.prefixCompare(commit) == 0) return true; } return false; } @Override public String modifyCommitMessage(String oldMessage) { return messageHandler.modifyCommitMessage(oldMessage); } }; try (Git git = new Git(repository)) { RebaseCommand command = git.rebase().setUpstream(commits.get(0).getParent(0)) .runInteractively(handler).setOperation(RebaseCommand.Operation.BEGIN); MergeStrategy strategy = Activator.getDefault().getPreferredMergeStrategy(); if (strategy != null) { command.setStrategy(strategy); } command.call(); } catch (GitAPIException e) { throw new TeamException(e.getLocalizedMessage(), e.getCause()); } progress.worked(1); ProjectUtil.refreshValidProjects(ProjectUtil.getValidOpenProjects(repository), progress.newChild(1)); } }; ResourcesPlugin.getWorkspace().run(action, getSchedulingRule(), IWorkspace.AVOID_UPDATE, m); }