Blame include/git2/stash.h

Packit ae9e2a
/*
Packit ae9e2a
 * Copyright (C) the libgit2 contributors. All rights reserved.
Packit ae9e2a
 *
Packit ae9e2a
 * This file is part of libgit2, distributed under the GNU GPL v2 with
Packit ae9e2a
 * a Linking Exception. For full terms see the included COPYING file.
Packit ae9e2a
 */
Packit ae9e2a
#ifndef INCLUDE_git_stash_h__
Packit ae9e2a
#define INCLUDE_git_stash_h__
Packit ae9e2a
Packit ae9e2a
#include "common.h"
Packit ae9e2a
#include "types.h"
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * @file git2/stash.h
Packit ae9e2a
 * @brief Git stash management routines
Packit ae9e2a
 * @ingroup Git
Packit ae9e2a
 * @{
Packit ae9e2a
 */
Packit ae9e2a
GIT_BEGIN_DECL
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Stash flags
Packit ae9e2a
 */
Packit ae9e2a
typedef enum {
Packit ae9e2a
	/**
Packit ae9e2a
	 * No option, default
Packit ae9e2a
	 */
Packit ae9e2a
	GIT_STASH_DEFAULT = 0,
Packit ae9e2a
Packit ae9e2a
	/**
Packit ae9e2a
	 * All changes already added to the index are left intact in
Packit ae9e2a
	 * the working directory
Packit ae9e2a
	 */
Packit ae9e2a
	GIT_STASH_KEEP_INDEX = (1 << 0),
Packit ae9e2a
Packit ae9e2a
	/**
Packit ae9e2a
	 * All untracked files are also stashed and then cleaned up
Packit ae9e2a
	 * from the working directory
Packit ae9e2a
	 */
Packit ae9e2a
	GIT_STASH_INCLUDE_UNTRACKED = (1 << 1),
Packit ae9e2a
Packit ae9e2a
	/**
Packit ae9e2a
	 * All ignored files are also stashed and then cleaned up from
Packit ae9e2a
	 * the working directory
Packit ae9e2a
	 */
Packit ae9e2a
	GIT_STASH_INCLUDE_IGNORED = (1 << 2),
Packit ae9e2a
} git_stash_flags;
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Save the local modifications to a new stash.
Packit ae9e2a
 *
Packit ae9e2a
 * @param out Object id of the commit containing the stashed state.
Packit ae9e2a
 * This commit is also the target of the direct reference refs/stash.
Packit ae9e2a
 *
Packit ae9e2a
 * @param repo The owning repository.
Packit ae9e2a
 *
Packit ae9e2a
 * @param stasher The identity of the person performing the stashing.
Packit ae9e2a
 *
Packit ae9e2a
 * @param message Optional description along with the stashed state.
Packit ae9e2a
 *
Packit ae9e2a
 * @param flags Flags to control the stashing process. (see GIT_STASH_* above)
Packit ae9e2a
 *
Packit ae9e2a
 * @return 0 on success, GIT_ENOTFOUND where there's nothing to stash,
Packit ae9e2a
 * or error code.
Packit ae9e2a
 */
Packit ae9e2a
GIT_EXTERN(int) git_stash_save(
Packit ae9e2a
	git_oid *out,
Packit ae9e2a
	git_repository *repo,
Packit ae9e2a
	const git_signature *stasher,
Packit ae9e2a
	const char *message,
Packit ae9e2a
	uint32_t flags);
Packit ae9e2a
Packit ae9e2a
/** Stash application flags. */
Packit ae9e2a
typedef enum {
Packit ae9e2a
	GIT_STASH_APPLY_DEFAULT = 0,
Packit ae9e2a
Packit ae9e2a
	/* Try to reinstate not only the working tree's changes,
Packit ae9e2a
	 * but also the index's changes.
Packit ae9e2a
	 */
Packit ae9e2a
	GIT_STASH_APPLY_REINSTATE_INDEX = (1 << 0),
Packit ae9e2a
} git_stash_apply_flags;
Packit ae9e2a
Packit ae9e2a
typedef enum {
Packit ae9e2a
	GIT_STASH_APPLY_PROGRESS_NONE = 0,
Packit ae9e2a
Packit ae9e2a
	/** Loading the stashed data from the object database. */
Packit ae9e2a
	GIT_STASH_APPLY_PROGRESS_LOADING_STASH,
Packit ae9e2a
Packit ae9e2a
	/** The stored index is being analyzed. */
Packit ae9e2a
	GIT_STASH_APPLY_PROGRESS_ANALYZE_INDEX,
Packit ae9e2a
Packit ae9e2a
	/** The modified files are being analyzed. */
Packit ae9e2a
	GIT_STASH_APPLY_PROGRESS_ANALYZE_MODIFIED,
Packit ae9e2a
Packit ae9e2a
	/** The untracked and ignored files are being analyzed. */
Packit ae9e2a
	GIT_STASH_APPLY_PROGRESS_ANALYZE_UNTRACKED,
Packit ae9e2a
Packit ae9e2a
	/** The untracked files are being written to disk. */
Packit ae9e2a
	GIT_STASH_APPLY_PROGRESS_CHECKOUT_UNTRACKED,
Packit ae9e2a
Packit ae9e2a
	/** The modified files are being written to disk. */
Packit ae9e2a
	GIT_STASH_APPLY_PROGRESS_CHECKOUT_MODIFIED,
Packit ae9e2a
Packit ae9e2a
	/** The stash was applied successfully. */
Packit ae9e2a
	GIT_STASH_APPLY_PROGRESS_DONE,
Packit ae9e2a
} git_stash_apply_progress_t;
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Stash application progress notification function.
Packit ae9e2a
 * Return 0 to continue processing, or a negative value to
Packit ae9e2a
 * abort the stash application.
Packit ae9e2a
 */
Packit ae9e2a
typedef int (*git_stash_apply_progress_cb)(
Packit ae9e2a
	git_stash_apply_progress_t progress,
Packit ae9e2a
	void *payload);
Packit ae9e2a
Packit ae9e2a
/** Stash application options structure.
Packit ae9e2a
 *
Packit ae9e2a
 * Initialize with the `GIT_STASH_APPLY_OPTIONS_INIT` macro to set
Packit ae9e2a
 * sensible defaults; for example:
Packit ae9e2a
 *
Packit ae9e2a
 *		git_stash_apply_options opts = GIT_STASH_APPLY_OPTIONS_INIT;
Packit ae9e2a
 */
Packit ae9e2a
typedef struct git_stash_apply_options {
Packit ae9e2a
	unsigned int version;
Packit ae9e2a
Packit ae9e2a
	/** See `git_stash_apply_flags_t`, above. */
Packit ae9e2a
	git_stash_apply_flags flags;
Packit ae9e2a
Packit ae9e2a
	/** Options to use when writing files to the working directory. */
Packit ae9e2a
	git_checkout_options checkout_options;
Packit ae9e2a
Packit ae9e2a
	/** Optional callback to notify the consumer of application progress. */
Packit ae9e2a
	git_stash_apply_progress_cb progress_cb;
Packit ae9e2a
	void *progress_payload;
Packit ae9e2a
} git_stash_apply_options;
Packit ae9e2a
Packit ae9e2a
#define GIT_STASH_APPLY_OPTIONS_VERSION 1
Packit ae9e2a
#define GIT_STASH_APPLY_OPTIONS_INIT { \
Packit ae9e2a
	GIT_STASH_APPLY_OPTIONS_VERSION, \
Packit ae9e2a
	GIT_STASH_APPLY_DEFAULT, \
Packit ae9e2a
	GIT_CHECKOUT_OPTIONS_INIT }
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Initializes a `git_stash_apply_options` with default values. Equivalent to
Packit ae9e2a
 * creating an instance with GIT_STASH_APPLY_OPTIONS_INIT.
