Commit Diff


commit - 1251a9e5a52510aea3b03f1228de7404e6f95a3a
commit + 36a387004fc6fb4f302443e39bdffc01d1c1ed47
blob - 1b87fc95dffd00b52b5bb2ed84f14d17871152da
blob + c82e0bfd137ee751f13b1a609bfe0391b7593e5f
--- got/got.c
+++ got/got.c
@@ -252,9 +252,10 @@ check_ancestry(struct got_worktree *worktree, struct g
 	struct got_object_id *head_commit_id = NULL;
 	struct got_commit_graph *graph = NULL;
 
-	head_ref = got_worktree_get_head_ref(worktree);
-	if (head_ref == NULL)
-		return got_error_from_errno();
+	err = got_ref_open(&head_ref, repo,
+	    got_worktree_get_head_ref_name(worktree));
+	if (err)
+		return err;
 
 	/* TODO: Check the reflog. The head ref may have been rebased. */
 	err = got_ref_resolve(&head_commit_id, repo, head_ref);
blob - 6986c3a84c17dafdf8fd94122de1c579ffde02e0
blob + 0261d1079837ceb9ddcff0869f8fce0735d7504b
--- include/got_worktree.h
+++ include/got_worktree.h
@@ -77,12 +77,6 @@ const struct got_error *got_worktree_match_path_prefix
 const char *got_worktree_get_head_ref_name(struct got_worktree *);
 
 /*
- * Get the work tree's HEAD reference.
- * The caller must dispose of it with free(3).
- */
-struct got_reference *got_worktree_get_head_ref(struct got_worktree *);
-
-/*
  * Get the current base commit ID of a worktree.
  */
 struct got_object_id *got_worktree_get_base_commit_id(struct got_worktree *);
blob - 9bfa860c4d468a811b3903a7fb3b4bbeac96caa9
blob + 222d5b80f24c09ad6693cbb5d9bd53a6e62dac75
--- 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;
-	struct got_reference *head_ref;
+	char *head_ref_name;
 	uuid_t uuid;
 
 	/*
blob - a4c605a756322f709713c648db7450a5fb552ae7
blob + 5b2aa32bae271bdfac6e8eaeeb67d203751b2ffb
--- lib/worktree.c
+++ lib/worktree.c
@@ -330,7 +330,6 @@ open_worktree(struct got_worktree **worktree, const ch
 	char *uuidstr = 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;
@@ -413,20 +412,16 @@ open_worktree(struct got_worktree **worktree, const ch
 
 	err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
 	    base_commit_id_str);
-	if (err)
-		goto done;
-
-	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);
+	err = read_meta_file(&(*worktree)->head_ref_name, path_got,
+	    GOT_WORKTREE_HEAD_REF);
 done:
 	if (repo)
 		got_repo_close(repo);
 	free(path_got);
 	free(path_lock);
-	free(head_ref_str);
 	free(base_commit_id_str);
 	free(uuidstr);
 	free(formatstr);
@@ -469,8 +464,7 @@ got_worktree_close(struct got_worktree *worktree)
 	free(worktree->repo_path);
 	free(worktree->path_prefix);
 	free(worktree->base_commit_id);
-	if (worktree->head_ref)
-		got_ref_close(worktree->head_ref);
+	free(worktree->head_ref_name);
 	if (worktree->lockfd != -1)
 		if (close(worktree->lockfd) != 0)
 			err = got_error_from_errno();
@@ -515,13 +509,7 @@ got_worktree_match_path_prefix(int *match, struct got_
 const char *
 got_worktree_get_head_ref_name(struct got_worktree *worktree)
 {
-	return got_ref_get_name(worktree->head_ref);
-}
-
-struct got_reference *
-got_worktree_get_head_ref(struct got_worktree *worktree)
-{
-	return got_ref_dup(worktree->head_ref);
+	return worktree->head_ref_name;
 }
 
 struct got_object_id *
@@ -2824,6 +2812,7 @@ got_worktree_commit(struct got_object_id **new_commit_
 	struct got_pathlist_entry *pe;
 	char *relpath = NULL;
 	const char *head_ref_name = NULL;
+	struct got_reference *head_ref = NULL;
 	struct got_commit_object *head_commit = NULL;
 	struct got_object_id *head_commit_id = NULL;
 	struct got_reference *head_ref2 = NULL;
@@ -2849,8 +2838,10 @@ got_worktree_commit(struct got_object_id **new_commit_
 	if (err)
 		goto done;
 
-	/* XXX should re-read head ref here now that work tree is locked */
-	err = got_ref_resolve(&head_commit_id, repo, worktree->head_ref);
+	err = got_ref_open(&head_ref, repo, worktree->head_ref_name);
+	if (err)
+		goto done;
+	err = got_ref_resolve(&head_commit_id, repo, head_ref);
 	if (err)
 		goto done;
 
@@ -2933,10 +2924,10 @@ got_worktree_commit(struct got_object_id **new_commit_
 		goto done;
 	}
 	/* Update branch head in repository. */
-	err = got_ref_change_ref(worktree->head_ref, *new_commit_id);
+	err = got_ref_change_ref(head_ref, *new_commit_id);
 	if (err)
 		goto done;
-	err = got_ref_write(worktree->head_ref, repo);
+	err = got_ref_write(head_ref, repo);
 	if (err)
 		goto done;
 	/* XXX race has ended here */
@@ -2969,6 +2960,8 @@ done:
 	free(relpath);
 	free(head_commit_id);
 	free(head_commit_id2);
+	if (head_ref)
+		got_ref_close(head_ref);
 	if (head_ref2)
 		got_ref_close(head_ref2);
 	return err;