1 /*
2  * Copyright (C) the libgit2 contributors. All rights reserved.
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 /**
8  * License: GPL-2.0(Linking Exception)
9  */
10 module libgit2.stash;
11 
12 
13 private static import libgit2.checkout;
14 private static import libgit2.oid;
15 private static import libgit2.strarray;
16 private static import libgit2.types;
17 private import libgit2.common: GIT_EXTERN;
18 
19 /*
20  * @file git2/stash.h
21  * @brief Git stash management routines
22  * @ingroup Git
23  * @{
24  */
25 extern (C):
26 nothrow @nogc:
27 public:
28 
29 /**
30  * Stash flags
31  */
32 enum git_stash_flags
33 {
34 	/**
35 	 * No option, default
36 	 */
37 	GIT_STASH_DEFAULT = 0,
38 
39 	/**
40 	 * All changes already added to the index are left intact in
41 	 * the working directory
42 	 */
43 	GIT_STASH_KEEP_INDEX = 1 << 0,
44 
45 	/**
46 	 * All untracked files are also stashed and then cleaned up
47 	 * from the working directory
48 	 */
49 	GIT_STASH_INCLUDE_UNTRACKED = 1 << 1,
50 
51 	/**
52 	 * All ignored files are also stashed and then cleaned up from
53 	 * the working directory
54 	 */
55 	GIT_STASH_INCLUDE_IGNORED = 1 << 2,
56 
57 	/**
58 	 * All changes in the index and working directory are left intact
59 	 */
60 	GIT_STASH_KEEP_ALL = 1 << 3,
61 }
62 
63 //Declaration name in C language
64 enum
65 {
66 	GIT_STASH_DEFAULT = .git_stash_flags.GIT_STASH_DEFAULT,
67 	GIT_STASH_KEEP_INDEX = .git_stash_flags.GIT_STASH_KEEP_INDEX,
68 	GIT_STASH_INCLUDE_UNTRACKED = .git_stash_flags.GIT_STASH_INCLUDE_UNTRACKED,
69 	GIT_STASH_INCLUDE_IGNORED = .git_stash_flags.GIT_STASH_INCLUDE_IGNORED,
70 	GIT_STASH_KEEP_ALL = .git_stash_flags.GIT_STASH_KEEP_ALL,
71 }
72 
73 /**
74  * Save the local modifications to a new stash.
75  *
76  * Params:
77  *      out_ = Object id of the commit containing the stashed state. This commit is also the target of the direct reference refs/stash.
78  *      repo = The owning repository.
79  *      stasher = The identity of the person performing the stashing.
80  *      message = Optional description along with the stashed state.
81  *      flags = Flags to control the stashing process. (see GIT_STASH_* above)
82  *
83  * Returns: 0 on success, git_error_code.GIT_ENOTFOUND where there's nothing to stash, or error code.
84  */
85 @GIT_EXTERN
86 int git_stash_save(libgit2.oid.git_oid* out_, libgit2.types.git_repository* repo, const (libgit2.types.git_signature)* stasher, const (char)* message, uint flags);
87 
88 /**
89  * Stash save options structure
90  *
91  * Initialize with `GIT_STASH_SAVE_OPTIONS_INIT`. Alternatively, you can
92  * use `git_stash_save_options_init`.
93  *
94  */
95 struct git_stash_save_options
96 {
97 	uint version_;
98 
99 	/**
100 	 * Flags to control the stashing process. (see GIT_STASH_* above)
101 	 */
102 	uint flags;
103 
104 	/**
105 	 * The identity of the person performing the stashing.
106 	 */
107 	const (libgit2.types.git_signature)* stasher;
108 
109 	/**
110 	 * Optional description along with the stashed state.
111 	 */
112 	const (char)* message;
113 
114 	/**
115 	 * Optional paths that control which files are stashed.
116 	 */
117 	libgit2.strarray.git_strarray paths;
118 }
119 
120 enum GIT_STASH_SAVE_OPTIONS_VERSION = 1;
121 
122 pragma(inline, true)
123 pure nothrow @safe @nogc @live
124 .git_stash_save_options GIT_STASH_SAVE_OPTIONS_INIT()
125 
126 	do
127 	{
128 		.git_stash_save_options OUTPUT =
129 		{
130 			version_: .GIT_STASH_SAVE_OPTIONS_VERSION,
131 		};
132 
133 		return OUTPUT;
134 	}
135 
136 /**
137  * Initialize git_stash_save_options structure
138  *
139  * Initializes a `git_stash_save_options` with default values. Equivalent to
140  * creating an instance with `GIT_STASH_SAVE_OPTIONS_INIT`.
141  *
142  * @param opts The `git_stash_save_options` struct to initialize.
143  * @param version_ The struct version; pass `GIT_STASH_SAVE_OPTIONS_VERSION`.
144  * @return Zero on success; -1 on failure.
145  */
146 @GIT_EXTERN
147 int git_stash_save_options_init(.git_stash_save_options* opts, uint version_);
148 
149 /**
150  * Save the local modifications to a new stash, with options.
151  *
152  * @param out_ Object id of the commit containing the stashed state. This commit is also the target of the direct reference refs/stash.
153  * @param repo The owning repository.
154  * @param opts The stash options.
155  * @return 0 on success, GIT_ENOTFOUND where there's nothing to stash, or error code.
156  */
157 @GIT_EXTERN
158 int git_stash_save_with_opts(libgit2.oid.git_oid* out_, libgit2.types.git_repository* repo, const (.git_stash_save_options)* opts);
159 
160 /**
161  * Stash application flags.
162  */
163 enum git_stash_apply_flags
164 {
165 	GIT_STASH_APPLY_DEFAULT = 0,
166 
167 	/**
168 	 * Try to reinstate not only the working tree's changes,
169 	 * but also the index's changes.
170 	 */
171 	GIT_STASH_APPLY_REINSTATE_INDEX = 1 << 0,
172 }
173 
174 //Declaration name in C language
175 enum
176 {
177 	GIT_STASH_APPLY_DEFAULT = .git_stash_apply_flags.GIT_STASH_APPLY_DEFAULT,
178 	GIT_STASH_APPLY_REINSTATE_INDEX = .git_stash_apply_flags.GIT_STASH_APPLY_REINSTATE_INDEX,
179 }
180 
181 /**
182  * Stash apply progression states
183  */
184 enum git_stash_apply_progress_t
185 {
186 	GIT_STASH_APPLY_PROGRESS_NONE = 0,
187 
188 	/**
189 	 * Loading the stashed data from the object database.
190 	 */
191 	GIT_STASH_APPLY_PROGRESS_LOADING_STASH,
192 
193 	/**
194 	 * The stored index is being analyzed.
195 	 */
196 	GIT_STASH_APPLY_PROGRESS_ANALYZE_INDEX,
197 
198 	/**
199 	 * The modified files are being analyzed.
200 	 */
201 	GIT_STASH_APPLY_PROGRESS_ANALYZE_MODIFIED,
202 
203 	/**
204 	 * The untracked and ignored files are being analyzed.
205 	 */
206 	GIT_STASH_APPLY_PROGRESS_ANALYZE_UNTRACKED,
207 
208 	/**
209 	 * The untracked files are being written to disk.
210 	 */
211 	GIT_STASH_APPLY_PROGRESS_CHECKOUT_UNTRACKED,
212 
213 	/**
214 	 * The modified files are being written to disk.
215 	 */
216 	GIT_STASH_APPLY_PROGRESS_CHECKOUT_MODIFIED,
217 
218 	/**
219 	 * The stash was applied successfully.
220 	 */
221 	GIT_STASH_APPLY_PROGRESS_DONE,
222 }
223 
224 //Declaration name in C language
225 enum
226 {
227 	GIT_STASH_APPLY_PROGRESS_NONE = .git_stash_apply_progress_t.GIT_STASH_APPLY_PROGRESS_NONE,
228 	GIT_STASH_APPLY_PROGRESS_LOADING_STASH = .git_stash_apply_progress_t.GIT_STASH_APPLY_PROGRESS_LOADING_STASH,
229 	GIT_STASH_APPLY_PROGRESS_ANALYZE_INDEX = .git_stash_apply_progress_t.GIT_STASH_APPLY_PROGRESS_ANALYZE_INDEX,
230 	GIT_STASH_APPLY_PROGRESS_ANALYZE_MODIFIED = .git_stash_apply_progress_t.GIT_STASH_APPLY_PROGRESS_ANALYZE_MODIFIED,
231 	GIT_STASH_APPLY_PROGRESS_ANALYZE_UNTRACKED = .git_stash_apply_progress_t.GIT_STASH_APPLY_PROGRESS_ANALYZE_UNTRACKED,
232 	GIT_STASH_APPLY_PROGRESS_CHECKOUT_UNTRACKED = .git_stash_apply_progress_t.GIT_STASH_APPLY_PROGRESS_CHECKOUT_UNTRACKED,
233 	GIT_STASH_APPLY_PROGRESS_CHECKOUT_MODIFIED = .git_stash_apply_progress_t.GIT_STASH_APPLY_PROGRESS_CHECKOUT_MODIFIED,
234 	GIT_STASH_APPLY_PROGRESS_DONE = .git_stash_apply_progress_t.GIT_STASH_APPLY_PROGRESS_DONE,
235 }
236 
237 /**
238  * Stash application progress notification function.
239  * Return 0 to continue processing, or a negative value to
240  * abort the stash application.
241  */
242 alias git_stash_apply_progress_cb = int function(.git_stash_apply_progress_t progress, void* payload);
243 
244 /**
245  * Stash application options structure
246  *
247  * Initialize with `GIT_STASH_APPLY_OPTIONS_INIT`. Alternatively, you can
248  * use `git_stash_apply_options_init`.
249  */
250 struct git_stash_apply_options
251 {
252 	uint version_;
253 
254 	/**
255 	 * See `git_stash_apply_flags`, above.
256 	 */
257 	uint flags;
258 
259 	/**
260 	 * Options to use when writing files to the working directory.
261 	 */
262 	libgit2.checkout.git_checkout_options checkout_options;
263 
264 	/**
265 	 * Optional callback to notify the consumer of application progress.
266 	 */
267 	.git_stash_apply_progress_cb progress_cb;
268 
269 	void* progress_payload;
270 }
271 
272 enum GIT_STASH_APPLY_OPTIONS_VERSION = 1;
273 
274 pragma(inline, true)
275 pure nothrow @safe @nogc @live
276 .git_stash_apply_options GIT_STASH_APPLY_OPTIONS_INIT()
277 
278 	do
279 	{
280 		.git_stash_apply_options OUTPUT =
281 		{
282 			version_: .GIT_STASH_APPLY_OPTIONS_VERSION,
283 			flags: .git_stash_apply_flags.GIT_STASH_APPLY_DEFAULT,
284 			checkout_options: libgit2.checkout.GIT_CHECKOUT_OPTIONS_INIT(),
285 		};
286 
287 		return OUTPUT;
288 	}
289 
290 /**
291  * Initialize git_stash_apply_options structure
292  *
293  * Initializes a `git_stash_apply_options` with default values. Equivalent to
294  * creating an instance with `GIT_STASH_APPLY_OPTIONS_INIT`.
295  *
296  * Params:
297  *      opts = The `git_stash_apply_options` struct to initialize.
298  *      version_ = The struct version; pass `GIT_STASH_APPLY_OPTIONS_VERSION`.
299  *
300  * Returns: Zero on success; -1 on failure.
301  */
302 @GIT_EXTERN
303 int git_stash_apply_options_init(.git_stash_apply_options* opts, uint version_);
304 
305 /**
306  * Apply a single stashed state from the stash list.
307  *
308  * If local changes in the working directory conflict with changes in the
309  * stash then git_error_code.GIT_EMERGECONFLICT will be returned.  In this case, the index
310  * will always remain unmodified and all files in the working directory will
311  * remain unmodified.  However, if you are restoring untracked files or
312  * ignored files and there is a conflict when applying the modified files,
313  * then those files will remain in the working directory.
314  *
315  * If passing the git_stash_apply_flags.GIT_STASH_APPLY_REINSTATE_INDEX flag and there would be
316  * conflicts when reinstating the index, the function will return
317  * git_error_code.GIT_EMERGECONFLICT and both the working directory and index will be left
318  * unmodified.
319  *
320  * Note that a minimum checkout strategy of `git_checkout_strategy_t.GIT_CHECKOUT_SAFE` is implied.
321  *
322  * Params:
323  *      repo = The owning repository.
324  *      index = The position within the stash list. 0 points to the most recent stashed state.
325  *      options = Optional options to control how stashes are applied.
326  *
327  * Returns: 0 on success, git_error_code.GIT_ENOTFOUND if there's no stashed state for the given index, git_error_code.GIT_EMERGECONFLICT if changes exist in the working directory, or an error code
328  */
329 @GIT_EXTERN
330 int git_stash_apply(libgit2.types.git_repository* repo, size_t index, const (.git_stash_apply_options)* options);
331 
332 /**
333  * This is a callback function you can provide to iterate over all the
334  * stashed states that will be invoked per entry.
335  *
336  * Returns: 0 to continue iterating or non-zero to stop.
337  */
338 /*
339  * Params:
340  *      index = The position within the stash list. 0 points to the most recent stashed state.
341  *      message = The stash message.
342  *      stash_id = The commit oid of the stashed state.
343  *      payload = Extra parameter to callback function.
344  */
345 alias git_stash_cb = int function(size_t index, const (char)* message, const (libgit2.oid.git_oid)* stash_id, void* payload);
346 
347 /**
348  * Loop over all the stashed states and issue a callback for each one.
349  *
350  * If the callback returns a non-zero value, this will stop looping.
351  *
352  * Params:
353  *      repo = Repository where to find the stash.
354  *      callback = Callback to invoke per found stashed state. The most recent stash state will be enumerated first.
355  *      payload = Extra parameter to callback function.
356  *
357  * Returns: 0 on success, non-zero callback return value, or error code.
358  */
359 @GIT_EXTERN
360 int git_stash_foreach(libgit2.types.git_repository* repo, .git_stash_cb callback, void* payload);
361 
362 /**
363  * Remove a single stashed state from the stash list.
364  *
365  * Params:
366  *      repo = The owning repository.
367  *      index = The position within the stash list. 0 points to the most recent stashed state.
368  *
369  * Returns: 0 on success, git_error_code.GIT_ENOTFOUND if there's no stashed state for the given index, or error code.
370  */
371 @GIT_EXTERN
372 int git_stash_drop(libgit2.types.git_repository* repo, size_t index);
373 
374 /**
375  * Apply a single stashed state from the stash list and remove it from the list
376  * if successful.
377  *
378  * Params:
379  *      repo = The owning repository.
380  *      index = The position within the stash list. 0 points to the most recent stashed state.
381  *      options = Optional options to control how stashes are applied.
382  *
383  * Returns: 0 on success, git_error_code.GIT_ENOTFOUND if there's no stashed state for the given index, or error code. (see git_stash_apply() above for details)
384  */
385 @GIT_EXTERN
386 int git_stash_pop(libgit2.types.git_repository* repo, size_t index, const (.git_stash_apply_options)* options);
387 
388 /* @} */