Packit ae9e2a
 *
Packit ae9e2a
 * @param opts the `git_stash_apply_options` instance to initialize.
Packit ae9e2a
 * @param version the version of the struct; you should pass
Packit ae9e2a
 *        `GIT_STASH_APPLY_OPTIONS_INIT` here.
Packit ae9e2a
 * @return Zero on success; -1 on failure.
Packit ae9e2a
 */
Packit ae9e2a
GIT_EXTERN(int) git_stash_apply_init_options(
Packit ae9e2a
	git_stash_apply_options *opts, unsigned int version);
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Apply a single stashed state from the stash list.
Packit ae9e2a
 *
Packit ae9e2a
 * If local changes in the working directory conflict with changes in the
Packit ae9e2a
 * stash then GIT_EMERGECONFLICT will be returned.  In this case, the index
Packit ae9e2a
 * will always remain unmodified and all files in the working directory will
Packit ae9e2a
 * remain unmodified.  However, if you are restoring untracked files or
Packit ae9e2a
 * ignored files and there is a conflict when applying the modified files,
Packit ae9e2a
 * then those files will remain in the working directory.
Packit ae9e2a
 *
Packit ae9e2a
 * If passing the GIT_STASH_APPLY_REINSTATE_INDEX flag and there would be
Packit ae9e2a
 * conflicts when reinstating the index, the function will return
