StatusCommand.java

  1. /*
  2.  * Copyright (C) 2011, Christian Halstrick <christian.halstrick@sap.com> and others
  3.  *
  4.  * This program and the accompanying materials are made available under the
  5.  * terms of the Eclipse Distribution License v. 1.0 which is available at
  6.  * https://www.eclipse.org/org/documents/edl-v10.php.
  7.  *
  8.  * SPDX-License-Identifier: BSD-3-Clause
  9.  */
  10. package org.eclipse.jgit.api;

  11. import java.io.IOException;
  12. import java.util.LinkedList;
  13. import java.util.List;

  14. import org.eclipse.jgit.api.errors.GitAPIException;
  15. import org.eclipse.jgit.api.errors.JGitInternalException;
  16. import org.eclipse.jgit.errors.NoWorkTreeException;
  17. import org.eclipse.jgit.lib.Constants;
  18. import org.eclipse.jgit.lib.IndexDiff;
  19. import org.eclipse.jgit.lib.ProgressMonitor;
  20. import org.eclipse.jgit.lib.Repository;
  21. import org.eclipse.jgit.submodule.SubmoduleWalk.IgnoreSubmoduleMode;
  22. import org.eclipse.jgit.treewalk.FileTreeIterator;
  23. import org.eclipse.jgit.treewalk.WorkingTreeIterator;
  24. import org.eclipse.jgit.treewalk.filter.PathFilterGroup;

  25. /**
  26.  * A class used to execute a {@code Status} command. It has setters for all
  27.  * supported options and arguments of this command and a {@link #call()} method
  28.  * to finally execute the command. Each instance of this class should only be
  29.  * used for one invocation of the command (means: one call to {@link #call()})
  30.  *
  31.  * @see <a href=
  32.  *      "http://www.kernel.org/pub/software/scm/git/docs/git-status.html" >Git
  33.  *      documentation about Status</a>
  34.  */
  35. public class StatusCommand extends GitCommand<Status> {
  36.     private WorkingTreeIterator workingTreeIt;
  37.     private List<String> paths = null;
  38.     private ProgressMonitor progressMonitor = null;

  39.     private IgnoreSubmoduleMode ignoreSubmoduleMode = null;

  40.     /**
  41.      * Constructor for StatusCommand.
  42.      *
  43.      * @param repo
  44.      *            a {@link org.eclipse.jgit.lib.Repository} object.
  45.      */
  46.     protected StatusCommand(Repository repo) {
  47.         super(repo);
  48.     }

  49.     /**
  50.      * Whether to ignore submodules
  51.      *
  52.      * @param mode
  53.      *            the
  54.      *            {@link org.eclipse.jgit.submodule.SubmoduleWalk.IgnoreSubmoduleMode}
  55.      * @return {@code this}
  56.      * @since 3.6
  57.      */
  58.     public StatusCommand setIgnoreSubmodules(IgnoreSubmoduleMode mode) {
  59.         ignoreSubmoduleMode = mode;
  60.         return this;
  61.     }

  62.     /**
  63.      * Show only the status of files which match the given paths. The path must
  64.      * either name a file or a directory exactly. All paths are always relative
  65.      * to the repository root. If a directory is specified all files recursively
  66.      * underneath that directory are matched. If this method is called multiple
  67.      * times then the status of those files is reported which match at least one
  68.      * of the given paths. Note that regex expressions or wildcards are not
  69.      * supported.
  70.      *
  71.      * @param path
  72.      *            repository-relative path of file/directory to show status for
  73.      *            (with <code>/</code> as separator)
  74.      * @return {@code this}
  75.      * @since 3.1
  76.      */
  77.     public StatusCommand addPath(String path) {
  78.         if (paths == null)
  79.             paths = new LinkedList<>();
  80.         paths.add(path);
  81.         return this;
  82.     }

  83.     /**
  84.      * Returns the paths filtering this status.
  85.      *
  86.      * @return the paths for which the status is shown or <code>null</code> if
  87.      *         the complete status for the whole repo is shown.
  88.      * @since 3.1
  89.      */
  90.     public List<String> getPaths() {
  91.         return paths;
  92.     }

  93.     /**
  94.      * {@inheritDoc}
  95.      * <p>
  96.      * Executes the {@code Status} command with all the options and parameters
  97.      * collected by the setter methods of this class. Each instance of this
  98.      * class should only be used for one invocation of the command. Don't call
  99.      * this method twice on an instance.
  100.      */
  101.     @Override
  102.     public Status call() throws GitAPIException, NoWorkTreeException {
  103.         if (workingTreeIt == null)
  104.             workingTreeIt = new FileTreeIterator(repo);

  105.         try {
  106.             IndexDiff diff = new IndexDiff(repo, Constants.HEAD, workingTreeIt);
  107.             if (ignoreSubmoduleMode != null)
  108.                 diff.setIgnoreSubmoduleMode(ignoreSubmoduleMode);
  109.             if (paths != null)
  110.                 diff.setFilter(PathFilterGroup.createFromStrings(paths));
  111.             if (progressMonitor == null)
  112.                 diff.diff();
  113.             else
  114.                 diff.diff(progressMonitor, ProgressMonitor.UNKNOWN,
  115.                         ProgressMonitor.UNKNOWN, ""); //$NON-NLS-1$
  116.             return new Status(diff);
  117.         } catch (IOException e) {
  118.             throw new JGitInternalException(e.getMessage(), e);
  119.         }
  120.     }

  121.     /**
  122.      * To set the {@link org.eclipse.jgit.treewalk.WorkingTreeIterator} which
  123.      * should be used. If this method is not called a standard
  124.      * {@link org.eclipse.jgit.treewalk.FileTreeIterator} is used.
  125.      *
  126.      * @param workingTreeIt
  127.      *            a working tree iterator
  128.      * @return {@code this}
  129.      */
  130.     public StatusCommand setWorkingTreeIt(WorkingTreeIterator workingTreeIt) {
  131.         this.workingTreeIt = workingTreeIt;
  132.         return this;
  133.     }

  134.     /**
  135.      * To set the {@link org.eclipse.jgit.lib.ProgressMonitor} which contains
  136.      * callback methods to inform you about the progress of this command.
  137.      *
  138.      * @param progressMonitor
  139.      *            a {@link org.eclipse.jgit.lib.ProgressMonitor} object.
  140.      * @return {@code this}
  141.      * @since 3.1
  142.      */
  143.     public StatusCommand setProgressMonitor(ProgressMonitor progressMonitor) {
  144.         this.progressMonitor = progressMonitor;
  145.         return this;
  146.     }
  147. }