@Override public RevertResult revert(String commit) throws GitException { RevCommit revCommit; RevertCommand revertCommand = getGit().revert(); try { revertCommand.include(this.repository.resolve(commit)); revCommit = revertCommand.call(); } catch (IOException | GitAPIException exception) { throw new GitException(exception.getMessage(), exception); } return newDto(RevertResult.class) .withRevertedCommits(getRevertedCommits(revertCommand)) .withConflicts(getRevertConflicts(revertCommand)) .withNewHead(revCommit != null ? revCommit.getId().getName() : null); }
private Map<String, RevertResult.RevertStatus> getRevertConflicts(RevertCommand revertCommand) { Map<String, RevertResult.RevertStatus> conflicts = new HashMap<>(); if (revertCommand.getFailingResult() != null) { Map<String, MergeFailureReason> failingPaths = revertCommand.getFailingResult().getFailingPaths(); if (failingPaths != null && !failingPaths.isEmpty()) { failingPaths .entrySet() .forEach( failure -> conflicts.put( failure.getKey(), getRevertStatusFromMergeFailureReason(failure.getValue()))); } } List<String> unmergedPaths = revertCommand.getUnmergedPaths(); if (unmergedPaths != null && !unmergedPaths.isEmpty()) { unmergedPaths .stream() .filter(unmergedPath -> !conflicts.containsKey(unmergedPath)) .forEach(unmergedPath -> conflicts.put(unmergedPath, RevertResult.RevertStatus.FAILED)); } return conflicts; }
@Override public boolean rollback( String name ) { if ( !isClean() ) { showMessageBox( BaseMessages.getString( PKG, "Dialog.Error" ), "Dirty working-tree" ); return false; } String commit = resolve( Constants.HEAD ).getName(); RevertCommand cmd = git.revert(); for ( int i = 0; i < getRevisions().size(); i++ ) { String commitId = getRevisions().get( i ).getName(); /* * Revert commits from HEAD to the specified commit in reverse order. */ cmd.include( resolve( commitId ) ); if ( commitId.equals( name ) ) { break; } } try { cmd.call(); git.reset().setRef( commit ).call(); return true; } catch ( Exception e ) { showMessageBox( BaseMessages.getString( PKG, "Dialog.Error" ), e.getMessage() ); } return false; }
/** * Creates a new commit that reverts the changes done in the previous (erroneous) commit(s) * @param git the git repository * @param commitsByRef one or more commits (can be null if at least one other "commitsBy*" is not null) * @param commitsById one or more commits (can be null if at least one other "commitsBy*" is not null) * @param commitsByNameAndId one or more commits (can be null if at least one other "commitsBy*" is not null) * @param strategy the merge strategy to use * @param ourCommitName named used for the "OURS" place when there are conflicts * (distinguishes our commits from others' commits) * @return the commit made that reverts previous commits' work * @throws GitAPIException */ public static RevCommit revertCommits(Git git, List<Ref> commitsByRef, List<AnyObjectId> commitsById, List<NamedCommit> commitsByNameAndId, MergeStrategy strategy, String ourCommitName) throws GitAPIException { RevertCommand revert = git.revert(); commitsByRef.forEach(revert::include); commitsById.forEach(revert::include); commitsByNameAndId.forEach(nc -> revert.include(nc.getName(), nc.getObjectId())); return revert .setStrategy(strategy) .setOurCommitName(ourCommitName) .call(); }
private List<String> getRevertedCommits(RevertCommand revertCommand) { List<Ref> jGitRevertedCommits = revertCommand.getRevertedRefs(); List<String> revertedCommits = new ArrayList<String>(); if (jGitRevertedCommits != null) { jGitRevertedCommits.forEach(ref -> revertedCommits.add(ref.getObjectId().name())); } return revertedCommits; }