Commit Diff


commit - de060dffe28619a317fa08ba93510e621429e235
commit + a1a7858afef7c2a2ed0c3a21bffca4affeb914de
blob - c0c6adc686d7806615bf8af3fcbf5d9649aefe7c
blob + 944448399ba4581f1641e96b14a54841a2a52336
--- lib/worktree.c
+++ lib/worktree.c
@@ -156,10 +156,14 @@ got_worktree_init(const char *path, struct got_referen
 {
 	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_referen
 	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_referen
 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;
 }