Commit 271d2a38b1a788e9bbd0865e35ee14dce08b5197

Stefan Sperling 2018-12-25T15:38:24

store parsed head reference in struct got_worktree

diff --git a/include/got_reference.h b/include/got_reference.h
index d8b1b74..677800f 100644
--- a/include/got_reference.h
+++ b/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 *);
diff --git a/lib/got_lib_worktree.h b/lib/got_lib_worktree.h
index 5fa0ba1..ef8ae1e 100644
--- a/lib/got_lib_worktree.h
+++ b/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.
diff --git a/lib/reference.c b/lib/reference.c
index f672627..a2c1904 100644
--- a/lib/reference.c
+++ b/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;
diff --git a/lib/worktree.c b/lib/worktree.c
index f8d6e1c..8ee905e 100644
--- a/lib/worktree.c
+++ b/lib/worktree.c
@@ -266,6 +266,7 @@ got_worktree_open(struct got_worktree **worktree, const char *path)
 	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, const char *path)
 	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 *worktree)
 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 *