RevCommitHandler.java

  1. /*
  2.  * Copyright (C) 2009, Google Inc.
  3.  * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org> and others
  4.  *
  5.  * This program and the accompanying materials are made available under the
  6.  * terms of the Eclipse Distribution License v. 1.0 which is available at
  7.  * https://www.eclipse.org/org/documents/edl-v10.php.
  8.  *
  9.  * SPDX-License-Identifier: BSD-3-Clause
  10.  */

  11. package org.eclipse.jgit.pgm.opt;

  12. import java.io.IOException;

  13. import org.eclipse.jgit.errors.IncorrectObjectTypeException;
  14. import org.eclipse.jgit.errors.MissingObjectException;
  15. import org.eclipse.jgit.lib.ObjectId;
  16. import org.eclipse.jgit.pgm.internal.CLIText;
  17. import org.eclipse.jgit.revwalk.RevCommit;
  18. import org.eclipse.jgit.revwalk.RevFlag;
  19. import org.kohsuke.args4j.CmdLineException;
  20. import org.kohsuke.args4j.CmdLineParser;
  21. import org.kohsuke.args4j.OptionDef;
  22. import org.kohsuke.args4j.spi.OptionHandler;
  23. import org.kohsuke.args4j.spi.Parameters;
  24. import org.kohsuke.args4j.spi.Setter;

  25. /**
  26.  * Custom argument handler {@link org.eclipse.jgit.revwalk.RevCommit} from
  27.  * string values.
  28.  * <p>
  29.  * Assumes the parser has been initialized with a Repository.
  30.  */
  31. public class RevCommitHandler extends OptionHandler<RevCommit> {
  32.     private final org.eclipse.jgit.pgm.opt.CmdLineParser clp;

  33.     /**
  34.      * Create a new handler for the command name.
  35.      * <p>
  36.      * This constructor is used only by args4j.
  37.      *
  38.      * @param parser
  39.      *            a {@link org.kohsuke.args4j.CmdLineParser} object.
  40.      * @param option
  41.      *            a {@link org.kohsuke.args4j.OptionDef} object.
  42.      * @param setter
  43.      *            a {@link org.kohsuke.args4j.spi.Setter} object.
  44.      */
  45.     public RevCommitHandler(final CmdLineParser parser, final OptionDef option,
  46.             final Setter<? super RevCommit> setter) {
  47.         super(parser, option, setter);
  48.         clp = (org.eclipse.jgit.pgm.opt.CmdLineParser) parser;
  49.     }

  50.     /** {@inheritDoc} */
  51.     @Override
  52.     public int parseArguments(Parameters params) throws CmdLineException {
  53.         String name = params.getParameter(0);

  54.         boolean interesting = true;
  55.         if (name.startsWith("^")) { //$NON-NLS-1$
  56.             name = name.substring(1);
  57.             interesting = false;
  58.         }

  59.         final int dot2 = name.indexOf(".."); //$NON-NLS-1$
  60.         if (dot2 != -1) {
  61.             if (!option.isMultiValued())
  62.                 throw new CmdLineException(clp,
  63.                         CLIText.format(CLIText.get().onlyOneMetaVarExpectedIn),
  64.                         option.metaVar(), name);

  65.             final String left = name.substring(0, dot2);
  66.             final String right = name.substring(dot2 + 2);
  67.             addOne(left, false);
  68.             addOne(right, true);
  69.             return 1;
  70.         }

  71.         addOne(name, interesting);
  72.         return 1;
  73.     }

  74.     private void addOne(String name, boolean interesting)
  75.             throws CmdLineException {
  76.         final ObjectId id;
  77.         try {
  78.             id = clp.getRepository().resolve(name);
  79.         } catch (IOException e) {
  80.             throw new CmdLineException(clp, CLIText.format(e.getMessage()));
  81.         }
  82.         if (id == null)
  83.             throw new CmdLineException(clp,
  84.                     CLIText.format(CLIText.get().notACommit), name);

  85.         final RevCommit c;
  86.         try {
  87.             c = clp.getRevWalk().parseCommit(id);
  88.         } catch (MissingObjectException | IncorrectObjectTypeException e) {
  89.             CmdLineException cle = new CmdLineException(clp,
  90.                     CLIText.format(CLIText.get().notACommit), name);
  91.             cle.initCause(e);
  92.             throw cle;
  93.         } catch (IOException e) {
  94.             throw new CmdLineException(clp,
  95.                     CLIText.format(CLIText.get().cannotReadBecause), name,
  96.                     e.getMessage());
  97.         }

  98.         if (interesting)
  99.             c.remove(RevFlag.UNINTERESTING);
  100.         else
  101.             c.add(RevFlag.UNINTERESTING);

  102.         setter.addValue(c);
  103.     }

  104.     /** {@inheritDoc} */
  105.     @Override
  106.     public String getDefaultMetaVariable() {
  107.         return CLIText.get().metaVar_commitish;
  108.     }
  109. }