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
12 import java.io.IOException;
13 import java.util.LinkedList;
14 import java.util.List;
15
16 import org.eclipse.jgit.api.errors.GitAPIException;
17 import org.eclipse.jgit.api.errors.JGitInternalException;
18 import org.eclipse.jgit.errors.NoWorkTreeException;
19 import org.eclipse.jgit.lib.Constants;
20 import org.eclipse.jgit.lib.IndexDiff;
21 import org.eclipse.jgit.lib.ProgressMonitor;
22 import org.eclipse.jgit.lib.Repository;
23 import org.eclipse.jgit.submodule.SubmoduleWalk.IgnoreSubmoduleMode;
24 import org.eclipse.jgit.treewalk.FileTreeIterator;
25 import org.eclipse.jgit.treewalk.WorkingTreeIterator;
26 import org.eclipse.jgit.treewalk.filter.PathFilterGroup;
27
28 /**
29 * A class used to execute a {@code Status} command. It has setters for all
30 * supported options and arguments of this command and a {@link #call()} method
31 * to finally execute the command. Each instance of this class should only be
32 * used for one invocation of the command (means: one call to {@link #call()})
33 *
34 * @see <a href=
35 * "http://www.kernel.org/pub/software/scm/git/docs/git-status.html" >Git
36 * documentation about Status</a>
37 */
38 public class StatusCommand extends GitCommand<Status> {
39 private WorkingTreeIterator workingTreeIt;
40 private List<String> paths = null;
41 private ProgressMonitor progressMonitor = null;
42
43 private IgnoreSubmoduleMode ignoreSubmoduleMode = null;
44
45 /**
46 * Constructor for StatusCommand.
47 *
48 * @param repo
49 * a {@link org.eclipse.jgit.lib.Repository} object.
50 */
51 protected StatusCommand(Repository repo) {
52 super(repo);
53 }
54
55 /**
56 * Whether to ignore submodules
57 *
58 * @param mode
59 * the
60 * {@link org.eclipse.jgit.submodule.SubmoduleWalk.IgnoreSubmoduleMode}
61 * @return {@code this}
62 * @since 3.6
63 */
64 public StatusCommand setIgnoreSubmodules(IgnoreSubmoduleMode mode) {
65 ignoreSubmoduleMode = mode;
66 return this;
67 }
68
69 /**
70 * Show only the status of files which match the given paths. The path must
71 * either name a file or a directory exactly. All paths are always relative
72 * to the repository root. If a directory is specified all files recursively
73 * underneath that directory are matched. If this method is called multiple
74 * times then the status of those files is reported which match at least one
75 * of the given paths. Note that regex expressions or wildcards are not
76 * supported.
77 *
78 * @param path
79 * repository-relative path of file/directory to show status for
80 * (with <code>/</code> as separator)
81 * @return {@code this}
82 * @since 3.1
83 */
84 public StatusCommand addPath(String path) {
85 if (paths == null)
86 paths = new LinkedList<>();
87 paths.add(path);
88 return this;
89 }
90
91 /**
92 * Returns the paths filtering this status.
93 *
94 * @return the paths for which the status is shown or <code>null</code> if
95 * the complete status for the whole repo is shown.
96 * @since 3.1
97 */
98 public List<String> getPaths() {
99 return paths;
100 }
101
102 /**
103 * {@inheritDoc}
104 * <p>
105 * Executes the {@code Status} command with all the options and parameters
106 * collected by the setter methods of this class. Each instance of this
107 * class should only be used for one invocation of the command. Don't call
108 * this method twice on an instance.
109 */
110 @Override
111 public Status call() throws GitAPIException, NoWorkTreeException {
112 if (workingTreeIt == null)
113 workingTreeIt = new FileTreeIterator(repo);
114
115 try {
116 IndexDiff diff = new IndexDiff(repo, Constants.HEAD, workingTreeIt);
117 if (ignoreSubmoduleMode != null)
118 diff.setIgnoreSubmoduleMode(ignoreSubmoduleMode);
119 if (paths != null)
120 diff.setFilter(PathFilterGroup.createFromStrings(paths));
121 if (progressMonitor == null)
122 diff.diff();
123 else
124 diff.diff(progressMonitor, ProgressMonitor.UNKNOWN,
125 ProgressMonitor.UNKNOWN, ""); //$NON-NLS-1$
126 return new Status(diff);
127 } catch (IOException e) {
128 throw new JGitInternalException(e.getMessage(), e);
129 }
130 }
131
132 /**
133 * To set the {@link org.eclipse.jgit.treewalk.WorkingTreeIterator} which
134 * should be used. If this method is not called a standard
135 * {@link org.eclipse.jgit.treewalk.FileTreeIterator} is used.
136 *
137 * @param workingTreeIt
138 * a working tree iterator
139 * @return {@code this}
140 */
141 public StatusCommand setWorkingTreeIt(WorkingTreeIterator workingTreeIt) {
142 this.workingTreeIt = workingTreeIt;
143 return this;
144 }
145
146 /**
147 * To set the {@link org.eclipse.jgit.lib.ProgressMonitor} which contains
148 * callback methods to inform you about the progress of this command.
149 *
150 * @param progressMonitor
151 * a {@link org.eclipse.jgit.lib.ProgressMonitor} object.
152 * @return {@code this}
153 * @since 3.1
154 */
155 public StatusCommand setProgressMonitor(ProgressMonitor progressMonitor) {
156 this.progressMonitor = progressMonitor;
157 return this;
158 }
159 }