Commit c442a90d575144387b9e5e1c769ba935013bbc71

Stefan Sperling 2019-03-10T15:55:28

read UUID back from work tree meta data

diff --git a/lib/fileindex.c b/lib/fileindex.c
index 9513f30..25d5b3b 100644
--- a/lib/fileindex.c
+++ b/lib/fileindex.c
@@ -25,6 +25,7 @@
 #include <sha1.h>
 #include <endian.h>
 #include <limits.h>
+#include <uuid.h>
 
 #include "got_error.h"
 #include "got_object.h"
diff --git a/lib/got_lib_worktree.h b/lib/got_lib_worktree.h
index d786edf..a8227e2 100644
--- a/lib/got_lib_worktree.h
+++ b/lib/got_lib_worktree.h
@@ -20,6 +20,7 @@ struct got_worktree {
 	char *path_prefix;
 	struct got_object_id *base_commit_id;
 	struct got_reference *head_ref;
+	uuid_t uuid;
 
 	/*
 	 * File descriptor for the lock file, open while a work tree is open.
diff --git a/lib/repository.c b/lib/repository.c
index 96c0dad..afdfaad 100644
--- a/lib/repository.c
+++ b/lib/repository.c
@@ -33,6 +33,7 @@
 #include <libgen.h>
 #include <stdint.h>
 #include <imsg.h>
+#include <uuid.h>
 
 #include "got_error.h"
 #include "got_reference.h"
diff --git a/lib/worktree.c b/lib/worktree.c
index 8c1bd06..2b9d193 100644
--- a/lib/worktree.c
+++ b/lib/worktree.c
@@ -315,12 +315,14 @@ open_worktree(struct got_worktree **worktree, const char *path)
 	const struct got_error *err = NULL;
 	char *path_got;
 	char *formatstr = NULL;
+	char *uuidstr = NULL;
 	char *path_lock = NULL;
 	char *base_commit_id_str = NULL;
 	char *head_ref_str = NULL;
 	int version, fd = -1;
 	const char *errstr;
 	struct got_repository *repo = NULL;
+	uint32_t uuid_status;
 
 	*worktree = NULL;
 
@@ -384,6 +386,15 @@ open_worktree(struct got_worktree **worktree, const char *path)
 	if (err)
 		goto done;
 
+	err = read_meta_file(&uuidstr, path_got, GOT_WORKTREE_UUID);
+	if (err)
+		goto done;
+	uuid_from_string(uuidstr, &(*worktree)->uuid, &uuid_status);
+	if (uuid_status != uuid_s_ok) {
+		err = got_error_uuid(uuid_status);
+		goto done;
+	}
+
 	err = got_repo_open(&repo, (*worktree)->repo_path);
 	if (err)
 		goto done;
@@ -405,6 +416,7 @@ done:
 	free(path_lock);
 	free(head_ref_str);
 	free(base_commit_id_str);
+	free(uuidstr);
 	if (err) {
 		if (fd != -1)
 			close(fd);
diff --git a/regress/worktree/worktree_test.c b/regress/worktree/worktree_test.c
index 82c5ca6..b33bfa3 100644
--- a/regress/worktree/worktree_test.c
+++ b/regress/worktree/worktree_test.c
@@ -28,6 +28,7 @@
 #include <util.h>
 #include <err.h>
 #include <unistd.h>
+#include <uuid.h>
 
 #include "got_error.h"
 #include "got_object.h"