store commit hash along with the HEAD ref in .got/head
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
diff --git a/lib/worktree.c b/lib/worktree.c
index c0c6adc..9444483 100644
--- a/lib/worktree.c
+++ b/lib/worktree.c
@@ -156,10 +156,14 @@ got_worktree_init(const char *path, struct got_reference *head_ref,
{
const struct got_error *err = NULL;
char *path_got = NULL;
- char *refstr = NULL;
+ char *ref_str = NULL;
char *repo_path = NULL;
char *formatstr = NULL;
char *absprefix = NULL;
+ struct got_object_id *commit_id = NULL;
+ char *id_str = NULL;
+ char *head_content = NULL;
+ int obj_type;
if (!got_path_is_absolute(prefix)) {
if (asprintf(&absprefix, "/%s", prefix) == -1)
@@ -192,13 +196,30 @@ got_worktree_init(const char *path, struct got_reference *head_ref,
if (err)
goto done;
- /* Write the HEAD reference. */
- refstr = got_ref_to_str(head_ref);
- if (refstr == NULL) {
+ /* Write the commit hash and HEAD reference. */
+ err = got_ref_resolve(&commit_id, repo, head_ref);
+ if (err)
+ goto done;
+ err = got_object_get_type(&obj_type, repo, commit_id);
+ if (err)
+ goto done;
+ if (obj_type != GOT_OBJ_TYPE_COMMIT) {
+ err = got_error(GOT_ERR_OBJ_TYPE);
+ goto done;
+ }
+ err = got_object_id_str(&id_str, commit_id);
+ if (err)
+ goto done;
+ ref_str = got_ref_to_str(head_ref);
+ if (ref_str == NULL) {
err = got_error_from_errno();
goto done;
}
- err = create_meta_file(path_got, GOT_WORKTREE_HEAD, refstr);
+ if (asprintf(&head_content, "%s %s", id_str, ref_str) == -1) {
+ err = got_error_from_errno();
+ goto done;
+ }
+ err = create_meta_file(path_got, GOT_WORKTREE_HEAD, head_content);
if (err)
goto done;
@@ -230,9 +251,12 @@ got_worktree_init(const char *path, struct got_reference *head_ref,
done:
free(path_got);
free(formatstr);
- free(refstr);
+ free(ref_str);
free(repo_path);
free(absprefix);
+ free(id_str);
+ free(commit_id);
+ free(head_content);
return err;
}