0
    private static boolean revertRepository(String commit, int parent)
            throws RevisionSyntaxException, AmbiguousObjectException, IncorrectObjectTypeException, IOException {
        try {
            RevWalk walk = new RevWalk(repository, 0);

            RevCommit revcom = walk.parseCommit(repository.resolve(commit));
            System.out.println("Parent Count : " + revcom.getParentCount());
            RevCommit parentCom = revcom.getParent(0);
            git.revert().include(parentCom).call();
            return true;
        } catch (Exception e) {
            System.out.println("Error in reverting the project! ):");
            e.printStackTrace();
            return false;
        }
    }

I wrote a function to revert a Merge commit of a repository, I even used "getParent(0)" to point to the first parent but still it throws an exception.

org.eclipse.jgit.api.errors.MultipleParentsNotAllowedException: Cannot revert commit '46d3a4007fe1418d53baabc16dec39275079684b' because it has 2 parents, only commits with exactly one parent are supported
    at org.eclipse.jgit.api.RevertCommand.call(RevertCommand.java:154)
    at Main.revertRepository(Main.java:64)
    at Main.main(Main.java:137)

I'm not sure what I have done wrong here. Thanks in advance (:

Josh Demail
  • 101
  • 1
  • 6
  • You haven't done anything wrong. JGit does not support reverting commits with multiple parents. Is the merge already pushed to a remove? If not, you could maybe reset to the previous commit. – Rüdiger Herrmann Mar 04 '20 at 07:13
  • Actually, a month back I used this method to revert a merge commit after spending hours on many forums and reviewing code examples, I tried the same now, but it's throwing the same exception, I'm a bit confused here what might have gone wrong – Josh Demail Mar 04 '20 at 07:16
  • It is just a limitation of JGit's `RevertCommand` which supports reverting commits with a single parent but does not support reverting commits with multiple parents. See here: https://github.com/eclipse/jgit/blob/master/org.eclipse.jgit/src/org/eclipse/jgit/api/RevertCommand.java#L120 – Rüdiger Herrmann Mar 04 '20 at 07:37
  • If you know which parent is on the mainline branch and you don't mind copying JGit's `RevertCommand` you can adapt the code with very few changes. – Rüdiger Herrmann Mar 04 '20 at 07:43

0 Answers0