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