From patchwork Sun Jan 26 22:02:10 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bence Ferdinandy X-Patchwork-Id: 13950908 Received: from aib29agh127.zrh1.oracleemaildelivery.com (aib29agh127.zrh1.oracleemaildelivery.com [192.29.178.127]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B9CBF1E521 for ; Sun, 26 Jan 2025 22:09:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.29.178.127 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737929381; cv=none; b=B2Pkpff0RP4F28hnM3BSmrOKoV/EogPBJX4WFzJtjtaQ2ZhmaabOiGYtJaxsXv42xrYvWk7p5M99sT7mpCJZDH7Uvaa1tNQpA4Liup9iivUdt4CnyX0sLGmPnrVn+AnejASfpmxf5wG12rEyCV3TpghGGh+h6FWNoAuSJHB/8ZI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737929381; c=relaxed/simple; bh=EJWovt9lmm9fK/kcujJdzUR2C5L+PHPva/m56w3/uu0=; h=From:To:Cc:Subject:Date:Message-id:In-reply-to:References: MIME-version; b=QLlUb1pmOYACbPy0F7ICFRmDUtGshcW7HsbhMy6+QHki6hI9Mp0KZQ/duroZ8DE+RJU/vIYcQ+vuszZqV0WwqjOXUfNqRMBFuxPOHSd7hY2w7ZUI497D27hLIIFgqIOt5jOrMf8f/tzEa6oYd1FtXSw1YaAZIouumyl6xSRW9JM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=fail (p=none dis=none) header.from=ferdinandy.com; spf=pass smtp.mailfrom=zrh1.rp.oracleemaildelivery.com; dkim=pass (2048-bit key) header.d=zrh1.rp.oracleemaildelivery.com header.i=@zrh1.rp.oracleemaildelivery.com header.b=mpuZOR6k; arc=none smtp.client-ip=192.29.178.127 Authentication-Results: smtp.subspace.kernel.org; dmarc=fail (p=none dis=none) header.from=ferdinandy.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=zrh1.rp.oracleemaildelivery.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=zrh1.rp.oracleemaildelivery.com header.i=@zrh1.rp.oracleemaildelivery.com header.b="mpuZOR6k" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; s=prod-zrh-20200406; d=zrh1.rp.oracleemaildelivery.com; h=Date:To:From:Subject:Message-Id:MIME-Version:Sender:List-Unsubscribe:List-Unsubscribe-Post; bh=wpXB9FcIRvnEGo0uLz9YWwlx3Spv3U4R9yrCbVkyPJg=; b=mpuZOR6k6A7PaVQEMQEkGODZW9+zc9JP4aHupLgpGTeSxBTm8okDfd7KWzakMaEVQAO/ciPaKpVq 1qt6yuCelLRBDI8rO1nMcBa0s3FiTAsS8d8k/N2/BaSHeUKblZeSAgOkN+u+2SnzJfsLMWL2Hk0g f6dJIglPLdGVfLMYtKqgtiZi+Ezm1vfK1olEXrwF86UOzgCDFYTc+gDMJVsM57OJbwq3obtcCGDi BqRBQJ6sq624foYfcwxZy8uSRo0yYtvawnU4GG0ibhxYsySPraEVO8NAgHDY0AKXxriX0fUe0LhV rzK7rdbWyoBAK4DZQ+sB5vDYTItsHVcFTaKihg== Received: by omta-ad1-fd3-402-eu-zurich-1.omtaad1.vcndpzrh.oraclevcn.com (Oracle Communications Messaging Server 8.1.0.1.20241107 64bit (built Nov 7 2024)) with ESMTPS id <0SQP00EFBUNJSQ90@omta-ad1-fd3-402-eu-zurich-1.omtaad1.vcndpzrh.oraclevcn.com> for git@vger.kernel.org; Sun, 26 Jan 2025 22:04:31 +0000 (GMT) List-Unsubscribe-Post: List-Unsubscribe=One-Click From: Bence Ferdinandy To: git@vger.kernel.org Cc: Christian Hesse , Christian Hesse , "Patrick Steinhardt" , Bence Ferdinandy Subject: [PATCH v2 1/2] fetch set_head: refactor to use remote directly Date: Sun, 26 Jan 2025 23:02:10 +0100 Message-id: <20250126220403.289742-1-bence@ferdinandy.com> In-reply-to: References: Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-version: 1.0 Content-transfer-encoding: 8bit Reporting-Meta: AAGxQJE2mvPvJYyGsn82nZBi6C+00/KI80E9EqRXX2QbGz3gT1lVU9+HX+sJCJZI 0MPjyFvpW1LwzYkg+DT5peaSWGuN9XSExqU3q63PprS9qAkIOvvuL4CJTX0KFm1d yLh7eYt8GixHeH5WvcOgGjp0SRGmCPg5886wbcXk2B4p3v02YNBGrcsh4EDEm4EA HrznAyMl8Vkx8+aW3wY//vKVRhNqoS0PoCdgsooqRLIS8D5TdSZex+76U5kPNfcs dmkqR8X7GCSDhMwA6TVa+HsyJMB3hc1QwmXsZWfW/n96i53YjActQYa7ljG9KTVO dI+FGsOugeY4jXEwY4DE4M5NB19ae3iTTGSViHkVzcvJZyacsiGhS/nf88Veorat neDpfGTSu4fV+35aZKIrwaHtUV1oj4ZVfsPNgv/Qn5Y0Dn1Vxg/YgW9awdMWLntF yC4IY0vQCvkFEv2hqPHhO0BYnsevOp/66Rir8XoIAji+u/fwMQtNZg1o As a preparatory step to use even more properties from the remote struct, refactor set_head to take the entire struct as a parameter, instead of the necessary bits. This also allows consolidating the use of gtransport->remote in set_head, making the access of the remote's properties consistent in the function. Signed-off-by: Bence Ferdinandy --- Notes: v2: - new patch builtin/fetch.c | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/builtin/fetch.c b/builtin/fetch.c index fe2b26c74a..3167b055d1 100644 --- a/builtin/fetch.c +++ b/builtin/fetch.c @@ -1617,13 +1617,13 @@ static void report_set_head(const char *remote, const char *head_name, strbuf_release(&buf_prefix); } -static int set_head(const struct ref *remote_refs, int follow_remote_head, - const char *no_warn_branch) +static int set_head(const struct ref *remote_refs, struct remote *remote) { int result = 0, create_only, is_bare, was_detached; struct strbuf b_head = STRBUF_INIT, b_remote_head = STRBUF_INIT, b_local_head = STRBUF_INIT; - const char *remote = gtransport->remote->name; + int follow_remote_head = remote->follow_remote_head; + const char *no_warn_branch = remote->no_warn_branch; char *head_name = NULL; struct ref *ref, *matches; struct ref *fetch_map = NULL, **fetch_map_tail = &fetch_map; @@ -1661,8 +1661,8 @@ static int set_head(const struct ref *remote_refs, int follow_remote_head, strbuf_addstr(&b_head, "HEAD"); strbuf_addf(&b_remote_head, "refs/heads/%s", head_name); } else { - strbuf_addf(&b_head, "refs/remotes/%s/HEAD", remote); - strbuf_addf(&b_remote_head, "refs/remotes/%s/%s", remote, head_name); + strbuf_addf(&b_head, "refs/remotes/%s/HEAD", remote->name); + strbuf_addf(&b_remote_head, "refs/remotes/%s/%s", remote->name, head_name); } /* make sure it's valid */ if (!is_bare && !refs_ref_exists(refs, b_remote_head.buf)) { @@ -1678,7 +1678,7 @@ static int set_head(const struct ref *remote_refs, int follow_remote_head, if (verbosity >= 0 && follow_remote_head == FOLLOW_REMOTE_WARN && (!no_warn_branch || strcmp(no_warn_branch, head_name))) - report_set_head(remote, head_name, &b_local_head, was_detached); + report_set_head(remote->name, head_name, &b_local_head, was_detached); cleanup: free(head_name); @@ -1924,8 +1924,7 @@ static int do_fetch(struct transport *transport, "you need to specify exactly one branch with the --set-upstream option")); } } - if (set_head(remote_refs, transport->remote->follow_remote_head, - transport->remote->no_warn_branch)) + if (set_head(remote_refs, transport->remote)) ; /* * Way too many cases where this can go wrong From patchwork Sun Jan 26 22:02:11 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bence Ferdinandy X-Patchwork-Id: 13950907 Received: from aib29agh122.zrh1.oracleemaildelivery.com (aib29agh122.zrh1.oracleemaildelivery.com [192.29.178.122]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 852A65684 for ; Sun, 26 Jan 2025 22:04:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.29.178.122 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737929077; cv=none; b=oOMZ1AsiMm4gxouupNb2T0nYhy49IRq3Dhac9eQ2CT8GMU964b8pYMqpMRXrNjIVDQkhK4kG0dSr96d3oKJy0KYZv28iAeHJuv7eBkonnX2pxPqBtTfXzGg3MSRmfW200cySN+XkKXKAu9zNRN7JUyZoQ+g+XQbclMOhkzZVbXI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737929077; c=relaxed/simple; bh=886ZzNsTY/qF/ruBfi6ihoGznKKoWwX3sYKOrw2ujSM=; h=From:To:Cc:Subject:Date:Message-id:In-reply-to:References: MIME-version; b=gUyHk86CXv17BUbQakAmJXQj0db3Z6l5SAxtXJ2MemNagda/EgzkHMdEGalK3yxUaL12U/6QJQhI85pr2xAXO/xIgaU8n9C5WH1zBaUGUepEXKaW4NAXg7oKeTNh14oi7r2dzEAWAknaH4zZBKdPlaFyHVdtW0286a2VoO/9XL4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=fail (p=none dis=none) header.from=ferdinandy.com; spf=pass smtp.mailfrom=zrh1.rp.oracleemaildelivery.com; dkim=pass (2048-bit key) header.d=zrh1.rp.oracleemaildelivery.com header.i=@zrh1.rp.oracleemaildelivery.com header.b=lCqqwvWJ; arc=none smtp.client-ip=192.29.178.122 Authentication-Results: smtp.subspace.kernel.org; dmarc=fail (p=none dis=none) header.from=ferdinandy.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=zrh1.rp.oracleemaildelivery.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=zrh1.rp.oracleemaildelivery.com header.i=@zrh1.rp.oracleemaildelivery.com header.b="lCqqwvWJ" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; s=prod-zrh-20200406; d=zrh1.rp.oracleemaildelivery.com; h=Date:To:From:Subject:Message-Id:MIME-Version:Sender:List-Unsubscribe:List-Unsubscribe-Post; bh=/SMi4sC4ZONTlkHDKMXWbWahG6fPgtMRqJx6UBv/E24=; b=lCqqwvWJ90BwiaOOOoj92hN+kW///kAKVL09W7vpFty9kqFgXqOvC9bOevi8cdet8GPFeUAXwsry mVtYzcYMxOSC4yppg0baahEVYsNQRkgTYEaTpKasVUQ6dB0NFNIWqyGrA55yK5vrgxcH1kIPWVBp W4jxZ/MEooQKXTeWiNFRrD6dZDihkuQIkyPucLOOvX2u2ifI5tWeytxW18etVHA1spCXWpzhPQbw ZtVvoSuOyvKj/8rTh2BGsGpPt2XBOCs5hVMJSCSW9LGT/ToldsnV3FIbueGsxrt6hBvr1TSoBEnv Mdv5MiCXjQcBYl5RcnPOrZLB/4ejHj9RxTKHiQ== Received: by omta-ad1-fd1-401-eu-zurich-1.omtaad1.vcndpzrh.oraclevcn.com (Oracle Communications Messaging Server 8.1.0.1.20241107 64bit (built Nov 7 2024)) with ESMTPS id <0SQP00M36UNKN710@omta-ad1-fd1-401-eu-zurich-1.omtaad1.vcndpzrh.oraclevcn.com> for git@vger.kernel.org; Sun, 26 Jan 2025 22:04:32 +0000 (GMT) List-Unsubscribe-Post: List-Unsubscribe=One-Click From: Bence Ferdinandy To: git@vger.kernel.org Cc: Christian Hesse , Christian Hesse , "Patrick Steinhardt" , Bence Ferdinandy Subject: [PATCH v2 2/2] fetch set_head: fix non-mirror remotes in bare repositories Date: Sun, 26 Jan 2025 23:02:11 +0100 Message-id: <20250126220403.289742-2-bence@ferdinandy.com> In-reply-to: <20250126220403.289742-1-bence@ferdinandy.com> References: <20250126220403.289742-1-bence@ferdinandy.com> Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-version: 1.0 Content-transfer-encoding: 8bit Reporting-Meta: AAGcIlV6XF6lzTI15503/qHCpMsBJuI0q7tH2id5Ehse+WP8PX2zQcVqEXV4R5VT Wh2L0e2NI5+03SoyerI7Dkhykyw5jkZzJBFln2dQRLIyCAawNbpd67PnNMcSRQLe 0fM4LmFo8iPkrYnIF+gk0SjJJRH96sohQHWSVPb7WvfWGkZ128sZ8RfhBFLYpz/s b4NQMAwBPBoznrepasf257DtbLw9e91D1a7VkxUm+WjKVJTcz6gS13Xs9gtqKQ59 PDHMxl72PnxRbe38YWcxc1kr0rqAC36ZOXrcePh4LBuRYBpsaDL+uQbddqs8A0cC IQVxyIHwaPPtFvD4HowAfOKxwsZGpHQ/vDmhgbhjErmYPx1m6Vw4tqd2XQoOxYvB 3V08YO8Q4bBSsnJE0eKo28dgiaREmhG5qFy+LXfGgH6HHdiBFaLh2kGLQPBuay6R ywP8E8pS+wqR+KzRuBlNDMrUUnuNiiQwiYhta/PvrmsMeB8ypp/wtUc= In b1b713f722 (fetch set_head: handle mirrored bare repositories, 2024-11-22) it was implicitly assumed that all remotes will be mirrors in a bare repository, thus fetching a non-mirrored remote could lead to HEAD pointing to a non-existent reference. Make sure we only overwrite HEAD if we are in a bare repository and fetching from a mirror. Otherwise, proceed as normally, and create refs/remotes//HEAD instead. Reported-by: Christian Hesse Signed-off-by: Bence Ferdinandy --- Notes: v2: - fixed too many parameters to set_head - cleaned up the style of tests builtin/fetch.c | 10 +++++----- t/t5505-remote.sh | 10 ++++++++++ t/t5510-fetch.sh | 17 +++++++++++++++++ 3 files changed, 32 insertions(+), 5 deletions(-) diff --git a/builtin/fetch.c b/builtin/fetch.c index 3167b055d1..1c740d5aac 100644 --- a/builtin/fetch.c +++ b/builtin/fetch.c @@ -1619,7 +1619,7 @@ static void report_set_head(const char *remote, const char *head_name, static int set_head(const struct ref *remote_refs, struct remote *remote) { - int result = 0, create_only, is_bare, was_detached; + int result = 0, create_only, baremirror, was_detached; struct strbuf b_head = STRBUF_INIT, b_remote_head = STRBUF_INIT, b_local_head = STRBUF_INIT; int follow_remote_head = remote->follow_remote_head; @@ -1655,9 +1655,9 @@ static int set_head(const struct ref *remote_refs, struct remote *remote) if (!head_name) goto cleanup; - is_bare = is_bare_repository(); - create_only = follow_remote_head == FOLLOW_REMOTE_ALWAYS ? 0 : !is_bare; - if (is_bare) { + baremirror = is_bare_repository() && remote->mirror; + create_only = follow_remote_head == FOLLOW_REMOTE_ALWAYS ? 0 : !baremirror; + if (baremirror) { strbuf_addstr(&b_head, "HEAD"); strbuf_addf(&b_remote_head, "refs/heads/%s", head_name); } else { @@ -1665,7 +1665,7 @@ static int set_head(const struct ref *remote_refs, struct remote *remote) strbuf_addf(&b_remote_head, "refs/remotes/%s/%s", remote->name, head_name); } /* make sure it's valid */ - if (!is_bare && !refs_ref_exists(refs, b_remote_head.buf)) { + if (!baremirror && !refs_ref_exists(refs, b_remote_head.buf)) { result = 1; goto cleanup; } diff --git a/t/t5505-remote.sh b/t/t5505-remote.sh index 519f7973e3..66e373f71d 100755 --- a/t/t5505-remote.sh +++ b/t/t5505-remote.sh @@ -589,6 +589,16 @@ test_expect_success 'add --mirror setting HEAD' ' ) ' +test_expect_success 'non-mirror fetch does not interfere with mirror' ' + test_when_finished rm -rf headnotmain && + ( + git init --bare -b notmain headnotmain && + cd headnotmain && + git remote add -f other ../two && + test "$(git symbolic-ref HEAD)" = "refs/heads/notmain" + ) +' + test_expect_success 'add --mirror=fetch' ' mkdir mirror-fetch && git init -b main mirror-fetch/parent && diff --git a/t/t5510-fetch.sh b/t/t5510-fetch.sh index 2d9587059f..c9d7b46c87 100755 --- a/t/t5510-fetch.sh +++ b/t/t5510-fetch.sh @@ -84,6 +84,23 @@ test_expect_success "fetch test remote HEAD" ' branch=$(git rev-parse refs/remotes/origin/main) && test "z$head" = "z$branch"' +test_expect_success "fetch test remote HEAD in bare repository" ' + test_when_finished rm -rf barerepo && + ( + cd "$D" && + git init --bare barerepo && + cd barerepo && + git remote add upstream ../two && + git fetch upstream && + git rev-parse --verify refs/remotes/upstream/HEAD && + git rev-parse --verify refs/remotes/upstream/main && + head=$(git rev-parse refs/remotes/upstream/HEAD) && + branch=$(git rev-parse refs/remotes/upstream/main) && + test "z$head" = "z$branch" + ) +' + + test_expect_success "fetch test remote HEAD change" ' cd "$D" && cd two &&