Commit Diff


commit - fc76cabb0c70b61d89701fce09ff96e538615379
commit + 271d2a38b1a788e9bbd0865e35ee14dce08b5197
blob - d8b1b74a97c6609fa5a8d85c2e75cb27f3c96171
blob + 677800fa5e5df772a4be66f4e6346d7e4f055b29
--- include/got_reference.h
+++ include/got_reference.h
@@ -46,5 +46,8 @@ struct got_reference *got_ref_dup(struct got_reference
 const struct got_error *got_ref_resolve(struct got_object_id **,
     struct got_repository *, struct got_reference *);
 
-/* Return a string representation of a reference. */
+/*
+ * Return a string representation of a reference.
+ * The caller must dispose of it with free(3).
+ */
 char *got_ref_to_str(struct got_reference *);
blob - 5fa0ba16f4cea03fe2779a8bf459fbf903861185
blob + ef8ae1ec5efb0aad7e3b9f4f8f4e89e9bac27fbe
--- lib/got_lib_worktree.h
+++ lib/got_lib_worktree.h
@@ -19,7 +19,7 @@ struct got_worktree {
 	char *repo_path;
 	char *path_prefix;
 	struct got_object_id *base_commit_id;
-	char *head_ref;
+	struct got_reference *head_ref;
 
 	/*
 	 * File descriptor for the lock file, open while a work tree is open.
blob - f672627472f988a3b6eaf5da33809177a8eaa389
blob + a2c190401d8f03bc571561ea443bf15c6b10b698
--- lib/reference.c
+++ lib/reference.c
@@ -299,15 +299,18 @@ char *
 got_ref_to_str(struct got_reference *ref)
 {
 	char *str;
-	if (ref->flags & GOT_REF_IS_SYMBOLIC) {
-		if (asprintf(&str, "ref: %s", ref->ref.symref.ref) == -1)
-			return NULL;
-	} else {
-		str = calloc(1, SHA1_DIGEST_STRING_LENGTH);
-		if (str == NULL)
-			return NULL;
-		str = got_sha1_digest_to_str(ref->ref.ref.sha1, str,
-		    SHA1_DIGEST_STRING_LENGTH);
+
+	if (ref->flags & GOT_REF_IS_SYMBOLIC)
+		return strdup(ref->ref.symref.ref);
+
+	str = malloc(SHA1_DIGEST_STRING_LENGTH);
+	if (str == NULL)
+		return NULL;
+
+	if (got_sha1_digest_to_str(ref->ref.ref.sha1, str,
+	    SHA1_DIGEST_STRING_LENGTH) == NULL) {
+		free(str);
+		return NULL;
 	}
 
 	return str;
blob - f8d6e1c9f31ed998a56d9e161ceda32926f2ba60
blob + 8ee905e2acc8ab92a9c22b4fee62e04990d46a0e
--- lib/worktree.c
+++ lib/worktree.c
@@ -266,6 +266,7 @@ got_worktree_open(struct got_worktree **worktree, cons
 	char *formatstr = 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;
@@ -341,16 +342,17 @@ got_worktree_open(struct got_worktree **worktree, cons
 	if (err)
 		goto done;
 
-	err = read_meta_file(&(*worktree)->head_ref, path_got,
-	    GOT_WORKTREE_HEAD_REF);
+	err = read_meta_file(&head_ref_str, path_got, GOT_WORKTREE_HEAD_REF);
 	if (err)
 		goto done;
 
+	err = got_ref_open(&(*worktree)->head_ref, repo, head_ref_str);
 done:
 	if (repo)
 		got_repo_close(repo);
 	free(path_got);
 	free(path_lock);
+	free(head_ref_str);
 	free(base_commit_id_str);
 	if (err) {
 		if (fd != -1)
@@ -371,7 +373,8 @@ got_worktree_close(struct got_worktree *worktree)
 	free(worktree->repo_path);
 	free(worktree->path_prefix);
 	free(worktree->base_commit_id);
-	free(worktree->head_ref);
+	if (worktree->head_ref)
+		got_ref_close(worktree->head_ref);
 	if (worktree->lockfd != -1)
 		close(worktree->lockfd);
 	free(worktree);
@@ -386,7 +389,7 @@ got_worktree_get_repo_path(struct got_worktree *worktr
 char *
 got_worktree_get_head_ref_name(struct got_worktree *worktree)
 {
-	return strdup(worktree->head_ref);
+	return got_ref_to_str(worktree->head_ref);
 }
 
 static const struct got_error *