store base commit in a dedicated meta file
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
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