From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-Status: No, score=-3.7 required=3.0 tests=AWL,BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_PASS,SPF_PASS shortcircuit=no autolearn=ham autolearn_force=no version=3.4.2 Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by dcvr.yhbt.net (Postfix) with ESMTP id 054D11FA09 for ; Wed, 10 Jun 2020 06:32:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726288AbgFJGcO (ORCPT ); Wed, 10 Jun 2020 02:32:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46066 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726251AbgFJGcD (ORCPT ); Wed, 10 Jun 2020 02:32:03 -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 756FCC08C5C1 for ; Tue, 9 Jun 2020 23:32:01 -0700 (PDT) Received: by mail-io1-xd43.google.com with SMTP id r2so928013ioo.4 for ; Tue, 09 Jun 2020 23:32:01 -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=VwHeaJTqJaoEYdbx4lhU8YbWZPxvvvv2zKngKjjXxsU=; b=tFib/1553u/IFCDRAv6u3E+O45Bul4x7BCzsOvEKZd4chMweQ+iiltpc2j9ELWos24 v0P0W4cpCV72l4LiTxy8SeAJZLRw5CQiwPiNnZxAvWnoX1cPmSTMLY69BgnLg+nqhE63 Qe2yf44TYoqcZtQ7jpx6FfVQmTg3qOMZhnGi1iNBaY3o2GfNrBvVrsGDuAIyvzULCyoc 3UacOFUpkoOqZARwkSjXEUti+/YEeVMrCPdwhRPmCVVitnUujS6qZz7+yyzAA/XmEmt6 sANsDy1SsykNZYV/d5EdsYLHXNKR0p4ngYkH9cxAQC1Eryan6ONqSaMdcZU4AtADUK29 owNQ== 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=VwHeaJTqJaoEYdbx4lhU8YbWZPxvvvv2zKngKjjXxsU=; b=WaGsqyodUA/yna+Ee6OKkolnSPLpqC3gMoYFuF+lbI7dkvPNSkuegBHDrI9pLOTFaO NqNyPPUWytHnlxGV/+NfGIbVEXrUrWlYQw8mVQnCBZxwl9tFVF8UwmKmxYT6+IFxKMty HUaeqCajMRSfuFgOkPO52Fs3o6c1Y/jK3tGtfoXm/TJzZ2pUQxwSPPfymkXZEigutUYm tGOJ0DF+QBGfBxOIWArLD7E0PExvDDgHewqfDImFbfhM+VU5RJ4/4MlG6Z6YCoJZtlg+ /f2dvOUX58tsArCjy/sXtY3l58qrOgLXCHRuZULnqvDW2WrJrPMq9bq/zGAKfq44GDAF lvlw== X-Gm-Message-State: AOAM5314+vfuP0mxLKpBiVc+FE/T/Bp2tkGSV/rgiiA0LT/PEId1nnCK J/hjAdc0B7Ldb+n/+hK6+QEUckjXK4w= X-Google-Smtp-Source: ABdhPJwnt/FRl+hdeCMfTnwwnriGgCXWlwSDrUkpOd9iCeLl6xU0GfFZaVYB2Br907t+V8QOJtLt/Q== X-Received: by 2002:a05:6602:2f0a:: with SMTP id q10mr1837071iow.134.1591770720135; Tue, 09 Jun 2020 23:32:00 -0700 (PDT) Received: from localhost.localdomain (user-12l2dpj.cable.mindspring.com. [69.81.55.51]) by smtp.gmail.com with ESMTPSA id f22sm8497868iob.18.2020.06.09.23.31.59 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 09 Jun 2020 23:31:59 -0700 (PDT) From: Eric Sunshine To: git@vger.kernel.org Cc: Duy Nguyen , =?UTF-8?q?Jonathan=20M=C3=BCller?= , Shourya Shukla , Junio C Hamano , Ramsay Jones , Eric Sunshine Subject: [PATCH v2 4/7] worktree: prune duplicate entries referencing same worktree path Date: Wed, 10 Jun 2020 02:30:46 -0400 Message-Id: <20200610063049.74666-5-sunshine@sunshineco.com> X-Mailer: git-send-email 2.27.0.90.gabb59f83a2 In-Reply-To: <20200610063049.74666-1-sunshine@sunshineco.com> References: <20200610063049.74666-1-sunshine@sunshineco.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org A fundamental restriction of linked working trees is that there must only ever be a single worktree associated with a particular path, thus "git worktree add" explicitly disallows creation of a new worktree at the same location as an existing registered worktree. Nevertheless, users can still "shoot themselves in the foot" by mucking with administrative files in .git/worktree//. Worse, "git worktree move" is careless[1] and allows a worktree to be moved atop a registered but missing worktree (which can happen, for instance, if the worktree is on removable media). For instance: $ git clone foo.git $ cd foo $ git worktree add ../bar $ git worktree add ../baz $ rm -rf ../bar $ git worktree move ../baz ../bar $ git worktree list .../foo beefd00f [master] .../bar beefd00f [bar] .../bar beefd00f [baz] Help users recover from this form of corruption by teaching "git worktree prune" to detect when multiple worktrees are associated with the same path. [1]: A subsequent commit will fix "git worktree move" validation to be more strict. Signed-off-by: Eric Sunshine --- builtin/worktree.c | 49 ++++++++++++++++++++++++++++++++++----- t/t2401-worktree-prune.sh | 12 ++++++++++ 2 files changed, 55 insertions(+), 6 deletions(-) diff --git a/builtin/worktree.c b/builtin/worktree.c index 706ac68751..65492752a7 100644 --- a/builtin/worktree.c +++ b/builtin/worktree.c @@ -67,7 +67,12 @@ static void delete_worktrees_dir_if_empty(void) rmdir(git_path("worktrees")); /* ignore failed removal */ } -static int should_prune_worktree(const char *id, struct strbuf *reason) +/* + * Return true if worktree entry should be pruned, along with the reason for + * pruning. Otherwise, return false and the worktree's path, or NULL if it + * cannot be determined. Caller is responsible for freeing returned path. + */ +static int should_prune_worktree(const char *id, struct strbuf *reason, char **wtpath) { struct stat st; char *path; @@ -75,6 +80,7 @@ static int should_prune_worktree(const char *id, struct strbuf *reason) size_t len; ssize_t read_result; + *wtpath = NULL; if (!is_directory(git_path("worktrees/%s", id))) { strbuf_addstr(reason, _("not a valid directory")); return 1; @@ -120,16 +126,17 @@ static int should_prune_worktree(const char *id, struct strbuf *reason) } path[len] = '\0'; if (!file_exists(path)) { - free(path); if (stat(git_path("worktrees/%s/index", id), &st) || st.st_mtime <= expire) { strbuf_addstr(reason, _("gitdir file points to non-existent location")); + free(path); return 1; } else { + *wtpath = path; return 0; } } - free(path); + *wtpath = path; return 0; } @@ -141,22 +148,52 @@ static void prune_worktree(const char *id, const char *reason) delete_git_dir(id); } +static int prune_cmp(const void *a, const void *b) +{ + const struct string_list_item *x = a; + const struct string_list_item *y = b; + int c; + + if ((c = fspathcmp(x->string, y->string))) + return c; + /* paths same; sort by .git/worktrees/ */ + return strcmp(x->util, y->util); +} + +static void prune_dups(struct string_list *l) +{ + int i; + + QSORT(l->items, l->nr, prune_cmp); + for (i = 1; i < l->nr; i++) { + if (!fspathcmp(l->items[i].string, l->items[i - 1].string)) + prune_worktree(l->items[i].util, "duplicate entry"); + } +} + static void prune_worktrees(void) { struct strbuf reason = STRBUF_INIT; + struct string_list kept = STRING_LIST_INIT_NODUP; DIR *dir = opendir(git_path("worktrees")); struct dirent *d; if (!dir) return; while ((d = readdir(dir)) != NULL) { + char *path; if (is_dot_or_dotdot(d->d_name)) continue; strbuf_reset(&reason); - if (!should_prune_worktree(d->d_name, &reason)) - continue; - prune_worktree(d->d_name, reason.buf); + if (should_prune_worktree(d->d_name, &reason, &path)) + prune_worktree(d->d_name, reason.buf); + else if (path) + string_list_append(&kept, path)->util = xstrdup(d->d_name); } closedir(dir); + + prune_dups(&kept); + string_list_clear(&kept, 1); + if (!show_only) delete_worktrees_dir_if_empty(); strbuf_release(&reason); diff --git a/t/t2401-worktree-prune.sh b/t/t2401-worktree-prune.sh index b7d6d5d45a..fd3916fee0 100755 --- a/t/t2401-worktree-prune.sh +++ b/t/t2401-worktree-prune.sh @@ -92,4 +92,16 @@ test_expect_success 'not prune proper checkouts' ' test -d .git/worktrees/nop ' +test_expect_success 'prune duplicate (linked/linked)' ' + test_when_finished rm -fr .git/worktrees w1 w2 && + git worktree add --detach w1 && + git worktree add --detach w2 && + sed "s/w2/w1/" .git/worktrees/w2/gitdir >.git/worktrees/w2/gitdir.new && + mv .git/worktrees/w2/gitdir.new .git/worktrees/w2/gitdir && + git worktree prune --verbose >actual && + test_i18ngrep "duplicate entry" actual && + test -d .git/worktrees/w1 && + ! test -d .git/worktrees/w2 +' + test_done -- 2.27.0.90.gabb59f83a2