summaryrefslogtreecommitdiff
path: root/worktree.c
diff options
context:
space:
mode:
authorMichael Rappazzo <rappazzo@gmail.com>2015-10-02 11:55:31 (GMT)
committerJunio C Hamano <gitster@pobox.com>2015-10-02 20:07:38 (GMT)
commitac6c561b598ae74d8e752e22607194536000e4db (patch)
tree691b1b7c6835177276ffca71cc59263f4c35c133 /worktree.c
parent8d530c4d64ffcc853889f7b385f554d53db375ed (diff)
downloadgit-ac6c561b598ae74d8e752e22607194536000e4db.zip
git-ac6c561b598ae74d8e752e22607194536000e4db.tar.gz
git-ac6c561b598ae74d8e752e22607194536000e4db.tar.bz2
worktree: add top-level worktree.c
worktree.c contains functions to work with and get information from worktrees. This introduction moves functions related to worktrees from branch.c into worktree.c Signed-off-by: Michael Rappazzo <rappazzo@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'worktree.c')
-rw-r--r--worktree.c82
1 files changed, 82 insertions, 0 deletions
diff --git a/worktree.c b/worktree.c
new file mode 100644
index 0000000..10e1496
--- /dev/null
+++ b/worktree.c
@@ -0,0 +1,82 @@
+#include "cache.h"
+#include "refs.h"
+#include "strbuf.h"
+#include "worktree.h"
+
+static char *find_linked_symref(const char *symref, const char *branch,
+ const char *id)
+{
+ struct strbuf sb = STRBUF_INIT;
+ struct strbuf path = STRBUF_INIT;
+ struct strbuf gitdir = STRBUF_INIT;
+ char *existing = NULL;
+
+ /*
+ * $GIT_COMMON_DIR/$symref (e.g. HEAD) is practically outside
+ * $GIT_DIR so resolve_ref_unsafe() won't work (it uses
+ * git_path). Parse the ref ourselves.
+ */
+ if (id)
+ strbuf_addf(&path, "%s/worktrees/%s/%s", get_git_common_dir(), id, symref);
+ else
+ strbuf_addf(&path, "%s/%s", get_git_common_dir(), symref);
+
+ if (!strbuf_readlink(&sb, path.buf, 0)) {
+ if (!starts_with(sb.buf, "refs/") ||
+ check_refname_format(sb.buf, 0))
+ goto done;
+ } else if (strbuf_read_file(&sb, path.buf, 0) >= 0 &&
+ starts_with(sb.buf, "ref:")) {
+ strbuf_remove(&sb, 0, strlen("ref:"));
+ strbuf_trim(&sb);
+ } else
+ goto done;
+ if (strcmp(sb.buf, branch))
+ goto done;
+ if (id) {
+ strbuf_reset(&path);
+ strbuf_addf(&path, "%s/worktrees/%s/gitdir", get_git_common_dir(), id);
+ if (strbuf_read_file(&gitdir, path.buf, 0) <= 0)
+ goto done;
+ strbuf_rtrim(&gitdir);
+ } else
+ strbuf_addstr(&gitdir, get_git_common_dir());
+ strbuf_strip_suffix(&gitdir, ".git");
+
+ existing = strbuf_detach(&gitdir, NULL);
+done:
+ strbuf_release(&path);
+ strbuf_release(&sb);
+ strbuf_release(&gitdir);
+
+ return existing;
+}
+
+char *find_shared_symref(const char *symref, const char *target)
+{
+ struct strbuf path = STRBUF_INIT;
+ DIR *dir;
+ struct dirent *d;
+ char *existing;
+
+ if ((existing = find_linked_symref(symref, target, NULL)))
+ return existing;
+
+ strbuf_addf(&path, "%s/worktrees", get_git_common_dir());
+ dir = opendir(path.buf);
+ strbuf_release(&path);
+ if (!dir)
+ return NULL;
+
+ while ((d = readdir(dir)) != NULL) {
+ if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
+ continue;
+ existing = find_linked_symref(symref, target, d->d_name);
+ if (existing)
+ goto done;
+ }
+done:
+ closedir(dir);
+
+ return existing;
+}