]> git.proxmox.com Git - libgit2.git/blob - include/git2/status.h
New upstream version 0.28.1+dfsg.1
[libgit2.git] / include / git2 / status.h
1 /*
2 * Copyright (C) the libgit2 contributors. All rights reserved.
3 *
4 * This file is part of libgit2, distributed under the GNU GPL v2 with
5 * a Linking Exception. For full terms see the included COPYING file.
6 */
7 #ifndef INCLUDE_git_status_h__
8 #define INCLUDE_git_status_h__
9
10 #include "common.h"
11 #include "types.h"
12 #include "strarray.h"
13 #include "diff.h"
14
15 /**
16 * @file git2/status.h
17 * @brief Git file status routines
18 * @defgroup git_status Git file status routines
19 * @ingroup Git
20 * @{
21 */
22 GIT_BEGIN_DECL
23
24 /**
25 * Status flags for a single file.
26 *
27 * A combination of these values will be returned to indicate the status of
28 * a file. Status compares the working directory, the index, and the
29 * current HEAD of the repository. The `GIT_STATUS_INDEX` set of flags
30 * represents the status of file in the index relative to the HEAD, and the
31 * `GIT_STATUS_WT` set of flags represent the status of the file in the
32 * working directory relative to the index.
33 */
34 typedef enum {
35 GIT_STATUS_CURRENT = 0,
36
37 GIT_STATUS_INDEX_NEW = (1u << 0),
38 GIT_STATUS_INDEX_MODIFIED = (1u << 1),
39 GIT_STATUS_INDEX_DELETED = (1u << 2),
40 GIT_STATUS_INDEX_RENAMED = (1u << 3),
41 GIT_STATUS_INDEX_TYPECHANGE = (1u << 4),
42
43 GIT_STATUS_WT_NEW = (1u << 7),
44 GIT_STATUS_WT_MODIFIED = (1u << 8),
45 GIT_STATUS_WT_DELETED = (1u << 9),
46 GIT_STATUS_WT_TYPECHANGE = (1u << 10),
47 GIT_STATUS_WT_RENAMED = (1u << 11),
48 GIT_STATUS_WT_UNREADABLE = (1u << 12),
49
50 GIT_STATUS_IGNORED = (1u << 14),
51 GIT_STATUS_CONFLICTED = (1u << 15),
52 } git_status_t;
53
54 /**
55 * Function pointer to receive status on individual files
56 *
57 * `path` is the relative path to the file from the root of the repository.
58 *
59 * `status_flags` is a combination of `git_status_t` values that apply.
60 *
61 * `payload` is the value you passed to the foreach function as payload.
62 */
63 typedef int GIT_CALLBACK(git_status_cb)(
64 const char *path, unsigned int status_flags, void *payload);
65
66 /**
67 * Select the files on which to report status.
68 *
69 * With `git_status_foreach_ext`, this will control which changes get
70 * callbacks. With `git_status_list_new`, these will control which
71 * changes are included in the list.
72 *
73 * - GIT_STATUS_SHOW_INDEX_AND_WORKDIR is the default. This roughly
74 * matches `git status --porcelain` regarding which files are
75 * included and in what order.
76 * - GIT_STATUS_SHOW_INDEX_ONLY only gives status based on HEAD to index
77 * comparison, not looking at working directory changes.
78 * - GIT_STATUS_SHOW_WORKDIR_ONLY only gives status based on index to
79 * working directory comparison, not comparing the index to the HEAD.
80 */
81 typedef enum {
82 GIT_STATUS_SHOW_INDEX_AND_WORKDIR = 0,
83 GIT_STATUS_SHOW_INDEX_ONLY = 1,
84 GIT_STATUS_SHOW_WORKDIR_ONLY = 2,
85 } git_status_show_t;
86
87 /**
88 * Flags to control status callbacks
89 *
90 * - GIT_STATUS_OPT_INCLUDE_UNTRACKED says that callbacks should be made
91 * on untracked files. These will only be made if the workdir files are
92 * included in the status "show" option.
93 * - GIT_STATUS_OPT_INCLUDE_IGNORED says that ignored files get callbacks.
94 * Again, these callbacks will only be made if the workdir files are
95 * included in the status "show" option.
96 * - GIT_STATUS_OPT_INCLUDE_UNMODIFIED indicates that callback should be
97 * made even on unmodified files.
98 * - GIT_STATUS_OPT_EXCLUDE_SUBMODULES indicates that submodules should be
99 * skipped. This only applies if there are no pending typechanges to
100 * the submodule (either from or to another type).
101 * - GIT_STATUS_OPT_RECURSE_UNTRACKED_DIRS indicates that all files in
102 * untracked directories should be included. Normally if an entire
103 * directory is new, then just the top-level directory is included (with
104 * a trailing slash on the entry name). This flag says to include all
105 * of the individual files in the directory instead.
106 * - GIT_STATUS_OPT_DISABLE_PATHSPEC_MATCH indicates that the given path
107 * should be treated as a literal path, and not as a pathspec pattern.
108 * - GIT_STATUS_OPT_RECURSE_IGNORED_DIRS indicates that the contents of
109 * ignored directories should be included in the status. This is like
110 * doing `git ls-files -o -i --exclude-standard` with core git.
111 * - GIT_STATUS_OPT_RENAMES_HEAD_TO_INDEX indicates that rename detection
112 * should be processed between the head and the index and enables
113 * the GIT_STATUS_INDEX_RENAMED as a possible status flag.
114 * - GIT_STATUS_OPT_RENAMES_INDEX_TO_WORKDIR indicates that rename
115 * detection should be run between the index and the working directory
116 * and enabled GIT_STATUS_WT_RENAMED as a possible status flag.
117 * - GIT_STATUS_OPT_SORT_CASE_SENSITIVELY overrides the native case
118 * sensitivity for the file system and forces the output to be in
119 * case-sensitive order
120 * - GIT_STATUS_OPT_SORT_CASE_INSENSITIVELY overrides the native case
121 * sensitivity for the file system and forces the output to be in
122 * case-insensitive order
123 * - GIT_STATUS_OPT_RENAMES_FROM_REWRITES indicates that rename detection
124 * should include rewritten files
125 * - GIT_STATUS_OPT_NO_REFRESH bypasses the default status behavior of
126 * doing a "soft" index reload (i.e. reloading the index data if the
127 * file on disk has been modified outside libgit2).
128 * - GIT_STATUS_OPT_UPDATE_INDEX tells libgit2 to refresh the stat cache
129 * in the index for files that are unchanged but have out of date stat
130 * information in the index. It will result in less work being done on
131 * subsequent calls to get status. This is mutually exclusive with the
132 * NO_REFRESH option.
133 *
134 * Calling `git_status_foreach()` is like calling the extended version
135 * with: GIT_STATUS_OPT_INCLUDE_IGNORED, GIT_STATUS_OPT_INCLUDE_UNTRACKED,
136 * and GIT_STATUS_OPT_RECURSE_UNTRACKED_DIRS. Those options are bundled
137 * together as `GIT_STATUS_OPT_DEFAULTS` if you want them as a baseline.
138 */
139 typedef enum {
140 GIT_STATUS_OPT_INCLUDE_UNTRACKED = (1u << 0),
141 GIT_STATUS_OPT_INCLUDE_IGNORED = (1u << 1),
142 GIT_STATUS_OPT_INCLUDE_UNMODIFIED = (1u << 2),
143 GIT_STATUS_OPT_EXCLUDE_SUBMODULES = (1u << 3),
144 GIT_STATUS_OPT_RECURSE_UNTRACKED_DIRS = (1u << 4),
145 GIT_STATUS_OPT_DISABLE_PATHSPEC_MATCH = (1u << 5),
146 GIT_STATUS_OPT_RECURSE_IGNORED_DIRS = (1u << 6),
147 GIT_STATUS_OPT_RENAMES_HEAD_TO_INDEX = (1u << 7),
148 GIT_STATUS_OPT_RENAMES_INDEX_TO_WORKDIR = (1u << 8),
149 GIT_STATUS_OPT_SORT_CASE_SENSITIVELY = (1u << 9),
150 GIT_STATUS_OPT_SORT_CASE_INSENSITIVELY = (1u << 10),
151 GIT_STATUS_OPT_RENAMES_FROM_REWRITES = (1u << 11),
152 GIT_STATUS_OPT_NO_REFRESH = (1u << 12),
153 GIT_STATUS_OPT_UPDATE_INDEX = (1u << 13),
154 GIT_STATUS_OPT_INCLUDE_UNREADABLE = (1u << 14),
155 GIT_STATUS_OPT_INCLUDE_UNREADABLE_AS_UNTRACKED = (1u << 15),
156 } git_status_opt_t;
157
158 #define GIT_STATUS_OPT_DEFAULTS \
159 (GIT_STATUS_OPT_INCLUDE_IGNORED | \
160 GIT_STATUS_OPT_INCLUDE_UNTRACKED | \
161 GIT_STATUS_OPT_RECURSE_UNTRACKED_DIRS)
162
163 /**
164 * Options to control how `git_status_foreach_ext()` will issue callbacks.
165 *
166 * This structure is set so that zeroing it out will give you relatively
167 * sane defaults.
168 *
169 * The `show` value is one of the `git_status_show_t` constants that
170 * control which files to scan and in what order.
171 *
172 * The `flags` value is an OR'ed combination of the `git_status_opt_t`
173 * values above.
174 *
175 * The `pathspec` is an array of path patterns to match (using
176 * fnmatch-style matching), or just an array of paths to match exactly if
177 * `GIT_STATUS_OPT_DISABLE_PATHSPEC_MATCH` is specified in the flags.
178 *
179 * The `baseline` is the tree to be used for comparison to the working directory
180 * and index; defaults to HEAD.
181 */
182 typedef struct {
183 unsigned int version;
184 git_status_show_t show;
185 unsigned int flags;
186 git_strarray pathspec;
187 git_tree *baseline;
188 } git_status_options;
189
190 #define GIT_STATUS_OPTIONS_VERSION 1
191 #define GIT_STATUS_OPTIONS_INIT {GIT_STATUS_OPTIONS_VERSION}
192
193 /**
194 * Initialize git_status_options structure
195 *
196 * Initializes a `git_status_options` with default values. Equivalent to
197 * creating an instance with `GIT_STATUS_OPTIONS_INIT`.
198 *
199 * @param opts The `git_status_options` struct to initialize.
200 * @param version The struct version; pass `GIT_STATUS_OPTIONS_VERSION`.
201 * @return Zero on success; -1 on failure.
202 */
203 GIT_EXTERN(int) git_status_init_options(
204 git_status_options *opts,
205 unsigned int version);
206
207 /**
208 * A status entry, providing the differences between the file as it exists
209 * in HEAD and the index, and providing the differences between the index
210 * and the working directory.
211 *
212 * The `status` value provides the status flags for this file.
213 *
214 * The `head_to_index` value provides detailed information about the
215 * differences between the file in HEAD and the file in the index.
216 *
217 * The `index_to_workdir` value provides detailed information about the
218 * differences between the file in the index and the file in the
219 * working directory.
220 */
221 typedef struct {
222 git_status_t status;
223 git_diff_delta *head_to_index;
224 git_diff_delta *index_to_workdir;
225 } git_status_entry;
226
227
228 /**
229 * Gather file statuses and run a callback for each one.
230 *
231 * The callback is passed the path of the file, the status (a combination of
232 * the `git_status_t` values above) and the `payload` data pointer passed
233 * into this function.
234 *
235 * If the callback returns a non-zero value, this function will stop looping
236 * and return that value to caller.
237 *
238 * @param repo A repository object
239 * @param callback The function to call on each file
240 * @param payload Pointer to pass through to callback function
241 * @return 0 on success, non-zero callback return value, or error code
242 */
243 GIT_EXTERN(int) git_status_foreach(
244 git_repository *repo,
245 git_status_cb callback,
246 void *payload);
247
248 /**
249 * Gather file status information and run callbacks as requested.
250 *
251 * This is an extended version of the `git_status_foreach()` API that
252 * allows for more granular control over which paths will be processed and
253 * in what order. See the `git_status_options` structure for details
254 * about the additional controls that this makes available.
255 *
256 * Note that if a `pathspec` is given in the `git_status_options` to filter
257 * the status, then the results from rename detection (if you enable it) may
258 * not be accurate. To do rename detection properly, this must be called
259 * with no `pathspec` so that all files can be considered.
260 *
261 * @param repo Repository object
262 * @param opts Status options structure
263 * @param callback The function to call on each file
264 * @param payload Pointer to pass through to callback function
265 * @return 0 on success, non-zero callback return value, or error code
266 */
267 GIT_EXTERN(int) git_status_foreach_ext(
268 git_repository *repo,
269 const git_status_options *opts,
270 git_status_cb callback,
271 void *payload);
272
273 /**
274 * Get file status for a single file.
275 *
276 * This tries to get status for the filename that you give. If no files
277 * match that name (in either the HEAD, index, or working directory), this
278 * returns GIT_ENOTFOUND.
279 *
280 * If the name matches multiple files (for example, if the `path` names a
281 * directory or if running on a case- insensitive filesystem and yet the
282 * HEAD has two entries that both match the path), then this returns
283 * GIT_EAMBIGUOUS because it cannot give correct results.
284 *
285 * This does not do any sort of rename detection. Renames require a set of
286 * targets and because of the path filtering, there is not enough
287 * information to check renames correctly. To check file status with rename
288 * detection, there is no choice but to do a full `git_status_list_new` and
289 * scan through looking for the path that you are interested in.
290 *
291 * @param status_flags Output combination of git_status_t values for file
292 * @param repo A repository object
293 * @param path The exact path to retrieve status for relative to the
294 * repository working directory
295 * @return 0 on success, GIT_ENOTFOUND if the file is not found in the HEAD,
296 * index, and work tree, GIT_EAMBIGUOUS if `path` matches multiple files
297 * or if it refers to a folder, and -1 on other errors.
298 */
299 GIT_EXTERN(int) git_status_file(
300 unsigned int *status_flags,
301 git_repository *repo,
302 const char *path);
303
304 /**
305 * Gather file status information and populate the `git_status_list`.
306 *
307 * Note that if a `pathspec` is given in the `git_status_options` to filter
308 * the status, then the results from rename detection (if you enable it) may
309 * not be accurate. To do rename detection properly, this must be called
310 * with no `pathspec` so that all files can be considered.
311 *
312 * @param out Pointer to store the status results in
313 * @param repo Repository object
314 * @param opts Status options structure
315 * @return 0 on success or error code
316 */
317 GIT_EXTERN(int) git_status_list_new(
318 git_status_list **out,
319 git_repository *repo,
320 const git_status_options *opts);
321
322 /**
323 * Gets the count of status entries in this list.
324 *
325 * If there are no changes in status (at least according the options given
326 * when the status list was created), this can return 0.
327 *
328 * @param statuslist Existing status list object
329 * @return the number of status entries
330 */
331 GIT_EXTERN(size_t) git_status_list_entrycount(
332 git_status_list *statuslist);
333
334 /**
335 * Get a pointer to one of the entries in the status list.
336 *
337 * The entry is not modifiable and should not be freed.
338 *
339 * @param statuslist Existing status list object
340 * @param idx Position of the entry
341 * @return Pointer to the entry; NULL if out of bounds
342 */
343 GIT_EXTERN(const git_status_entry *) git_status_byindex(
344 git_status_list *statuslist,
345 size_t idx);
346
347 /**
348 * Free an existing status list
349 *
350 * @param statuslist Existing status list object
351 */
352 GIT_EXTERN(void) git_status_list_free(
353 git_status_list *statuslist);
354
355 /**
356 * Test if the ignore rules apply to a given file.
357 *
358 * This function checks the ignore rules to see if they would apply to the
359 * given file. This indicates if the file would be ignored regardless of
360 * whether the file is already in the index or committed to the repository.
361 *
362 * One way to think of this is if you were to do "git add ." on the
363 * directory containing the file, would it be added or not?
364 *
365 * @param ignored Boolean returning 0 if the file is not ignored, 1 if it is
366 * @param repo A repository object
367 * @param path The file to check ignores for, rooted at the repo's workdir.
368 * @return 0 if ignore rules could be processed for the file (regardless
369 * of whether it exists or not), or an error < 0 if they could not.
370 */
371 GIT_EXTERN(int) git_status_should_ignore(
372 int *ignored,
373 git_repository *repo,
374 const char *path);
375
376 /** @} */
377 GIT_END_DECL
378 #endif