From a7aa73a535a7f21357e9c7dece8376f30a29681f Mon Sep 17 00:00:00 2001 From: Patrick Steinhardt Date: Tue, 2 May 2017 10:02:36 +0200 Subject: worktree: introduce git_worktree_add options The `git_worktree_add` function currently accepts only a path and name for the new work tree. As we may want to expand these parameters in future versions without adding additional parameters to the function for every option, this commit introduces our typical pattern of an options struct. Right now, this structure is still empty, which will change with the next commit. --- include/git2/worktree.h | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) (limited to 'include/git2') diff --git a/include/git2/worktree.h b/include/git2/worktree.h index 4c4f9284d..bc1e40243 100644 --- a/include/git2/worktree.h +++ b/include/git2/worktree.h @@ -74,6 +74,25 @@ GIT_EXTERN(void) git_worktree_free(git_worktree *wt); */ GIT_EXTERN(int) git_worktree_validate(const git_worktree *wt); +typedef struct git_worktree_add_options { + unsigned int version; +} git_worktree_add_options; + +#define GIT_WORKTREE_ADD_OPTIONS_VERSION 1 +#define GIT_WORKTREE_ADD_OPTIONS_INIT {GIT_WORKTREE_ADD_OPTIONS_VERSION} + +/** + * Initializes a `git_worktree_add_options` with default vaules. + * Equivalent to creating an instance with + * GIT_WORKTREE_ADD_OPTIONS_INIT. + * + * @param opts the struct to initialize + * @param version Verison of struct; pass `GIT_WORKTREE_ADD_OPTIONS_VERSION` + * @return Zero on success; -1 on failure. + */ +int git_worktree_add_init_options(git_worktree_add_options *opts, + unsigned int version); + /** * Add a new working tree * @@ -85,9 +104,12 @@ GIT_EXTERN(int) git_worktree_validate(const git_worktree *wt); * @param repo Repository to create working tree for * @param name Name of the working tree * @param path Path to create working tree at + * @param opts Options to modify default behavior. May be NULL * @return 0 or an error code */ -GIT_EXTERN(int) git_worktree_add(git_worktree **out, git_repository *repo, const char *name, const char *path); +GIT_EXTERN(int) git_worktree_add(git_worktree **out, git_repository *repo, + const char *name, const char *path, + const git_worktree_add_options *opts); /** * Lock worktree if not already locked -- cgit v1.2.1 From 8264a30f4fbfce96f433f01fa6fe537e5cb3570d Mon Sep 17 00:00:00 2001 From: Patrick Steinhardt Date: Tue, 2 May 2017 10:11:28 +0200 Subject: worktree: support creating locked worktrees When creating a new worktree, we do have a potential race with us creating the worktree and another process trying to delete the same worktree as it is being created. As such, the upstream git project has introduced a flag `git worktree add --locked`, which will cause the newly created worktree to be locked immediately after its creation. This mitigates the race condition. We want to be able to mirror the same behavior. As such, a new flag `locked` is added to the options structure of `git_worktree_add` which allows the user to enable this behavior. --- include/git2/worktree.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'include/git2') diff --git a/include/git2/worktree.h b/include/git2/worktree.h index bc1e40243..84e2bc92e 100644 --- a/include/git2/worktree.h +++ b/include/git2/worktree.h @@ -76,10 +76,12 @@ GIT_EXTERN(int) git_worktree_validate(const git_worktree *wt); typedef struct git_worktree_add_options { unsigned int version; + + char lock; /**< lock newly created worktree */ } git_worktree_add_options; #define GIT_WORKTREE_ADD_OPTIONS_VERSION 1 -#define GIT_WORKTREE_ADD_OPTIONS_INIT {GIT_WORKTREE_ADD_OPTIONS_VERSION} +#define GIT_WORKTREE_ADD_OPTIONS_INIT {GIT_WORKTREE_ADD_OPTIONS_VERSION,0} /** * Initializes a `git_worktree_add_options` with default vaules. -- cgit v1.2.1 From 883eeb5f909f230822c0bc025d3f9ce79781de04 Mon Sep 17 00:00:00 2001 From: Patrick Steinhardt Date: Tue, 2 May 2017 12:35:59 +0200 Subject: worktree: switch over worktree pruning to an opts structure The current signature of `git_worktree_prune` accepts a flags field to alter its behavior. This is not as flexible as we'd like it to be when we want to enable passing additional options in the future. As the function has not been part of any release yet, we are still free to alter its current signature. This commit does so by using our usual pattern of an options structure, which is easily extendable without breaking the API. --- include/git2/worktree.h | 41 ++++++++++++++++++++++++++++++++--------- 1 file changed, 32 insertions(+), 9 deletions(-) (limited to 'include/git2') diff --git a/include/git2/worktree.h b/include/git2/worktree.h index 84e2bc92e..e3bafc3d0 100644 --- a/include/git2/worktree.h +++ b/include/git2/worktree.h @@ -161,23 +161,44 @@ typedef enum { GIT_WORKTREE_PRUNE_WORKING_TREE = 1u << 2, } git_worktree_prune_t; +typedef struct git_worktree_prune_options { + unsigned int version; + + uint32_t flags; +} git_worktree_prune_options; + +#define GIT_WORKTREE_PRUNE_OPTIONS_VERSION 1 +#define GIT_WORKTREE_PRUNE_OPTIONS_INIT {GIT_WORKTREE_PRUNE_OPTIONS_VERSION,0} + +/** + * Initializes a `git_worktree_prune_options` with default vaules. + * Equivalent to creating an instance with + * GIT_WORKTREE_PRUNE_OPTIONS_INIT. + * + * @param opts the struct to initialize + * @param version Verison of struct; pass `GIT_WORKTREE_PRUNE_OPTIONS_VERSION` + * @return Zero on success; -1 on failure. + */ +GIT_EXTERN(int) git_worktree_prune_init_options( + git_worktree_prune_options *opts, + unsigned int version); + /** - * Is the worktree prunable with the given set of flags? + * Is the worktree prunable with the given options? * * A worktree is not prunable in the following scenarios: * * - the worktree is linking to a valid on-disk worktree. The - * GIT_WORKTREE_PRUNE_VALID flag will cause this check to be - * ignored. - * - the worktree is not valid but locked. The - * GIT_WORKRTEE_PRUNE_LOCKED flag will cause this check to be - * ignored. + * `valid` member will cause this check to be ignored. + * - the worktree is locked. The `locked` flag will cause this + * check to be ignored. * * If the worktree is not valid and not locked or if the above * flags have been passed in, this function will return a * positive value. */ -GIT_EXTERN(int) git_worktree_is_prunable(git_worktree *wt, unsigned flags); +GIT_EXTERN(int) git_worktree_is_prunable(git_worktree *wt, + git_worktree_prune_options *opts); /** * Prune working tree @@ -187,10 +208,12 @@ GIT_EXTERN(int) git_worktree_is_prunable(git_worktree *wt, unsigned flags); * `git_worktree_is_prunable` succeeds. * * @param wt Worktree to prune - * @param flags git_worktree_prune_t flags + * @param opts Specifies which checks to override. See + * `git_worktree_is_prunable`. May be NULL * @return 0 or an error code */ -GIT_EXTERN(int) git_worktree_prune(git_worktree *wt, unsigned flags); +GIT_EXTERN(int) git_worktree_prune(git_worktree *wt, + git_worktree_prune_options *opts); /** @} */ GIT_END_DECL -- cgit v1.2.1 From f0848dd7e0d137939eb969af03bd75f9bf318286 Mon Sep 17 00:00:00 2001 From: Edward Thomson Date: Sun, 4 Jun 2017 22:44:30 +0100 Subject: worktree: upgrade lock to an int --- include/git2/worktree.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'include/git2') diff --git a/include/git2/worktree.h b/include/git2/worktree.h index e3bafc3d0..d3fa88e3f 100644 --- a/include/git2/worktree.h +++ b/include/git2/worktree.h @@ -77,7 +77,7 @@ GIT_EXTERN(int) git_worktree_validate(const git_worktree *wt); typedef struct git_worktree_add_options { unsigned int version; - char lock; /**< lock newly created worktree */ + int lock; /**< lock newly created worktree */ } git_worktree_add_options; #define GIT_WORKTREE_ADD_OPTIONS_VERSION 1 -- cgit v1.2.1