Back to home page

EIC code displayed by LXR

 
 

    


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

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_checkout_h__
0008 #define INCLUDE_git_checkout_h__
0009 
0010 #include "common.h"
0011 #include "types.h"
0012 #include "diff.h"
0013 
0014 /**
0015  * @file git2/checkout.h
0016  * @brief Git checkout routines
0017  * @defgroup git_checkout Git checkout routines
0018  * @ingroup Git
0019  * @{
0020  */
0021 GIT_BEGIN_DECL
0022 
0023 /**
0024  * Checkout behavior flags
0025  *
0026  * In libgit2, checkout is used to update the working directory and index
0027  * to match a target tree.  Unlike git checkout, it does not move the HEAD
0028  * commit for you - use `git_repository_set_head` or the like to do that.
0029  *
0030  * Checkout looks at (up to) four things: the "target" tree you want to
0031  * check out, the "baseline" tree of what was checked out previously, the
0032  * working directory for actual files, and the index for staged changes.
0033  *
0034  * You give checkout one of three strategies for update:
0035  *
0036  * - `GIT_CHECKOUT_NONE` is a dry-run strategy that checks for conflicts,
0037  *   etc., but doesn't make any actual changes.
0038  *
0039  * - `GIT_CHECKOUT_FORCE` is at the opposite extreme, taking any action to
0040  *   make the working directory match the target (including potentially
0041  *   discarding modified files).
0042  *
0043  * - `GIT_CHECKOUT_SAFE` is between these two options, it will only make
0044  *   modifications that will not lose changes.
0045  *
0046  *                         |  target == baseline   |  target != baseline  |
0047  *    ---------------------|-----------------------|----------------------|
0048  *     workdir == baseline |       no action       |  create, update, or  |
0049  *                         |                       |     delete file      |
0050  *    ---------------------|-----------------------|----------------------|
0051  *     workdir exists and  |       no action       |   conflict (notify   |
0052  *       is != baseline    | notify dirty MODIFIED | and cancel checkout) |
0053  *    ---------------------|-----------------------|----------------------|
0054  *      workdir missing,   | notify dirty DELETED  |     create file      |
0055  *      baseline present   |                       |                      |
0056  *    ---------------------|-----------------------|----------------------|
0057  *
0058  * To emulate `git checkout`, use `GIT_CHECKOUT_SAFE` with a checkout
0059  * notification callback (see below) that displays information about dirty
0060  * files.  The default behavior will cancel checkout on conflicts.
0061  *
0062  * To emulate `git checkout-index`, use `GIT_CHECKOUT_SAFE` with a
0063  * notification callback that cancels the operation if a dirty-but-existing
0064  * file is found in the working directory.  This core git command isn't
0065  * quite "force" but is sensitive about some types of changes.
0066  *
0067  * To emulate `git checkout -f`, use `GIT_CHECKOUT_FORCE`.
0068  *
0069  *
0070  * There are some additional flags to modify the behavior of checkout:
0071  *
0072  * - GIT_CHECKOUT_ALLOW_CONFLICTS makes SAFE mode apply safe file updates
0073  *   even if there are conflicts (instead of cancelling the checkout).
0074  *
0075  * - GIT_CHECKOUT_REMOVE_UNTRACKED means remove untracked files (i.e. not
0076  *   in target, baseline, or index, and not ignored) from the working dir.
0077  *
0078  * - GIT_CHECKOUT_REMOVE_IGNORED means remove ignored files (that are also
0079  *   untracked) from the working directory as well.
0080  *
0081  * - GIT_CHECKOUT_UPDATE_ONLY means to only update the content of files that
0082  *   already exist.  Files will not be created nor deleted.  This just skips
0083  *   applying adds, deletes, and typechanges.
0084  *
0085  * - GIT_CHECKOUT_DONT_UPDATE_INDEX prevents checkout from writing the
0086  *   updated files' information to the index.
0087  *
0088  * - Normally, checkout will reload the index and git attributes from disk
0089  *   before any operations.  GIT_CHECKOUT_NO_REFRESH prevents this reload.
0090  *
0091  * - Unmerged index entries are conflicts.  GIT_CHECKOUT_SKIP_UNMERGED skips
0092  *   files with unmerged index entries instead.  GIT_CHECKOUT_USE_OURS and
0093  *   GIT_CHECKOUT_USE_THEIRS to proceed with the checkout using either the
0094  *   stage 2 ("ours") or stage 3 ("theirs") version of files in the index.
0095  *
0096  * - GIT_CHECKOUT_DONT_OVERWRITE_IGNORED prevents ignored files from being
0097  *   overwritten.  Normally, files that are ignored in the working directory
0098  *   are not considered "precious" and may be overwritten if the checkout
0099  *   target contains that file.
0100  *
0101  * - GIT_CHECKOUT_DONT_REMOVE_EXISTING prevents checkout from removing
0102  *   files or folders that fold to the same name on case insensitive
0103  *   filesystems.  This can cause files to retain their existing names
0104  *   and write through existing symbolic links.
0105  */
0106 typedef enum {
0107     GIT_CHECKOUT_NONE = 0, /**< default is a dry run, no actual updates */
0108 
0109     /**
0110      * Allow safe updates that cannot overwrite uncommitted data.
0111      * If the uncommitted changes don't conflict with the checked out files,
0112      * the checkout will still proceed, leaving the changes intact.
0113      *
0114      * Mutually exclusive with GIT_CHECKOUT_FORCE.
0115      * GIT_CHECKOUT_FORCE takes precedence over GIT_CHECKOUT_SAFE.
0116      */
0117     GIT_CHECKOUT_SAFE = (1u << 0),
0118 
0119     /**
0120      * Allow all updates to force working directory to look like index.
0121      *
0122      * Mutually exclusive with GIT_CHECKOUT_SAFE.
0123      * GIT_CHECKOUT_FORCE takes precedence over GIT_CHECKOUT_SAFE.
0124      */
0125     GIT_CHECKOUT_FORCE = (1u << 1),
0126 
0127 
0128     /** Allow checkout to recreate missing files */
0129     GIT_CHECKOUT_RECREATE_MISSING = (1u << 2),
0130 
0131     /** Allow checkout to make safe updates even if conflicts are found */
0132     GIT_CHECKOUT_ALLOW_CONFLICTS = (1u << 4),
0133 
0134     /** Remove untracked files not in index (that are not ignored) */
0135     GIT_CHECKOUT_REMOVE_UNTRACKED = (1u << 5),
0136 
0137     /** Remove ignored files not in index */
0138     GIT_CHECKOUT_REMOVE_IGNORED = (1u << 6),
0139 
0140     /** Only update existing files, don't create new ones */
0141     GIT_CHECKOUT_UPDATE_ONLY = (1u << 7),
0142 
0143     /**
0144      * Normally checkout updates index entries as it goes; this stops that.
0145      * Implies `GIT_CHECKOUT_DONT_WRITE_INDEX`.
0146      */
0147     GIT_CHECKOUT_DONT_UPDATE_INDEX = (1u << 8),
0148 
0149     /** Don't refresh index/config/etc before doing checkout */
0150     GIT_CHECKOUT_NO_REFRESH = (1u << 9),
0151 
0152     /** Allow checkout to skip unmerged files */
0153     GIT_CHECKOUT_SKIP_UNMERGED = (1u << 10),
0154     /** For unmerged files, checkout stage 2 from index */
0155     GIT_CHECKOUT_USE_OURS = (1u << 11),
0156     /** For unmerged files, checkout stage 3 from index */
0157     GIT_CHECKOUT_USE_THEIRS = (1u << 12),
0158 
0159     /** Treat pathspec as simple list of exact match file paths */
0160     GIT_CHECKOUT_DISABLE_PATHSPEC_MATCH = (1u << 13),
0161 
0162     /** Ignore directories in use, they will be left empty */
0163     GIT_CHECKOUT_SKIP_LOCKED_DIRECTORIES = (1u << 18),
0164 
0165     /** Don't overwrite ignored files that exist in the checkout target */
0166     GIT_CHECKOUT_DONT_OVERWRITE_IGNORED = (1u << 19),
0167 
0168     /** Write normal merge files for conflicts */
0169     GIT_CHECKOUT_CONFLICT_STYLE_MERGE = (1u << 20),
0170 
0171     /** Include common ancestor data in diff3 format files for conflicts */
0172     GIT_CHECKOUT_CONFLICT_STYLE_DIFF3 = (1u << 21),
0173 
0174     /** Don't overwrite existing files or folders */
0175     GIT_CHECKOUT_DONT_REMOVE_EXISTING = (1u << 22),
0176 
0177     /** Normally checkout writes the index upon completion; this prevents that. */
0178     GIT_CHECKOUT_DONT_WRITE_INDEX = (1u << 23),
0179 
0180     /**
0181      * Show what would be done by a checkout.  Stop after sending
0182      * notifications; don't update the working directory or index.
0183      */
0184     GIT_CHECKOUT_DRY_RUN = (1u << 24),
0185 
0186     /** Include common ancestor data in zdiff3 format for conflicts */
0187     GIT_CHECKOUT_CONFLICT_STYLE_ZDIFF3 = (1u << 25),
0188 
0189     /**
0190      * THE FOLLOWING OPTIONS ARE NOT YET IMPLEMENTED
0191      */
0192 
0193     /** Recursively checkout submodules with same options (NOT IMPLEMENTED) */
0194     GIT_CHECKOUT_UPDATE_SUBMODULES = (1u << 16),
0195     /** Recursively checkout submodules if HEAD moved in super repo (NOT IMPLEMENTED) */
0196     GIT_CHECKOUT_UPDATE_SUBMODULES_IF_CHANGED = (1u << 17)
0197 
0198 } git_checkout_strategy_t;
0199 
0200 /**
0201  * Checkout notification flags
0202  *
0203  * Checkout will invoke an options notification callback (`notify_cb`) for
0204  * certain cases - you pick which ones via `notify_flags`:
0205  *
0206  * Returning a non-zero value from this callback will cancel the checkout.
0207  * The non-zero return value will be propagated back and returned by the
0208  * git_checkout_... call.
0209  *
0210  * Notification callbacks are made prior to modifying any files on disk,
0211  * so canceling on any notification will still happen prior to any files
0212  * being modified.
0213  */
0214 typedef enum {
0215     GIT_CHECKOUT_NOTIFY_NONE      = 0,
0216 
0217     /**
0218      * Invokes checkout on conflicting paths.
0219      */
0220     GIT_CHECKOUT_NOTIFY_CONFLICT  = (1u << 0),
0221 
0222     /**
0223      * Notifies about "dirty" files, i.e. those that do not need an update
0224      * but no longer match the baseline.  Core git displays these files when
0225      * checkout runs, but won't stop the checkout.
0226      */
0227     GIT_CHECKOUT_NOTIFY_DIRTY     = (1u << 1),
0228 
0229     /**
0230      * Sends notification for any file changed.
0231      */
0232     GIT_CHECKOUT_NOTIFY_UPDATED   = (1u << 2),
0233 
0234     /**
0235      * Notifies about untracked files.
0236      */
0237     GIT_CHECKOUT_NOTIFY_UNTRACKED = (1u << 3),
0238 
0239     /**
0240      * Notifies about ignored files.
0241      */
0242     GIT_CHECKOUT_NOTIFY_IGNORED   = (1u << 4),
0243 
0244     GIT_CHECKOUT_NOTIFY_ALL       = 0x0FFFFu
0245 } git_checkout_notify_t;
0246 
0247 /** Checkout performance-reporting structure */
0248 typedef struct {
0249     size_t mkdir_calls;
0250     size_t stat_calls;
0251     size_t chmod_calls;
0252 } git_checkout_perfdata;
0253 
0254 /** Checkout notification callback function */
0255 typedef int GIT_CALLBACK(git_checkout_notify_cb)(
0256     git_checkout_notify_t why,
0257     const char *path,
0258     const git_diff_file *baseline,
0259     const git_diff_file *target,
0260     const git_diff_file *workdir,
0261     void *payload);
0262 
0263 /** Checkout progress notification function */
0264 typedef void GIT_CALLBACK(git_checkout_progress_cb)(
0265     const char *path,
0266     size_t completed_steps,
0267     size_t total_steps,
0268     void *payload);
0269 
0270 /** Checkout perfdata notification function */
0271 typedef void GIT_CALLBACK(git_checkout_perfdata_cb)(
0272     const git_checkout_perfdata *perfdata,
0273     void *payload);
0274 
0275 /**
0276  * Checkout options structure
0277  *
0278  * Initialize with `GIT_CHECKOUT_OPTIONS_INIT`. Alternatively, you can
0279  * use `git_checkout_options_init`.
0280  *
0281  */
0282 typedef struct git_checkout_options {
0283     unsigned int version; /**< The version */
0284 
0285     unsigned int checkout_strategy; /**< default will be a safe checkout */
0286 
0287     int disable_filters;    /**< don't apply filters like CRLF conversion */
0288     unsigned int dir_mode;  /**< default is 0755 */
0289     unsigned int file_mode; /**< default is 0644 or 0755 as dictated by blob */
0290     int file_open_flags;    /**< default is O_CREAT | O_TRUNC | O_WRONLY */
0291 
0292     unsigned int notify_flags; /**< see `git_checkout_notify_t` above */
0293 
0294     /**
0295      * Optional callback to get notifications on specific file states.
0296      * @see git_checkout_notify_t
0297      */
0298     git_checkout_notify_cb notify_cb;
0299 
0300     /** Payload passed to notify_cb */
0301     void *notify_payload;
0302 
0303     /** Optional callback to notify the consumer of checkout progress. */
0304     git_checkout_progress_cb progress_cb;
0305 
0306     /** Payload passed to progress_cb */
0307     void *progress_payload;
0308 
0309     /**
0310      * A list of wildmatch patterns or paths.
0311      *
0312      * By default, all paths are processed. If you pass an array of wildmatch
0313      * patterns, those will be used to filter which paths should be taken into
0314      * account.
0315      *
0316      * Use GIT_CHECKOUT_DISABLE_PATHSPEC_MATCH to treat as a simple list.
0317      */
0318     git_strarray paths;
0319 
0320     /**
0321      * The expected content of the working directory; defaults to HEAD.
0322      *
0323      * If the working directory does not match this baseline information,
0324      * that will produce a checkout conflict.
0325      */
0326     git_tree *baseline;
0327 
0328     /**
0329      * Like `baseline` above, though expressed as an index.  This
0330      * option overrides `baseline`.
0331      */
0332     git_index *baseline_index;
0333 
0334     const char *target_directory; /**< alternative checkout path to workdir */
0335 
0336     const char *ancestor_label; /**< the name of the common ancestor side of conflicts */
0337     const char *our_label; /**< the name of the "our" side of conflicts */
0338     const char *their_label; /**< the name of the "their" side of conflicts */
0339 
0340     /** Optional callback to notify the consumer of performance data. */
0341     git_checkout_perfdata_cb perfdata_cb;
0342 
0343     /** Payload passed to perfdata_cb */
0344     void *perfdata_payload;
0345 } git_checkout_options;
0346 
0347 #define GIT_CHECKOUT_OPTIONS_VERSION 1
0348 #define GIT_CHECKOUT_OPTIONS_INIT {GIT_CHECKOUT_OPTIONS_VERSION, GIT_CHECKOUT_SAFE}
0349 
0350 /**
0351  * Initialize git_checkout_options structure
0352  *
0353  * Initializes a `git_checkout_options` with default values. Equivalent to creating
0354  * an instance with GIT_CHECKOUT_OPTIONS_INIT.
0355  *
0356  * @param opts The `git_checkout_options` struct to initialize.
0357  * @param version The struct version; pass `GIT_CHECKOUT_OPTIONS_VERSION`.
0358  * @return Zero on success; -1 on failure.
0359  */
0360 GIT_EXTERN(int) git_checkout_options_init(
0361     git_checkout_options *opts,
0362     unsigned int version);
0363 
0364 /**
0365  * Updates files in the index and the working tree to match the content of
0366  * the commit pointed at by HEAD.
0367  *
0368  * Note that this is _not_ the correct mechanism used to switch branches;
0369  * do not change your `HEAD` and then call this method, that would leave
0370  * you with checkout conflicts since your working directory would then
0371  * appear to be dirty.  Instead, checkout the target of the branch and
0372  * then update `HEAD` using `git_repository_set_head` to point to the
0373  * branch you checked out.
0374  *
0375  * @param repo repository to check out (must be non-bare)
0376  * @param opts specifies checkout options (may be NULL)
0377  * @return 0 on success, GIT_EUNBORNBRANCH if HEAD points to a non
0378  *         existing branch, non-zero value returned by `notify_cb`, or
0379  *         other error code < 0 (use git_error_last for error details)
0380  */
0381 GIT_EXTERN(int) git_checkout_head(
0382     git_repository *repo,
0383     const git_checkout_options *opts);
0384 
0385 /**
0386  * Updates files in the working tree to match the content of the index.
0387  *
0388  * @param repo repository into which to check out (must be non-bare)
0389  * @param index index to be checked out (or NULL to use repository index)
0390  * @param opts specifies checkout options (may be NULL)
0391  * @return 0 on success, non-zero return value from `notify_cb`, or error
0392  *         code < 0 (use git_error_last for error details)
0393  */
0394 GIT_EXTERN(int) git_checkout_index(
0395     git_repository *repo,
0396     git_index *index,
0397     const git_checkout_options *opts);
0398 
0399 /**
0400  * Updates files in the index and working tree to match the content of the
0401  * tree pointed at by the treeish.
0402  *
0403  * @param repo repository to check out (must be non-bare)
0404  * @param treeish a commit, tag or tree which content will be used to update
0405  * the working directory (or NULL to use HEAD)
0406  * @param opts specifies checkout options (may be NULL)
0407  * @return 0 on success, non-zero return value from `notify_cb`, or error
0408  *         code < 0 (use git_error_last for error details)
0409  */
0410 GIT_EXTERN(int) git_checkout_tree(
0411     git_repository *repo,
0412     const git_object *treeish,
0413     const git_checkout_options *opts);
0414 
0415 /** @} */
0416 GIT_END_DECL
0417 #endif