]> git.proxmox.com Git - libgit2.git/blame - include/git2/stash.h
New upstream version 0.27.7+dfsg.1
[libgit2.git] / include / git2 / stash.h
CommitLineData
590fb68b 1/*
359fc2d2 2 * Copyright (C) the libgit2 contributors. All rights reserved.
590fb68b 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_stash_h__
8#define INCLUDE_git_stash_h__
9
10#include "common.h"
11#include "types.h"
12
13/**
14 * @file git2/stash.h
15 * @brief Git stash management routines
16 * @ingroup Git
17 * @{
18 */
19GIT_BEGIN_DECL
20
a295bd2d
CMN
21/**
22 * Stash flags
23 */
1d8ec670 24typedef enum {
a295bd2d
CMN
25 /**
26 * No option, default
27 */
590fb68b 28 GIT_STASH_DEFAULT = 0,
29
a295bd2d
CMN
30 /**
31 * All changes already added to the index are left intact in
32 * the working directory
590fb68b 33 */
34 GIT_STASH_KEEP_INDEX = (1 << 0),
35
a295bd2d
CMN
36 /**
37 * All untracked files are also stashed and then cleaned up
38 * from the working directory
590fb68b 39 */
40 GIT_STASH_INCLUDE_UNTRACKED = (1 << 1),
41
a295bd2d
CMN
42 /**
43 * All ignored files are also stashed and then cleaned up from
44 * the working directory
590fb68b 45 */
46 GIT_STASH_INCLUDE_IGNORED = (1 << 2),
1d8ec670 47} git_stash_flags;
590fb68b 48
49/**
50 * Save the local modifications to a new stash.
51 *
52 * @param out Object id of the commit containing the stashed state.
53 * This commit is also the target of the direct reference refs/stash.
54 *
55 * @param repo The owning repository.
56 *
57 * @param stasher The identity of the person performing the stashing.
58 *
59 * @param message Optional description along with the stashed state.
60 *
1d8ec670 61 * @param flags Flags to control the stashing process. (see GIT_STASH_* above)
590fb68b 62 *
63 * @return 0 on success, GIT_ENOTFOUND where there's nothing to stash,
64 * or error code.
65 */
590fb68b 66GIT_EXTERN(int) git_stash_save(
67 git_oid *out,
68 git_repository *repo,
2274993b 69 const git_signature *stasher,
590fb68b 70 const char *message,
95746a57 71 uint32_t flags);
590fb68b 72
19c80a6f 73/** Stash application flags. */
bf8dd3f5 74typedef enum {
19c80a6f 75 GIT_STASH_APPLY_DEFAULT = 0,
bf8dd3f5
POL
76
77 /* Try to reinstate not only the working tree's changes,
19c80a6f 78 * but also the index's changes.
bf8dd3f5 79 */
19c80a6f
ET
80 GIT_STASH_APPLY_REINSTATE_INDEX = (1 << 0),
81} git_stash_apply_flags;
82
4ea3eebf
ET
83typedef enum {
84 GIT_STASH_APPLY_PROGRESS_NONE = 0,
85
86 /** Loading the stashed data from the object database. */
87 GIT_STASH_APPLY_PROGRESS_LOADING_STASH,
88
89 /** The stored index is being analyzed. */
90 GIT_STASH_APPLY_PROGRESS_ANALYZE_INDEX,
91
92 /** The modified files are being analyzed. */
93 GIT_STASH_APPLY_PROGRESS_ANALYZE_MODIFIED,
94
95 /** The untracked and ignored files are being analyzed. */
96 GIT_STASH_APPLY_PROGRESS_ANALYZE_UNTRACKED,
97
98 /** The untracked files are being written to disk. */
99 GIT_STASH_APPLY_PROGRESS_CHECKOUT_UNTRACKED,
100
101 /** The modified files are being written to disk. */
102 GIT_STASH_APPLY_PROGRESS_CHECKOUT_MODIFIED,
103
104 /** The stash was applied successfully. */
105 GIT_STASH_APPLY_PROGRESS_DONE,
106} git_stash_apply_progress_t;
107
108/**
109 * Stash application progress notification function.
110 * Return 0 to continue processing, or a negative value to
111 * abort the stash application.
112 */
113typedef int (*git_stash_apply_progress_cb)(
114 git_stash_apply_progress_t progress,
115 void *payload);
116
19c80a6f
ET
117/** Stash application options structure.
118 *
119 * Initialize with the `GIT_STASH_APPLY_OPTIONS_INIT` macro to set
120 * sensible defaults; for example:
121 *
122 * git_stash_apply_options opts = GIT_STASH_APPLY_OPTIONS_INIT;
123 */
124typedef struct git_stash_apply_options {
125 unsigned int version;
126
127 /** See `git_stash_apply_flags_t`, above. */
128 git_stash_apply_flags flags;
129
130 /** Options to use when writing files to the working directory. */
131 git_checkout_options checkout_options;
4ea3eebf
ET
132
133 /** Optional callback to notify the consumer of application progress. */
134 git_stash_apply_progress_cb progress_cb;
135 void *progress_payload;
19c80a6f
ET
136} git_stash_apply_options;
137
138#define GIT_STASH_APPLY_OPTIONS_VERSION 1
139#define GIT_STASH_APPLY_OPTIONS_INIT { \
140 GIT_STASH_APPLY_OPTIONS_VERSION, \
141 GIT_STASH_APPLY_DEFAULT, \
142 GIT_CHECKOUT_OPTIONS_INIT }
143
144/**
145 * Initializes a `git_stash_apply_options` with default values. Equivalent to
146 * creating an instance with GIT_STASH_APPLY_OPTIONS_INIT.
147 *
148 * @param opts the `git_stash_apply_options` instance to initialize.
149 * @param version the version of the struct; you should pass
150 * `GIT_STASH_APPLY_OPTIONS_INIT` here.
151 * @return Zero on success; -1 on failure.
152 */
d02720d8 153GIT_EXTERN(int) git_stash_apply_init_options(
19c80a6f 154 git_stash_apply_options *opts, unsigned int version);
bf8dd3f5
POL
155
156/**
157 * Apply a single stashed state from the stash list.
158 *
958950b6
ET
159 * If local changes in the working directory conflict with changes in the
160 * stash then GIT_EMERGECONFLICT will be returned. In this case, the index
161 * will always remain unmodified and all files in the working directory will
162 * remain unmodified. However, if you are restoring untracked files or
163 * ignored files and there is a conflict when applying the modified files,
164 * then those files will remain in the working directory.
bf8dd3f5 165 *
19c80a6f
ET
166 * If passing the GIT_STASH_APPLY_REINSTATE_INDEX flag and there would be
167 * conflicts when reinstating the index, the function will return
168 * GIT_EMERGECONFLICT and both the working directory and index will be left
169 * unmodified.
170 *
171 * Note that a minimum checkout strategy of `GIT_CHECKOUT_SAFE` is implied.
bf8dd3f5
POL
172 *
173 * @param repo The owning repository.
bf8dd3f5 174 * @param index The position within the stash list. 0 points to the
f0957589 175 * most recent stashed state.
93e2c744 176 * @param options Optional options to control how stashes are applied.
bf8dd3f5 177 *
958950b6
ET
178 * @return 0 on success, GIT_ENOTFOUND if there's no stashed state for the
179 * given index, GIT_EMERGECONFLICT if changes exist in the working
180 * directory, or an error code
bf8dd3f5
POL
181 */
182GIT_EXTERN(int) git_stash_apply(
183 git_repository *repo,
184 size_t index,
19c80a6f 185 const git_stash_apply_options *options);
bf8dd3f5 186
23388413 187/**
373cf6a9
RB
188 * This is a callback function you can provide to iterate over all the
189 * stashed states that will be invoked per entry.
23388413 190 *
191 * @param index The position within the stash list. 0 points to the
373cf6a9 192 * most recent stashed state.
23388413 193 * @param message The stash message.
1d8ec670 194 * @param stash_id The commit oid of the stashed state.
23388413 195 * @param payload Extra parameter to callback function.
bf8dd3f5 196 * @return 0 to continue iterating or non-zero to stop.
23388413 197 */
1d8ec670 198typedef int (*git_stash_cb)(
23388413 199 size_t index,
200 const char* message,
1d8ec670 201 const git_oid *stash_id,
23388413 202 void *payload);
203
204/**
205 * Loop over all the stashed states and issue a callback for each one.
206 *
207 * If the callback returns a non-zero value, this will stop looping.
208 *
209 * @param repo Repository where to find the stash.
210 *
373cf6a9
RB
211 * @param callback Callback to invoke per found stashed state. The most
212 * recent stash state will be enumerated first.
23388413 213 *
214 * @param payload Extra parameter to callback function.
215 *
bf8dd3f5 216 * @return 0 on success, non-zero callback return value, or error code.
23388413 217 */
218GIT_EXTERN(int) git_stash_foreach(
219 git_repository *repo,
1d8ec670 220 git_stash_cb callback,
23388413 221 void *payload);
222
e4c64cf2 223/**
224 * Remove a single stashed state from the stash list.
225 *
226 * @param repo The owning repository.
227 *
228 * @param index The position within the stash list. 0 points to the
229 * most recent stashed state.
230 *
bf8dd3f5
POL
231 * @return 0 on success, GIT_ENOTFOUND if there's no stashed state for the given
232 * index, or error code.
e4c64cf2 233 */
e4c64cf2 234GIT_EXTERN(int) git_stash_drop(
235 git_repository *repo,
236 size_t index);
237
bf8dd3f5
POL
238/**
239 * Apply a single stashed state from the stash list and remove it from the list
240 * if successful.
241 *
242 * @param repo The owning repository.
bf8dd3f5 243 * @param index The position within the stash list. 0 points to the
f0957589 244 * most recent stashed state.
93e2c744 245 * @param options Optional options to control how stashes are applied.
bf8dd3f5
POL
246 *
247 * @return 0 on success, GIT_ENOTFOUND if there's no stashed state for the given
248 * index, or error code. (see git_stash_apply() above for details)
249*/
250GIT_EXTERN(int) git_stash_pop(
251 git_repository *repo,
252 size_t index,
19c80a6f 253 const git_stash_apply_options *options);
bf8dd3f5 254
590fb68b 255/** @} */
256GIT_END_DECL
257#endif