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