stamp worktrees with a format number
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
diff --git a/lib/got_worktree_priv.h b/lib/got_worktree_priv.h
index 97fb6c8..f49af44 100644
--- a/lib/got_worktree_priv.h
+++ b/lib/got_worktree_priv.h
@@ -22,3 +22,6 @@ struct got_worktree {
#define GOT_WORKTREE_GOT_DIR ".got"
#define GOT_WORKTREE_FILE_INDEX "fileindex"
#define GOT_WORKTREE_REPOSITORY "repository"
+#define GOT_WORKTREE_FORMAT "format"
+
+#define GOT_WORKTREE_FORMAT_VERSION 1
diff --git a/lib/worktree.c b/lib/worktree.c
index f655715..b1ad87e 100644
--- a/lib/worktree.c
+++ b/lib/worktree.c
@@ -42,8 +42,10 @@ got_worktree_init(const char *path, struct got_reference *head_ref,
char *indexpath = NULL;
char *headpath = NULL;
char *repopath = NULL;
+ char *formatpath = NULL;
char *refstr = NULL;
char *path_repos = NULL;
+ char *formatstr = NULL;
char buf[4];
ssize_t n;
int fd;
@@ -143,7 +145,7 @@ got_worktree_init(const char *path, struct got_reference *head_ref,
goto done;
}
- /* Store path to repository. */
+ /* Store path to repository. */
if (asprintf(&repopath, "%s/%s", gotpath, GOT_WORKTREE_REPOSITORY)
== -1) {
err = got_error(GOT_ERR_NO_MEM);
@@ -184,6 +186,40 @@ got_worktree_init(const char *path, struct got_reference *head_ref,
goto done;
}
+ /* Stamp repository with format file. */
+ if (asprintf(&formatpath, "%s/%s", gotpath, GOT_WORKTREE_FORMAT)
+ == -1) {
+ err = got_error(GOT_ERR_NO_MEM);
+ goto done;
+ }
+ if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
+ err = got_error(GOT_ERR_NO_MEM);
+ goto done;
+ }
+ fd = open(formatpath, O_RDWR | O_CREAT | O_EXCL | O_EXLOCK | O_NOFOLLOW,
+ GOT_DEFAULT_FILE_MODE);
+ if (fd == -1) {
+ err = got_error_from_errno();
+ goto done;
+ }
+ n = read(fd, buf, sizeof(buf));
+ if (n != 0) {
+ err = (n == -1 ? got_error_from_errno() :
+ got_error(GOT_ERR_WORKTREE_EXISTS));
+ close(fd);
+ goto done;
+ }
+ n = dprintf(fd, "%s\n", formatstr);
+ if (n != strlen(formatstr) + 1) {
+ err = got_error_from_errno();
+ close(fd);
+ goto done;
+ }
+ if (close(fd) == -1) {
+ err = got_error_from_errno();
+ goto done;
+ }
+
done:
free(abspath);
free(normpath);
@@ -191,6 +227,8 @@ done:
free(indexpath);
free(headpath);
free(repopath);
+ free(formatpath);
+ free(formatstr);
free(refstr);
free(path_repos);
return err;
diff --git a/regress/worktree/worktree_test.c b/regress/worktree/worktree_test.c
index e9f6cee..cad1391 100644
--- a/regress/worktree/worktree_test.c
+++ b/regress/worktree/worktree_test.c
@@ -83,6 +83,7 @@ remove_workdir(const char *worktree_path)
remove_meta_file(worktree_path, GOT_REF_HEAD);
remove_meta_file(worktree_path, GOT_WORKTREE_FILE_INDEX);
remove_meta_file(worktree_path, GOT_WORKTREE_REPOSITORY);
+ remove_meta_file(worktree_path, GOT_WORKTREE_FORMAT);
remove_got_dir(worktree_path);
rmdir(worktree_path);
}
@@ -135,6 +136,8 @@ worktree_init(const char *repo_path)
goto done;
if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_REPOSITORY))
goto done;
+ if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_FORMAT))
+ goto done;
ok = 1;
remove_workdir(worktree_path);
done:
@@ -224,15 +227,23 @@ worktree_init_exists(const char *repo_path)
unlink(path);
free(path);
+ if (!obstruct_meta_file(&path, worktree_path, GOT_WORKTREE_FORMAT))
+ goto done;
+ err = got_worktree_init(worktree_path, head_ref, repo);
+ if (err != NULL && err->code == GOT_ERR_ERRNO && errno == EEXIST)
+ ok++;
+ unlink(path);
+ free(path);
+
done:
if (head_ref)
got_ref_close(head_ref);
if (repo)
got_repo_close(repo);
free(gotpath);
- if (ok == 3)
+ if (ok == 4)
remove_workdir(worktree_path);
- return (ok == 3);
+ return (ok == 4);
}
#define RUN_TEST(expr, name) \