2 * Copyright (C) the libgit2 contributors. All rights reserved.
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.
7 #ifndef INCLUDE_git_worktree_h__
8 #define INCLUDE_git_worktree_h__
16 * @file git2/worktrees.h
17 * @brief Git worktree related functions
18 * @defgroup git_commit Git worktree related functions
25 * List names of linked working trees
27 * The returned list should be released with `git_strarray_free`
28 * when no longer needed.
30 * @param out pointer to the array of working tree names
31 * @param repo the repo to use when listing working trees
32 * @return 0 or an error code
34 GIT_EXTERN(int) git_worktree_list(git_strarray
*out
, git_repository
*repo
);
37 * Lookup a working tree by its name for a given repository
39 * @param out Output pointer to looked up worktree or `NULL`
40 * @param repo The repository containing worktrees
41 * @param name Name of the working tree to look up
42 * @return 0 or an error code
44 GIT_EXTERN(int) git_worktree_lookup(git_worktree
**out
, git_repository
*repo
, const char *name
);
47 * Open a worktree of a given repository
49 * If a repository is not the main tree but a worktree, this
50 * function will look up the worktree inside the parent
51 * repository and create a new `git_worktree` structure.
53 * @param out Out-pointer for the newly allocated worktree
54 * @param repo Repository to look up worktree for
55 * @return 0 or an error code
57 GIT_EXTERN(int) git_worktree_open_from_repository(git_worktree
**out
, git_repository
*repo
);
60 * Free a previously allocated worktree
62 * @param wt worktree handle to close. If NULL nothing occurs.
64 GIT_EXTERN(void) git_worktree_free(git_worktree
*wt
);
67 * Check if worktree is valid
69 * A valid worktree requires both the git data structures inside
70 * the linked parent repository and the linked working copy to be
73 * @param wt Worktree to check
74 * @return 0 when worktree is valid, error-code otherwise
76 GIT_EXTERN(int) git_worktree_validate(const git_worktree
*wt
);
79 * Worktree add options structure
81 * Initialize with `GIT_WORKTREE_ADD_OPTIONS_INIT`. Alternatively, you can
82 * use `git_worktree_add_options_init`.
85 typedef struct git_worktree_add_options
{
88 int lock
; /**< lock newly created worktree */
89 git_reference
*ref
; /**< reference to use for the new worktree HEAD */
92 * Options for the checkout.
94 git_checkout_options checkout_options
;
95 } git_worktree_add_options
;
97 #define GIT_WORKTREE_ADD_OPTIONS_VERSION 1
98 #define GIT_WORKTREE_ADD_OPTIONS_INIT {GIT_WORKTREE_ADD_OPTIONS_VERSION,0,NULL,GIT_CHECKOUT_OPTIONS_INIT}
101 * Initialize git_worktree_add_options structure
103 * Initializes a `git_worktree_add_options` with default values. Equivalent to
104 * creating an instance with `GIT_WORKTREE_ADD_OPTIONS_INIT`.
106 * @param opts The `git_worktree_add_options` struct to initialize.
107 * @param version The struct version; pass `GIT_WORKTREE_ADD_OPTIONS_VERSION`.
108 * @return Zero on success; -1 on failure.
110 GIT_EXTERN(int) git_worktree_add_options_init(git_worktree_add_options
*opts
,
111 unsigned int version
);
114 * Add a new working tree
116 * Add a new working tree for the repository, that is create the
117 * required data structures inside the repository and check out
118 * the current HEAD at `path`
120 * @param out Output pointer containing new working tree
121 * @param repo Repository to create working tree for
122 * @param name Name of the working tree
123 * @param path Path to create working tree at
124 * @param opts Options to modify default behavior. May be NULL
125 * @return 0 or an error code
127 GIT_EXTERN(int) git_worktree_add(git_worktree
**out
, git_repository
*repo
,
128 const char *name
, const char *path
,
129 const git_worktree_add_options
*opts
);
132 * Lock worktree if not already locked
134 * Lock a worktree, optionally specifying a reason why the linked
135 * working tree is being locked.
137 * @param wt Worktree to lock
138 * @param reason Reason why the working tree is being locked
139 * @return 0 on success, non-zero otherwise
141 GIT_EXTERN(int) git_worktree_lock(git_worktree
*wt
, const char *reason
);
144 * Unlock a locked worktree
146 * @param wt Worktree to unlock
147 * @return 0 on success, 1 if worktree was not locked, error-code
150 GIT_EXTERN(int) git_worktree_unlock(git_worktree
*wt
);
153 * Check if worktree is locked
155 * A worktree may be locked if the linked working tree is stored
156 * on a portable device which is not available.
158 * @param reason Buffer to store reason in. If NULL no reason is stored.
159 * @param wt Worktree to check
160 * @return 0 when the working tree not locked, a value greater
161 * than zero if it is locked, less than zero if there was an
164 GIT_EXTERN(int) git_worktree_is_locked(git_buf
*reason
, const git_worktree
*wt
);
167 * Retrieve the name of the worktree
169 * @param wt Worktree to get the name for
170 * @return The worktree's name. The pointer returned is valid for the
171 * lifetime of the git_worktree
173 GIT_EXTERN(const char *) git_worktree_name(const git_worktree
*wt
);
176 * Retrieve the filesystem path for the worktree
178 * @param wt Worktree to get the path for
179 * @return The worktree's filesystem path. The pointer returned
180 * is valid for the lifetime of the git_worktree.
182 GIT_EXTERN(const char *) git_worktree_path(const git_worktree
*wt
);
185 * Flags which can be passed to git_worktree_prune to alter its
189 /* Prune working tree even if working tree is valid */
190 GIT_WORKTREE_PRUNE_VALID
= 1u << 0,
191 /* Prune working tree even if it is locked */
192 GIT_WORKTREE_PRUNE_LOCKED
= 1u << 1,
193 /* Prune checked out working tree */
194 GIT_WORKTREE_PRUNE_WORKING_TREE
= 1u << 2
195 } git_worktree_prune_t
;
198 * Worktree prune options structure
200 * Initialize with `GIT_WORKTREE_PRUNE_OPTIONS_INIT`. Alternatively, you can
201 * use `git_worktree_prune_options_init`.
204 typedef struct git_worktree_prune_options
{
205 unsigned int version
;
207 /** A combination of `git_worktree_prune_t` */
209 } git_worktree_prune_options
;
211 #define GIT_WORKTREE_PRUNE_OPTIONS_VERSION 1
212 #define GIT_WORKTREE_PRUNE_OPTIONS_INIT {GIT_WORKTREE_PRUNE_OPTIONS_VERSION,0}
215 * Initialize git_worktree_prune_options structure
217 * Initializes a `git_worktree_prune_options` with default values. Equivalent to
218 * creating an instance with `GIT_WORKTREE_PRUNE_OPTIONS_INIT`.
220 * @param opts The `git_worktree_prune_options` struct to initialize.
221 * @param version The struct version; pass `GIT_WORKTREE_PRUNE_OPTIONS_VERSION`.
222 * @return Zero on success; -1 on failure.
224 GIT_EXTERN(int) git_worktree_prune_options_init(
225 git_worktree_prune_options
*opts
,
226 unsigned int version
);
229 * Is the worktree prunable with the given options?
231 * A worktree is not prunable in the following scenarios:
233 * - the worktree is linking to a valid on-disk worktree. The
234 * `valid` member will cause this check to be ignored.
235 * - the worktree is locked. The `locked` flag will cause this
236 * check to be ignored.
238 * If the worktree is not valid and not locked or if the above
239 * flags have been passed in, this function will return a
242 * @param wt Worktree to check.
243 * @param opts The prunable options.
244 * @return 1 if the worktree is prunable, 0 otherwise, or an error code.
246 GIT_EXTERN(int) git_worktree_is_prunable(git_worktree
*wt
,
247 git_worktree_prune_options
*opts
);
252 * Prune the working tree, that is remove the git data
253 * structures on disk. The repository will only be pruned of
254 * `git_worktree_is_prunable` succeeds.
256 * @param wt Worktree to prune
257 * @param opts Specifies which checks to override. See
258 * `git_worktree_is_prunable`. May be NULL
259 * @return 0 or an error code
261 GIT_EXTERN(int) git_worktree_prune(git_worktree
*wt
,
262 git_worktree_prune_options
*opts
);