Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:59:39

0001 /*
0002  * Copyright (C) the libgit2 contributors. All rights reserved.
0003  *
0004  * This file is part of libgit2, distributed under the GNU GPL v2 with
0005  * a Linking Exception. For full terms see the included COPYING file.
0006  */
0007 #ifndef INCLUDE_git_status_h__
0008 #define INCLUDE_git_status_h__
0009 
0010 #include "common.h"
0011 #include "types.h"
0012 #include "strarray.h"
0013 #include "diff.h"
0014 
0015 /**
0016  * @file git2/status.h
0017  * @brief Git file status routines
0018  * @defgroup git_status Git file status routines
0019  * @ingroup Git
0020  * @{
0021  */
0022 GIT_BEGIN_DECL
0023 
0024 /**
0025  * Status flags for a single file.
0026  *
0027  * A combination of these values will be returned to indicate the status of
0028  * a file.  Status compares the working directory, the index, and the
0029  * current HEAD of the repository.  The `GIT_STATUS_INDEX` set of flags
0030  * represents the status of file in the index relative to the HEAD, and the
0031  * `GIT_STATUS_WT` set of flags represent the status of the file in the
0032  * working directory relative to the index.
0033  */
0034 typedef enum {
0035     GIT_STATUS_CURRENT = 0,
0036 
0037     GIT_STATUS_INDEX_NEW        = (1u << 0),
0038     GIT_STATUS_INDEX_MODIFIED   = (1u << 1),
0039     GIT_STATUS_INDEX_DELETED    = (1u << 2),
0040     GIT_STATUS_INDEX_RENAMED    = (1u << 3),
0041     GIT_STATUS_INDEX_TYPECHANGE = (1u << 4),
0042 
0043     GIT_STATUS_WT_NEW           = (1u << 7),
0044     GIT_STATUS_WT_MODIFIED      = (1u << 8),
0045     GIT_STATUS_WT_DELETED       = (1u << 9),
0046     GIT_STATUS_WT_TYPECHANGE    = (1u << 10),
0047     GIT_STATUS_WT_RENAMED       = (1u << 11),
0048     GIT_STATUS_WT_UNREADABLE    = (1u << 12),
0049 
0050     GIT_STATUS_IGNORED          = (1u << 14),
0051     GIT_STATUS_CONFLICTED       = (1u << 15)
0052 } git_status_t;
0053 
0054 /**
0055  * Function pointer to receive status on individual files
0056  *
0057  * `path` is the relative path to the file from the root of the repository.
0058  *
0059  * `status_flags` is a combination of `git_status_t` values that apply.
0060  *
0061  * `payload` is the value you passed to the foreach function as payload.
0062  */
0063 typedef int GIT_CALLBACK(git_status_cb)(
0064     const char *path, unsigned int status_flags, void *payload);
0065 
0066 /**
0067  * Select the files on which to report status.
0068  *
0069  * With `git_status_foreach_ext`, this will control which changes get
0070  * callbacks.  With `git_status_list_new`, these will control which
0071  * changes are included in the list.
0072  */
0073 typedef enum {
0074     /**
0075      * The default. This roughly matches `git status --porcelain` regarding
0076      * which files are included and in what order.
0077      */
0078     GIT_STATUS_SHOW_INDEX_AND_WORKDIR = 0,
0079 
0080     /**
0081      * Only gives status based on HEAD to index comparison, not looking at
0082      * working directory changes.
0083      */
0084     GIT_STATUS_SHOW_INDEX_ONLY = 1,
0085 
0086     /**
0087      * Only gives status based on index to working directory comparison,
0088      * not comparing the index to the HEAD.
0089      */
0090     GIT_STATUS_SHOW_WORKDIR_ONLY = 2
0091 } git_status_show_t;
0092 
0093 /**
0094  * Flags to control status callbacks
0095  *
0096  * Calling `git_status_foreach()` is like calling the extended version
0097  * with: GIT_STATUS_OPT_INCLUDE_IGNORED, GIT_STATUS_OPT_INCLUDE_UNTRACKED,
0098  * and GIT_STATUS_OPT_RECURSE_UNTRACKED_DIRS.  Those options are bundled
0099  * together as `GIT_STATUS_OPT_DEFAULTS` if you want them as a baseline.
0100  */
0101 typedef enum {
0102     /**
0103      * Says that callbacks should be made on untracked files.
0104      * These will only be made if the workdir files are included in the status
0105      * "show" option.
0106      */
0107     GIT_STATUS_OPT_INCLUDE_UNTRACKED                = (1u << 0),
0108 
0109     /**
0110      * Says that ignored files get callbacks.
0111      * Again, these callbacks will only be made if the workdir files are
0112      * included in the status "show" option.
0113      */
0114     GIT_STATUS_OPT_INCLUDE_IGNORED                  = (1u << 1),
0115 
0116     /**
0117      * Indicates that callback should be made even on unmodified files.
0118      */
0119     GIT_STATUS_OPT_INCLUDE_UNMODIFIED               = (1u << 2),
0120 
0121     /**
0122      * Indicates that submodules should be skipped.
0123      * This only applies if there are no pending typechanges to the submodule
0124      * (either from or to another type).
0125      */
0126     GIT_STATUS_OPT_EXCLUDE_SUBMODULES               = (1u << 3),
0127 
0128     /**
0129      * Indicates that all files in untracked directories should be included.
0130      * Normally if an entire directory is new, then just the top-level
0131      * directory is included (with a trailing slash on the entry name).
0132      * This flag says to include all of the individual files in the directory
0133      * instead.
0134      */
0135     GIT_STATUS_OPT_RECURSE_UNTRACKED_DIRS           = (1u << 4),
0136 
0137     /**
0138      * Indicates that the given path should be treated as a literal path,
0139      * and not as a pathspec pattern.
0140      */
0141     GIT_STATUS_OPT_DISABLE_PATHSPEC_MATCH           = (1u << 5),
0142 
0143     /**
0144      * Indicates that the contents of ignored directories should be included
0145      * in the status. This is like doing `git ls-files -o -i --exclude-standard`
0146      * with core git.
0147      */
0148     GIT_STATUS_OPT_RECURSE_IGNORED_DIRS             = (1u << 6),
0149 
0150     /**
0151      * Indicates that rename detection should be processed between the head and
0152      * the index and enables the GIT_STATUS_INDEX_RENAMED as a possible status
0153      * flag.
0154      */
0155     GIT_STATUS_OPT_RENAMES_HEAD_TO_INDEX            = (1u << 7),
0156 
0157     /**
0158      * Indicates that rename detection should be run between the index and the
0159      * working directory and enabled GIT_STATUS_WT_RENAMED as a possible status
0160      * flag.
0161      */
0162     GIT_STATUS_OPT_RENAMES_INDEX_TO_WORKDIR         = (1u << 8),
0163 
0164     /**
0165      * Overrides the native case sensitivity for the file system and forces
0166      * the output to be in case-sensitive order.
0167      */
0168     GIT_STATUS_OPT_SORT_CASE_SENSITIVELY            = (1u << 9),
0169 
0170     /**
0171      * Overrides the native case sensitivity for the file system and forces
0172      * the output to be in case-insensitive order.
0173      */
0174     GIT_STATUS_OPT_SORT_CASE_INSENSITIVELY          = (1u << 10),
0175 
0176     /**
0177      * Iindicates that rename detection should include rewritten files.
0178      */
0179     GIT_STATUS_OPT_RENAMES_FROM_REWRITES            = (1u << 11),
0180 
0181     /**
0182      * Bypasses the default status behavior of doing a "soft" index reload
0183      * (i.e. reloading the index data if the file on disk has been modified
0184      * outside libgit2).
0185      */
0186     GIT_STATUS_OPT_NO_REFRESH                       = (1u << 12),
0187 
0188     /**
0189      * Tells libgit2 to refresh the stat cache in the index for files that are
0190      * unchanged but have out of date stat einformation in the index.
0191      * It will result in less work being done on subsequent calls to get status.
0192      * This is mutually exclusive with the NO_REFRESH option.
0193      */
0194     GIT_STATUS_OPT_UPDATE_INDEX                     = (1u << 13),
0195 
0196     /**
0197      * Normally files that cannot be opened or read are ignored as
0198      * these are often transient files; this option will return
0199      * unreadable files as `GIT_STATUS_WT_UNREADABLE`.
0200      */
0201     GIT_STATUS_OPT_INCLUDE_UNREADABLE               = (1u << 14),
0202 
0203     /**
0204      * Unreadable files will be detected and given the status
0205      * untracked instead of unreadable.
0206      */
0207     GIT_STATUS_OPT_INCLUDE_UNREADABLE_AS_UNTRACKED  = (1u << 15)
0208 } git_status_opt_t;
0209 
0210 #define GIT_STATUS_OPT_DEFAULTS \
0211     (GIT_STATUS_OPT_INCLUDE_IGNORED | \
0212     GIT_STATUS_OPT_INCLUDE_UNTRACKED | \
0213     GIT_STATUS_OPT_RECURSE_UNTRACKED_DIRS)
0214 
0215 /**
0216  * Options to control how `git_status_foreach_ext()` will issue callbacks.
0217  *
0218  * Initialize with `GIT_STATUS_OPTIONS_INIT`. Alternatively, you can
0219  * use `git_status_options_init`.
0220  *
0221  */
0222 typedef struct {
0223     /**
0224      * The struct version; pass `GIT_STATUS_OPTIONS_VERSION`.
0225      */
0226     unsigned int version;
0227 
0228     /**
0229      * The `show` value is one of the `git_status_show_t` constants that
0230      * control which files to scan and in what order. The default is
0231      * `GIT_STATUS_SHOW_INDEX_AND_WORKDIR`.
0232      */
0233     git_status_show_t show;
0234 
0235     /**
0236      * The `flags` value is an OR'ed combination of the
0237      * `git_status_opt_t` values above. The default is
0238      * `GIT_STATUS_OPT_DEFAULTS`, which matches git's default
0239      * behavior.
0240      */
0241     unsigned int      flags;
0242 
0243     /**
0244      * The `pathspec` is an array of path patterns to match (using
0245      * fnmatch-style matching), or just an array of paths to match
0246      * exactly if `GIT_STATUS_OPT_DISABLE_PATHSPEC_MATCH` is specified
0247      * in the flags.
0248      */
0249     git_strarray      pathspec;
0250 
0251     /**
0252      * The `baseline` is the tree to be used for comparison to the
0253      * working directory and index; defaults to HEAD.
0254      */
0255     git_tree          *baseline;
0256 
0257     /**
0258      * Threshold above which similar files will be considered renames.
0259      * This is equivalent to the -M option. Defaults to 50.
0260      */
0261     uint16_t          rename_threshold;
0262 } git_status_options;
0263 
0264 #define GIT_STATUS_OPTIONS_VERSION 1
0265 #define GIT_STATUS_OPTIONS_INIT {GIT_STATUS_OPTIONS_VERSION}
0266 
0267 /**
0268  * Initialize git_status_options structure
0269  *
0270  * Initializes a `git_status_options` with default values. Equivalent to
0271  * creating an instance with `GIT_STATUS_OPTIONS_INIT`.
0272  *
0273  * @param opts The `git_status_options` struct to initialize.
0274  * @param version The struct version; pass `GIT_STATUS_OPTIONS_VERSION`.
0275  * @return Zero on success; -1 on failure.
0276  */
0277 GIT_EXTERN(int) git_status_options_init(
0278     git_status_options *opts,
0279     unsigned int version);
0280 
0281 /**
0282  * A status entry, providing the differences between the file as it exists
0283  * in HEAD and the index, and providing the differences between the index
0284  * and the working directory.
0285  *
0286  * The `status` value provides the status flags for this file.
0287  *
0288  * The `head_to_index` value provides detailed information about the
0289  * differences between the file in HEAD and the file in the index.
0290  *
0291  * The `index_to_workdir` value provides detailed information about the
0292  * differences between the file in the index and the file in the
0293  * working directory.
0294  */
0295 typedef struct {
0296     git_status_t status;
0297     git_diff_delta *head_to_index;
0298     git_diff_delta *index_to_workdir;
0299 } git_status_entry;
0300 
0301 
0302 /**
0303  * Gather file statuses and run a callback for each one.
0304  *
0305  * The callback is passed the path of the file, the status (a combination of
0306  * the `git_status_t` values above) and the `payload` data pointer passed
0307  * into this function.
0308  *
0309  * If the callback returns a non-zero value, this function will stop looping
0310  * and return that value to caller.
0311  *
0312  * @param repo A repository object
0313  * @param callback The function to call on each file
0314  * @param payload Pointer to pass through to callback function
0315  * @return 0 on success, non-zero callback return value, or error code
0316  */
0317 GIT_EXTERN(int) git_status_foreach(
0318     git_repository *repo,
0319     git_status_cb callback,
0320     void *payload);
0321 
0322 /**
0323  * Gather file status information and run callbacks as requested.
0324  *
0325  * This is an extended version of the `git_status_foreach()` API that
0326  * allows for more granular control over which paths will be processed and
0327  * in what order.  See the `git_status_options` structure for details
0328  * about the additional controls that this makes available.
0329  *
0330  * Note that if a `pathspec` is given in the `git_status_options` to filter
0331  * the status, then the results from rename detection (if you enable it) may
0332  * not be accurate.  To do rename detection properly, this must be called
0333  * with no `pathspec` so that all files can be considered.
0334  *
0335  * @param repo Repository object
0336  * @param opts Status options structure
0337  * @param callback The function to call on each file
0338  * @param payload Pointer to pass through to callback function
0339  * @return 0 on success, non-zero callback return value, or error code
0340  */
0341 GIT_EXTERN(int) git_status_foreach_ext(
0342     git_repository *repo,
0343     const git_status_options *opts,
0344     git_status_cb callback,
0345     void *payload);
0346 
0347 /**
0348  * Get file status for a single file.
0349  *
0350  * This tries to get status for the filename that you give.  If no files
0351  * match that name (in either the HEAD, index, or working directory), this
0352  * returns GIT_ENOTFOUND.
0353  *
0354  * If the name matches multiple files (for example, if the `path` names a
0355  * directory or if running on a case- insensitive filesystem and yet the
0356  * HEAD has two entries that both match the path), then this returns
0357  * GIT_EAMBIGUOUS because it cannot give correct results.
0358  *
0359  * This does not do any sort of rename detection.  Renames require a set of
0360  * targets and because of the path filtering, there is not enough
0361  * information to check renames correctly.  To check file status with rename
0362  * detection, there is no choice but to do a full `git_status_list_new` and
0363  * scan through looking for the path that you are interested in.
0364  *
0365  * @param status_flags Output combination of git_status_t values for file
0366  * @param repo A repository object
0367  * @param path The exact path to retrieve status for relative to the
0368  * repository working directory
0369  * @return 0 on success, GIT_ENOTFOUND if the file is not found in the HEAD,
0370  *      index, and work tree, GIT_EAMBIGUOUS if `path` matches multiple files
0371  *      or if it refers to a folder, and -1 on other errors.
0372  */
0373 GIT_EXTERN(int) git_status_file(
0374     unsigned int *status_flags,
0375     git_repository *repo,
0376     const char *path);
0377 
0378 /**
0379  * Gather file status information and populate the `git_status_list`.
0380  *
0381  * Note that if a `pathspec` is given in the `git_status_options` to filter
0382  * the status, then the results from rename detection (if you enable it) may
0383  * not be accurate.  To do rename detection properly, this must be called
0384  * with no `pathspec` so that all files can be considered.
0385  *
0386  * @param out Pointer to store the status results in
0387  * @param repo Repository object
0388  * @param opts Status options structure
0389  * @return 0 on success or error code
0390  */
0391 GIT_EXTERN(int) git_status_list_new(
0392     git_status_list **out,
0393     git_repository *repo,
0394     const git_status_options *opts);
0395 
0396 /**
0397  * Gets the count of status entries in this list.
0398  *
0399  * If there are no changes in status (at least according the options given
0400  * when the status list was created), this can return 0.
0401  *
0402  * @param statuslist Existing status list object
0403  * @return the number of status entries
0404  */
0405 GIT_EXTERN(size_t) git_status_list_entrycount(
0406     git_status_list *statuslist);
0407 
0408 /**
0409  * Get a pointer to one of the entries in the status list.
0410  *
0411  * The entry is not modifiable and should not be freed.
0412  *
0413  * @param statuslist Existing status list object
0414  * @param idx Position of the entry
0415  * @return Pointer to the entry; NULL if out of bounds
0416  */
0417 GIT_EXTERN(const git_status_entry *) git_status_byindex(
0418     git_status_list *statuslist,
0419     size_t idx);
0420 
0421 /**
0422  * Free an existing status list
0423  *
0424  * @param statuslist Existing status list object
0425  */
0426 GIT_EXTERN(void) git_status_list_free(
0427     git_status_list *statuslist);
0428 
0429 /**
0430  * Test if the ignore rules apply to a given file.
0431  *
0432  * This function checks the ignore rules to see if they would apply to the
0433  * given file.  This indicates if the file would be ignored regardless of
0434  * whether the file is already in the index or committed to the repository.
0435  *
0436  * One way to think of this is if you were to do "git add ." on the
0437  * directory containing the file, would it be added or not?
0438  *
0439  * @param ignored Boolean returning 0 if the file is not ignored, 1 if it is
0440  * @param repo A repository object
0441  * @param path The file to check ignores for, rooted at the repo's workdir.
0442  * @return 0 if ignore rules could be processed for the file (regardless
0443  *         of whether it exists or not), or an error < 0 if they could not.
0444  */
0445 GIT_EXTERN(int) git_status_should_ignore(
0446     int *ignored,
0447     git_repository *repo,
0448     const char *path);
0449 
0450 /** @} */
0451 GIT_END_DECL
0452 #endif