From patchwork Thu Feb 13 18:59:08 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Linus Arver via GitGitGadget X-Patchwork-Id: 11381053 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 0E151139A for ; Thu, 13 Feb 2020 18:59:17 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id DEBAD218AC for ; Thu, 13 Feb 2020 18:59:16 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="FV+783xn" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728272AbgBMS7O (ORCPT ); Thu, 13 Feb 2020 13:59:14 -0500 Received: from mail-wr1-f67.google.com ([209.85.221.67]:43097 "EHLO mail-wr1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727665AbgBMS7O (ORCPT ); Thu, 13 Feb 2020 13:59:14 -0500 Received: by mail-wr1-f67.google.com with SMTP id r11so7994086wrq.10 for ; Thu, 13 Feb 2020 10:59:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=message-id:in-reply-to:references:from:date:subject:fcc :content-transfer-encoding:mime-version:to:cc; bh=IdKCESMQ+SzRi7//uH5ZlU0/7iBrSjkU4CP5tGuZb7k=; b=FV+783xn5jbc/QcUBuDxVE8bp78RYDN1mTw5N/Sfasz0NC0op+Z0MuvkrGtII9+gMX pswTA0YSlzHY/sFo8OgF2Zj3y5WXnt0snzoVTiljmHBpJhR++rQ6RDNW/nNWy82sp7vL IbYL+os8HHd2zWRnee0v+x3pehuc2AsVOG51/CJEuEF1wHO9diZ2n+L5UGaMkVUxBMmF nhSQzFRGBohd0gmQTxiZyUNWbNXwNsY4jKwYH3PndzO2CacD2RKvRueFzS6wTyOA94Zo GOZEdH5OWiFE3zyp7l4qz/A5PG8lLYdovbyMJl01OECoL9ewKy5dZURpzRZ+REZ7fssa b5UQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:message-id:in-reply-to:references:from:date :subject:fcc:content-transfer-encoding:mime-version:to:cc; bh=IdKCESMQ+SzRi7//uH5ZlU0/7iBrSjkU4CP5tGuZb7k=; b=acM2W7kcTcxF4yzleAaNS2bF1UZyegQzAvyxfrgt7Xd41nI80XsPKD2XU5rm3TInLF luy5KF8u9U7V1i7oDf4PB9CEYFsvkCIHb3MLIrBiI8MfZWe60f87LpLwIO1r1Dfi6HRa JFyVHEOIoK/P6fOh9vNF5B1DTjUkENDRFJ8DgmBYXLGplDpPplQT31tTdFQ8P72gAh1U CrkIbhovWl4p1v2t1LBBr8gxBOe4WyT/YzHbpSYR+RTPkBb++xH4OppT5w590KxR/Rx3 bIRGrkiBUfFewlOR3KAyghq/6P4p1rtAkNqx+l3Bwtq4fR3SpFGk7fbfc3aF0qGIOLJ/ 3Pyw== X-Gm-Message-State: APjAAAWiNiFYaAhkVjcBsRPy+0dca5Zc2cVmx5kW7kpbymfe4LrJADx0 odrUjyQLxhXWYxypxZHk2v4JJ/47 X-Google-Smtp-Source: APXvYqwYpZ0STRwK/MaLXSUIiE6tdXO0v/k5XnWrMSHY1giaP87jpLKwul9fpyMYUUryfRWNjj6SRA== X-Received: by 2002:a5d:6886:: with SMTP id h6mr23227296wru.154.1581620352638; Thu, 13 Feb 2020 10:59:12 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id m21sm3942535wmi.27.2020.02.13.10.59.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 13 Feb 2020 10:59:12 -0800 (PST) Message-Id: <902c8a3f17153ebd3871aa51e5cabe9338438655.1581620351.git.gitgitgadget@gmail.com> In-Reply-To: References: From: "Hariom Verma via GitGitGadget" Date: Thu, 13 Feb 2020 18:59:08 +0000 Subject: [PATCH 1/3] get_main_worktree(): allow it to be called in the Git directory Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Hariom Verma , Hariom Verma Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Hariom Verma When called in the Git directory of a non-bare repository, this function would not return the directory of the main worktree, but of the Git directory instead. The reason: when the Git directory is the current working directory, the absolute path of the common directory will be reported with a trailing `/.git/.`, which the code of `get_main_worktree()` does not handle correctly. Let's fix this. Helped-by: Johannes Schindelin Signed-off-by: Hariom Verma --- worktree.c | 1 + 1 file changed, 1 insertion(+) diff --git a/worktree.c b/worktree.c index 5b4793caa3..7c8cd21317 100644 --- a/worktree.c +++ b/worktree.c @@ -51,6 +51,7 @@ static struct worktree *get_main_worktree(void) struct strbuf worktree_path = STRBUF_INIT; strbuf_add_absolute_path(&worktree_path, get_git_common_dir()); + strbuf_strip_suffix(&worktree_path, "/."); if (!strbuf_strip_suffix(&worktree_path, "/.git")) strbuf_strip_suffix(&worktree_path, "/."); From patchwork Thu Feb 13 18:59:09 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Linus Arver via GitGitGadget X-Patchwork-Id: 11381051 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 0E2C91395 for ; Thu, 13 Feb 2020 18:59:16 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id E16EC206CC for ; Thu, 13 Feb 2020 18:59:15 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="LkOSnBmg" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728306AbgBMS7P (ORCPT ); Thu, 13 Feb 2020 13:59:15 -0500 Received: from mail-wm1-f66.google.com ([209.85.128.66]:54201 "EHLO mail-wm1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728222AbgBMS7O (ORCPT ); Thu, 13 Feb 2020 13:59:14 -0500 Received: by mail-wm1-f66.google.com with SMTP id s10so7442544wmh.3 for ; Thu, 13 Feb 2020 10:59:14 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=message-id:in-reply-to:references:from:date:subject:fcc :content-transfer-encoding:mime-version:to:cc; bh=YsSaa/Z5vJG5CKfKt+TlxmKS+vUz5pHfvqPlP5QYe48=; b=LkOSnBmgchmcUg8MTbR9QkYWgECrHCDHSyt5Zt1H1JJ9PYmbKzscsWn0jdGb26wWhY DryT7J7CH+TBve/BqK21ztEzILvbjkCzl/RtfjbVJu5V5/td3c8JYPAWyWfaQGxHDE99 JtmbcTjT7d5svvZKKBqk3XXzVcy/7/yTDTAzuzsVa2wnAjkPCLWFvFA0630wNoOgDsD1 w4Tk9M6jwbNPkh8EM798o5JONK033sJpImXHOdDtoMbUyuJ/mcqk2M0xhOd1MlEfFk+n oEJ1yXhV5jBoz9kz17/MkshDyyDNgqYkPnGXkosW+00v+XSc8cXP+hKUooTjczESwmKc Z1vg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:message-id:in-reply-to:references:from:date :subject:fcc:content-transfer-encoding:mime-version:to:cc; bh=YsSaa/Z5vJG5CKfKt+TlxmKS+vUz5pHfvqPlP5QYe48=; b=h/EBdGgIP3NdL1RWtqqw65SvlHSNk+FxjMruwMzm/eC4Vq+kQkDPq6gq8EMZEJuI9W QxOnKR8xl5UGOPHFbLsPLo5AGmTcZdheTuHV6AX6OJNV+v/+msmik1eRQxTenhWWIKhx IGs9k3rT0ijM+DB8XVa/4O0F3VOXd9aPfEAec9oBHIbfcUeTEoPiC6npzSs89X2mCgB8 RTwBXdfacHOPCtUGJsvPkN5tFzCc/kIexU2YhlL8MO6Gsh7CqOy4VU5DR0VOkvYP7y5C Kg7lH2HQLEpeMxRNSRwLKvcKeD7+OyeyHBMYDX0X9Ge5Ckevd1reIePGrrymUYGVsH90 jj3Q== X-Gm-Message-State: APjAAAUbbzt/ENScRMP5IpT+r4hZ52zZRiYGeuNCSJsm8sDHV62Iw4mY Ia/vVnlYyND8zML3ooty6csa+Jsm X-Google-Smtp-Source: APXvYqzmPi3Se7legm1dMdihaXAaAwwwEK2dBpHCew5H1LWNlTutD1AcpUhcJqpjpIVTkEgwzZA4FQ== X-Received: by 2002:a05:600c:2503:: with SMTP id d3mr6997318wma.84.1581620353311; Thu, 13 Feb 2020 10:59:13 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id n8sm3780841wrx.42.2020.02.13.10.59.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 13 Feb 2020 10:59:12 -0800 (PST) Message-Id: In-Reply-To: References: From: "Hariom Verma via GitGitGadget" Date: Thu, 13 Feb 2020 18:59:09 +0000 Subject: [PATCH 2/3] t5509: initialized `pushee` as bare repository Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Hariom Verma , Hariom Verma Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Hariom Verma `receive.denyCurrentBranch` currently has a bug where it allows pushing into the current branch of a non-bare repository as long as it does not have any commits. This would cause t5509 to fail once that bug is fixed because it pushes into an unborn current branch. In t5509, no operations are performed inside `pushee`, as it is only a target for `git push` and `git ls-remote` calls. Therefore it does not need to have a worktree. So, it is safe to change `pushee` to a bare repository. Helped-by: Johannes Schindelin Signed-off-by: Hariom Verma --- t/t5509-fetch-push-namespaces.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/t/t5509-fetch-push-namespaces.sh b/t/t5509-fetch-push-namespaces.sh index 75cbfcc392..e3975bd21d 100755 --- a/t/t5509-fetch-push-namespaces.sh +++ b/t/t5509-fetch-push-namespaces.sh @@ -20,7 +20,7 @@ test_expect_success setup ' ) && commit0=$(cd original && git rev-parse HEAD^) && commit1=$(cd original && git rev-parse HEAD) && - git init pushee && + git init --bare pushee && git init puller ' From patchwork Thu Feb 13 18:59:10 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Linus Arver via GitGitGadget X-Patchwork-Id: 11381055 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 6D7E1139A for ; Thu, 13 Feb 2020 18:59:18 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 44160218AC for ; Thu, 13 Feb 2020 18:59:18 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Ec8Inef/" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728309AbgBMS7R (ORCPT ); Thu, 13 Feb 2020 13:59:17 -0500 Received: from mail-wm1-f67.google.com ([209.85.128.67]:55737 "EHLO mail-wm1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728281AbgBMS7P (ORCPT ); Thu, 13 Feb 2020 13:59:15 -0500 Received: by mail-wm1-f67.google.com with SMTP id q9so7433657wmj.5 for ; Thu, 13 Feb 2020 10:59:14 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=message-id:in-reply-to:references:from:date:subject:fcc :content-transfer-encoding:mime-version:to:cc; bh=I/tmn/heGEuyyW3OW6ZqgPnBfQmjdjVsUC5ImpXSbsw=; b=Ec8Inef/vTCYiNSxfEgTW3tBipsZKvgFFPZ6/aaBJM5lOB1qfXuqYcgtGxsXtQ7ZuG MYEINOi0raPJV5T5L/Bz0/sfJlhVR7tAkLYzYuBa2Ymkg3jal2xTIIfq85BDuH0zG1vd GbV/o8Msy+a3ZF1afVEj3uIRJYULeP453wb5sLSjy+kb5bx+N6toF4btRCsW7wPYHdZD 8lue8Z134omYPtb+yOZ1L3W2j9AA+aU81Izs43aUy/WtGXbie53Ta/nOG6McW+x22WQx Xd5kcMSLwGrql8qus9TyrmMQ957sfpOS3PfBfpf4WKEvUiSIrT2eM9xn+Fwqfh8pzTol qmHA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:message-id:in-reply-to:references:from:date :subject:fcc:content-transfer-encoding:mime-version:to:cc; bh=I/tmn/heGEuyyW3OW6ZqgPnBfQmjdjVsUC5ImpXSbsw=; b=oQuBsqOuwFTOpURNZeZfNEwWjKCRm73uWgBFoXZE2pO2FNBGAhGjdagaSZRasgw6pi jfGHDX+bp+IQ6/B9NbABNVKxJuC8qQpn893u76YaozcajImtuD0l4D++nTRkv1qDiUt9 yftEnmdQOVGmB8gDt+WZ/850WuBcX4YpphSfV2OdO6ij3nL0DAm2Bh1D+K6FLenIJuft r4AGLmu/UekRpA4YGaFmxeLrFxyDztkhARvgGNn8cnVVSQZFDWsMDY7Yo3EXkoXZO2wo IEsJ0wmgzvW170b/jpYCzNMKgRzkuKEA1Bl0n4m0L9I5MfeYtT4oUGD+g3WJnerlhg9v 4/5g== X-Gm-Message-State: APjAAAXAgREfsLthC2YMal45GQOiWIo5SJ7WCrDcVWJ+2Q8YomnLsgbL cjvMYK6eifiBo9QPuu6RGkEbb6zv X-Google-Smtp-Source: APXvYqwiSovmHcsrQve8HK0/hQFRiV8uo1HNhnX8yfSd+4MI+bs5PiO6sTAtxUiGxlGs1JUviHRmsg== X-Received: by 2002:a1c:b789:: with SMTP id h131mr7155233wmf.148.1581620354015; Thu, 13 Feb 2020 10:59:14 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id f62sm4193718wmf.36.2020.02.13.10.59.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 13 Feb 2020 10:59:13 -0800 (PST) Message-Id: <3352c0bffc19f17518b292ad36c38f902801b06a.1581620351.git.gitgitgadget@gmail.com> In-Reply-To: References: From: "Hariom Verma via GitGitGadget" Date: Thu, 13 Feb 2020 18:59:10 +0000 Subject: [PATCH 3/3] receive.denyCurrentBranch: respect all worktrees Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Hariom Verma , Hariom Verma Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Hariom Verma The receive.denyCurrentBranch config option controls what happens if you push to a branch that is checked out into a non-bare repository. By default, it rejects it. It can be disabled via `ignore` or `warn`. Another yet trickier option is `updateInstead`. However, this setting was forgotten when the git worktree command was introduced: only the main worktree's current branch is respected. With this change, all worktrees are respected. That change also leads to revealing another bug, i.e. `receive.denyCurrentBranch = true` was ignored when pushing into a non-bare repository's unborn current branch. As `is_ref_checked_out()` returns 0 which means `receive-pack` does not get into conditional statement to switch `deny_current_branch` accordingly(ignore, warn, refuse, unconfigured, updateInstead). receive.denyCurrentBranch uses the function `refs_resolve_ref_unsafe()` (called via `resolve_refdup()`) to resolve the symbolic ref HEAD, but that function fails when HEAD does not point at a valid commit. As we replace the call to `refs_resolve_ref_unsafe()` with `find_shared_symref()`, which has no problem finding the worktree for a given branch even if it is unborn yet, this bug is fixed at the same time: receive.denyCurrentBranch now also handles worktrees with unborn branches as intended. Helped-by: Johannes Schindelin Signed-off-by: Hariom Verma --- builtin/receive-pack.c | 37 ++++++++++++++++++++----------------- t/t5516-fetch-push.sh | 11 +++++++++++ 2 files changed, 31 insertions(+), 17 deletions(-) diff --git a/builtin/receive-pack.c b/builtin/receive-pack.c index 411e0b4d99..b5ca3123b7 100644 --- a/builtin/receive-pack.c +++ b/builtin/receive-pack.c @@ -27,6 +27,7 @@ #include "object-store.h" #include "protocol.h" #include "commit-reach.h" +#include "worktree.h" static const char * const receive_pack_usage[] = { N_("git receive-pack "), @@ -816,16 +817,6 @@ static int run_update_hook(struct command *cmd) return finish_command(&proc); } -static int is_ref_checked_out(const char *ref) -{ - if (is_bare_repository()) - return 0; - - if (!head_name) - return 0; - return !strcmp(head_name, ref); -} - static char *refuse_unconfigured_deny_msg = N_("By default, updating the current branch in a non-bare repository\n" "is denied, because it will make the index and work tree inconsistent\n" @@ -997,16 +988,27 @@ static const char *push_to_checkout(unsigned char *hash, return NULL; } -static const char *update_worktree(unsigned char *sha1) +static const char *update_worktree(unsigned char *sha1, const struct worktree *worktree) { - const char *retval; - const char *work_tree = git_work_tree_cfg ? git_work_tree_cfg : ".."; + const char *retval, *work_tree, *git_dir = NULL; struct argv_array env = ARGV_ARRAY_INIT; + if (worktree && worktree->path) + work_tree = worktree->path; + else if (git_work_tree_cfg) + work_tree = git_work_tree_cfg; + else + work_tree = ".."; + if (is_bare_repository()) return "denyCurrentBranch = updateInstead needs a worktree"; + + if (worktree) + git_dir = get_worktree_git_dir(worktree); + if (!git_dir) + git_dir = get_git_dir(); - argv_array_pushf(&env, "GIT_DIR=%s", absolute_path(get_git_dir())); + argv_array_pushf(&env, "GIT_DIR=%s", absolute_path(git_dir)); if (!find_hook(push_to_checkout_hook)) retval = push_to_deploy(sha1, &env, work_tree); @@ -1026,6 +1028,7 @@ static const char *update(struct command *cmd, struct shallow_info *si) struct object_id *old_oid = &cmd->old_oid; struct object_id *new_oid = &cmd->new_oid; int do_update_worktree = 0; + const struct worktree *worktree = is_bare_repository() ? NULL : find_shared_symref("HEAD", name); /* only refs/... are allowed */ if (!starts_with(name, "refs/") || check_refname_format(name + 5, 0)) { @@ -1037,7 +1040,7 @@ static const char *update(struct command *cmd, struct shallow_info *si) free(namespaced_name); namespaced_name = strbuf_detach(&namespaced_name_buf, NULL); - if (is_ref_checked_out(namespaced_name)) { + if (worktree) { switch (deny_current_branch) { case DENY_IGNORE: break; @@ -1069,7 +1072,7 @@ static const char *update(struct command *cmd, struct shallow_info *si) return "deletion prohibited"; } - if (head_name && !strcmp(namespaced_name, head_name)) { + if (worktree || (head_name && !strcmp(namespaced_name, head_name))) { switch (deny_delete_current) { case DENY_IGNORE: break; @@ -1118,7 +1121,7 @@ static const char *update(struct command *cmd, struct shallow_info *si) } if (do_update_worktree) { - ret = update_worktree(new_oid->hash); + ret = update_worktree(new_oid->hash, find_shared_symref("HEAD", name)); if (ret) return ret; } diff --git a/t/t5516-fetch-push.sh b/t/t5516-fetch-push.sh index c81ca360ac..6608e391f0 100755 --- a/t/t5516-fetch-push.sh +++ b/t/t5516-fetch-push.sh @@ -1712,4 +1712,15 @@ test_expect_success 'updateInstead with push-to-checkout hook' ' ) ' +test_expect_success 'denyCurrentBranch and worktrees' ' + git worktree add new-wt && + git clone . cloned && + test_commit -C cloned first && + test_config receive.denyCurrentBranch refuse && + test_must_fail git -C cloned push origin HEAD:new-wt && + test_config receive.denyCurrentBranch updateInstead && + git -C cloned push origin HEAD:new-wt && + test_must_fail git -C cloned push --delete origin new-wt +' + test_done