Packit ae9e2a
 * GIT_EMERGECONFLICT and both the working directory and index will be left
Packit ae9e2a
 * unmodified.
Packit ae9e2a
 *
Packit ae9e2a
 * Note that a minimum checkout strategy of `GIT_CHECKOUT_SAFE` is implied.
Packit ae9e2a
 *
Packit ae9e2a
 * @param repo The owning repository.
Packit ae9e2a
 * @param index The position within the stash list. 0 points to the
Packit ae9e2a
 *              most recent stashed state.
Packit ae9e2a
 * @param options Optional options to control how stashes are applied.
Packit ae9e2a
 *
Packit ae9e2a
 * @return 0 on success, GIT_ENOTFOUND if there's no stashed state for the
Packit ae9e2a
 *         given index, GIT_EMERGECONFLICT if changes exist in the working
Packit ae9e2a
 *         directory, or an error code
Packit ae9e2a
 */
Packit ae9e2a
GIT_EXTERN(int) git_stash_apply(
Packit ae9e2a
	git_repository *repo,
Packit ae9e2a
	size_t index,
Packit ae9e2a
	const git_stash_apply_options *options);
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * This is a callback function you can provide to iterate over all the
Packit ae9e2a
 * stashed states that will be invoked per entry.
Packit ae9e2a
 *
Packit ae9e2a
 * @param index The position within the stash list. 0 points to the
Packit ae9e2a
 *              most recent stashed state.
Packit ae9e2a
 * @param message The stash message.
Packit ae9e2a
 * @param stash_id The commit oid of the stashed state.
Packit ae9e2a
 * @param payload Extra parameter to callback function.
Packit ae9e2a
 * @return 0 to continue iterating or non-zero to stop.
Packit ae9e2a
 */
Packit ae9e2a
typedef int (*git_stash_cb)(
Packit ae9e2a
	size_t index,
Packit ae9e2a
	const char* message,
Packit ae9e2a
	const git_oid *stash_id,
Packit ae9e2a
	void *payload);
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Loop over all the stashed states and issue a callback for each one.
Packit ae9e2a
 *
Packit ae9e2a
 * If the callback returns a non-zero value, this will stop looping.
Packit ae9e2a
 *
Packit ae9e2a
 * @param repo Repository where to find the stash.
Packit ae9e2a
 *
Packit ae9e2a
 * @param callback Callback to invoke per found stashed state. The most
Packit ae9e2a
 *                 recent stash state will be enumerated first.
Packit ae9e2a
 *
Packit ae9e2a
 * @param payload Extra parameter to callback function.
Packit ae9e2a
 *
Packit ae9e2a
 * @return 0 on success, non-zero callback return value, or error code.
Packit ae9e2a
 */
Packit ae9e2a
GIT_EXTERN(int) git_stash_foreach(
Packit ae9e2a
	git_repository *repo,
Packit ae9e2a
	git_stash_cb callback,
Packit ae9e2a
	void *payload);
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Remove a single stashed state from the stash list.
Packit ae9e2a
 *
Packit ae9e2a
 * @param repo The owning repository.
Packit ae9e2a
 *
Packit ae9e2a
 * @param index The position within the stash list. 0 points to the
Packit ae9e2a
 * most recent stashed state.
Packit ae9e2a
 *
Packit ae9e2a
 * @return 0 on success, GIT_ENOTFOUND if there's no stashed state for the given
Packit ae9e2a
 * index, or error code.
Packit ae9e2a
 */
Packit ae9e2a
GIT_EXTERN(int) git_stash_drop(
Packit ae9e2a
	git_repository *repo,
Packit ae9e2a
	size_t index);
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Apply a single stashed state from the stash list and remove it from the list
Packit ae9e2a
 * if successful.
Packit ae9e2a
 *
Packit ae9e2a
 * @param repo The owning repository.
Packit ae9e2a
 * @param index The position within the stash list. 0 points to the
Packit ae9e2a
 *              most recent stashed state.
Packit ae9e2a
 * @param options Optional options to control how stashes are applied.
Packit ae9e2a
 *
Packit ae9e2a
 * @return 0 on success, GIT_ENOTFOUND if there's no stashed state for the given
Packit ae9e2a
 * index, or error code. (see git_stash_apply() above for details)
Packit ae9e2a
*/
Packit ae9e2a
GIT_EXTERN(int) git_stash_pop(
Packit ae9e2a
	git_repository *repo,
Packit ae9e2a
	size_t index,
Packit ae9e2a
	const git_stash_apply_options *options);
Packit ae9e2a
Packit ae9e2a
/** @} */
Packit ae9e2a
GIT_END_DECL
Packit ae9e2a
#endif