From patchwork Mon Aug 31 06:57:56 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Sunshine X-Patchwork-Id: 11745577 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 B665C913 for ; Mon, 31 Aug 2020 06:59:12 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 9E700208A9 for ; Mon, 31 Aug 2020 06:59:12 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="K3igeYjz" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726574AbgHaG7I (ORCPT ); Mon, 31 Aug 2020 02:59:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60072 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725829AbgHaG7G (ORCPT ); Mon, 31 Aug 2020 02:59:06 -0400 Received: from mail-io1-xd42.google.com (mail-io1-xd42.google.com [IPv6:2607:f8b0:4864:20::d42]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AC8B2C061573 for ; Sun, 30 Aug 2020 23:59:04 -0700 (PDT) Received: by mail-io1-xd42.google.com with SMTP id h10so4799722ioq.6 for ; Sun, 30 Aug 2020 23:59:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=6+1oJBhMqB6No61lU1+MpTpY2WNpKWz2IVTiODSFM1M=; b=K3igeYjzOslhYcis+qL3Nehl+EQTqBOD7ovAX4Jr6P5JpC5ENjxvrHxPwGx1Kx/l/n zGYUbxdlGs+QNIsUwzSlerl/TKnpKrRLmqWPSbJ4xEtHZNWL9g96z3vLejSDLFMpaFjG 62pgyQT/QkSyBwV4g5Jy9P7FH34pb1+JHIlafZvDkCCoB2wTYHEw1aiqkAjeC8ROtiyN mUi1tsjFeJD1LSLqGbRdt+NyrNuU/Ko8Dl/6Z2WAXq2S3fFPF/lNvjLuWuBPowb38wkZ tJztgF2luopWsuAF8Laadiqb2V09U3O4JWqqqKIQy5LByaMl3i091RxrZPTcZtKqA9K4 fxEQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references:mime-version:content-transfer-encoding; bh=6+1oJBhMqB6No61lU1+MpTpY2WNpKWz2IVTiODSFM1M=; b=dmneD4pBayd/6k7Ve+DQKTqSiFi1EZD6zIhNF3nVmn52dSk8mpTAK6K6vluqR+zvdo qjM5eOmEcCRJUc1CziemI7nGNXPspm2khz8aKeH7b8NmOViyMMHybWSgVJxaY8Yr7V6I sIA9H4kVOkzhr2CpLHGs6ynpo7EmwiSA7FoPaXj/sCcxnQQ9PHPBUC1VK9TQLTSXHJGW f+9fumSS+gmcw0rnbZhPSF7b/7cYYK5IALGUdo+1x4Js0pxDrTVGa1sPp4xiZGhQsGdY uJLRcEiFJxx49o5xAwTYOzSnfEj5z+U6BiJ/c9JuhXywH7wMJnMmoMiJnOqVKO+6Z6Au tFdg== X-Gm-Message-State: AOAM531Rd2VnfdDxGOz0i71LP+2Ozj4uWj6F3s83PhKBrSHqvrYiSEwK mZRhfs+MUddmYM8yzlD2SAK9zgPcVVQ= X-Google-Smtp-Source: ABdhPJw/5XU+ruPq02Lec2PkC4XXrjv8sz8L6qnlbFmr7SUZVD0y/YeTEHbNlDCBX0twZ/ZY8+Dt0Q== X-Received: by 2002:a02:8286:: with SMTP id t6mr13329jag.87.1598857143153; Sun, 30 Aug 2020 23:59:03 -0700 (PDT) Received: from localhost.localdomain (user-12l2dpj.cable.mindspring.com. [69.81.55.51]) by smtp.gmail.com with ESMTPSA id x1sm1878615ilo.50.2020.08.30.23.59.01 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sun, 30 Aug 2020 23:59:02 -0700 (PDT) From: Eric Sunshine To: git@vger.kernel.org Cc: =?utf-8?q?Henr=C3=A9_Botha?= , Jeff King , Junio C Hamano , Johannes Schindelin , Ramsay Jones , Eric Sunshine Subject: [PATCH v2 1/5] worktree: add skeleton "repair" command Date: Mon, 31 Aug 2020 02:57:56 -0400 Message-Id: <20200831065800.62502-2-sunshine@sunshineco.com> X-Mailer: git-send-email 2.28.0.531.g41c3d8a546 In-Reply-To: <20200831065800.62502-1-sunshine@sunshineco.com> References: <20200827082129.56149-1-sunshine@sunshineco.com> <20200831065800.62502-1-sunshine@sunshineco.com> MIME-Version: 1.0 Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Worktree administrative files can become corrupted or outdated due to external factors. Although, it is often possible to recover from such situations by hand-tweaking these files, doing so requires intimate knowledge of worktree internals. While information necessary to make such repairs manually can be obtained from git-worktree.txt and gitrepository-layout.txt, we can assist users more directly by teaching git-worktree how to repair its administrative files itself (at least to some extent). Therefore, add a "git worktree repair" command which attempts to correct common problems which may arise due to factors beyond Git's control. At this stage, the "repair" command is a mere skeleton; subsequent commits will flesh out the functionality. Signed-off-by: Eric Sunshine --- Documentation/git-worktree.txt | 6 ++++++ builtin/worktree.c | 15 +++++++++++++++ t/t2406-worktree-repair.sh | 11 +++++++++++ 3 files changed, 32 insertions(+) create mode 100755 t/t2406-worktree-repair.sh diff --git a/Documentation/git-worktree.txt b/Documentation/git-worktree.txt index 6ee6ec7982..ae432d39a8 100644 --- a/Documentation/git-worktree.txt +++ b/Documentation/git-worktree.txt @@ -15,6 +15,7 @@ SYNOPSIS 'git worktree move' 'git worktree prune' [-n] [-v] [--expire ] 'git worktree remove' [-f] +'git worktree repair' 'git worktree unlock' DESCRIPTION @@ -110,6 +111,11 @@ and no modification in tracked files) can be removed. Unclean working trees or ones with submodules can be removed with `--force`. The main working tree cannot be removed. +repair:: + +Repair working tree administrative files, if possible, if they have +become corrupted or outdated due to external factors. + unlock:: Unlock a working tree, allowing it to be pruned, moved or deleted. diff --git a/builtin/worktree.c b/builtin/worktree.c index 378f332b5d..88af412d4f 100644 --- a/builtin/worktree.c +++ b/builtin/worktree.c @@ -1030,6 +1030,19 @@ static int remove_worktree(int ac, const char **av, const char *prefix) return ret; } +static int repair(int ac, const char **av, const char *prefix) +{ + struct option options[] = { + OPT_END() + }; + int rc = 0; + + ac = parse_options(ac, av, prefix, options, worktree_usage, 0); + if (ac) + usage_with_options(worktree_usage, options); + return rc; +} + int cmd_worktree(int ac, const char **av, const char *prefix) { struct option options[] = { @@ -1056,5 +1069,7 @@ int cmd_worktree(int ac, const char **av, const char *prefix) return move_worktree(ac - 1, av + 1, prefix); if (!strcmp(av[1], "remove")) return remove_worktree(ac - 1, av + 1, prefix); + if (!strcmp(av[1], "repair")) + return repair(ac - 1, av + 1, prefix); usage_with_options(worktree_usage, options); } diff --git a/t/t2406-worktree-repair.sh b/t/t2406-worktree-repair.sh new file mode 100755 index 0000000000..cc679e1a21 --- /dev/null +++ b/t/t2406-worktree-repair.sh @@ -0,0 +1,11 @@ +#!/bin/sh + +test_description='test git worktree repair' + +. ./test-lib.sh + +test_expect_success setup ' + test_commit init +' + +test_done From patchwork Mon Aug 31 06:57:57 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Sunshine X-Patchwork-Id: 11745585 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 6D4C6913 for ; Mon, 31 Aug 2020 06:59:26 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 4B45E208A9 for ; Mon, 31 Aug 2020 06:59:26 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="uyS1ELMG" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727844AbgHaG7U (ORCPT ); Mon, 31 Aug 2020 02:59:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60082 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725891AbgHaG7G (ORCPT ); Mon, 31 Aug 2020 02:59:06 -0400 Received: from mail-io1-xd44.google.com (mail-io1-xd44.google.com [IPv6:2607:f8b0:4864:20::d44]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2E7AEC061755 for ; Sun, 30 Aug 2020 23:59:05 -0700 (PDT) Received: by mail-io1-xd44.google.com with SMTP id l8so4827318ios.2 for ; Sun, 30 Aug 2020 23:59:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=3CJJa4kEA1ZML0Bx7zOmTmNSwC4jaZoRn+dElykuMk4=; b=uyS1ELMGb+/H5LqJuznxhlBo/HmstrF9BjzIvEqffNlWdQqTQlKmBztrtF/DBuUZ9p qXPy0kHqGYCrsCC826UaOjzroyo74ZK2hoq8Bm3+FLFt9oqUwDnncuaGbROu9JMA/w/V P0afPAZW4Q8JMV/kRNkN6S+H98PSOLZUuwApGDYfhwRQ6J2Ly1A/YJSamsHS9PgpbrNC O98RhLDzRT11i82Uib0yMksrP2Ng1cs3zvUCzeraGHj8dv7E2F1znPvZYuoQnpZzRAP7 lHo4LDRYUD5dKDVYoGXaumRv7//P7ZnrEY6VMSP2Oeg3Hi42rIpSxgxy+RODztahLZ9/ uslg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references:mime-version:content-transfer-encoding; bh=3CJJa4kEA1ZML0Bx7zOmTmNSwC4jaZoRn+dElykuMk4=; b=eXJ+6jAmZzpyY0hDFXW3KkUATEO5XDr2kmRePWSWvirNz5EaadN35zllz26wjOEMrg 5emmksboJrgoCQxW2i+jo2I2DGDpZxYvQkp3nRKCTttLpjnM0VWB1/Gulo2C343gisNj 41fGpoI5px26dGbi1kHW6uRGBxmOZOuipIfJqiluj0gzwB5YoGczKd79QB+Ub6M7NZZW hEJXPx4Wk3T+RyxgMrsGwHb7+dSBCpkWx80IfEuRDT/DeeyxzqaALwWclfYQ1rKHm0vs zC479YbEOzajbE7X8Pn32/WqgLsk3h4ruoXuJV/jnAzPRGCZ/fKmVd1p52KLozUfZLp1 sdBw== X-Gm-Message-State: AOAM531MctkgxaaO+y5++Fk3bzzGMDM+A3T/19qIWLkynhJjsYjwhYkc xP+qHu+we5iigSW/weyVZpqP9Dc70K4= X-Google-Smtp-Source: ABdhPJz7d+N050CkmSn/A9UOz0vYj9Y1zvxwtnjCgCFHoTcKx2abpzNF5itqGQ3+VENn3g2Q1MRVGg== X-Received: by 2002:a02:94c8:: with SMTP id x66mr35888jah.64.1598857144309; Sun, 30 Aug 2020 23:59:04 -0700 (PDT) Received: from localhost.localdomain (user-12l2dpj.cable.mindspring.com. [69.81.55.51]) by smtp.gmail.com with ESMTPSA id x1sm1878615ilo.50.2020.08.30.23.59.03 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sun, 30 Aug 2020 23:59:03 -0700 (PDT) From: Eric Sunshine To: git@vger.kernel.org Cc: =?utf-8?q?Henr=C3=A9_Botha?= , Jeff King , Junio C Hamano , Johannes Schindelin , Ramsay Jones , Eric Sunshine Subject: [PATCH v2 2/5] worktree: teach "repair" to fix worktree back-links to main worktree Date: Mon, 31 Aug 2020 02:57:57 -0400 Message-Id: <20200831065800.62502-3-sunshine@sunshineco.com> X-Mailer: git-send-email 2.28.0.531.g41c3d8a546 In-Reply-To: <20200831065800.62502-1-sunshine@sunshineco.com> References: <20200827082129.56149-1-sunshine@sunshineco.com> <20200831065800.62502-1-sunshine@sunshineco.com> MIME-Version: 1.0 Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org The .git file in a linked worktree is a "gitfile" which points back to the .git/worktrees/ entry in the main worktree or bare repository. If a worktree's .git file is deleted or becomes corrupted or outdated, then the linked worktree won't know how to find the repository or any of its own administrative files (such as 'index', 'HEAD', etc.). An easy way for the .git file to become outdated is for the user to move the main worktree or bare repository. Although it is possible to manually update each linked worktree's .git file to reflect the new repository location, doing so requires a level of knowledge about worktree internals beyond what a user should be expected to know offhand. Therefore, teach "git worktree repair" how to repair broken or outdated worktree .git files automatically. (For this to work, the command must be invoked from within the main worktree or bare repository, or from within a worktree which has not become disconnected from the repository -- such as one which was created after the repository was moved.) Signed-off-by: Eric Sunshine --- Documentation/git-worktree.txt | 10 ++++- builtin/worktree.c | 12 +++++ t/t2406-worktree-repair.sh | 82 ++++++++++++++++++++++++++++++++++ worktree.c | 61 +++++++++++++++++++++++++ worktree.h | 11 +++++ 5 files changed, 175 insertions(+), 1 deletion(-) diff --git a/Documentation/git-worktree.txt b/Documentation/git-worktree.txt index ae432d39a8..34fe47cecd 100644 --- a/Documentation/git-worktree.txt +++ b/Documentation/git-worktree.txt @@ -98,7 +98,10 @@ with `--reason`. move:: Move a working tree to a new location. Note that the main working tree -or linked working trees containing submodules cannot be moved. +or linked working trees containing submodules cannot be moved with this +command. (The `git worktree repair` command, however, can reestablish +the connection with linked working trees if you move the main working +tree manually.) prune:: @@ -115,6 +118,11 @@ repair:: Repair working tree administrative files, if possible, if they have become corrupted or outdated due to external factors. ++ +For instance, if the main working tree (or bare repository) is moved, +linked working trees will be unable to locate it. Running `repair` in +the main working tree will reestablish the connection from linked +working trees back to the main working tree. unlock:: diff --git a/builtin/worktree.c b/builtin/worktree.c index 88af412d4f..68b0032428 100644 --- a/builtin/worktree.c +++ b/builtin/worktree.c @@ -1030,6 +1030,17 @@ static int remove_worktree(int ac, const char **av, const char *prefix) return ret; } +static void report_repair(int iserr, const char *path, const char *msg, void *cb_data) +{ + if (!iserr) { + printf_ln(_("repair: %s: %s"), msg, path); + } else { + int *exit_status = (int *)cb_data; + fprintf_ln(stderr, _("error: %s: %s"), msg, path); + *exit_status = 1; + } +} + static int repair(int ac, const char **av, const char *prefix) { struct option options[] = { @@ -1040,6 +1051,7 @@ static int repair(int ac, const char **av, const char *prefix) ac = parse_options(ac, av, prefix, options, worktree_usage, 0); if (ac) usage_with_options(worktree_usage, options); + repair_worktrees(report_repair, &rc); return rc; } diff --git a/t/t2406-worktree-repair.sh b/t/t2406-worktree-repair.sh index cc679e1a21..ef59cdce95 100755 --- a/t/t2406-worktree-repair.sh +++ b/t/t2406-worktree-repair.sh @@ -8,4 +8,86 @@ test_expect_success setup ' test_commit init ' +test_expect_success 'skip missing worktree' ' + test_when_finished "git worktree prune" && + git worktree add --detach missing && + rm -rf missing && + git worktree repair >out 2>err && + test_must_be_empty out && + test_must_be_empty err +' + +test_expect_success 'worktree path not directory' ' + test_when_finished "git worktree prune" && + git worktree add --detach notdir && + rm -rf notdir && + >notdir && + test_must_fail git worktree repair >out 2>err && + test_must_be_empty out && + test_i18ngrep "not a directory" err +' + +test_expect_success "don't clobber .git repo" ' + test_when_finished "rm -rf repo && git worktree prune" && + git worktree add --detach repo && + rm -rf repo && + test_create_repo repo && + test_must_fail git worktree repair >out 2>err && + test_must_be_empty out && + test_i18ngrep ".git is not a file" err +' + +test_corrupt_gitfile () { + butcher=$1 && + problem=$2 && + repairdir=${3:-.} && + test_when_finished 'rm -rf corrupt && git worktree prune' && + git worktree add --detach corrupt && + git -C corrupt rev-parse --absolute-git-dir >expect && + eval "$butcher" && + git -C "$repairdir" worktree repair >out 2>err && + test_i18ngrep "$problem" out && + test_must_be_empty err && + git -C corrupt rev-parse --absolute-git-dir >actual && + test_cmp expect actual +} + +test_expect_success 'repair missing .git file' ' + test_corrupt_gitfile "rm -f corrupt/.git" ".git file broken" +' + +test_expect_success 'repair bogus .git file' ' + test_corrupt_gitfile "echo \"gitdir: /nowhere\" >corrupt/.git" \ + ".git file broken" +' + +test_expect_success 'repair incorrect .git file' ' + test_when_finished "rm -rf other && git worktree prune" && + test_create_repo other && + other=$(git -C other rev-parse --absolute-git-dir) && + test_corrupt_gitfile "echo \"gitdir: $other\" >corrupt/.git" \ + ".git file incorrect" +' + +test_expect_success 'repair .git file from main/.git' ' + test_corrupt_gitfile "rm -f corrupt/.git" ".git file broken" .git +' + +test_expect_success 'repair .git file from linked worktree' ' + test_when_finished "rm -rf other && git worktree prune" && + git worktree add --detach other && + test_corrupt_gitfile "rm -f corrupt/.git" ".git file broken" other +' + +test_expect_success 'repair .git file from bare.git' ' + test_when_finished "rm -rf bare.git corrupt && git worktree prune" && + git clone --bare . bare.git && + git -C bare.git worktree add --detach ../corrupt && + git -C corrupt rev-parse --absolute-git-dir >expect && + rm -f corrupt/.git && + git -C bare.git worktree repair && + git -C corrupt rev-parse --absolute-git-dir >actual && + test_cmp expect actual +' + test_done diff --git a/worktree.c b/worktree.c index 62217b4a6b..3ad93cc4aa 100644 --- a/worktree.c +++ b/worktree.c @@ -571,3 +571,64 @@ int other_head_refs(each_ref_fn fn, void *cb_data) free_worktrees(worktrees); return ret; } + +/* + * Repair worktree's /path/to/worktree/.git file if missing, corrupt, or not + * pointing at /worktrees/. + */ +static void repair_gitfile(struct worktree *wt, + worktree_repair_fn fn, void *cb_data) +{ + struct strbuf dotgit = STRBUF_INIT; + struct strbuf repo = STRBUF_INIT; + char *backlink; + const char *repair = NULL; + int err; + + /* missing worktree can't be repaired */ + if (!file_exists(wt->path)) + return; + + if (!is_directory(wt->path)) { + fn(1, wt->path, _("not a directory"), cb_data); + return; + } + + strbuf_realpath(&repo, git_common_path("worktrees/%s", wt->id), 1); + strbuf_addf(&dotgit, "%s/.git", wt->path); + backlink = xstrdup_or_null(read_gitfile_gently(dotgit.buf, &err)); + + if (err == READ_GITFILE_ERR_NOT_A_FILE) + fn(1, wt->path, _(".git is not a file"), cb_data); + else if (err) + repair = _(".git file broken"); + else if (fspathcmp(backlink, repo.buf)) + repair = _(".git file incorrect"); + + if (repair) { + fn(0, wt->path, repair, cb_data); + write_file(dotgit.buf, "gitdir: %s", repo.buf); + } + + free(backlink); + strbuf_release(&repo); + strbuf_release(&dotgit); +} + +static void repair_noop(int iserr, const char *path, const char *msg, + void *cb_data) +{ + /* nothing */ +} + +void repair_worktrees(worktree_repair_fn fn, void *cb_data) +{ + struct worktree **worktrees = get_worktrees(); + struct worktree **wt = worktrees + 1; /* +1 skips main worktree */ + + if (!fn) + fn = repair_noop; + for (; *wt; wt++) + repair_gitfile(*wt, fn, cb_data); + free_worktrees(worktrees); +} diff --git a/worktree.h b/worktree.h index 516744c433..4fcb01348c 100644 --- a/worktree.h +++ b/worktree.h @@ -89,6 +89,17 @@ int validate_worktree(const struct worktree *wt, void update_worktree_location(struct worktree *wt, const char *path_); +typedef void (* worktree_repair_fn)(int iserr, const char *path, + const char *msg, void *cb_data); + +/* + * Visit each registered linked worktree and repair corruptions. For each + * repair made or error encountered while attempting a repair, the callback + * function, if non-NULL, is called with the path of the worktree and a + * description of the repair or error, along with the callback user-data. + */ +void repair_worktrees(worktree_repair_fn, void *cb_data); + /* * Free up the memory for worktree(s) */ From patchwork Mon Aug 31 06:57:58 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Sunshine X-Patchwork-Id: 11745581 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 EA427138A for ; Mon, 31 Aug 2020 06:59:21 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id C993D208DB for ; Mon, 31 Aug 2020 06:59:21 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="S0xT5V0J" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727859AbgHaG7V (ORCPT ); Mon, 31 Aug 2020 02:59:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60084 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726618AbgHaG7I (ORCPT ); Mon, 31 Aug 2020 02:59:08 -0400 Received: from mail-io1-xd2d.google.com (mail-io1-xd2d.google.com [IPv6:2607:f8b0:4864:20::d2d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B4FC1C061236 for ; Sun, 30 Aug 2020 23:59:06 -0700 (PDT) Received: by mail-io1-xd2d.google.com with SMTP id u126so4763855iod.12 for ; Sun, 30 Aug 2020 23:59:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=MTE4ITPVf8njHTROTxm3oXPhL6Y6OWgYs8COmJqEeWI=; b=S0xT5V0Jx/SQjbpOAApTYauMQ64mj28dmPiAUQUnHxeVn8LAKwmDwkCqXpGKMM1RFX aWgkNsGvaocw0IhOMj3ZW0ts+Xv7FxNrYRgPViTamiJuCb1okBQzV1pMReFhJA+g1tRI HlNofpnqMRYOKI4zwnIcBEd9kR8RjsoLTBEFMjsXq14024cKJdedK2aiL1qMIcZ0BD0R WZ5TtWn2OF4NxodD3ciD0rer1673/M/K2R3aTj6wrjaUgWW0sbahvDXJ3XFl7RlbBtkt otoAC4PdJ36pPz3/T044UngoG1Wd3nE+rlcAXoh+6mfMryW3tSPLaZIbfyewEU2mbuut 1LQg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references:mime-version:content-transfer-encoding; bh=MTE4ITPVf8njHTROTxm3oXPhL6Y6OWgYs8COmJqEeWI=; b=gkIKg7QUxPYKHveanID+cMIGYxVYSpigrIeSNxha94+BD1lVVFRzFEfQPQCmva6ahu nAaAR8oW0vgZthLG7TameFhvLYFtPhbRtR3+ZP+X983zLtrghWaaA7SRn0cvD09mdUYl EuSNfQ0dOABnGlHr3hndTIm6OklcYU91BU3aXMohijEEKCacfAjUdmbZaP7Sglt4dXHN rgSCn9jJFV9haqbjZTki9/wv3+Vu5Zh6rpTX/OApisyMSUnPy+xQuhMiHch5RUZy6XE0 ko6PSP27bA6ci1qqU6m140IBAnSvobP/Isa7FEOJnRzMXjUh1Q7Bir2Rr1pU4zLtfetJ 05xA== X-Gm-Message-State: AOAM530FJyyxZcsF01eJ2/wNRNgz3wiFQqC+SDteAl+3KYh4iEfBeT0a 7ewWt5FlhowtUJ+xgqkPBTZ1q2UY/VU= X-Google-Smtp-Source: ABdhPJx7aBhvcRIoJ8Bz+8ZfggAXQWAfdxRKhWzY8C8jW4mwiPSfkGplWnpx9hspgw7eqsCnGM36yA== X-Received: by 2002:a02:820b:: with SMTP id o11mr26084jag.136.1598857145451; Sun, 30 Aug 2020 23:59:05 -0700 (PDT) Received: from localhost.localdomain (user-12l2dpj.cable.mindspring.com. [69.81.55.51]) by smtp.gmail.com with ESMTPSA id x1sm1878615ilo.50.2020.08.30.23.59.04 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sun, 30 Aug 2020 23:59:04 -0700 (PDT) From: Eric Sunshine To: git@vger.kernel.org Cc: =?utf-8?q?Henr=C3=A9_Botha?= , Jeff King , Junio C Hamano , Johannes Schindelin , Ramsay Jones , Eric Sunshine Subject: [PATCH v2 3/5] worktree: teach "repair" to fix outgoing links to worktrees Date: Mon, 31 Aug 2020 02:57:58 -0400 Message-Id: <20200831065800.62502-4-sunshine@sunshineco.com> X-Mailer: git-send-email 2.28.0.531.g41c3d8a546 In-Reply-To: <20200831065800.62502-1-sunshine@sunshineco.com> References: <20200827082129.56149-1-sunshine@sunshineco.com> <20200831065800.62502-1-sunshine@sunshineco.com> MIME-Version: 1.0 Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org The .git/worktrees//gitdir file points at the location of a linked worktree's .git file. Its content must be of the form /path/to/worktree/.git (from which the location of the worktree itself can be derived by stripping the "/.git" suffix). If the gitdir file is deleted or becomes corrupted or outdated, then Git will be unable to find the linked worktree. An easy way for the gitdir file to become outdated is for the user to move the worktree manually (without using "git worktree move"). Although it is possible to manually update the gitdir file to reflect the new linked worktree location, doing so requires a level of knowledge about worktree internals beyond what a user should be expected to know offhand. Therefore, teach "git worktree repair" how to repair broken or outdated .git/worktrees//gitdir files automatically. (For this to work, the command must either be invoked from within the worktree whose gitdir file requires repair, or from within the main or any linked worktree by providing the path of the broken worktree as an argument to "git worktree repair".) Signed-off-by: Eric Sunshine --- Documentation/git-worktree.txt | 14 ++++-- builtin/worktree.c | 7 ++- t/t2406-worktree-repair.sh | 86 ++++++++++++++++++++++++++++++++++ worktree.c | 74 +++++++++++++++++++++++++++++ worktree.h | 12 +++++ 5 files changed, 188 insertions(+), 5 deletions(-) diff --git a/Documentation/git-worktree.txt b/Documentation/git-worktree.txt index 34fe47cecd..f70cda4b36 100644 --- a/Documentation/git-worktree.txt +++ b/Documentation/git-worktree.txt @@ -15,7 +15,7 @@ SYNOPSIS 'git worktree move' 'git worktree prune' [-n] [-v] [--expire ] 'git worktree remove' [-f] -'git worktree repair' +'git worktree repair' [...] 'git worktree unlock' DESCRIPTION @@ -114,7 +114,7 @@ and no modification in tracked files) can be removed. Unclean working trees or ones with submodules can be removed with `--force`. The main working tree cannot be removed. -repair:: +repair [...]:: Repair working tree administrative files, if possible, if they have become corrupted or outdated due to external factors. @@ -123,6 +123,13 @@ For instance, if the main working tree (or bare repository) is moved, linked working trees will be unable to locate it. Running `repair` in the main working tree will reestablish the connection from linked working trees back to the main working tree. ++ +Similarly, if a linked working tree is moved without using `git worktree +move`, the main working tree (or bare repository) will be unable to +locate it. Running `repair` within the recently-moved working tree will +reestablish the connection. If multiple linked working trees are moved, +running `repair` from any working tree with each tree's new `` as +an argument, will reestablish the connection to all the specified paths. unlock:: @@ -317,7 +324,8 @@ in the entry's directory. For example, if a linked working tree is moved to `/newpath/test-next` and its `.git` file points to `/path/main/.git/worktrees/test-next`, then update `/path/main/.git/worktrees/test-next/gitdir` to reference `/newpath/test-next` -instead. +instead. Better yet, run `git worktree repair` to reestablish the connection +automatically. To prevent a `$GIT_DIR/worktrees` entry from being pruned (which can be useful in some situations, such as when the diff --git a/builtin/worktree.c b/builtin/worktree.c index 68b0032428..8165343145 100644 --- a/builtin/worktree.c +++ b/builtin/worktree.c @@ -1043,15 +1043,18 @@ static void report_repair(int iserr, const char *path, const char *msg, void *cb static int repair(int ac, const char **av, const char *prefix) { + const char **p; + const char *self[] = { ".", NULL }; struct option options[] = { OPT_END() }; int rc = 0; ac = parse_options(ac, av, prefix, options, worktree_usage, 0); - if (ac) - usage_with_options(worktree_usage, options); repair_worktrees(report_repair, &rc); + p = ac > 0 ? av : self; + for (; *p; p++) + repair_worktree_at_path(*p, report_repair, &rc); return rc; } diff --git a/t/t2406-worktree-repair.sh b/t/t2406-worktree-repair.sh index ef59cdce95..1fe468bfe8 100755 --- a/t/t2406-worktree-repair.sh +++ b/t/t2406-worktree-repair.sh @@ -90,4 +90,90 @@ test_expect_success 'repair .git file from bare.git' ' test_cmp expect actual ' +test_expect_success 'invalid worktree path' ' + test_must_fail git worktree repair /notvalid >out 2>err && + test_must_be_empty out && + test_i18ngrep "not a valid path" err +' + +test_expect_success 'repo not found; .git not file' ' + test_when_finished "rm -rf not-a-worktree" && + test_create_repo not-a-worktree && + test_must_fail git worktree repair not-a-worktree >out 2>err && + test_must_be_empty out && + test_i18ngrep ".git is not a file" err +' + +test_expect_success 'repo not found; .git file broken' ' + test_when_finished "rm -rf orig moved && git worktree prune" && + git worktree add --detach orig && + echo /invalid >orig/.git && + mv orig moved && + test_must_fail git worktree repair moved >out 2>err && + test_must_be_empty out && + test_i18ngrep ".git file broken" err +' + +test_expect_success 'repair broken gitdir' ' + test_when_finished "rm -rf orig moved && git worktree prune" && + git worktree add --detach orig && + sed s,orig/\.git$,moved/.git, .git/worktrees/orig/gitdir >expect && + rm .git/worktrees/orig/gitdir && + mv orig moved && + git worktree repair moved >out 2>err && + test_cmp expect .git/worktrees/orig/gitdir && + test_i18ngrep "gitdir unreadable" out && + test_must_be_empty err +' + +test_expect_success 'repair incorrect gitdir' ' + test_when_finished "rm -rf orig moved && git worktree prune" && + git worktree add --detach orig && + sed s,orig/\.git$,moved/.git, .git/worktrees/orig/gitdir >expect && + mv orig moved && + git worktree repair moved >out 2>err && + test_cmp expect .git/worktrees/orig/gitdir && + test_i18ngrep "gitdir incorrect" out && + test_must_be_empty err +' + +test_expect_success 'repair gitdir (implicit) from linked worktree' ' + test_when_finished "rm -rf orig moved && git worktree prune" && + git worktree add --detach orig && + sed s,orig/\.git$,moved/.git, .git/worktrees/orig/gitdir >expect && + mv orig moved && + git -C moved worktree repair >out 2>err && + test_cmp expect .git/worktrees/orig/gitdir && + test_i18ngrep "gitdir incorrect" out && + test_must_be_empty err +' + +test_expect_success 'unable to repair gitdir (implicit) from main worktree' ' + test_when_finished "rm -rf orig moved && git worktree prune" && + git worktree add --detach orig && + cat .git/worktrees/orig/gitdir >expect && + mv orig moved && + git worktree repair >out 2>err && + test_cmp expect .git/worktrees/orig/gitdir && + test_must_be_empty out && + test_must_be_empty err +' + +test_expect_success 'repair multiple gitdir files' ' + test_when_finished "rm -rf orig1 orig2 moved1 moved2 && + git worktree prune" && + git worktree add --detach orig1 && + git worktree add --detach orig2 && + sed s,orig1/\.git$,moved1/.git, .git/worktrees/orig1/gitdir >expect1 && + sed s,orig2/\.git$,moved2/.git, .git/worktrees/orig2/gitdir >expect2 && + mv orig1 moved1 && + mv orig2 moved2 && + git worktree repair moved1 moved2 >out 2>err && + test_cmp expect1 .git/worktrees/orig1/gitdir && + test_cmp expect2 .git/worktrees/orig2/gitdir && + test_i18ngrep "gitdir incorrect:.*orig1/gitdir$" out && + test_i18ngrep "gitdir incorrect:.*orig2/gitdir$" out && + test_must_be_empty err +' + test_done diff --git a/worktree.c b/worktree.c index 3ad93cc4aa..46a5fb8447 100644 --- a/worktree.c +++ b/worktree.c @@ -632,3 +632,77 @@ void repair_worktrees(worktree_repair_fn fn, void *cb_data) repair_gitfile(*wt, fn, cb_data); free_worktrees(worktrees); } + +static int is_main_worktree_path(const char *path) +{ + struct strbuf target = STRBUF_INIT; + struct strbuf maindir = STRBUF_INIT; + int cmp; + + strbuf_add_real_path(&target, path); + strbuf_strip_suffix(&target, "/.git"); + strbuf_add_real_path(&maindir, get_git_common_dir()); + strbuf_strip_suffix(&maindir, "/.git"); + cmp = fspathcmp(maindir.buf, target.buf); + + strbuf_release(&maindir); + strbuf_release(&target); + return !cmp; +} + +/* + * Repair /worktrees//gitdir if missing, corrupt, or not pointing at + * the worktree's path. + */ +void repair_worktree_at_path(const char *path, + worktree_repair_fn fn, void *cb_data) +{ + struct strbuf dotgit = STRBUF_INIT; + struct strbuf realdotgit = STRBUF_INIT; + struct strbuf gitdir = STRBUF_INIT; + struct strbuf olddotgit = STRBUF_INIT; + char *backlink = NULL; + const char *repair = NULL; + int err; + + if (!fn) + fn = repair_noop; + + if (is_main_worktree_path(path)) + goto done; + + strbuf_addf(&dotgit, "%s/.git", path); + if (!strbuf_realpath(&realdotgit, dotgit.buf, 0)) { + fn(1, path, _("not a valid path"), cb_data); + goto done; + } + + backlink = xstrdup_or_null(read_gitfile_gently(realdotgit.buf, &err)); + if (err == READ_GITFILE_ERR_NOT_A_FILE) { + fn(1, realdotgit.buf, _("unable to locate repository; .git is not a file"), cb_data); + goto done; + } else if (err) { + fn(1, realdotgit.buf, _("unable to locate repository; .git file broken"), cb_data); + goto done; + } + + strbuf_addf(&gitdir, "%s/gitdir", backlink); + if (strbuf_read_file(&olddotgit, gitdir.buf, 0) < 0) + repair = _("gitdir unreadable"); + else { + strbuf_rtrim(&olddotgit); + if (fspathcmp(olddotgit.buf, realdotgit.buf)) + repair = _("gitdir incorrect"); + } + + if (repair) { + fn(0, gitdir.buf, repair, cb_data); + write_file(gitdir.buf, "%s", realdotgit.buf); + } +done: + free(backlink); + strbuf_release(&olddotgit); + strbuf_release(&gitdir); + strbuf_release(&realdotgit); + strbuf_release(&dotgit); +} diff --git a/worktree.h b/worktree.h index 4fcb01348c..ff7b62e434 100644 --- a/worktree.h +++ b/worktree.h @@ -100,6 +100,18 @@ typedef void (* worktree_repair_fn)(int iserr, const char *path, */ void repair_worktrees(worktree_repair_fn, void *cb_data); +/* + * Repair administrative files corresponding to the worktree at the given path. + * The worktree's .git file pointing at the repository must be intact for the + * repair to succeed. Useful for re-associating an orphaned worktree with the + * repository if the worktree has been moved manually (without using "git + * worktree move"). For each repair made or error encountered while attempting + * a repair, the callback function, if non-NULL, is called with the path of the + * worktree and a description of the repair or error, along with the callback + * user-data. + */ +void repair_worktree_at_path(const char *, worktree_repair_fn, void *cb_data); + /* * Free up the memory for worktree(s) */ From patchwork Mon Aug 31 06:57:59 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Eric Sunshine X-Patchwork-Id: 11745587 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 2D10B913 for ; Mon, 31 Aug 2020 06:59:27 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 117F92072D for ; Mon, 31 Aug 2020 06:59:27 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="S+l7qPRV" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727842AbgHaG7U (ORCPT ); Mon, 31 Aug 2020 02:59:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60086 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725954AbgHaG7H (ORCPT ); Mon, 31 Aug 2020 02:59:07 -0400 Received: from mail-io1-xd43.google.com (mail-io1-xd43.google.com [IPv6:2607:f8b0:4864:20::d43]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A263EC061573 for ; Sun, 30 Aug 2020 23:59:07 -0700 (PDT) Received: by mail-io1-xd43.google.com with SMTP id g14so4841997iom.0 for ; Sun, 30 Aug 2020 23:59:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=2KK4AphuII7tWHXVVnabDu6ebr07vqPjbP766wPgBq8=; b=S+l7qPRVfvHsMfiWMPl+0HHSB/9f9DcdvLIEPNWx7uXeLfP83DkuA2a1eve9+mourU bZUpF7+kzLoI2FQh/7tI6wxPm84/dF2e06ZyZqxyMI5/91BnFl/A4/Ws6/TVUTB9vYxW O/YuPUjykWlshO3Xr4Pxt54DJy+s12NkVX+l/k99H6NY64PyGFx8dVB5rIQrR3hvilIC UfkjEJXtefIMM5zHfODHCDUJp6JiEMw9uJ/aO6ZG25ID3wLl6p6hGba0V50S4n91Sj34 ODWEy2z8/IDBKbEQIQH+xvOpQRPixjAfeSGcTDYpk4Tir60w6iv7rsPfrW4ZS019AWhy SikA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references:mime-version:content-transfer-encoding; bh=2KK4AphuII7tWHXVVnabDu6ebr07vqPjbP766wPgBq8=; b=N5Cg+vWMIgpkwQxjO+9Krsw7QFygcJB//4drKDj96cwnuPFhxdoTgHjbMBuD7L9zQd XZLjz1HtWSbXJE6Z2Hk3JA8/Nt67XKBtUPSfYrSBeOUDKkTRyZlgrUUjanktqQ/vlyCn qrh7b3PXtC2SP1h+dX0P7ByPZ/WHPWDWBd/hLiYD8BFHa4d2JFchqHGpSa3pbKzzruZ4 aDwqqI2jmm49A+46LxxAkUP++vTr2cwdLfwviF+Omz6rvnLpdBldM/QR7uBGaWXIv1MY G2T5GwsEv8V/7DzmAizEZEmpQt/cCg1HAGeiIwE1asK2piH8voYj/P/ijOcCUaRK1HDj PUnQ== X-Gm-Message-State: AOAM530CdP/7i63CYTjvYKkBBoqwCth+cU2onEHTlOHa+pKWRoLKsvpa z4h9LW4KHuRjkvw/sv42Y50x9vGhOSY= X-Google-Smtp-Source: ABdhPJwvuSmpuLnZtWzbH+rVowdaHRG3sfljutpxXQxQft/VftQ28ZqnLoJ6VqXx4/orOv0bbai3hA== X-Received: by 2002:a02:454:: with SMTP id 81mr26911jab.142.1598857146650; Sun, 30 Aug 2020 23:59:06 -0700 (PDT) Received: from localhost.localdomain (user-12l2dpj.cable.mindspring.com. [69.81.55.51]) by smtp.gmail.com with ESMTPSA id x1sm1878615ilo.50.2020.08.30.23.59.05 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sun, 30 Aug 2020 23:59:05 -0700 (PDT) From: Eric Sunshine To: git@vger.kernel.org Cc: =?utf-8?q?Henr=C3=A9_Botha?= , Jeff King , Junio C Hamano , Johannes Schindelin , Ramsay Jones , Eric Sunshine Subject: [PATCH v2 4/5] init: teach --separate-git-dir to repair linked worktrees Date: Mon, 31 Aug 2020 02:57:59 -0400 Message-Id: <20200831065800.62502-5-sunshine@sunshineco.com> X-Mailer: git-send-email 2.28.0.531.g41c3d8a546 In-Reply-To: <20200831065800.62502-1-sunshine@sunshineco.com> References: <20200827082129.56149-1-sunshine@sunshineco.com> <20200831065800.62502-1-sunshine@sunshineco.com> MIME-Version: 1.0 Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org A linked worktree's .git file is a "gitfile" pointing at the .git/worktrees/ directory within the repository. When `git init --separate-git-dir=` is used on an existing repository to relocate the repository's .git/ directory to a different location, it neglects to update the .git files of linked worktrees, thus breaking the worktrees by making it impossible for them to locate the repository. Fix this by teaching --separate-git-dir to repair the .git file of each linked worktree to point at the new repository location. Reported-by: Henré Botha Signed-off-by: Eric Sunshine --- builtin/init-db.c | 2 ++ t/t0001-init.sh | 11 +++++++++++ 2 files changed, 13 insertions(+) diff --git a/builtin/init-db.c b/builtin/init-db.c index bbc9bc78f9..7b915d88ab 100644 --- a/builtin/init-db.c +++ b/builtin/init-db.c @@ -9,6 +9,7 @@ #include "builtin.h" #include "exec-cmd.h" #include "parse-options.h" +#include "worktree.h" #ifndef DEFAULT_GIT_TEMPLATE_DIR #define DEFAULT_GIT_TEMPLATE_DIR "/usr/share/git-core/templates" @@ -364,6 +365,7 @@ static void separate_git_dir(const char *git_dir, const char *git_link) if (rename(src, git_dir)) die_errno(_("unable to move %s to %s"), src, git_dir); + repair_worktrees(NULL, NULL); } write_file(git_link, "gitdir: %s", git_dir); diff --git a/t/t0001-init.sh b/t/t0001-init.sh index 50222a10c5..e489eb4ddb 100755 --- a/t/t0001-init.sh +++ b/t/t0001-init.sh @@ -405,6 +405,17 @@ test_expect_success SYMLINKS 're-init to move gitdir symlink' ' test_path_is_dir realgitdir/refs ' +test_expect_success 're-init to move gitdir with linked worktrees' ' + test_when_finished "rm -rf mainwt linkwt seprepo" && + git init mainwt && + test_commit -C mainwt gumby && + git -C mainwt worktree add --detach ../linkwt && + git -C mainwt init --separate-git-dir ../seprepo && + git -C mainwt rev-parse --git-common-dir >expect && + git -C linkwt rev-parse --git-common-dir >actual && + test_cmp expect actual +' + test_expect_success MINGW '.git hidden' ' rm -rf newdir && ( From patchwork Mon Aug 31 06:58:00 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Eric Sunshine X-Patchwork-Id: 11745583 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 1290C913 for ; Mon, 31 Aug 2020 06:59:25 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id EF3D3208DB for ; Mon, 31 Aug 2020 06:59:24 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="OHH9RiHf" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727872AbgHaG7V (ORCPT ); Mon, 31 Aug 2020 02:59:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60092 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727799AbgHaG7J (ORCPT ); Mon, 31 Aug 2020 02:59:09 -0400 Received: from mail-io1-xd42.google.com (mail-io1-xd42.google.com [IPv6:2607:f8b0:4864:20::d42]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 14354C061575 for ; Sun, 30 Aug 2020 23:59:09 -0700 (PDT) Received: by mail-io1-xd42.google.com with SMTP id g128so4774211iof.11 for ; Sun, 30 Aug 2020 23:59:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=InFgH+cFkstzFiPOBjm5B+UoOAkTHeaKKQoVHPUPcwo=; b=OHH9RiHff3FzhVzx+8Z/AxXd/C3qOW+GrqjSBL9Xp6oujRQMaqwR9T9SnK9ecAqG/t fd3XfhiOvy/5Y5roQlm4CQRHBqB100ArCwBUx+YSTjO6uFJVAjoJ23YIGF5+A20K4w3x w2lRX5J7KQPbhrwOm8EGrIsUkkQFRSckniOsJdPF9ZVlyNPzVPVCXSFil7aqSptSiVHo 9rH8jBsXtzuQ58aOW8BSdvb3Unf1fbEN15+1YGE+Z2z0f8L+v31EsFMt0fgC/LVwgzYC OKtqrASDXNXzBXrhb0+0jk6p7P7LOpzSPzGNR9/Wjoe4sE5QarJGZ2DXymoVpiH8zwOv uIIw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references:mime-version:content-transfer-encoding; bh=InFgH+cFkstzFiPOBjm5B+UoOAkTHeaKKQoVHPUPcwo=; b=Vt0EOu04Wej918D1FvvjwlesNDMBkrvApMIJGVFjwx2t+dFkTBiEOsuiYrc87mOUYk rLavWvC0+UUykx7minUW+yNZBxsE/T8CJTl6f03203PZwyj2opatKHh+ayiTmCzOATia ZIQ97tFtbzsMgJqhHZVpn/Q2b+JZiPMFMUSHxvi0lgYg4bZGglhq5fDOWLTL9QMHvm3m JcRpmKS2oWM4qdiISDbwbea6LD6QYf/ae98O6sZYv6TiOhvrJ2NhH1PAYC0pFj0gYUiG Vs3MYZ1RJ4DOXGZFXV5qY4ku8daDXfIuFw/G9WTLz0yQUwiQO7BnWeNGRmbV/DERpZAR 6Y7g== X-Gm-Message-State: AOAM532blbydDwKGtT42ukHYuPyBeBH4NTJPhU6CzWOOh0nlT3AE7omC h3MD47I7UQ0eFZC4gjaRK42klv5cZ2w= X-Google-Smtp-Source: ABdhPJzq0wC6JCXMrh/P+Cvb7Akz72bIJYA3mYPYQ932xGRA66twPb1Cs0wrIBoKfKryqyUESpFlxQ== X-Received: by 2002:a02:840f:: with SMTP id k15mr30836jah.100.1598857147870; Sun, 30 Aug 2020 23:59:07 -0700 (PDT) Received: from localhost.localdomain (user-12l2dpj.cable.mindspring.com. [69.81.55.51]) by smtp.gmail.com with ESMTPSA id x1sm1878615ilo.50.2020.08.30.23.59.06 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sun, 30 Aug 2020 23:59:07 -0700 (PDT) From: Eric Sunshine To: git@vger.kernel.org Cc: =?utf-8?q?Henr=C3=A9_Botha?= , Jeff King , Junio C Hamano , Johannes Schindelin , Ramsay Jones , Eric Sunshine Subject: [PATCH v2 5/5] init: make --separate-git-dir work from within linked worktree Date: Mon, 31 Aug 2020 02:58:00 -0400 Message-Id: <20200831065800.62502-6-sunshine@sunshineco.com> X-Mailer: git-send-email 2.28.0.531.g41c3d8a546 In-Reply-To: <20200831065800.62502-1-sunshine@sunshineco.com> References: <20200827082129.56149-1-sunshine@sunshineco.com> <20200831065800.62502-1-sunshine@sunshineco.com> MIME-Version: 1.0 Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org The intention of `git init --separate-work-dir=` is to move the .git/ directory to a location outside of the main worktree. When used within a linked worktree, however, rather than moving the .git/ directory as intended, it instead incorrectly moves the worktree's .git/worktrees/ directory to , thus disconnecting the linked worktree from its parent repository and breaking the worktree in the process since its local .git file no longer points at a location at which it can find the object database. Fix this broken behavior. An intentional side-effect of this change is that it also closes a loophole not caught by ccf236a23a (init: disallow --separate-git-dir with bare repository, 2020-08-09) in which the check to prevent --separate-git-dir being used in conjunction with a bare repository was unable to detect the invalid combination when invoked from within a linked worktree. Therefore, add a test to verify that this loophole is closed, as well. Reported-by: Henré Botha Signed-off-by: Eric Sunshine --- builtin/init-db.c | 24 ++++++++++++++++++++++++ t/t0001-init.sh | 21 +++++++++++++++++++-- 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/builtin/init-db.c b/builtin/init-db.c index 7b915d88ab..cd3e760541 100644 --- a/builtin/init-db.c +++ b/builtin/init-db.c @@ -642,6 +642,30 @@ int cmd_init_db(int argc, const char **argv, const char *prefix) if (!git_dir) git_dir = DEFAULT_GIT_DIR_ENVIRONMENT; + /* + * When --separate-git-dir is used inside a linked worktree, take + * care to ensure that the common .git/ directory is relocated, not + * the worktree-specific .git/worktrees// directory. + */ + if (real_git_dir) { + int err; + const char *p; + struct strbuf sb = STRBUF_INIT; + + p = read_gitfile_gently(git_dir, &err); + if (p && get_common_dir(&sb, p)) { + struct strbuf mainwt = STRBUF_INIT; + + strbuf_addbuf(&mainwt, &sb); + strbuf_strip_suffix(&mainwt, "/.git"); + if (chdir(mainwt.buf) < 0) + die_errno(_("cannot chdir to %s"), mainwt.buf); + strbuf_release(&mainwt); + git_dir = strbuf_detach(&sb, NULL); + } + strbuf_release(&sb); + } + if (is_bare_repository_cfg < 0) is_bare_repository_cfg = guess_repository_type(git_dir); diff --git a/t/t0001-init.sh b/t/t0001-init.sh index e489eb4ddb..2f7c3dcd0f 100755 --- a/t/t0001-init.sh +++ b/t/t0001-init.sh @@ -329,6 +329,15 @@ test_expect_success 'implicit bare & --separate-git-dir incompatible' ' test_i18ngrep "incompatible" err ' +test_expect_success 'bare & --separate-git-dir incompatible within worktree' ' + test_when_finished "rm -rf bare.git linkwt seprepo" && + test_commit gumby && + git clone --bare . bare.git && + git -C bare.git worktree add --detach ../linkwt && + test_must_fail git -C linkwt init --separate-git-dir seprepo 2>err && + test_i18ngrep "incompatible" err +' + test_lazy_prereq GETCWD_IGNORES_PERMS ' base=GETCWD_TEST_BASE_DIR && mkdir -p $base/dir && @@ -405,15 +414,23 @@ test_expect_success SYMLINKS 're-init to move gitdir symlink' ' test_path_is_dir realgitdir/refs ' -test_expect_success 're-init to move gitdir with linked worktrees' ' +sep_git_dir_worktree () { test_when_finished "rm -rf mainwt linkwt seprepo" && git init mainwt && test_commit -C mainwt gumby && git -C mainwt worktree add --detach ../linkwt && - git -C mainwt init --separate-git-dir ../seprepo && + git -C "$1" init --separate-git-dir ../seprepo && git -C mainwt rev-parse --git-common-dir >expect && git -C linkwt rev-parse --git-common-dir >actual && test_cmp expect actual +} + +test_expect_success 're-init to move gitdir with linked worktrees' ' + sep_git_dir_worktree mainwt +' + +test_expect_success 're-init to move gitdir within linked worktree' ' + sep_git_dir_worktree linkwt ' test_expect_success MINGW '.git hidden' '