summaryrefslogtreecommitdiff
path: root/src/worktree.c
diff options
context:
space:
mode:
authorPatrick Steinhardt <ps@pks.im>2017-05-02 10:11:28 +0200
committerPatrick Steinhardt <ps@pks.im>2017-05-05 15:39:25 +0200
commit8264a30f4fbfce96f433f01fa6fe537e5cb3570d (patch)
treea944129c72e1f6a7268d96ad2e285948ff87be06 /src/worktree.c
parenta7aa73a535a7f21357e9c7dece8376f30a29681f (diff)
downloadlibgit2-8264a30f4fbfce96f433f01fa6fe537e5cb3570d.tar.gz
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.
Diffstat (limited to 'src/worktree.c')
-rw-r--r--src/worktree.c15
1 files changed, 15 insertions, 0 deletions
diff --git a/src/worktree.c b/src/worktree.c
index 6e797f362..b9ed75991 100644
--- a/src/worktree.c
+++ b/src/worktree.c
@@ -318,6 +318,21 @@ int git_worktree_add(git_worktree **out, git_repository *repo,
if ((err = git_path_prettify_dir(&wddir, worktree, NULL)) < 0)
goto out;
+ if (wtopts.lock) {
+ int fd;
+
+ if ((err = git_buf_joinpath(&buf, gitdir.ptr, "locked")) < 0)
+ goto out;
+
+ if ((fd = p_creat(buf.ptr, 0644)) < 0) {
+ err = fd;
+ goto out;
+ }
+
+ p_close(fd);
+ git_buf_clear(&buf);
+ }
+
/* Create worktree .git file */
if ((err = git_buf_printf(&buf, "gitdir: %s\n", gitdir.ptr)) < 0)
goto out;