Status.java

  1. /*
  2.  * Copyright (C) 2011, 2013 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.util.Collections;
  12. import java.util.HashSet;
  13. import java.util.Map;
  14. import java.util.Set;

  15. import org.eclipse.jgit.lib.IndexDiff;
  16. import org.eclipse.jgit.lib.IndexDiff.StageState;

  17. /**
  18.  * A class telling where the working-tree, the index and the current HEAD differ
  19.  * from each other. Collections are exposed containing the paths of the modified
  20.  * files. E.g. to find out which files are dirty in the working tree (modified
  21.  * but not added) you would inspect the collection returned by
  22.  * {@link #getModified()}.
  23.  * <p>
  24.  * The same path can be returned by multiple getters. E.g. if a modification has
  25.  * been added to the index and afterwards the corresponding working tree file is
  26.  * again modified this path will be returned by {@link #getModified()} and
  27.  * {@link #getChanged()}
  28.  */
  29. public class Status {
  30.     private final IndexDiff diff;

  31.     private final boolean clean;

  32.     private final boolean hasUncommittedChanges;

  33.     /**
  34.      * Constructor for Status.
  35.      *
  36.      * @param diff
  37.      *            the {@link org.eclipse.jgit.lib.IndexDiff} having the status
  38.      */
  39.     public Status(IndexDiff diff) {
  40.         super();
  41.         this.diff = diff;
  42.         hasUncommittedChanges = !diff.getAdded().isEmpty() //
  43.                 || !diff.getChanged().isEmpty() //
  44.                 || !diff.getRemoved().isEmpty() //
  45.                 || !diff.getMissing().isEmpty() //
  46.                 || !diff.getModified().isEmpty() //
  47.                 || !diff.getConflicting().isEmpty();
  48.         clean = !hasUncommittedChanges //
  49.                 && diff.getUntracked().isEmpty();
  50.     }

  51.     /**
  52.      * Whether the status is clean
  53.      *
  54.      * @return {@code true} if no differences exist between the working-tree,
  55.      *         the index, and the current HEAD, {@code false} if differences do
  56.      *         exist
  57.      */
  58.     public boolean isClean() {
  59.         return clean;
  60.     }

  61.     /**
  62.      * Whether there are uncommitted changes
  63.      *
  64.      * @return {@code true} if any tracked file is changed
  65.      * @since 3.2
  66.      */
  67.     public boolean hasUncommittedChanges() {
  68.         return hasUncommittedChanges;
  69.     }

  70.     /**
  71.      * Get files added to the index
  72.      *
  73.      * @return list of files added to the index, not in HEAD (e.g. what you get
  74.      *         if you call {@code git add ...} on a newly created file)
  75.      */
  76.     public Set<String> getAdded() {
  77.         return Collections.unmodifiableSet(diff.getAdded());
  78.     }

  79.     /**
  80.      * Get changed files from HEAD to index
  81.      *
  82.      * @return list of files changed from HEAD to index (e.g. what you get if
  83.      *         you modify an existing file and call 'git add ...' on it)
  84.      */
  85.     public Set<String> getChanged() {
  86.         return Collections.unmodifiableSet(diff.getChanged());
  87.     }

  88.     /**
  89.      * Get removed files
  90.      *
  91.      * @return list of files removed from index, but in HEAD (e.g. what you get
  92.      *         if you call 'git rm ...' on a existing file)
  93.      */
  94.     public Set<String> getRemoved() {
  95.         return Collections.unmodifiableSet(diff.getRemoved());
  96.     }

  97.     /**
  98.      * Get missing files
  99.      *
  100.      * @return list of files in index, but not filesystem (e.g. what you get if
  101.      *         you call 'rm ...' on a existing file)
  102.      */
  103.     public Set<String> getMissing() {
  104.         return Collections.unmodifiableSet(diff.getMissing());
  105.     }

  106.     /**
  107.      * Get modified files relative to the index
  108.      *
  109.      * @return list of files modified on disk relative to the index (e.g. what
  110.      *         you get if you modify an existing file without adding it to the
  111.      *         index)
  112.      */
  113.     public Set<String> getModified() {
  114.         return Collections.unmodifiableSet(diff.getModified());
  115.     }

  116.     /**
  117.      * Get untracked files
  118.      *
  119.      * @return list of files that are not ignored, and not in the index. (e.g.
  120.      *         what you get if you create a new file without adding it to the
  121.      *         index)
  122.      */
  123.     public Set<String> getUntracked() {
  124.         return Collections.unmodifiableSet(diff.getUntracked());
  125.     }

  126.     /**
  127.      * Get untracked folders
  128.      *
  129.      * @return set of directories that are not ignored, and not in the index.
  130.      */
  131.     public Set<String> getUntrackedFolders() {
  132.         return Collections.unmodifiableSet(diff.getUntrackedFolders());
  133.     }

  134.     /**
  135.      * Get conflicting files
  136.      *
  137.      * @return list of files that are in conflict. (e.g what you get if you
  138.      *         modify file that was modified by someone else in the meantime)
  139.      */
  140.     public Set<String> getConflicting() {
  141.         return Collections.unmodifiableSet(diff.getConflicting());
  142.     }

  143.     /**
  144.      * Get StageState of conflicting files
  145.      *
  146.      * @return a map from conflicting path to its
  147.      *         {@link org.eclipse.jgit.lib.IndexDiff.StageState}.
  148.      * @since 3.0
  149.      */
  150.     public Map<String, StageState> getConflictingStageState() {
  151.         return Collections.unmodifiableMap(diff.getConflictingStageStates());
  152.     }

  153.     /**
  154.      * Get ignored files which are not in the index
  155.      *
  156.      * @return set of files and folders that are ignored and not in the index.
  157.      */
  158.     public Set<String> getIgnoredNotInIndex() {
  159.         return Collections.unmodifiableSet(diff.getIgnoredNotInIndex());
  160.     }

  161.     /**
  162.      * Get uncommitted changes, i.e. all files changed in the index or working
  163.      * tree
  164.      *
  165.      * @return set of files and folders that are known to the repo and changed
  166.      *         either in the index or in the working tree.
  167.      * @since 3.2
  168.      */
  169.     public Set<String> getUncommittedChanges() {
  170.         Set<String> uncommittedChanges = new HashSet<>();
  171.         uncommittedChanges.addAll(diff.getAdded());
  172.         uncommittedChanges.addAll(diff.getChanged());
  173.         uncommittedChanges.addAll(diff.getRemoved());
  174.         uncommittedChanges.addAll(diff.getMissing());
  175.         uncommittedChanges.addAll(diff.getModified());
  176.         uncommittedChanges.addAll(diff.getConflicting());
  177.         return uncommittedChanges;
  178.     }
  179. }