Commit ec22038e8d0a46e692c6093e0a35503f4af398d7

Stefan Sperling 2019-03-10T15:45:57

add a UUID to work tree meta data

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;