diff options
author | Patrick Steinhardt <ps@pks.im> | 2015-10-21 16:03:04 +0200 |
---|---|---|
committer | Patrick Steinhardt <ps@pks.im> | 2017-02-13 11:01:09 +0100 |
commit | 2a503485fae6c93c76bd0465c8b3fad5d9e19f6d (patch) | |
tree | 37fb761871bdbd1acfa6feea714c177d7c9cd3dc /src/worktree.c | |
parent | dea7488e93bdd9a0291d518af58b1cde6d71aca9 (diff) | |
download | libgit2-2a503485fae6c93c76bd0465c8b3fad5d9e19f6d.tar.gz |
worktree: implement locking mechanisms
Working trees support locking by creating a file `locked` inside
the tree's gitdir with an optional reason inside. Support this
feature by adding functions to get and set the locking status.
Diffstat (limited to 'src/worktree.c')
-rw-r--r-- | src/worktree.c | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/src/worktree.c b/src/worktree.c index 3c6cfee45..fa5a916c0 100644 --- a/src/worktree.c +++ b/src/worktree.c @@ -143,6 +143,7 @@ int git_worktree_lookup(git_worktree **out, git_repository *repo, const char *na goto out; } wt->gitdir_path = git_buf_detach(&path); + wt->locked = !!git_worktree_is_locked(NULL, wt); (*out) = wt; @@ -283,3 +284,75 @@ out: return err; } + +int git_worktree_lock(git_worktree *wt, char *creason) +{ + git_buf buf = GIT_BUF_INIT, path = GIT_BUF_INIT; + int err; + + assert(wt); + + if ((err = git_worktree_is_locked(NULL, wt)) < 0) + goto out; + + if ((err = git_buf_joinpath(&path, wt->gitdir_path, "locked")) < 0) + goto out; + + if (creason) + git_buf_attach_notowned(&buf, creason, strlen(creason)); + + if ((err = git_futils_writebuffer(&buf, path.ptr, O_CREAT|O_EXCL|O_WRONLY, 0644)) < 0) + goto out; + + wt->locked = 1; + +out: + git_buf_free(&path); + + return err; +} + +int git_worktree_unlock(git_worktree *wt) +{ + git_buf path = GIT_BUF_INIT; + + assert(wt); + + if (!git_worktree_is_locked(NULL, wt)) + return 0; + + if (git_buf_joinpath(&path, wt->gitdir_path, "locked") < 0) + return -1; + + if (p_unlink(path.ptr) != 0) { + git_buf_free(&path); + return -1; + } + + wt->locked = 0; + + git_buf_free(&path); + + return 0; +} + +int git_worktree_is_locked(git_buf *reason, const git_worktree *wt) +{ + git_buf path = GIT_BUF_INIT; + int ret; + + assert(wt); + + if (reason) + git_buf_clear(reason); + + if ((ret = git_buf_joinpath(&path, wt->gitdir_path, "locked")) < 0) + goto out; + if ((ret = git_path_exists(path.ptr)) && reason) + git_futils_readbuffer(reason, path.ptr); + +out: + git_buf_free(&path); + + return ret; +} |