Commit 65596e15914f3e70494e33e84a842d7ce360c1e7

Stefan Sperling 2018-12-24T19:59:50

store base commit in a dedicated meta file

diff --git a/lib/got_lib_worktree.h b/lib/got_lib_worktree.h
index 5694eae..f308ade 100644
--- a/lib/got_lib_worktree.h
+++ b/lib/got_lib_worktree.h
@@ -39,6 +39,7 @@ struct got_worktree {
 #define GOT_WORKTREE_REPOSITORY		"repository"
 #define GOT_WORKTREE_PATH_PREFIX	"path-prefix"
 #define GOT_WORKTREE_HEAD		"head"
+#define GOT_WORKTREE_BASE		"base"
 #define GOT_WORKTREE_LOCK		"lock"
 #define GOT_WORKTREE_FORMAT		"format"
 
diff --git a/lib/worktree.c b/lib/worktree.c
index c0c6adc..f44df5b 100644
--- a/lib/worktree.c
+++ b/lib/worktree.c
@@ -155,11 +155,23 @@ got_worktree_init(const char *path, struct got_reference *head_ref,
     const char *prefix, struct got_repository *repo)
 {
 	const struct got_error *err = NULL;
+	struct got_object_id *commit_id = NULL;
+	int obj_type;
 	char *path_got = NULL;
 	char *refstr = NULL;
 	char *repo_path = NULL;
 	char *formatstr = NULL;
 	char *absprefix = NULL;
+	char *basestr = NULL;
+
+	err = got_ref_resolve(&commit_id, repo, head_ref);
+	if (err)
+		return err;
+	err = got_object_get_type(&obj_type, repo, commit_id);
+	if (err)
+		return err;
+	if (obj_type != GOT_OBJ_TYPE_COMMIT)
+		return got_error(GOT_ERR_OBJ_TYPE);
 
 	if (!got_path_is_absolute(prefix)) {
 		if (asprintf(&absprefix, "/%s", prefix) == -1)
@@ -202,6 +214,14 @@ got_worktree_init(const char *path, struct got_reference *head_ref,
 	if (err)
 		goto done;
 
+	/* Record our base commit. */
+	err = got_object_id_str(&basestr, commit_id);
+	if (err)
+		goto done;
+	err = create_meta_file(path_got, GOT_WORKTREE_BASE, basestr);
+	if (err)
+		goto done;
+
 	/* Store path to repository. */
 	repo_path = got_repo_get_path(repo);
 	if (repo_path == NULL) {
@@ -228,11 +248,13 @@ got_worktree_init(const char *path, struct got_reference *head_ref,
 		goto done;
 
 done:
+	free(commit_id);
 	free(path_got);
 	free(formatstr);
 	free(refstr);
 	free(repo_path);
 	free(absprefix);
+	free(basestr);
 	return err;
 }
 
diff --git a/regress/worktree/worktree_test.c b/regress/worktree/worktree_test.c
index f5e074f..5157cfd 100644
--- a/regress/worktree/worktree_test.c
+++ b/regress/worktree/worktree_test.c
@@ -86,6 +86,8 @@ remove_worktree(const char *worktree_path)
 {
 	if (!remove_meta_file(worktree_path, GOT_WORKTREE_HEAD))
 		return 0;
+	if (!remove_meta_file(worktree_path, GOT_WORKTREE_BASE))
+		return 0;
 	if (!remove_meta_file(worktree_path, GOT_WORKTREE_FILE_INDEX))
 		return 0;
 	if (!remove_meta_file(worktree_path, GOT_WORKTREE_REPOSITORY))
@@ -172,6 +174,8 @@ worktree_init(const char *repo_path)
 	/* Ensure required files were created. */
 	if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_HEAD))
 		goto done;
+	if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_BASE))
+		goto done;
 	if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_LOCK))
 		goto done;
 	if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_FILE_INDEX))
@@ -273,6 +277,9 @@ worktree_init_exists(const char *repo_path)
 	    GOT_WORKTREE_HEAD))
 		goto done;
 	if (!obstruct_meta_file_and_init(&ok, repo, worktree_path,
+	    GOT_WORKTREE_BASE))
+		goto done;
+	if (!obstruct_meta_file_and_init(&ok, repo, worktree_path,
 	    GOT_WORKTREE_LOCK))
 		goto done;
 	if (!obstruct_meta_file_and_init(&ok, repo, worktree_path,
@@ -292,9 +299,9 @@ done:
 	if (repo)
 		got_repo_close(repo);
 	free(gotpath);
-	if (ok == 6)
+	if (ok == 7)
 		remove_worktree(worktree_path);
-	return (ok == 6);
+	return (ok == 7);
 }
 
 static void