add a UUID to work tree meta data
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
diff --git a/lib/got_lib_worktree.h b/lib/got_lib_worktree.h
index ef8ae1e..d786edf 100644
--- a/lib/got_lib_worktree.h
+++ b/lib/got_lib_worktree.h
@@ -42,6 +42,7 @@ struct got_worktree {
#define GOT_WORKTREE_BASE_COMMIT "base-commit"
#define GOT_WORKTREE_LOCK "lock"
#define GOT_WORKTREE_FORMAT "format"
+#define GOT_WORKTREE_UUID "uuid"
#define GOT_WORKTREE_FORMAT_VERSION 1
#define GOT_WORKTREE_INVALID_COMMIT_ID GOT_SHA1_STRING_ZERO
diff --git a/lib/worktree.c b/lib/worktree.c
index f562feb..8c1bd06 100644
--- a/lib/worktree.c
+++ b/lib/worktree.c
@@ -31,6 +31,7 @@
#include <zlib.h>
#include <fnmatch.h>
#include <libgen.h>
+#include <uuid.h>
#include "got_error.h"
#include "got_repository.h"
@@ -193,12 +194,15 @@ got_worktree_init(const char *path, struct got_reference *head_ref,
{
const struct got_error *err = NULL;
struct got_object_id *commit_id = NULL;
+ uuid_t uuid;
+ uint32_t uuid_status;
int obj_type;
char *path_got = NULL;
char *refstr = NULL;
char *formatstr = NULL;
char *absprefix = NULL;
char *basestr = NULL;
+ char *uuidstr = NULL;
err = got_ref_resolve(&commit_id, repo, head_ref);
if (err)
@@ -270,6 +274,21 @@ got_worktree_init(const char *path, struct got_reference *head_ref,
if (err)
goto done;
+ /* Generate UUID. */
+ uuid_create(&uuid, &uuid_status);
+ if (uuid_status != uuid_s_ok) {
+ err = got_error_uuid(uuid_status);
+ goto done;
+ }
+ uuid_to_string(&uuid, &uuidstr, &uuid_status);
+ if (uuid_status != uuid_s_ok) {
+ err = got_error_uuid(uuid_status);
+ goto done;
+ }
+ err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
+ if (err)
+ goto done;
+
/* Stamp work tree with format file. */
if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
err = got_error_from_errno();
@@ -286,6 +305,7 @@ done:
free(refstr);
free(absprefix);
free(basestr);
+ free(uuidstr);
return err;
}
diff --git a/regress/worktree/worktree_test.c b/regress/worktree/worktree_test.c
index 5d9dfdd..82c5ca6 100644
--- a/regress/worktree/worktree_test.c
+++ b/regress/worktree/worktree_test.c
@@ -99,6 +99,8 @@ remove_worktree(const char *worktree_path)
return 0;
if (!remove_meta_file(worktree_path, GOT_WORKTREE_FORMAT))
return 0;
+ if (!remove_meta_file(worktree_path, GOT_WORKTREE_UUID))
+ return 0;
if (!remove_got_dir(worktree_path))
return 0;
if (rmdir(worktree_path) == -1)
@@ -188,6 +190,8 @@ worktree_init(const char *repo_path)
goto done;
if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_FORMAT))
goto done;
+ if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_UUID))
+ goto done;
if (!remove_worktree(worktree_path))
goto done;