From patchwork Fri Jan 19 14:27:01 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: karthik nayak X-Patchwork-Id: 13523844 Received: from mail-ed1-f41.google.com (mail-ed1-f41.google.com [209.85.208.41]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 679CE53815 for ; Fri, 19 Jan 2024 14:27:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.41 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705674432; cv=none; b=PmqIIx3QDW3wCbvTOqfLc5ojcYA1IJ/7nX615e7ooafc0o5bDCHNmJD2ICTteMncGBsqukK+WaOz3IZvYUMvuAuuiC2q+ORDf7wyRCZxCKeY6Hvj4Rqvw+4/1dT+ghgZ8dCsGGX5ki9Aabb5gzW7yo9C1zVGqheaQ+TeimmNaPg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705674432; c=relaxed/simple; bh=kwm9DntFPe2kKovbX5xVcI6VHG+8Ik8SXNrOP5M7iF4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=j185g4QnEQDEee9Boi0jTzbVFk9IqLawRV9RTKBUiSbBmmV2PZj/s8FeDBBGXP7GRFXpdPuPNCHfP9c2nl9iuuoCuWdzyItsgJMhqi6IX4XQ/5J3aYGlsjvIwKCtEH6EoGIHD33Y6CG8tnDQYON5pycj1fKOKlOaDYmV0xRtEGQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=NqAYofHs; arc=none smtp.client-ip=209.85.208.41 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="NqAYofHs" Received: by mail-ed1-f41.google.com with SMTP id 4fb4d7f45d1cf-55a070af87eso779693a12.3 for ; Fri, 19 Jan 2024 06:27:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1705674429; x=1706279229; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=3Jun3QQa1XYGBUywPprR1ps1htbnuwuq0MStTK4Rkbg=; b=NqAYofHs6CGV/ZZyv4vU2viyXIlgRYYE9LcFWGcWh3bCRo4t6li5/oRemVl3TaCV+K b17NpkeIIG60ikFTN9ytKevSRA01QJX74+6jCY1ahQn6JL9BOsL7vr6QpuU+DYI1dqz+ iG8I4PERgW8avKSLd4rPMJGYZ4QG9Z+BYOvPVVdb8kOnX8/h3plL7tatO2S7zOFWzEd6 NM8MuldDVIHWlJEfpmPOK0FUScMI8/MFPSgnlUlhfilYJ1SYCi48wPfZxOotgvDILPfw RKKuMP5TmjrO7i0o1VpDXJVcx9EUHy6OhF7zfzQw1WGLWz1S2VgodZuoxIDMJRVNzzNQ 6Bdg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1705674429; x=1706279229; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=3Jun3QQa1XYGBUywPprR1ps1htbnuwuq0MStTK4Rkbg=; b=BMZ+EuYv/A3oqYJc8crzEJJg8j8S5eMnnsCoh+naq3tNMu2kM+gUTJQIodufSTqEPT Ri7t11iu5FuaNcw6uTSDnIU+xuvNLrEl59QRkctZ1hMlgIkr5T+Qeh0TD+ajcJgG4XoW 8arBTFcc9AI47qOryH1XuaBkvfPPw+PGWNq7Ov82ZA+vQW/JkfI6pzBQ5dKlAU/WVt9+ 3CQvo0Rpcv0Jb+uRhBCV6Mfc2SSYGwp7mIJwDz3vUwp92kopQswqd7DK7TQyGa16jrii zJ3E0rB6nANHHBfISZJbtm7fvJjXPWAAA1xrgGJLWpsPBX9s6frrO4Wrac8EurzYzQy/ XELA== X-Gm-Message-State: AOJu0YzgdzugA5hKhucP+fGWWCVH4El0WpnUcpI/3BhuwzeHbT10MEuX /7rJYIWo6N+ghOFBSwAcTjexYn1s5ETH1iGh18XtRPbejWKNsbiGsbHv87WN X-Google-Smtp-Source: AGHT+IHaUc1y5FW5H1G59eZBwx0FUft7AlGCOyInWSnChgKdObGgcTek1qyEyvkANfKujEuxuzI8KQ== X-Received: by 2002:a17:906:3614:b0:a2a:2498:93c5 with SMTP id q20-20020a170906361400b00a2a249893c5mr1442291ejb.73.1705674428917; Fri, 19 Jan 2024 06:27:08 -0800 (PST) Received: from localhost.localdomain (h-213.61.124.196.host.de.colt.net. [213.61.124.196]) by smtp.gmail.com with ESMTPSA id t13-20020a17090616cd00b00a2ea45637desm3843494ejd.112.2024.01.19.06.27.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 19 Jan 2024 06:27:08 -0800 (PST) From: Karthik Nayak To: git@vger.kernel.org Cc: Karthik Nayak Subject: [PATCH 1/5] refs: expose `is_pseudoref_syntax()` Date: Fri, 19 Jan 2024 15:27:01 +0100 Message-ID: <20240119142705.139374-2-karthik.188@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240119142705.139374-1-karthik.188@gmail.com> References: <20240119142705.139374-1-karthik.188@gmail.com> Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 The `is_pseudoref_syntax()` function is static, since it is only used within `refs.c`. In the following commit, we will use this function to provide an utility to add pseudorefs to the loose refs cache. So let's expose this function via `refs.h`. Signed-off-by: Karthik Nayak --- refs.c | 2 +- refs.h | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/refs.c b/refs.c index 2f58a3460a..5999605230 100644 --- a/refs.c +++ b/refs.c @@ -827,7 +827,7 @@ int is_per_worktree_ref(const char *refname) starts_with(refname, "refs/rewritten/"); } -static int is_pseudoref_syntax(const char *refname) +int is_pseudoref_syntax(const char *refname) { const char *c; diff --git a/refs.h b/refs.h index ff113bb12a..f1bbad83fb 100644 --- a/refs.h +++ b/refs.h @@ -846,6 +846,12 @@ const char **hidden_refs_to_excludes(const struct strvec *hide_refs); /* Is this a per-worktree ref living in the refs/ namespace? */ int is_per_worktree_ref(const char *refname); +/* + * Check whether a refname matches the pseudoref syntax. This is a surface + * level check and can present false positives. + */ +int is_pseudoref_syntax(const char *refname); + /* Describes how a refname relates to worktrees */ enum ref_worktree_type { REF_WORKTREE_CURRENT, /* implicitly per worktree, eg. HEAD or From patchwork Fri Jan 19 14:27:02 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: karthik nayak X-Patchwork-Id: 13523846 Received: from mail-ed1-f50.google.com (mail-ed1-f50.google.com [209.85.208.50]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E71C453818 for ; Fri, 19 Jan 2024 14:27:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.50 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705674434; cv=none; b=aZ1KPQ0uQ+yKAUhL/3L1YomIYknquRIK1UMiMwsYcjJw6s8MosK7u6elZnbVv+U8BIgunFZsatRxY7b7ZjLVua9LLki7PqhnRX93qo9iqKtEOat8u2f7hC/TjoAPPCzFfJeNsJ4dJlX91em5RB2a3OP+o+6TIuJMthE9fnVMGyI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705674434; c=relaxed/simple; bh=VBuUIZj4MXSaR6BJrvBC8JFRnGuB2YlgjIo9azYdXXs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=XzkagyZRpAK+9W4AICsPVEK068qulJ7AdAhhC31z6uKac7JpfkHJUk4wSblrrQ7vb/xBNMraKI9iGOEX7t0RuCqa7g01xKmliH1G+zKCyV4MMNMNrnbpeckFCRk56nSrS9bFzypeWEkBx5gofANM82sfNQ2+emX4FSIetTeNwHQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=BOqTQlga; arc=none smtp.client-ip=209.85.208.50 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="BOqTQlga" Received: by mail-ed1-f50.google.com with SMTP id 4fb4d7f45d1cf-55a38fb45c1so968327a12.1 for ; Fri, 19 Jan 2024 06:27:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1705674430; x=1706279230; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=XFRFr0UHXnxVbfHpXYLEn59tpdZE0hdmL6wUKuGLr/U=; b=BOqTQlgaVq0cBWDHB/3YdD5H89Y2MMMG+gBsYJtEQ1f05HeMSTIGzzID2zTbmG+tfx xi4H3aziuEKXgcRIZjyNY8yUk24wRtlxc7DLnVITF74o3tY/+i2h+hEgG1IXxzP6QZig Mv6xuA2xtkbRDnTtMJwItJzU+iZv2yPd5Hmpurns0ioHx3vc3ddcfiRzfnIIh9Owd8r+ 8cIgaJr2er1fWdO0ZlUk5wSZBvJDa0c5KupEYQuuXwZMIHODkQwJXI9p0HmQJ4N9GIeX Fx+p9BSdpSIp/uR0HKRizhrKU2LDdQ/W8VvhP2x1qNGcdgU9qYU4IUb30xJk+en8JA/2 dI7w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1705674430; x=1706279230; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=XFRFr0UHXnxVbfHpXYLEn59tpdZE0hdmL6wUKuGLr/U=; b=neFQZkZ7yMalbeqh955xQCxOB2jLRBzFV+bhGgFV0AdtCnJbJfDSDa4T0Y8e4qjXr8 mWBEeKDwWACD06zVrKtz184QRGnpFLlLmrXuzFrU6RdgWygwVEL2nBKMf5pQSEo+DqWH yTHNGsrSqVz9UvDFsrD/vtyEBuDhGcsIca4AhWoSP4b3UvKQkTGkdzVMH0j+ao5+VuJV 5yXObNxX7vUatesAzHBssyezupwDloCLDEflP/eZ1KFUmv2IrEsSMca7yjgrI//Z6upN fKqtoIl8RPELaL6xLpPOSQ0lGASAK6Jtqldgqg/GFnGbTf3jdpnUp1+TBpXtFWNgXSJ/ pKkA== X-Gm-Message-State: AOJu0YznMQbsQSc6wlLIXzQuHbov5EqG8R6rjofkSFPBOSBUPS9+JaGY Vkd8quuZDEgmh3Rm3FYsfcqhWntf1ayvKqf09f1pg6wBJjOhjio6Hdkj10qy X-Google-Smtp-Source: AGHT+IFYOpaEfJ28uQlnnfP7UHsJ28LWUYdtlcrb2nbHTJl3lsX8IxazuC35InyZOLJgdlEXihhxZA== X-Received: by 2002:a17:907:a606:b0:a28:c5dc:4802 with SMTP id vt6-20020a170907a60600b00a28c5dc4802mr1571173ejc.31.1705674429578; Fri, 19 Jan 2024 06:27:09 -0800 (PST) Received: from localhost.localdomain (h-213.61.124.196.host.de.colt.net. [213.61.124.196]) by smtp.gmail.com with ESMTPSA id t13-20020a17090616cd00b00a2ea45637desm3843494ejd.112.2024.01.19.06.27.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 19 Jan 2024 06:27:09 -0800 (PST) From: Karthik Nayak To: git@vger.kernel.org Cc: Karthik Nayak Subject: [PATCH 2/5] refs: make `is_pseudoref_syntax()` stricter Date: Fri, 19 Jan 2024 15:27:02 +0100 Message-ID: <20240119142705.139374-3-karthik.188@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240119142705.139374-1-karthik.188@gmail.com> References: <20240119142705.139374-1-karthik.188@gmail.com> Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 The `is_pseudoref_syntax()` function is used to determine if a particular refname follows the pseudoref syntax. The pseudoref syntax is loosely defined at this instance as all refs which are in caps and use underscores. Most of the pseudorefs also have the "HEAD" suffix. Using this information we make the `is_pseudoref_syntax()` function stricter, by adding the check for "HEAD" suffix and for refs which don't end with the HEAD suffix, matching them with a predetermined list. This requires fixing up t1407 to use the "HEAD" suffix for creation of pseudorefs. Signed-off-by: Karthik Nayak --- refs.c | 21 ++++++++++++++++++--- t/t1407-worktree-ref-store.sh | 12 ++++++------ 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/refs.c b/refs.c index 5999605230..b84e173762 100644 --- a/refs.c +++ b/refs.c @@ -829,6 +829,14 @@ int is_per_worktree_ref(const char *refname) int is_pseudoref_syntax(const char *refname) { + /* TODO: move these pseudorefs to have _HEAD suffix */ + static const char *const irregular_pseudorefs[] = { + "BISECT_EXPECTED_REV", + "NOTES_MERGE_PARTIAL", + "NOTES_MERGE_REF", + "AUTO_MERGE" + }; + size_t i; const char *c; for (c = refname; *c; c++) { @@ -837,10 +845,17 @@ int is_pseudoref_syntax(const char *refname) } /* - * HEAD is not a pseudoref, but it certainly uses the - * pseudoref syntax. + * Most pseudorefs end with _HEAD. HEAD itself is not a + * pseudoref, but it certainly uses the pseudoref syntax. */ - return 1; + if (ends_with(refname, "HEAD")) + return 1; + + for (i = 0; i < ARRAY_SIZE(irregular_pseudorefs); i++) + if (!strcmp(refname, irregular_pseudorefs[i])) + return 1; + + return 0; } static int is_current_worktree_ref(const char *ref) { diff --git a/t/t1407-worktree-ref-store.sh b/t/t1407-worktree-ref-store.sh index 05b1881c59..53592c95f3 100755 --- a/t/t1407-worktree-ref-store.sh +++ b/t/t1407-worktree-ref-store.sh @@ -61,18 +61,18 @@ test_expect_success 'create_symref(FOO, refs/heads/main)' ' # PSEUDO-WT and refs/bisect/random do not create reflogs by default, so it is # not testing a realistic scenario. test_expect_success REFFILES 'for_each_reflog()' ' - echo $ZERO_OID > .git/logs/PSEUDO-MAIN && + echo $ZERO_OID >.git/logs/PSEUDO_MAIN_HEAD && mkdir -p .git/logs/refs/bisect && - echo $ZERO_OID > .git/logs/refs/bisect/random && + echo $ZERO_OID >.git/logs/refs/bisect/random && - echo $ZERO_OID > .git/worktrees/wt/logs/PSEUDO-WT && + echo $ZERO_OID >.git/worktrees/wt/logs/PSEUDO_WT_HEAD && mkdir -p .git/worktrees/wt/logs/refs/bisect && - echo $ZERO_OID > .git/worktrees/wt/logs/refs/bisect/wt-random && + echo $ZERO_OID >.git/worktrees/wt/logs/refs/bisect/wt-random && $RWT for-each-reflog | cut -d" " -f 2- | sort >actual && cat >expected <<-\EOF && HEAD 0x1 - PSEUDO-WT 0x0 + PSEUDO_WT_HEAD 0x0 refs/bisect/wt-random 0x0 refs/heads/main 0x0 refs/heads/wt-main 0x0 @@ -82,7 +82,7 @@ test_expect_success REFFILES 'for_each_reflog()' ' $RMAIN for-each-reflog | cut -d" " -f 2- | sort >actual && cat >expected <<-\EOF && HEAD 0x1 - PSEUDO-MAIN 0x0 + PSEUDO_MAIN_HEAD 0x0 refs/bisect/random 0x0 refs/heads/main 0x0 refs/heads/wt-main 0x0 From patchwork Fri Jan 19 14:27:03 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: karthik nayak X-Patchwork-Id: 13523845 Received: from mail-ed1-f52.google.com (mail-ed1-f52.google.com [209.85.208.52]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DCD3C53E13 for ; Fri, 19 Jan 2024 14:27:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.52 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705674434; cv=none; b=Hwsui+UhBiGGFmXEkwlqm7MQiYaK1tDJoWwBCUu5ml89/jb3JIg4mRDvhWsWrO+9yqU5BEyeaHTil8HNeWZ7IunTtAaIGkeiKuuFCh2TaNeChh04cZ0tOryaom2MSyyS/L8d3W8Ms0SzOjj4slex8oifS3yQbToY41Ha6XApWFE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705674434; c=relaxed/simple; bh=BX5x9sx5pZRWtjp0L38xZ/Jkmz3JEO6DYYr8suwZj1Q=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=P3c82kENBvMZUe6ow/egoB5cMbznWwYYFVVbHI5mOt1BuxBAe9lkcLocr/005p5mD7iFIoQaHLpLNArPpaHtqREKUYDQJp/0vYCEho0JTuoy9bJRg2DnKsvaezLBMdKlCI99++FlVL2nf+jsQdj/97kW2yzR0AvEwRkZ0pURa1U= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=ikBNo9Nm; arc=none smtp.client-ip=209.85.208.52 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="ikBNo9Nm" Received: by mail-ed1-f52.google.com with SMTP id 4fb4d7f45d1cf-55a539d205aso979111a12.3 for ; Fri, 19 Jan 2024 06:27:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1705674431; x=1706279231; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=A45/rUp3Q9sY0XBiQ4II+DhCohIPNRG9FdMcFBVL7UY=; b=ikBNo9NmyH741DdAEZm0HRKz7IIRTXu4ADt2zchkY3iMKUbZEdQBYoy9xlE2Jp7j5B BWmB2vIIGCAU2iJSfU3Q0TUc3a1juUhu3jx2VLZj4CiV0LsqtQ+KMlsWRZ7yWfsCNJVO 4wwP/Lkt8/U+Lg+cOXDE1NrwOz8in3lVT2yNpk2MnNuo0IVIVGw9QfDebwtv/BCqZrBD s1C/RJhzy7niy0vWp4WtunX0fjrY/hfrsEAfp/RXo0qobOZ3H5GwAzqKjzuDAlgNcXJa S/VjgG3z5+T6KjGQhchbwmhO85T74jMouc7DGeKJVI4uSK//M5eq6UTBo6SzfqXNJ1LQ W1gw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1705674431; x=1706279231; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=A45/rUp3Q9sY0XBiQ4II+DhCohIPNRG9FdMcFBVL7UY=; b=VIrwOo7zXWMh53Ps9ykc5wuhzfoWnFaqIMmi7HV7/bYrw1mfRuIN0TImn1cQICjKrX hbr8Fhone7mNAimn/lPh5Aqbr+5ALZwL5GEIG7PuEVxG+syEa8P2A+vH6BqKoG/R29Mo xyxZ7QpqcEqiAjkDnAfjINrgM1bLpybKHL6lCcg6Zej8vb9816yRZeBW9dWfUw9chWER vzERr5JE0J1eqY4V1XlyT7tywahqi58LIfMjdnfPvEHf4rpVltcI5PZ1TPnL+DF8tTT1 JilcBwGXmfTHnxUtUiTLhCr4yWGo5hGTNwTuM3juedp7PvFHeQl+Z3HVuDljoP4yUcJ7 kalw== X-Gm-Message-State: AOJu0YyM/XOGucglbCEjQa3NuXwL8Unlao4OWKjHogdACPGsryk3bFcd u0CvMIT+1rT1cWD7wj8UMq6Y3nsAfnVtnTt6afDfw7Wvm79O/7cmq+tGUBPd X-Google-Smtp-Source: AGHT+IHdtphesWx99mKgLTffnux1PQly44Vm97rEMdozJYAJa12sjEcpJ0JSBwBLL8j+uGXghxpqew== X-Received: by 2002:a17:906:d78a:b0:a2c:4a17:1d66 with SMTP id pj10-20020a170906d78a00b00a2c4a171d66mr1675872ejb.47.1705674430420; Fri, 19 Jan 2024 06:27:10 -0800 (PST) Received: from localhost.localdomain (h-213.61.124.196.host.de.colt.net. [213.61.124.196]) by smtp.gmail.com with ESMTPSA id t13-20020a17090616cd00b00a2ea45637desm3843494ejd.112.2024.01.19.06.27.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 19 Jan 2024 06:27:09 -0800 (PST) From: Karthik Nayak To: git@vger.kernel.org Cc: Karthik Nayak Subject: [PATCH 3/5] refs: extract out `loose_fill_ref_dir_regular_file()` Date: Fri, 19 Jan 2024 15:27:03 +0100 Message-ID: <20240119142705.139374-4-karthik.188@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240119142705.139374-1-karthik.188@gmail.com> References: <20240119142705.139374-1-karthik.188@gmail.com> Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Extract out the code for adding a single file to the loose ref dir as `loose_fill_ref_dir_regular_file()` from `loose_fill_ref_dir()` in `refs/files-backend.c`. This allows us to use this function independently in the following commits where we add code to also add pseudorefs to the ref dir. Signed-off-by: Karthik Nayak --- refs/files-backend.c | 62 +++++++++++++++++++++++--------------------- 1 file changed, 33 insertions(+), 29 deletions(-) diff --git a/refs/files-backend.c b/refs/files-backend.c index 6734f2a309..a4884f557d 100644 --- a/refs/files-backend.c +++ b/refs/files-backend.c @@ -229,6 +229,38 @@ static void add_per_worktree_entries_to_dir(struct ref_dir *dir, const char *dir } } +static void loose_fill_ref_dir_regular_file(struct files_ref_store *refs, + const char *refname, + struct ref_dir *dir) +{ + struct object_id oid; + int flag; + + if (!refs_resolve_ref_unsafe(&refs->base, refname, RESOLVE_REF_READING, + &oid, &flag)) { + oidclr(&oid); + flag |= REF_ISBROKEN; + } else if (is_null_oid(&oid)) { + /* + * It is so astronomically unlikely + * that null_oid is the OID of an + * actual object that we consider its + * appearance in a loose reference + * file to be repo corruption + * (probably due to a software bug). + */ + flag |= REF_ISBROKEN; + } + + if (check_refname_format(refname, REFNAME_ALLOW_ONELEVEL)) { + if (!refname_is_safe(refname)) + die("loose refname is dangerous: %s", refname); + oidclr(&oid); + flag |= REF_BAD_NAME | REF_ISBROKEN; + } + add_entry_to_dir(dir, create_ref_entry(refname, &oid, flag)); +} + /* * Read the loose references from the namespace dirname into dir * (without recursing). dirname must end with '/'. dir must be the @@ -257,8 +289,6 @@ static void loose_fill_ref_dir(struct ref_store *ref_store, strbuf_add(&refname, dirname, dirnamelen); while ((de = readdir(d)) != NULL) { - struct object_id oid; - int flag; unsigned char dtype; if (de->d_name[0] == '.') @@ -274,33 +304,7 @@ static void loose_fill_ref_dir(struct ref_store *ref_store, create_dir_entry(dir->cache, refname.buf, refname.len)); } else if (dtype == DT_REG) { - if (!refs_resolve_ref_unsafe(&refs->base, - refname.buf, - RESOLVE_REF_READING, - &oid, &flag)) { - oidclr(&oid); - flag |= REF_ISBROKEN; - } else if (is_null_oid(&oid)) { - /* - * It is so astronomically unlikely - * that null_oid is the OID of an - * actual object that we consider its - * appearance in a loose reference - * file to be repo corruption - * (probably due to a software bug). - */ - flag |= REF_ISBROKEN; - } - - if (check_refname_format(refname.buf, - REFNAME_ALLOW_ONELEVEL)) { - if (!refname_is_safe(refname.buf)) - die("loose refname is dangerous: %s", refname.buf); - oidclr(&oid); - flag |= REF_BAD_NAME | REF_ISBROKEN; - } - add_entry_to_dir(dir, - create_ref_entry(refname.buf, &oid, flag)); + loose_fill_ref_dir_regular_file(refs, refname.buf, dir); } strbuf_setlen(&refname, dirnamelen); } From patchwork Fri Jan 19 14:27:04 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: karthik nayak X-Patchwork-Id: 13523847 Received: from mail-ej1-f47.google.com (mail-ej1-f47.google.com [209.85.218.47]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3264E53E1F for ; Fri, 19 Jan 2024 14:27:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.47 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705674434; cv=none; b=Z0BIF6OpPTkbKUVby2dwkyGAUR/wgjXnapYn5cM2RE6/gyWSckICKzQdi85oYIyNix2qx7r8UXBEF82fhOeoDEHrvIMbxNr+bzXuQhqdca8N6Er/awk4oXm8gWQDm9TFSRk9JSPEAAO4ytGK5BFoN9B25J30nAOaWiQPzU7rd5g= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705674434; c=relaxed/simple; bh=Tr+QMsllTzSTYzPcobC7Tfj3dw7qC5s+es/6REkOWpg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=VAvnDUHZslFuXZvx15yzj1GuVutonfaTrBLH2dfNB0+XEE8SV3L0Mfvfwgr7hbU/p69D09XYDeqi2PZ05+sOavHv661bd1JZUlgkaEZuQNPQLvhpxGfTHuAA58bZaaRKA/9+KiFcy7pf2JtNyY3GCLE7rQn8MlDu3ppbQY2c2UA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=JYALlSP5; arc=none smtp.client-ip=209.85.218.47 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="JYALlSP5" Received: by mail-ej1-f47.google.com with SMTP id a640c23a62f3a-a2821884a09so59882166b.2 for ; Fri, 19 Jan 2024 06:27:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1705674431; x=1706279231; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=qRZmxORpUKZjk2A8x6xi95lQbptlhJ3YliK6oOZmo/0=; b=JYALlSP5D1jUe4vnQjFlxTYbbay04vrpEEMjS7fzbb27cyY50NuT8ILf5Pob4O5Bze bg4ASuwLUkwEQGuPtp/OF6D9TgF+HJwt+nN9Im3I0SP+snh9TGwojBbTbI8XL18GPx89 D4MKOyEwhC//EAh5xgYDz7JaoX9fuG6jvH0/BuJjXPpFwRdvPNVmccFnSpRVQHz2uBqF WocAJzo11n838yZRX0Hnxr4Detq4EWH7Og28CO6E3zz7ahIlkwFPcLTTJy9bYEBzzxf3 +/ImE1v3dhc8bOnBx7fC0MjonhRI3Ul+y9xdMOjJc91iFUrcveJjsUSMCCUwJLcHgcpv dsfw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1705674431; x=1706279231; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=qRZmxORpUKZjk2A8x6xi95lQbptlhJ3YliK6oOZmo/0=; b=jrreRji/uXJc92b6KxQZY6BoRn/HKednD5/QdT9qFyU3L16d6AbH2rAp8plnDV26PN 1KxoA+XTyZX1owYfTry1uQMM1QowrO0cR3l7rjkLRBMifu37GyDnfOC1WapvKpAzl/1k m1vKkvmLGStz9NozcOsdTg5LesS84VgYamdIc04Gyen2YCA+hAQs4YauKfMZogI+g0NT lVfwUCMCUAPOSq4ruqkidC/gJe4/pwRTbXl3Di4I9ZOnyH/fiMjGdSuFgVhue9INSvQu V1iiREKvAzyW3AW54LU7Znw9o8aZ0QFA0PjUwngphjqEkFRm1ZZ0RcIXvz2kiRQf2hAp DYCQ== X-Gm-Message-State: AOJu0YzyW6/obiIxu1AF+U6OXKab93bCmNhMQ6ZX6yl1fZDWhCrdeSK4 NAIJLRyg28Vx2YZQNjuynYRQWbhDq+9ifyeYq6/BLeHMbQmZhUu9cjcPqq6s X-Google-Smtp-Source: AGHT+IGH470S5DXTec2qeBUc9UsnWaC6do0Ac9KGE4X1rWOfHWMe/2+k2VLJb0L1o7DaMjohyadvmQ== X-Received: by 2002:a17:906:f918:b0:a2d:79b6:bbec with SMTP id lc24-20020a170906f91800b00a2d79b6bbecmr1751077ejb.40.1705674431097; Fri, 19 Jan 2024 06:27:11 -0800 (PST) Received: from localhost.localdomain (h-213.61.124.196.host.de.colt.net. [213.61.124.196]) by smtp.gmail.com with ESMTPSA id t13-20020a17090616cd00b00a2ea45637desm3843494ejd.112.2024.01.19.06.27.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 19 Jan 2024 06:27:10 -0800 (PST) From: Karthik Nayak To: git@vger.kernel.org Cc: Karthik Nayak Subject: [PATCH 4/5] refs: introduce `refs_for_each_all_refs()` Date: Fri, 19 Jan 2024 15:27:04 +0100 Message-ID: <20240119142705.139374-5-karthik.188@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240119142705.139374-1-karthik.188@gmail.com> References: <20240119142705.139374-1-karthik.188@gmail.com> Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Introduce a new ref iteration flag `DO_FOR_EACH_INCLUDE_ALL_REFS`, which will be used to iterate over all refs. In the files backend this is limited to regular refs, pseudorefs and HEAD. For other backends like the reftable this is the universal set of all refs stored in the backend. Refs which fall outside the `refs/` and aren't either pseudorefs or HEAD are more of a grey area. This is because we don't block the users from creating such refs but they are not officially supported. In the files backend, we cannot isolate such files from other files. Introduce `refs_for_each_all_refs()` which calls `do_for_each_ref()` with this newly introduced flag. In `refs/files-backend.c`, introduce a new function `add_pseudoref_like_entries()` to add pseudorefs and HEAD to the `ref_dir`. We then finally call `add_pseudoref_like_entries()` whenever the `DO_FOR_EACH_INCLUDE_ALL_REFS` flag is set. Any new ref backend will also have to implement similar changes on its end. Signed-off-by: Karthik Nayak --- refs.c | 7 +++++ refs.h | 6 +++++ refs/files-backend.c | 64 ++++++++++++++++++++++++++++++++++++++++---- refs/refs-internal.h | 7 +++++ 4 files changed, 79 insertions(+), 5 deletions(-) diff --git a/refs.c b/refs.c index b84e173762..e527c6199d 100644 --- a/refs.c +++ b/refs.c @@ -1722,6 +1722,13 @@ int for_each_rawref(each_ref_fn fn, void *cb_data) return refs_for_each_rawref(get_main_ref_store(the_repository), fn, cb_data); } +int refs_for_each_all_refs(struct ref_store *refs, each_ref_fn fn, + void *cb_data) +{ + return do_for_each_ref(refs, "", NULL, fn, 0, + DO_FOR_EACH_INCLUDE_ALL_REFS, cb_data); +} + static int qsort_strcmp(const void *va, const void *vb) { const char *a = *(const char **)va; diff --git a/refs.h b/refs.h index f1bbad83fb..fef70b6599 100644 --- a/refs.h +++ b/refs.h @@ -393,6 +393,12 @@ int for_each_namespaced_ref(const char **exclude_patterns, int refs_for_each_rawref(struct ref_store *refs, each_ref_fn fn, void *cb_data); int for_each_rawref(each_ref_fn fn, void *cb_data); +/* + * Iterates over all ref types, regular, pseudorefs and HEAD. + */ +int refs_for_each_all_refs(struct ref_store *refs, each_ref_fn fn, + void *cb_data); + /* * Normalizes partial refs to their fully qualified form. * Will prepend to the if it doesn't start with 'refs/'. diff --git a/refs/files-backend.c b/refs/files-backend.c index a4884f557d..95a73b11bb 100644 --- a/refs/files-backend.c +++ b/refs/files-backend.c @@ -315,9 +315,58 @@ static void loose_fill_ref_dir(struct ref_store *ref_store, add_per_worktree_entries_to_dir(dir, dirname); } -static struct ref_cache *get_loose_ref_cache(struct files_ref_store *refs) +/* + * Add pseudorefs and HEAD to the ref dir by parsing the directory + * for any files which follow the pseudoref syntax. + */ +static void add_pseudoref_like_entries(struct ref_store *ref_store, + struct ref_dir *dir, + const char *dirname) +{ + struct files_ref_store *refs = + files_downcast(ref_store, REF_STORE_READ, "fill_ref_dir"); + struct strbuf path = STRBUF_INIT, refname = STRBUF_INIT; + struct dirent *de; + size_t dirnamelen; + DIR *d; + + files_ref_path(refs, &path, dirname); + + d = opendir(path.buf); + if (!d) { + strbuf_release(&path); + return; + } + + strbuf_addstr(&refname, dirname); + dirnamelen = refname.len; + + while ((de = readdir(d)) != NULL) { + unsigned char dtype; + + if (de->d_name[0] == '.') + continue; + if (ends_with(de->d_name, ".lock")) + continue; + strbuf_addstr(&refname, de->d_name); + + dtype = get_dtype(de, &path, 1); + if (dtype == DT_REG && is_pseudoref_syntax(de->d_name)) + loose_fill_ref_dir_regular_file(refs, refname.buf, dir); + + strbuf_setlen(&refname, dirnamelen); + } + strbuf_release(&refname); + strbuf_release(&path); + closedir(d); +} + +static struct ref_cache *get_loose_ref_cache(struct files_ref_store *refs, + unsigned int flags) { if (!refs->loose) { + struct ref_dir *dir; + /* * Mark the top-level directory complete because we * are about to read the only subdirectory that can @@ -328,12 +377,17 @@ static struct ref_cache *get_loose_ref_cache(struct files_ref_store *refs) /* We're going to fill the top level ourselves: */ refs->loose->root->flag &= ~REF_INCOMPLETE; + dir = get_ref_dir(refs->loose->root); + + if (flags & DO_FOR_EACH_INCLUDE_ALL_REFS) + add_pseudoref_like_entries(dir->cache->ref_store, dir, + refs->loose->root->name); + /* * Add an incomplete entry for "refs/" (to be filled * lazily): */ - add_entry_to_dir(get_ref_dir(refs->loose->root), - create_dir_entry(refs->loose, "refs/", 5)); + add_entry_to_dir(dir, create_dir_entry(refs->loose, "refs/", 5)); } return refs->loose; } @@ -861,7 +915,7 @@ static struct ref_iterator *files_ref_iterator_begin( * disk, and re-reads it if not. */ - loose_iter = cache_ref_iterator_begin(get_loose_ref_cache(refs), + loose_iter = cache_ref_iterator_begin(get_loose_ref_cache(refs, flags), prefix, ref_store->repo, 1); /* @@ -1222,7 +1276,7 @@ static int files_pack_refs(struct ref_store *ref_store, packed_refs_lock(refs->packed_ref_store, LOCK_DIE_ON_ERROR, &err); - iter = cache_ref_iterator_begin(get_loose_ref_cache(refs), NULL, + iter = cache_ref_iterator_begin(get_loose_ref_cache(refs, 0), NULL, the_repository, 0); while ((ok = ref_iterator_advance(iter)) == ITER_OK) { /* diff --git a/refs/refs-internal.h b/refs/refs-internal.h index 4af83bf9a5..981a91c4c6 100644 --- a/refs/refs-internal.h +++ b/refs/refs-internal.h @@ -260,6 +260,13 @@ enum do_for_each_ref_flags { * INCLUDE_BROKEN, since they are otherwise not included at all. */ DO_FOR_EACH_OMIT_DANGLING_SYMREFS = (1 << 2), + + /* + * Include all refs in the $GIT_DIR in contrast to generally only listing + * references having the "refs/" prefix. In the files-backend this is + * limited to regular refs, pseudorefs and HEAD. + */ + DO_FOR_EACH_INCLUDE_ALL_REFS = (1 << 3), }; /* From patchwork Fri Jan 19 14:27:05 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: karthik nayak X-Patchwork-Id: 13523848 Received: from mail-ej1-f45.google.com (mail-ej1-f45.google.com [209.85.218.45]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 23EF854649 for ; Fri, 19 Jan 2024 14:27:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.45 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705674435; cv=none; b=RvTHdgDR0SurTdmwGNLAGsKwDdxJlqLtcDrRMT02SmUVGvvVikj8Tv0dCrAoTUV/IgHGmpRPYAGgtQhXJcUygnvdnqzZeSxIHyDrZ7dG7pwtUcaMGCtfRSLONAeiP2peWVEbZcWPKsv9CNDo9z780Zkobe9y0nrov2WjAHB1yms= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705674435; c=relaxed/simple; bh=C7vDmXLEbXtY3oiqVbuMkZJlKMjB4TZLmmsS529QZQY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=UM/Kd+gfgOff6ezY12mdHussbDLetZL8KN8xALYRbbUZpEJEGvRgAlBIe7eeG2vwrpQTDlPT0I1IlOvrY6kia7UnxwZqozySj8aN2cs1RmZAw402y3jlj0iLKUCY5sdHagw8UaOgHo/zU/JnmYV0dFLtAMo1nSffWan10OXd2F0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=PxQpqk+X; arc=none smtp.client-ip=209.85.218.45 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="PxQpqk+X" Received: by mail-ej1-f45.google.com with SMTP id a640c23a62f3a-a28a6cef709so81037066b.1 for ; Fri, 19 Jan 2024 06:27:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1705674432; x=1706279232; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=mZfgvTKUs0RX0aUMcHBIQZV5urECTbu3T6yTamxXDFk=; b=PxQpqk+XdCcX6MD2RrCaJOAI+h1GsKevWin2Me1UYZxRWbBacSSmpzF2FJbkhCrIbI iWVSNVKFDEwxDYv8D4McG3Cs82W1/SAgCuZQu2WyT7OyBJdmzCCglR3iTRf3T4vjBU2f hx2mC3st+r21wufVfhr8k7Du4B8fCfLdr8bD23jzFoA8jrR+unkUZ3MD2SDZrJ33pwGp Su4N0fkH7jqey2UFucfttzBAtI/KyEpgsB9b0ki35jnsIUDlGd8JWTpOJdot62oulmdp VppQZevkAaZ01jliG25umhl90bihR9nqRAJKFAHD5Huf3cH6CJ2/SoPEKm2gBDsTORY8 3Sgg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1705674432; x=1706279232; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=mZfgvTKUs0RX0aUMcHBIQZV5urECTbu3T6yTamxXDFk=; b=HEpzdQhKb/+h3u+hgpuFsrPLell3OXpEQqICau2nPEEq0TN2pyzA14weSkOLNJbOve t54e/vYAnTllFsMwJdbV0y/xN/NjR/HHUcxQI4dvel6huRGCW2E2VDTv6YwfNUMS6vNw 4eGbPgqXhCH4AnXinkgGETxNMiGOiI+hjYjJ4J5hLZWbh5F3L+n3vJEE0cIw4voIU0Ej aKVO72LBkaaiNET2waPiuZJyGKrAUyj1FabamOg9h4FUNXQN9WPR+htTTUORSaUEcr+f 4dG0hjuwjJG/1IaO60wQojV0ioHYhmnJ48mVSIlph6H3m/HRH3fIID7fFf0PJ4O6shUM 78qQ== X-Gm-Message-State: AOJu0YyjfG1jYMxa/TgJ+u1glWBMSb9wLs2zuqNbN2Jgon/LbXPRMzwT 0zKu6bx2yShjx6O+ONjVAV6xTEHhHJwvwQeCwNiVH3LDFT7kRr5I0oJpeajQ X-Google-Smtp-Source: AGHT+IGyh4PCYSBUQMdKdweXZLUu8Io7lQbuif18UHh6BUUlgK2oT48Mb6jM/P53/olppTgFTAxMOQ== X-Received: by 2002:a17:906:fa98:b0:a2e:d71f:bd4c with SMTP id lt24-20020a170906fa9800b00a2ed71fbd4cmr1532770ejb.68.1705674431750; Fri, 19 Jan 2024 06:27:11 -0800 (PST) Received: from localhost.localdomain (h-213.61.124.196.host.de.colt.net. [213.61.124.196]) by smtp.gmail.com with ESMTPSA id t13-20020a17090616cd00b00a2ea45637desm3843494ejd.112.2024.01.19.06.27.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 19 Jan 2024 06:27:11 -0800 (PST) From: Karthik Nayak To: git@vger.kernel.org Cc: Karthik Nayak Subject: [PATCH 5/5] for-each-ref: avoid filtering on empty pattern Date: Fri, 19 Jan 2024 15:27:05 +0100 Message-ID: <20240119142705.139374-6-karthik.188@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240119142705.139374-1-karthik.188@gmail.com> References: <20240119142705.139374-1-karthik.188@gmail.com> Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 When the user uses an empty string pattern (""), we don't match any refs in git-for-each-ref(1). This is because in git-for-each-ref(1), we use path based matching and an empty string doesn't match any path. In this commit we change this behavior by making empty string pattern match all references. This is done by introducing a new flag `FILTER_REFS_NO_FILTER` in `ref-filter.c`, which uses the newly introduced `refs_for_each_all_refs()` function to iterate over all the refs in the repository. Signed-off-by: Karthik Nayak --- Documentation/git-for-each-ref.txt | 3 ++- builtin/for-each-ref.c | 21 +++++++++++++++++- ref-filter.c | 13 ++++++++++-- ref-filter.h | 4 +++- t/t6302-for-each-ref-filter.sh | 34 ++++++++++++++++++++++++++++++ 5 files changed, 70 insertions(+), 5 deletions(-) diff --git a/Documentation/git-for-each-ref.txt b/Documentation/git-for-each-ref.txt index be9543f684..b1cb482bf5 100644 --- a/Documentation/git-for-each-ref.txt +++ b/Documentation/git-for-each-ref.txt @@ -32,7 +32,8 @@ OPTIONS If one or more patterns are given, only refs are shown that match against at least one pattern, either using fnmatch(3) or literally, in the latter case matching completely or from the - beginning up to a slash. + beginning up to a slash. If an empty string is provided all refs + are printed, including HEAD and pseudorefs. --stdin:: If `--stdin` is supplied, then the list of patterns is read from diff --git a/builtin/for-each-ref.c b/builtin/for-each-ref.c index 3885a9c28e..5aa879e8be 100644 --- a/builtin/for-each-ref.c +++ b/builtin/for-each-ref.c @@ -25,6 +25,7 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix) struct ref_format format = REF_FORMAT_INIT; int from_stdin = 0; struct strvec vec = STRVEC_INIT; + unsigned int flags = FILTER_REFS_ALL; struct option opts[] = { OPT_BIT('s', "shell", &format.quote_style, @@ -93,11 +94,29 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix) /* vec.v is NULL-terminated, just like 'argv'. */ filter.name_patterns = vec.v; } else { + size_t i; + filter.name_patterns = argv; + + /* + * Search for any empty string pattern, if it exists then we + * print all refs without any filtering. + */ + i = 0; + while (argv[i]) { + if (!argv[i][0]) { + flags = FILTER_REFS_NO_FILTER; + /* doing this removes any pattern from being matched */ + filter.name_patterns[0] = NULL; + break; + } + + i++; + } } filter.match_as_path = 1; - filter_and_format_refs(&filter, FILTER_REFS_ALL, sorting, &format); + filter_and_format_refs(&filter, flags, sorting, &format); ref_filter_clear(&filter); ref_sorting_release(sorting); diff --git a/ref-filter.c b/ref-filter.c index 35b989e1df..6dac133b87 100644 --- a/ref-filter.c +++ b/ref-filter.c @@ -2622,6 +2622,11 @@ static int for_each_fullref_in_pattern(struct ref_filter *filter, each_ref_fn cb, void *cb_data) { + if (filter->kind & FILTER_REFS_NO_FILTER) { + return refs_for_each_all_refs( + get_main_ref_store(the_repository), cb, cb_data); + } + if (!filter->match_as_path) { /* * in this case, the patterns are applied after @@ -2775,8 +2780,12 @@ static struct ref_array_item *apply_ref_filter(const char *refname, const struct /* Obtain the current ref kind from filter_ref_kind() and ignore unwanted refs. */ kind = filter_ref_kind(filter, refname); - if (!(kind & filter->kind)) + if (filter->kind & FILTER_REFS_NO_FILTER) { + if (kind == FILTER_REFS_DETACHED_HEAD) + kind = FILTER_REFS_OTHERS; + } else if (!(kind & filter->kind)) { return NULL; + } if (!filter_pattern_match(filter, refname)) return NULL; @@ -3041,7 +3050,7 @@ static int do_filter_refs(struct ref_filter *filter, unsigned int type, each_ref ret = for_each_fullref_in("refs/remotes/", fn, cb_data); else if (filter->kind == FILTER_REFS_TAGS) ret = for_each_fullref_in("refs/tags/", fn, cb_data); - else if (filter->kind & FILTER_REFS_ALL) + else if (filter->kind & FILTER_REFS_ALL || filter->kind & FILTER_REFS_NO_FILTER) ret = for_each_fullref_in_pattern(filter, fn, cb_data); if (!ret && (filter->kind & FILTER_REFS_DETACHED_HEAD)) head_ref(fn, cb_data); diff --git a/ref-filter.h b/ref-filter.h index 07cd6f6da3..1eab325ce0 100644 --- a/ref-filter.h +++ b/ref-filter.h @@ -22,7 +22,9 @@ #define FILTER_REFS_ALL (FILTER_REFS_TAGS | FILTER_REFS_BRANCHES | \ FILTER_REFS_REMOTES | FILTER_REFS_OTHERS) #define FILTER_REFS_DETACHED_HEAD 0x0020 -#define FILTER_REFS_KIND_MASK (FILTER_REFS_ALL | FILTER_REFS_DETACHED_HEAD) +#define FILTER_REFS_NO_FILTER 0x0040 +#define FILTER_REFS_KIND_MASK (FILTER_REFS_ALL | FILTER_REFS_DETACHED_HEAD | \ + FILTER_REFS_NO_FILTER) struct atom_value; struct ref_sorting; diff --git a/t/t6302-for-each-ref-filter.sh b/t/t6302-for-each-ref-filter.sh index 82f3d1ea0f..3922326cab 100755 --- a/t/t6302-for-each-ref-filter.sh +++ b/t/t6302-for-each-ref-filter.sh @@ -31,6 +31,40 @@ test_expect_success 'setup some history and refs' ' git update-ref refs/odd/spot main ' +cat >expect <<-\EOF + HEAD + ORIG_HEAD + refs/heads/main + refs/heads/side + refs/odd/spot + refs/tags/annotated-tag + refs/tags/doubly-annotated-tag + refs/tags/doubly-signed-tag + refs/tags/four + refs/tags/one + refs/tags/signed-tag + refs/tags/three + refs/tags/two +EOF + +test_expect_success 'empty pattern prints pseudorefs' ' + git update-ref ORIG_HEAD main && + git for-each-ref --format="%(refname)" "" >actual && + test_cmp expect actual +' + +test_expect_success 'empty pattern with other patterns' ' + git update-ref ORIG_HEAD main && + git for-each-ref --format="%(refname)" "" "refs/" >actual && + test_cmp expect actual +' + +test_expect_success 'empty pattern towards the end' ' + git update-ref ORIG_HEAD main && + git for-each-ref --format="%(refname)" "refs/" "" >actual && + test_cmp expect actual +' + test_expect_success 'filtering with --points-at' ' cat >expect <<-\EOF && refs/heads/main