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.worktree;
8 
9 
10 private static import libgit2_d.buffer;
11 private static import libgit2_d.strarray;
12 private static import libgit2_d.types;
13 
14 /**
15  * @file git2/worktrees.h
16  * @brief Git worktree related functions
17  * @defgroup git_commit Git worktree related functions
18  * @ingroup Git
19  * @{
20  */
21 extern (C):
22 nothrow @nogc:
23 public:
24 
25 /**
26  * List names of linked working trees
27  *
28  * The returned list should be released with `git_strarray_free`
29  * when no longer needed.
30  *
31  * Params:
32  *      out_ = pointer to the array of working tree names
33  *      repo = the repo to use when listing working trees
34  *
35  * Returns: 0 or an error code
36  */
37 //GIT_EXTERN
38 int git_worktree_list(libgit2_d.strarray.git_strarray* out_, libgit2_d.types.git_repository* repo);
39 
40 /**
41  * Lookup a working tree by its name for a given repository
42  *
43  * Params:
44  *      out_ = Output pointer to looked up worktree or `null`
45  *      repo = The repository containing worktrees
46  *      name = Name of the working tree to look up
47  *
48  * Returns: 0 or an error code
49  */
50 //GIT_EXTERN
51 int git_worktree_lookup(libgit2_d.types.git_worktree** out_, libgit2_d.types.git_repository* repo, const (char)* name);
52 
53 /**
54  * Open a worktree of a given repository
55  *
56  * If a repository is not the main tree but a worktree, this
57  * function will look up the worktree inside the parent
58  * repository and create a new `git_worktree` structure.
59  *
60  * Params:
61  *      out_ = Out-pointer for the newly allocated worktree
62  *      repo = Repository to look up worktree for
63  */
64 //GIT_EXTERN
65 int git_worktree_open_from_repository(libgit2_d.types.git_worktree** out_, libgit2_d.types.git_repository* repo);
66 
67 /**
68  * Free a previously allocated worktree
69  *
70  * Params:
71  *      wt = worktree handle to close. If null nothing occurs.
72  */
73 //GIT_EXTERN
74 void git_worktree_free(libgit2_d.types.git_worktree* wt);
75 
76 /**
77  * Check if worktree is valid
78  *
79  * A valid worktree requires both the git data structures inside
80  * the linked parent repository and the linked working copy to be
81  * present.
82  *
83  * Params:
84  *      wt = Worktree to check
85  *
86  * Returns: 0 when worktree is valid, error-code otherwise
87  */
88 //GIT_EXTERN
89 int git_worktree_validate(const (libgit2_d.types.git_worktree)* wt);
90 
91 /**
92  * Worktree add options structure
93  *
94  * Initialize with `GIT_WORKTREE_ADD_OPTIONS_INIT`. Alternatively, you can
95  * use `git_worktree_add_options_init`.
96  */
97 struct git_worktree_add_options
98 {
99 	uint version_;
100 
101 	/**
102 	 * lock newly created worktree
103 	 */
104 	int lock;
105 
106 	/**
107 	 * reference to use for the new worktree HEAD
108 	 */
109 	libgit2_d.types.git_reference* ref_;
110 }
111 
112 enum GIT_WORKTREE_ADD_OPTIONS_VERSION = 1;
113 
114 pragma(inline, true)
115 pure nothrow @safe @nogc
116 .git_worktree_add_options GIT_WORKTREE_ADD_OPTIONS_INIT()
117 
118 	do
119 	{
120 		.git_worktree_add_options OUTPUT =
121 		{
122 			version_: .GIT_WORKTREE_ADD_OPTIONS_VERSION,
123 			lock: 0,
124 			ref_: null,
125 		};
126 
127 		return OUTPUT;
128 	}
129 
130 /**
131  * Initialize git_worktree_add_options structure
132  *
133  * Initializes a `git_worktree_add_options` with default values. Equivalent to
134  * creating an instance with `GIT_WORKTREE_ADD_OPTIONS_INIT`.
135  *
136  * Params:
137  *      opts = The `git_worktree_add_options` struct to initialize.
138  *      version = The struct version; pass `GIT_WORKTREE_ADD_OPTIONS_VERSION`.
139  *
140  * Returns: Zero on success; -1 on failure.
141  */
142 //GIT_EXTERN
143 int git_worktree_add_options_init(.git_worktree_add_options* opts, uint version_);
144 
145 /**
146  * Add a new working tree
147  *
148  * Add a new working tree for the repository, that is create the
149  * required data structures inside the repository and check out
150  * the current HEAD at `path`
151  *
152  * Params:
153  *      out_ = Output pointer containing new working tree
154  *      repo = Repository to create working tree for
155  *      name = Name of the working tree
156  *      path = Path to create working tree at
157  *      opts = Options to modify default behavior. May be null
158  *
159  * Returns: 0 or an error code
160  */
161 //GIT_EXTERN
162 int git_worktree_add(libgit2_d.types.git_worktree** out_, libgit2_d.types.git_repository* repo, const (char)* name, const (char)* path, const (.git_worktree_add_options)* opts);
163 
164 /**
165  * Lock worktree if not already locked
166  *
167  * Lock a worktree, optionally specifying a reason why the linked
168  * working tree is being locked.
169  *
170  * Params:
171  *      wt = Worktree to lock
172  *      reason = Reason why the working tree is being locked
173  *
174  * Returns: 0 on success, non-zero otherwise
175  */
176 //GIT_EXTERN
177 int git_worktree_lock(libgit2_d.types.git_worktree* wt, const (char)* reason);
178 
179 /**
180  * Unlock a locked worktree
181  *
182  * Params:
183  *      wt = Worktree to unlock
184  *
185  * Returns: 0 on success, 1 if worktree was not locked, error-code otherwise
186  */
187 //GIT_EXTERN
188 int git_worktree_unlock(libgit2_d.types.git_worktree* wt);
189 
190 /**
191  * Check if worktree is locked
192  *
193  * A worktree may be locked if the linked working tree is stored
194  * on a portable device which is not available.
195  *
196  * Params:
197  *      reason = Buffer to store reason in. If null no reason is stored.
198  *      wt = Worktree to check
199  *
200  * Returns: 0 when the working tree not locked, a value greater than zero if it is locked, less than zero if there was an error
201  */
202 //GIT_EXTERN
203 int git_worktree_is_locked(libgit2_d.buffer.git_buf* reason, const (libgit2_d.types.git_worktree)* wt);
204 
205 /**
206  * Retrieve the name of the worktree
207  *
208  * Params:
209  *      wt = Worktree to get the name for
210  *
211  * Returns: The worktree's name. The pointer returned is valid for the lifetime of the git_worktree
212  */
213 //GIT_EXTERN
214 const (char)* git_worktree_name(const (libgit2_d.types.git_worktree)* wt);
215 
216 /**
217  * Retrieve the filesystem path for the worktree
218  *
219  * Params:
220  *      wt = Worktree to get the path for
221  *
222  * Returns: The worktree's filesystem path. The pointer returned is valid for the lifetime of the git_worktree.
223  */
224 //GIT_EXTERN
225 const (char)* git_worktree_path(const (libgit2_d.types.git_worktree)* wt);
226 
227 /**
228  * Flags which can be passed to git_worktree_prune to alter its
229  * behavior.
230  */
231 enum git_worktree_prune_t
232 {
233 	/**
234 	 * Prune working tree even if working tree is valid
235 	 */
236 	GIT_WORKTREE_PRUNE_VALID = 1u << 0,
237 
238 	/**
239 	 * Prune working tree even if it is locked
240 	 */
241 	GIT_WORKTREE_PRUNE_LOCKED = 1u << 1,
242 
243 	/**
244 	 * Prune checked out working tree
245 	 */
246 	GIT_WORKTREE_PRUNE_WORKING_TREE = 1u << 2,
247 }
248 
249 /**
250  * Worktree prune options structure
251  *
252  * Initialize with `GIT_WORKTREE_PRUNE_OPTIONS_INIT`. Alternatively, you can
253  * use `git_worktree_prune_options_init`.
254  */
255 struct git_worktree_prune_options
256 {
257 	uint version_;
258 
259 	uint flags;
260 }
261 
262 enum GIT_WORKTREE_PRUNE_OPTIONS_VERSION = 1;
263 
264 pragma(inline, true)
265 pure nothrow @safe @nogc
266 .git_worktree_prune_options GIT_WORKTREE_PRUNE_OPTIONS_INIT()
267 
268 	do
269 	{
270 		.git_worktree_prune_options OUTPUT =
271 		{
272 			version_: .GIT_WORKTREE_PRUNE_OPTIONS_VERSION,
273 			flags: 0,
274 		};
275 
276 		return OUTPUT;
277 	}
278 
279 /**
280  * Initialize git_worktree_prune_options structure
281  *
282  * Initializes a `git_worktree_prune_options` with default values. Equivalent to
283  * creating an instance with `GIT_WORKTREE_PRUNE_OPTIONS_INIT`.
284  *
285  * Params:
286  *      opts = The `git_worktree_prune_options` struct to initialize.
287  *      version = The struct version; pass `GIT_WORKTREE_PRUNE_OPTIONS_VERSION`.
288  *
289  * Returns: Zero on success; -1 on failure.
290  */
291 //GIT_EXTERN
292 int git_worktree_prune_options_init(.git_worktree_prune_options* opts, uint version_);
293 
294 /**
295  * Is the worktree prunable with the given options?
296  *
297  * A worktree is not prunable in the following scenarios:
298  *
299  * - the worktree is linking to a valid on-disk worktree. The
300  *   `valid` member will cause this check to be ignored.
301  * - the worktree is locked. The `locked` flag will cause this
302  *   check to be ignored.
303  *
304  * If the worktree is not valid and not locked or if the above
305  * flags have been passed in, this function will return a
306  * positive value.
307  */
308 //GIT_EXTERN
309 int git_worktree_is_prunable(libgit2_d.types.git_worktree* wt, .git_worktree_prune_options* opts);
310 
311 /**
312  * Prune working tree
313  *
314  * Prune the working tree, that is remove the git data
315  * structures on disk. The repository will only be pruned of
316  * `git_worktree_is_prunable` succeeds.
317  *
318  * Params:
319  *      wt = Worktree to prune
320  *      opts = Specifies which checks to override. See `git_worktree_is_prunable`. May be null
321  *
322  * Returns: 0 or an error code
323  */
324 //GIT_EXTERN
325 int git_worktree_prune(libgit2_d.types.git_worktree* wt, .git_worktree_prune_options* opts);
326 
327 /** @} */