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