Blame include/git2/checkout.h

Packit Service 20376f
/*
Packit Service 20376f
 * Copyright (C) the libgit2 contributors. All rights reserved.
Packit Service 20376f
 *
Packit Service 20376f
 * This file is part of libgit2, distributed under the GNU GPL v2 with
Packit Service 20376f
 * a Linking Exception. For full terms see the included COPYING file.
Packit Service 20376f
 */
Packit Service 20376f
#ifndef INCLUDE_git_checkout_h__
Packit Service 20376f
#define INCLUDE_git_checkout_h__
Packit Service 20376f
Packit Service 20376f
#include "common.h"
Packit Service 20376f
#include "types.h"
Packit Service 20376f
#include "diff.h"
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * @file git2/checkout.h
Packit Service 20376f
 * @brief Git checkout routines
Packit Service 20376f
 * @defgroup git_checkout Git checkout routines
Packit Service 20376f
 * @ingroup Git
Packit Service 20376f
 * @{
Packit Service 20376f
 */
Packit Service 20376f
GIT_BEGIN_DECL
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * Checkout behavior flags
Packit Service 20376f
 *
Packit Service 20376f
 * In libgit2, checkout is used to update the working directory and index
Packit Service 20376f
 * to match a target tree.  Unlike git checkout, it does not move the HEAD
Packit Service 20376f
 * commit for you - use `git_repository_set_head` or the like to do that.
Packit Service 20376f
 *
Packit Service 20376f
 * Checkout looks at (up to) four things: the "target" tree you want to
Packit Service 20376f
 * check out, the "baseline" tree of what was checked out previously, the
Packit Service 20376f
 * working directory for actual files, and the index for staged changes.
Packit Service 20376f
 *
Packit Service 20376f
 * You give checkout one of three strategies for update:
Packit Service 20376f
 *
Packit Service 20376f
 * - `GIT_CHECKOUT_NONE` is a dry-run strategy that checks for conflicts,
Packit Service 20376f
 *   etc., but doesn't make any actual changes.
Packit Service 20376f
 *
Packit Service 20376f
 * - `GIT_CHECKOUT_FORCE` is at the opposite extreme, taking any action to
Packit Service 20376f
 *   make the working directory match the target (including potentially
Packit Service 20376f
 *   discarding modified files).
Packit Service 20376f
 *
Packit Service 20376f
 * - `GIT_CHECKOUT_SAFE` is between these two options, it will only make
Packit Service 20376f
 *   modifications that will not lose changes.
Packit Service 20376f
 *
Packit Service 20376f
 *                         |  target == baseline   |  target != baseline  |
Packit Service 20376f
 *    ---------------------|-----------------------|----------------------|
Packit Service 20376f
 *     workdir == baseline |       no action       |  create, update, or  |
Packit Service 20376f
 *                         |                       |     delete file      |
Packit Service 20376f
 *    ---------------------|-----------------------|----------------------|
Packit Service 20376f
 *     workdir exists and  |       no action       |   conflict (notify   |
Packit Service 20376f
 *       is != baseline    | notify dirty MODIFIED | and cancel checkout) |
Packit Service 20376f
 *    ---------------------|-----------------------|----------------------|
Packit Service 20376f
 *      workdir missing,   | notify dirty DELETED  |     create file      |
Packit Service 20376f
 *      baseline present   |                       |                      |
Packit Service 20376f
 *    ---------------------|-----------------------|----------------------|
Packit Service 20376f
 *
Packit Service 20376f
 * To emulate `git checkout`, use `GIT_CHECKOUT_SAFE` with a checkout
Packit Service 20376f
 * notification callback (see below) that displays information about dirty
Packit Service 20376f
 * files.  The default behavior will cancel checkout on conflicts.
Packit Service 20376f
 *
Packit Service 20376f
 * To emulate `git checkout-index`, use `GIT_CHECKOUT_SAFE` with a
Packit Service 20376f
 * notification callback that cancels the operation if a dirty-but-existing
Packit Service 20376f
 * file is found in the working directory.  This core git command isn't
Packit Service 20376f
 * quite "force" but is sensitive about some types of changes.
Packit Service 20376f
 *
Packit Service 20376f
 * To emulate `git checkout -f`, use `GIT_CHECKOUT_FORCE`.
Packit Service 20376f
 *
Packit Service 20376f
 *
Packit Service 20376f
 * There are some additional flags to modified the behavior of checkout:
Packit Service 20376f
 *
Packit Service 20376f
 * - GIT_CHECKOUT_ALLOW_CONFLICTS makes SAFE mode apply safe file updates
Packit Service 20376f
 *   even if there are conflicts (instead of cancelling the checkout).
Packit Service 20376f
 *
Packit Service 20376f
 * - GIT_CHECKOUT_REMOVE_UNTRACKED means remove untracked files (i.e. not
Packit Service 20376f
 *   in target, baseline, or index, and not ignored) from the working dir.
Packit Service 20376f
 *
Packit Service 20376f
 * - GIT_CHECKOUT_REMOVE_IGNORED means remove ignored files (that are also
Packit Service 20376f
 *   untracked) from the working directory as well.
Packit Service 20376f
 *
Packit Service 20376f
 * - GIT_CHECKOUT_UPDATE_ONLY means to only update the content of files that
Packit Service 20376f
 *   already exist.  Files will not be created nor deleted.  This just skips
Packit Service 20376f
 *   applying adds, deletes, and typechanges.
Packit Service 20376f
 *
Packit Service 20376f
 * - GIT_CHECKOUT_DONT_UPDATE_INDEX prevents checkout from writing the
Packit Service 20376f
 *   updated files' information to the index.
Packit Service 20376f
 *
Packit Service 20376f
 * - Normally, checkout will reload the index and git attributes from disk
Packit Service 20376f
 *   before any operations.  GIT_CHECKOUT_NO_REFRESH prevents this reload.
Packit Service 20376f
 *
Packit Service 20376f
 * - Unmerged index entries are conflicts.  GIT_CHECKOUT_SKIP_UNMERGED skips
Packit Service 20376f
 *   files with unmerged index entries instead.  GIT_CHECKOUT_USE_OURS and
Packit Service 20376f
 *   GIT_CHECKOUT_USE_THEIRS to proceed with the checkout using either the
Packit Service 20376f
 *   stage 2 ("ours") or stage 3 ("theirs") version of files in the index.
Packit Service 20376f
 *
Packit Service 20376f
 * - GIT_CHECKOUT_DONT_OVERWRITE_IGNORED prevents ignored files from being
Packit Service 20376f
 *   overwritten.  Normally, files that are ignored in the working directory
Packit Service 20376f
 *   are not considered "precious" and may be overwritten if the checkout
Packit Service 20376f
 *   target contains that file.
Packit Service 20376f
 *
Packit Service 20376f
 * - GIT_CHECKOUT_DONT_REMOVE_EXISTING prevents checkout from removing
Packit Service 20376f
 *   files or folders that fold to the same name on case insensitive
Packit Service 20376f
 *   filesystems.  This can cause files to retain their existing names
Packit Service 20376f
 *   and write through existing symbolic links.
Packit Service 20376f
 */
Packit Service 20376f
typedef enum {
Packit Service 20376f
	GIT_CHECKOUT_NONE = 0, /**< default is a dry run, no actual updates */
Packit Service 20376f
Packit Service 20376f
	/** Allow safe updates that cannot overwrite uncommitted data */
Packit Service 20376f
	GIT_CHECKOUT_SAFE = (1u << 0),
Packit Service 20376f
Packit Service 20376f
	/** Allow all updates to force working directory to look like index */
Packit Service 20376f
	GIT_CHECKOUT_FORCE = (1u << 1),
Packit Service 20376f
Packit Service 20376f
Packit Service 20376f
	/** Allow checkout to recreate missing files */
Packit Service 20376f
	GIT_CHECKOUT_RECREATE_MISSING = (1u << 2),
Packit Service 20376f
Packit Service 20376f
	/** Allow checkout to make safe updates even if conflicts are found */
Packit Service 20376f
	GIT_CHECKOUT_ALLOW_CONFLICTS = (1u << 4),
Packit Service 20376f
Packit Service 20376f
	/** Remove untracked files not in index (that are not ignored) */
Packit Service 20376f
	GIT_CHECKOUT_REMOVE_UNTRACKED = (1u << 5),
Packit Service 20376f
Packit Service 20376f
	/** Remove ignored files not in index */
Packit Service 20376f
	GIT_CHECKOUT_REMOVE_IGNORED = (1u << 6),
Packit Service 20376f
Packit Service 20376f
	/** Only update existing files, don't create new ones */
Packit Service 20376f
	GIT_CHECKOUT_UPDATE_ONLY = (1u << 7),
Packit Service 20376f
Packit Service 20376f
	/**
Packit Service 20376f
	 * Normally checkout updates index entries as it goes; this stops that.
Packit Service 20376f
	 * Implies `GIT_CHECKOUT_DONT_WRITE_INDEX`.
Packit Service 20376f
	 */
Packit Service 20376f
	GIT_CHECKOUT_DONT_UPDATE_INDEX = (1u << 8),
Packit Service 20376f
Packit Service 20376f
	/** Don't refresh index/config/etc before doing checkout */
Packit Service 20376f
	GIT_CHECKOUT_NO_REFRESH = (1u << 9),
Packit Service 20376f
Packit Service 20376f
	/** Allow checkout to skip unmerged files */
Packit Service 20376f
	GIT_CHECKOUT_SKIP_UNMERGED = (1u << 10),
Packit Service 20376f
	/** For unmerged files, checkout stage 2 from index */
Packit Service 20376f
	GIT_CHECKOUT_USE_OURS = (1u << 11),
Packit Service 20376f
	/** For unmerged files, checkout stage 3 from index */
Packit Service 20376f
	GIT_CHECKOUT_USE_THEIRS = (1u << 12),
Packit Service 20376f
Packit Service 20376f
	/** Treat pathspec as simple list of exact match file paths */
Packit Service 20376f
	GIT_CHECKOUT_DISABLE_PATHSPEC_MATCH = (1u << 13),
Packit Service 20376f
Packit Service 20376f
	/** Ignore directories in use, they will be left empty */
Packit Service 20376f
	GIT_CHECKOUT_SKIP_LOCKED_DIRECTORIES = (1u << 18),
Packit Service 20376f
Packit Service 20376f
	/** Don't overwrite ignored files that exist in the checkout target */
Packit Service 20376f
	GIT_CHECKOUT_DONT_OVERWRITE_IGNORED = (1u << 19),
Packit Service 20376f
Packit Service 20376f
	/** Write normal merge files for conflicts */
Packit Service 20376f
	GIT_CHECKOUT_CONFLICT_STYLE_MERGE = (1u << 20),
Packit Service 20376f
Packit Service 20376f
	/** Include common ancestor data in diff3 format files for conflicts */
Packit Service 20376f
	GIT_CHECKOUT_CONFLICT_STYLE_DIFF3 = (1u << 21),
Packit Service 20376f
Packit Service 20376f
	/** Don't overwrite existing files or folders */
Packit Service 20376f
	GIT_CHECKOUT_DONT_REMOVE_EXISTING = (1u << 22),
Packit Service 20376f
Packit Service 20376f
	/** Normally checkout writes the index upon completion; this prevents that. */
Packit Service 20376f
	GIT_CHECKOUT_DONT_WRITE_INDEX = (1u << 23),
Packit Service 20376f
Packit Service 20376f
	/**
Packit Service 20376f
	 * THE FOLLOWING OPTIONS ARE NOT YET IMPLEMENTED
Packit Service 20376f
	 */
Packit Service 20376f
Packit Service 20376f
	/** Recursively checkout submodules with same options (NOT IMPLEMENTED) */
Packit Service 20376f
	GIT_CHECKOUT_UPDATE_SUBMODULES = (1u << 16),
Packit Service 20376f
	/** Recursively checkout submodules if HEAD moved in super repo (NOT IMPLEMENTED) */
Packit Service 20376f
	GIT_CHECKOUT_UPDATE_SUBMODULES_IF_CHANGED = (1u << 17),
Packit Service 20376f
Packit Service 20376f
} git_checkout_strategy_t;
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * Checkout notification flags
Packit Service 20376f
 *
Packit Service 20376f
 * Checkout will invoke an options notification callback (`notify_cb`) for
Packit Service 20376f
 * certain cases - you pick which ones via `notify_flags`:
Packit Service 20376f
 *
Packit Service 20376f
 * - GIT_CHECKOUT_NOTIFY_CONFLICT invokes checkout on conflicting paths.
Packit Service 20376f
 *
Packit Service 20376f
 * - GIT_CHECKOUT_NOTIFY_DIRTY notifies about "dirty" files, i.e. those that
Packit Service 20376f
 *   do not need an update but no longer match the baseline.  Core git
Packit Service 20376f
 *   displays these files when checkout runs, but won't stop the checkout.
Packit Service 20376f
 *
Packit Service 20376f
 * - GIT_CHECKOUT_NOTIFY_UPDATED sends notification for any file changed.
Packit Service 20376f
 *
Packit Service 20376f
 * - GIT_CHECKOUT_NOTIFY_UNTRACKED notifies about untracked files.
Packit Service 20376f
 *
Packit Service 20376f
 * - GIT_CHECKOUT_NOTIFY_IGNORED notifies about ignored files.
Packit Service 20376f
 *
Packit Service 20376f
 * Returning a non-zero value from this callback will cancel the checkout.
Packit Service 20376f
 * The non-zero return value will be propagated back and returned by the
Packit Service 20376f
 * git_checkout_... call.
Packit Service 20376f
 *
Packit Service 20376f
 * Notification callbacks are made prior to modifying any files on disk,
Packit Service 20376f
 * so canceling on any notification will still happen prior to any files
Packit Service 20376f
 * being modified.
Packit Service 20376f
 */
Packit Service 20376f
typedef enum {
Packit Service 20376f
	GIT_CHECKOUT_NOTIFY_NONE      = 0,
Packit Service 20376f
	GIT_CHECKOUT_NOTIFY_CONFLICT  = (1u << 0),
Packit Service 20376f
	GIT_CHECKOUT_NOTIFY_DIRTY     = (1u << 1),
Packit Service 20376f
	GIT_CHECKOUT_NOTIFY_UPDATED   = (1u << 2),
Packit Service 20376f
	GIT_CHECKOUT_NOTIFY_UNTRACKED = (1u << 3),
Packit Service 20376f
	GIT_CHECKOUT_NOTIFY_IGNORED   = (1u << 4),
Packit Service 20376f
Packit Service 20376f
	GIT_CHECKOUT_NOTIFY_ALL       = 0x0FFFFu
Packit Service 20376f
} git_checkout_notify_t;
Packit Service 20376f
Packit Service 20376f
typedef struct {
Packit Service 20376f
	size_t mkdir_calls;
Packit Service 20376f
	size_t stat_calls;
Packit Service 20376f
	size_t chmod_calls;
Packit Service 20376f
} git_checkout_perfdata;
Packit Service 20376f
Packit Service 20376f
/** Checkout notification callback function */
Packit Service 20376f
typedef int (*git_checkout_notify_cb)(
Packit Service 20376f
	git_checkout_notify_t why,
Packit Service 20376f
	const char *path,
Packit Service 20376f
	const git_diff_file *baseline,
Packit Service 20376f
	const git_diff_file *target,
Packit Service 20376f
	const git_diff_file *workdir,
Packit Service 20376f
	void *payload);
Packit Service 20376f
Packit Service 20376f
/** Checkout progress notification function */
Packit Service 20376f
typedef void (*git_checkout_progress_cb)(
Packit Service 20376f
	const char *path,
Packit Service 20376f
	size_t completed_steps,
Packit Service 20376f
	size_t total_steps,
Packit Service 20376f
	void *payload);
Packit Service 20376f
Packit Service 20376f
/** Checkout perfdata notification function */
Packit Service 20376f
typedef void (*git_checkout_perfdata_cb)(
Packit Service 20376f
	const git_checkout_perfdata *perfdata,
Packit Service 20376f
	void *payload);
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * Checkout options structure
Packit Service 20376f
 *
Packit Service 20376f
 * Zero out for defaults.  Initialize with `GIT_CHECKOUT_OPTIONS_INIT` macro to
Packit Service 20376f
 * correctly set the `version` field.  E.g.
Packit Service 20376f
 *
Packit Service 20376f
 *		git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
Packit Service 20376f
 */
Packit Service 20376f
typedef struct git_checkout_options {
Packit Service 20376f
	unsigned int version;
Packit Service 20376f
Packit Service 20376f
	unsigned int checkout_strategy; /**< default will be a dry run */
Packit Service 20376f
Packit Service 20376f
	int disable_filters;    /**< don't apply filters like CRLF conversion */
Packit Service 20376f
	unsigned int dir_mode;  /**< default is 0755 */
Packit Service 20376f
	unsigned int file_mode; /**< default is 0644 or 0755 as dictated by blob */
Packit Service 20376f
	int file_open_flags;    /**< default is O_CREAT | O_TRUNC | O_WRONLY */
Packit Service 20376f
Packit Service 20376f
	unsigned int notify_flags; /**< see `git_checkout_notify_t` above */
Packit Service 20376f
	git_checkout_notify_cb notify_cb;
Packit Service 20376f
	void *notify_payload;
Packit Service 20376f
Packit Service 20376f
	/** Optional callback to notify the consumer of checkout progress. */
Packit Service 20376f
	git_checkout_progress_cb progress_cb;
Packit Service 20376f
	void *progress_payload;
Packit Service 20376f
Packit Service 20376f
	/** When not zeroed out, array of fnmatch patterns specifying which
Packit Service 20376f
	 *  paths should be taken into account, otherwise all files.  Use
Packit Service 20376f
	 *  GIT_CHECKOUT_DISABLE_PATHSPEC_MATCH to treat as simple list.
Packit Service 20376f
	 */
Packit Service 20376f
	git_strarray paths;
Packit Service 20376f
Packit Service 20376f
	/** The expected content of the working directory; defaults to HEAD.
Packit Service 20376f
	 *  If the working directory does not match this baseline information,
Packit Service 20376f
	 *  that will produce a checkout conflict.
Packit Service 20376f
	 */
Packit Service 20376f
	git_tree *baseline;
Packit Service 20376f
Packit Service 20376f
	/** Like `baseline` above, though expressed as an index.  This
Packit Service 20376f
	 *  option overrides `baseline`.
Packit Service 20376f
	 */
Packit Service 20376f
	git_index *baseline_index; /**< expected content of workdir, expressed as an index. */
Packit Service 20376f
Packit Service 20376f
	const char *target_directory; /**< alternative checkout path to workdir */
Packit Service 20376f
Packit Service 20376f
	const char *ancestor_label; /**< the name of the common ancestor side of conflicts */
Packit Service 20376f
	const char *our_label; /**< the name of the "our" side of conflicts */
Packit Service 20376f
	const char *their_label; /**< the name of the "their" side of conflicts */
Packit Service 20376f
Packit Service 20376f
	/** Optional callback to notify the consumer of performance data. */
Packit Service 20376f
	git_checkout_perfdata_cb perfdata_cb;
Packit Service 20376f
	void *perfdata_payload;
Packit Service 20376f
} git_checkout_options;
Packit Service 20376f
Packit Service 20376f
#define GIT_CHECKOUT_OPTIONS_VERSION 1
Packit Service 20376f
#define GIT_CHECKOUT_OPTIONS_INIT {GIT_CHECKOUT_OPTIONS_VERSION}
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
* Initializes a `git_checkout_options` with default values. Equivalent to
Packit Service 20376f
* creating an instance with GIT_CHECKOUT_OPTIONS_INIT.
Packit Service 20376f
*
Packit Service 20376f
* @param opts the `git_checkout_options` struct to initialize.
Packit Service 20376f
* @param version Version of struct; pass `GIT_CHECKOUT_OPTIONS_VERSION`
Packit Service 20376f
* @return Zero on success; -1 on failure.
Packit Service 20376f
*/
Packit Service 20376f
GIT_EXTERN(int) git_checkout_init_options(
Packit Service 20376f
	git_checkout_options *opts,
Packit Service 20376f
	unsigned int version);
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * Updates files in the index and the working tree to match the content of
Packit Service 20376f
 * the commit pointed at by HEAD.
Packit Service 20376f
 *
Packit Service 20376f
 * Note that this is _not_ the correct mechanism used to switch branches;
Packit Service 20376f
 * do not change your `HEAD` and then call this method, that would leave
Packit Service 20376f
 * you with checkout conflicts since your working directory would then
Packit Service 20376f
 * appear to be dirty.  Instead, checkout the target of the branch and
Packit Service 20376f
 * then update `HEAD` using `git_repository_set_head` to point to the
Packit Service 20376f
 * branch you checked out.
Packit Service 20376f
 *
Packit Service 20376f
 * @param repo repository to check out (must be non-bare)
Packit Service 20376f
 * @param opts specifies checkout options (may be NULL)
Packit Service 20376f
 * @return 0 on success, GIT_EUNBORNBRANCH if HEAD points to a non
Packit Service 20376f
 *         existing branch, non-zero value returned by `notify_cb`, or
Packit Service 20376f
 *         other error code < 0 (use giterr_last for error details)
Packit Service 20376f
 */
Packit Service 20376f
GIT_EXTERN(int) git_checkout_head(
Packit Service 20376f
	git_repository *repo,
Packit Service 20376f
	const git_checkout_options *opts);
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * Updates files in the working tree to match the content of the index.
Packit Service 20376f
 *
Packit Service 20376f
 * @param repo repository into which to check out (must be non-bare)
Packit Service 20376f
 * @param index index to be checked out (or NULL to use repository index)
Packit Service 20376f
 * @param opts specifies checkout options (may be NULL)
Packit Service 20376f
 * @return 0 on success, non-zero return value from `notify_cb`, or error
Packit Service 20376f
 *         code < 0 (use giterr_last for error details)
Packit Service 20376f
 */
Packit Service 20376f
GIT_EXTERN(int) git_checkout_index(
Packit Service 20376f
	git_repository *repo,
Packit Service 20376f
	git_index *index,
Packit Service 20376f
	const git_checkout_options *opts);
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * Updates files in the index and working tree to match the content of the
Packit Service 20376f
 * tree pointed at by the treeish.
Packit Service 20376f
 *
Packit Service 20376f
 * @param repo repository to check out (must be non-bare)
Packit Service 20376f
 * @param treeish a commit, tag or tree which content will be used to update
Packit Service 20376f
 * the working directory (or NULL to use HEAD)
Packit Service 20376f
 * @param opts specifies checkout options (may be NULL)
Packit Service 20376f
 * @return 0 on success, non-zero return value from `notify_cb`, or error
Packit Service 20376f
 *         code < 0 (use giterr_last for error details)
Packit Service 20376f
 */
Packit Service 20376f
GIT_EXTERN(int) git_checkout_tree(
Packit Service 20376f
	git_repository *repo,
Packit Service 20376f
	const git_object *treeish,
Packit Service 20376f
	const git_checkout_options *opts);
Packit Service 20376f
Packit Service 20376f
/** @} */
Packit Service 20376f
GIT_END_DECL
Packit Service 20376f
#endif