git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 6/6] builtin/worktree: create refdb via ref backend

From
Patrick Steinhardt <ps@pks.im>
Date
Dec 28, 2023, 10:00 UTC
Message-ID
<aae969301fa6792263d32362b36d071de4f6ed94.1703754513.git.ps@pks.im>
In-Reply-To
<cover.1703754513.git.ps@pks.im>

When creating a worktree we create the worktree's ref database manually by first writing a "HEAD" file so that the directory is recognized as a Git repository by other commands, and then running git-update-ref(1) or git-symbolic-ref(1) to write the actual value. But while this is fine for the files backend, this logic simply assumes too much about how the ref backend works and will leave behind an invalid ref database once any other ref backend lands.

Refactor the code to instead use `refs_init_db()` to initialize the ref database so that git-worktree(1) itself does not need to know about how to initialize it. This will allow future ref backends to customize how the per-worktree ref database is set up. Furthermore, as we now already have a worktree ref store around, we can also avoid spawning external commands to write the HEAD reference and instead use the refs API to do so.

Note that we do not have an equivalent to passing the `--quiet` flag to git-symbolic-ref(1) as we did before. This flag does not have an effect anyway though, as git-symbolic-ref(1) only honors it when reading a symref, but never when writing one.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
---
 builtin/worktree.c | 48 +++++++++++++++++++++-------------------------
 1 file changed, 22 insertions(+), 26 deletions(-)
diff --git a/builtin/worktree.c b/builtin/worktree.c
index 58937a2a68..9d935bee84 100644
--- a/builtin/worktree.c
+++ b/builtin/worktree.c
@@ -416,7 +416,6 @@ static int add_worktree(const char *path, const char *refname,
 	struct strbuf sb_git = STRBUF_INIT, sb_repo = STRBUF_INIT;
 	struct strbuf sb = STRBUF_INIT, realpath = STRBUF_INIT;
 	const char *name;
-	struct child_process cp = CHILD_PROCESS_INIT;
 	struct strvec child_env = STRVEC_INIT;
 	unsigned int counter = 0;
 	int len, ret;
@@ -424,7 +423,8 @@ static int add_worktree(const char *path, const char *refname,
 	struct commit *commit = NULL;
 	int is_branch = 0;
 	struct strbuf sb_name = STRBUF_INIT;
-	struct worktree **worktrees;
+	struct worktree **worktrees, *wt = NULL;
+	struct ref_store *wt_refs;
 
 	worktrees = get_worktrees();
 	check_candidate_path(path, opts->force, worktrees, "add");
@@ -500,15 +500,26 @@ static int add_worktree(const char *path, const char *refname,
 	write_file(sb.buf, "../..");
 
 	/*
-	 * This is to keep resolve_ref() happy. We need a valid HEAD
-	 * or is_git_directory() will reject the directory. Any value which
-	 * looks like an object ID will do since it will be immediately
-	 * replaced by the symbolic-ref or update-ref invocation in the new
-	 * worktree.
+	 * Set up the ref store of the worktree and create the HEAD reference.
 	 */
-	strbuf_reset(&sb);
-	strbuf_addf(&sb, "%s/HEAD", sb_repo.buf);
-	write_file(sb.buf, "%s", oid_to_hex(null_oid()));
+	wt = get_linked_worktree(name);
+	if (!wt) {
+		ret = error(_("could not find created worktree '%s'"), name);
+		goto done;
+	}
+	wt_refs = get_worktree_ref_store(wt);
+
+	ret = refs_init_db(wt_refs, REFS_INIT_DB_IS_WORKTREE, &sb);
+	if (ret)
+		goto done;
+
+	if (!is_branch && commit)
+		ret = refs_update_ref(wt_refs, NULL, "HEAD", &commit->object.oid,
+				      NULL, 0, UPDATE_REFS_MSG_ON_ERR);
+	else
+		ret = refs_create_symref(wt_refs, "HEAD", symref.buf, NULL);
+	if (ret)
+		goto done;
 
 	/*
 	 * If the current worktree has sparse-checkout enabled, then copy
@@ -527,22 +538,6 @@ static int add_worktree(const char *path, const char *refname,
 
 	strvec_pushf(&child_env, "%s=%s", GIT_DIR_ENVIRONMENT, sb_git.buf);
 	strvec_pushf(&child_env, "%s=%s", GIT_WORK_TREE_ENVIRONMENT, path);
-	cp.git_cmd = 1;
-
-	if (!is_branch && commit) {
-		strvec_pushl(&cp.args, "update-ref", "HEAD",
-			     oid_to_hex(&commit->object.oid), NULL);
-	} else {
-		strvec_pushl(&cp.args, "symbolic-ref", "HEAD",
-			     symref.buf, NULL);
-		if (opts->quiet)
-			strvec_push(&cp.args, "--quiet");
-	}
-
-	strvec_pushv(&cp.env, child_env.v);
-	ret = run_command(&cp);
-	if (ret)
-		goto done;
 
 	if (opts->orphan &&
 	    (ret = make_worktree_orphan(refname, opts, &child_env)))
@@ -588,6 +583,7 @@ static int add_worktree(const char *path, const char *refname,
 	strbuf_release(&sb_git);
 	strbuf_release(&sb_name);
 	strbuf_release(&realpath);
+	free_worktree(wt);
 	return ret;
 }
 
-- 
2.43.GIT
Previous: Patrick SteinhardtNext: Junio C Hamano
Message 10 of 21 in “worktree: initialize refdb via ref backends”
  1. 0/6 worktree: initialize refdb via ref backendsPatrick Steinhardt, Dec 28, 2023
  2. 1/6 refs: prepare `refs_init_db()` for initializing worktree refsPatrick Steinhardt, Dec 28, 2023
  3. 2/6 setup: move creation of "refs/" into the files backendPatrick Steinhardt, Dec 28, 2023
  4. Karthik NayakJan 2, 2024
  5. Patrick SteinhardtJan 3, 2024
  6. 3/6 refs/files: skip creation of "refs/{heads,tags}" for worktreesPatrick Steinhardt, Dec 28, 2023
  7. Eric SunshineDec 29, 2023
  8. 4/6 builtin/worktree: move setup of commondir file earlierPatrick Steinhardt, Dec 28, 2023
  9. 5/6 worktree: expose interface to look up worktree by namePatrick Steinhardt, Dec 28, 2023
  10. 6/6 builtin/worktree: create refdb via ref backendPatrick Steinhardt, Dec 28, 2023
  11. Junio C HamanoDec 28, 2023
  12. Patrick SteinhardtDec 28, 2023
  13. 0/6 worktree: initialize refdb via ref backendsPatrick Steinhardt, Jan 8, 2024
  14. 1/6 refs: prepare `refs_init_db()` for initializing worktree refsPatrick Steinhardt, Jan 8, 2024
  15. 2/6 setup: move creation of "refs/" into the files backendPatrick Steinhardt, Jan 8, 2024
  16. 3/6 refs/files: skip creation of "refs/{heads,tags}" for worktreesPatrick Steinhardt, Jan 8, 2024
  17. 4/6 builtin/worktree: move setup of commondir file earlierPatrick Steinhardt, Jan 8, 2024
  18. 5/6 worktree: expose interface to look up worktree by namePatrick Steinhardt, Jan 8, 2024
  19. 6/6 builtin/worktree: create refdb via ref backendPatrick Steinhardt, Jan 8, 2024
  20. Karthik NayakJan 16, 2024
  21. Junio C HamanoJan 16, 2024

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.