From patchwork Fri Feb 23 10:01:08 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: karthik nayak X-Patchwork-Id: 13568826 Received: from mail-ej1-f52.google.com (mail-ej1-f52.google.com [209.85.218.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 3ED3218B04 for ; Fri, 23 Feb 2024 10:01:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.52 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708682481; cv=none; b=im1aNpYMQ9CkvCJ6zfq8S8/K1UbdYQYLjEuuGQSx528A2c4HedXyVDpU3bf14/q2BKyRTBCBmpgeR1okDZcNWzjBftmfac1uI9ENj+okgthUugVkW78/IHGveREcsQR4saM42pMmnicx+1QomT7emFcJ7vl8Ia8CXC6KaDy/otw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708682481; c=relaxed/simple; bh=yzRxaL+HHK8Zu68BOpG4ssldBsb6EFGbH6VT0IItJj8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=EqImF2mUC5jy/zURFkcmbMRwOtz+LE2O7QBch2oxPGZXlQGOWHlMuY5IM1RkPVI/aBQDL8fyW11JPpu4kwLDxuL/ke2GKd/2W5v0Xy3/c3+Iv9vFvdCY0encYXYSbCIhlQSSWpn0shIVE/YGPNTagDdW6OVgpvWpTnYqV40vJLY= 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=CHkB4rLs; arc=none smtp.client-ip=209.85.218.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="CHkB4rLs" Received: by mail-ej1-f52.google.com with SMTP id a640c23a62f3a-a3e75e30d36so126180666b.1 for ; Fri, 23 Feb 2024 02:01:18 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1708682477; x=1709287277; 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=PctASsrsThL9yoXZ71bHQqXE+0f5q6v2Pb/muS/pwq8=; b=CHkB4rLsY3+O9ETU0OrtEzCmX4nMgQc/feg3Abb4yOTVgT4yMcyzq562OsGk9NuEZE 7lNRdZ9jmCfc2E3ByqVWmPtyVfA6sS/2xJ9g7W9htAyt1VsRUrGkaWeTnwklDdzlaYN4 nywo4DbAA/myFAi/BekHLxOEBPRJdYjlls0NlHbYN70+YKfAovYDj69Tqu2MSz/uRVBB ijN0LTqunK0916IprNI8KVASUdBVDQoEcZtaiC8to5Cy00UHY0ZxHY2LrAP1yeCZF42A QU8U4kxhPICHVCJ8H+icAsyAjrh1UtbxEsVn+AR+M1L/X4mHoHRgtzoNiYjtVfwPuDON M1Cw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708682477; x=1709287277; 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=PctASsrsThL9yoXZ71bHQqXE+0f5q6v2Pb/muS/pwq8=; b=JjwSmrnFanPyClnKLtudUBfLfXir0LFbB+mAL2dV/dMiqkB9vhkLeOyrDdG77aFXRa 2hMMe3FARLGRi6VlqE1hsFAGaq0t2/0S7HKalj1aghv4gkDX5C1bKu2l7y7G5BOSd07C RpLDf2bPR/MKc/fZcLP1hEYzlxFJvk93Ilu4IjNB6x0FxHkafNxsO7NOgPkz21EH2Rnd IU8ZeA2B00q8pknk9N7+mUZIpxeQaHETJBzIWm4MJZRFSpnjfJaoNYHF3jrJMho/sZnS dFNZQemnF8wGON1mk9PAI+bf4guXjx1iE8YEeNdIaS2WwSoNvgAaLQGfChoxBy4jw6K6 lWmg== X-Gm-Message-State: AOJu0Yy9QLoOs8ffNF6POgYFFrqK2UEdJIOYG3hEc1Bv8Y4nVf+WzXkx UuRG2CWWxBc4Elk9oyQT9MmJx7sRlsv8FqM2Dx0M+ptWaPrUcNgo0CzPt/awaOw= X-Google-Smtp-Source: AGHT+IG6xMuKAW+NhbKnF8BMki8aayL5oGdCECRBBLwlk2MTQ4wzjx2GDwjJ4O34H0styKBIWBjorQ== X-Received: by 2002:a17:906:3b0b:b0:a3e:7f80:ecac with SMTP id g11-20020a1709063b0b00b00a3e7f80ecacmr4222631ejf.12.1708682476780; Fri, 23 Feb 2024 02:01:16 -0800 (PST) Received: from laptop.fritz.box ([2a02:2455:826e:4900:660d:e728:4b64:50ff]) by smtp.gmail.com with ESMTPSA id ha1-20020a170906a88100b00a381ca0e589sm6734917ejb.22.2024.02.23.02.01.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 23 Feb 2024 02:01:16 -0800 (PST) From: Karthik Nayak To: git@vger.kernel.org Cc: gitster@pobox.com, ps@pks.im, phillip.wood123@gmail.com, Karthik Nayak , Jeff King Subject: [PATCH v5 1/5] refs: introduce `is_pseudoref()` and `is_headref()` Date: Fri, 23 Feb 2024 11:01:08 +0100 Message-ID: <20240223100112.44127-2-karthik.188@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240223100112.44127-1-karthik.188@gmail.com> References: <20240119142705.139374-1-karthik.188@gmail.com> <20240223100112.44127-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 two new functions `is_pseudoref()` and `is_headref()`. This provides the necessary functionality for us to add pseudorefs and HEAD to the loose ref cache in the files backend, allowing us to build tooling to print these refs. The `is_pseudoref()` function internally calls `is_pseudoref_syntax()` but adds onto it by also checking to ensure that the pseudoref either ends with a "_HEAD" suffix or matches a list of exceptions. After which we also parse the contents of the pseudoref to ensure that it conforms to the ref format. We cannot directly add the new syntax checks to `is_pseudoref_syntax()` because the function is also used by `is_current_worktree_ref()` and making it stricter to match only known pseudorefs might have unintended consequences due to files like 'BISECT_START' which isn't a pseudoref but sometimes contains object ID. Keeping this in mind, we leave `is_pseudoref_syntax()` as is and create `is_pseudoref()` which is stricter. Ideally we'd want to move the new syntax checks to `is_pseudoref_syntax()` but a prerequisite for this would be to actually remove the exception list by converting those pseudorefs to also contain a '_HEAD' suffix and perhaps move bisect related files like 'BISECT_START' to a new directory similar to the 'rebase-merge' directory. Helped-by: Jeff King Signed-off-by: Karthik Nayak --- refs.c | 41 +++++++++++++++++++++++++++++++++++++++++ refs.h | 3 +++ 2 files changed, 44 insertions(+) diff --git a/refs.c b/refs.c index fff343c256..3546d90831 100644 --- a/refs.c +++ b/refs.c @@ -860,6 +860,47 @@ static int is_pseudoref_syntax(const char *refname) return 1; } +int is_pseudoref(struct ref_store *refs, const char *refname) +{ + static const char *const irregular_pseudorefs[] = { + "AUTO_MERGE", + "BISECT_EXPECTED_REV", + "NOTES_MERGE_PARTIAL", + "NOTES_MERGE_REF", + "MERGE_AUTOSTASH", + }; + struct object_id oid; + size_t i; + + if (!is_pseudoref_syntax(refname)) + return 0; + + if (ends_with(refname, "_HEAD")) { + refs_resolve_ref_unsafe(refs, refname, + RESOLVE_REF_READING | RESOLVE_REF_NO_RECURSE, + &oid, NULL); + return !is_null_oid(&oid); + } + + for (i = 0; i < ARRAY_SIZE(irregular_pseudorefs); i++) + if (!strcmp(refname, irregular_pseudorefs[i])) { + refs_resolve_ref_unsafe(refs, refname, + RESOLVE_REF_READING | RESOLVE_REF_NO_RECURSE, + &oid, NULL); + return !is_null_oid(&oid); + } + + return 0; +} + +int is_headref(struct ref_store *refs, const char *refname) +{ + if (!strcmp(refname, "HEAD")) + return refs_ref_exists(refs, refname); + + return 0; +} + static int is_current_worktree_ref(const char *ref) { return is_pseudoref_syntax(ref) || is_per_worktree_ref(ref); } diff --git a/refs.h b/refs.h index 303c5fac4d..f66cdd731c 100644 --- a/refs.h +++ b/refs.h @@ -1023,4 +1023,7 @@ extern struct ref_namespace_info ref_namespace[NAMESPACE__COUNT]; */ void update_ref_namespace(enum ref_namespace namespace, char *ref); +int is_pseudoref(struct ref_store *refs, const char *refname); +int is_headref(struct ref_store *refs, const char *refname); + #endif /* REFS_H */ From patchwork Fri Feb 23 10:01:09 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: karthik nayak X-Patchwork-Id: 13568827 Received: from mail-ed1-f49.google.com (mail-ed1-f49.google.com [209.85.208.49]) (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 AB23B5CDCE for ; Fri, 23 Feb 2024 10:01:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.49 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708682481; cv=none; b=HfE4S7BK+9bokZvpap5Z7JEB6bG1OTn5EGxgpHa5O1gffZY66pLk46t6IkhWieY+74u9gj+w32N2HZP9r59dhE8iEamYY684AtboVokIx2zU2JaMSMGuFUjyaampG4qD8OCrnGQQPo0ynBjDTdRwDVFH4Tl32c/SFrG14HnSv9M= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708682481; c=relaxed/simple; bh=VfltkTB10ZIBhlAE6ooGiSF5wKaiCn9vjFBaF9tYY0A=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=C7MmQ/qRxkydNIwHpL/0cYd7xNCVoi8t0F7mi0UD5sr9KM+iNNORrldvMWDsUF/uxY09reP7zPDcD7FgKPbkhY0aNjpJbLtIvbRnBk8/oTKEprrJXeUGEklZCtFm7imgXk7UTPES5oyGhwpuMNTrBwwOjYiInWBV7MQXDWUclSc= 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=ZMuo3CVT; arc=none smtp.client-ip=209.85.208.49 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="ZMuo3CVT" Received: by mail-ed1-f49.google.com with SMTP id 4fb4d7f45d1cf-563d32ee33aso691049a12.2 for ; Fri, 23 Feb 2024 02:01:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1708682478; x=1709287278; 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=+imelKk34ScyPqozaBb4Uj6heZ6F/trGf/qMfVX4OiQ=; b=ZMuo3CVTIw1WD9ZDspw897mkE9scjndKxlLGMhtBoSt88zZdumTHbPs0/1fy7Zqu+6 BoXUVXJ+hRo9koZnzoKUNWqsyDCCVaPsynOhIN1Ht0ExHld9lM+WV9yncB2NdO5Y2mgy FhH4PW80bunITNCLYtvuUZx3Pzxw+f9PQssBAvpEC80Vfsx5q9fen81lgUn0RuZ7mYzh VtcJjoZgQaDVwPgnTNgK9URFroVXDLHB2qttGDEfaNjA+rHZXmFHAKp0pTiR5sZtC7Xr /9lqXEkpH+pG9ldO3gndk0wekFqccgVy+AdIjKhTa1EL/IFN/w+itkOq+mlNrJjuvfHj QgcQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708682478; x=1709287278; 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=+imelKk34ScyPqozaBb4Uj6heZ6F/trGf/qMfVX4OiQ=; b=qYaJneFH66gXw60INlwG9P5FO2etVk0Q5o678kdarmPiaDOjdmfmUI3G3WaJrxVksg Nj2vOCgB4QQalnPw24wc9ghGO9XWIzv1M4rWCPZrkNXuaFm90WI6qiWMClMr+1OlsfxK NF1T2cYmL7JURhrTygP98w1oPn0i6b3FDk5Rq2mizYJNxakC7MtHvEWtXdwluVfXoXcn Fn3qemVRf35WaGLJ2ZvMOrQNZM9/tvb/f5SH8vP1kIcpayTYVZz4aH2eGN3wlPQfhTn3 kL8S40Ts6+HxMIzumtRVI/jG6NZTogdDG+b6pjEh4m6dUwFzXexIPO+H2BjxxlMSxp08 MfDg== X-Gm-Message-State: AOJu0YwXO6K7bTOaURpgP/+njH5svo1GjXyvyzEDw8PZRikeYl2ySM5z grlfWVP5ElbdCUo56hxA/zJYvhUOfCkrFm5eMvmVvNllwqpSC8S3qYPuzPICwG0= X-Google-Smtp-Source: AGHT+IHxbizbmK03di/Ydat9S3flOu7t0ujGtIQGSWLtoEx4d9kdchVcfv+o5/M00IRqrw10fTU/KQ== X-Received: by 2002:a17:906:a40f:b0:a3e:e678:c2ae with SMTP id l15-20020a170906a40f00b00a3ee678c2aemr848588ejz.10.1708682477575; Fri, 23 Feb 2024 02:01:17 -0800 (PST) Received: from laptop.fritz.box ([2a02:2455:826e:4900:660d:e728:4b64:50ff]) by smtp.gmail.com with ESMTPSA id ha1-20020a170906a88100b00a381ca0e589sm6734917ejb.22.2024.02.23.02.01.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 23 Feb 2024 02:01:17 -0800 (PST) From: Karthik Nayak To: git@vger.kernel.org Cc: gitster@pobox.com, ps@pks.im, phillip.wood123@gmail.com, Karthik Nayak Subject: [PATCH v5 2/5] refs: extract out `loose_fill_ref_dir_regular_file()` Date: Fri, 23 Feb 2024 11:01:09 +0100 Message-ID: <20240223100112.44127-3-karthik.188@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240223100112.44127-1-karthik.188@gmail.com> References: <20240119142705.139374-1-karthik.188@gmail.com> <20240223100112.44127-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 75dcc21ecb..65128821a8 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 Feb 23 10:01:10 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: karthik nayak X-Patchwork-Id: 13568828 Received: from mail-ej1-f42.google.com (mail-ej1-f42.google.com [209.85.218.42]) (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 B22AC5CDDA for ; Fri, 23 Feb 2024 10:01:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.42 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708682482; cv=none; b=tfrJ4YYS6LaUrUZMhY5Ak01cxReIsxeRvcHmbzW8JpQH9EXz4aMlNKtZbM4fJV5CkV4/D9JxjfZCM30UfFdi+7l74i/Lm8bIEnPK3aW2okS+IZFLB0jKa5iteWRNxyFdVHQ3BMUMAR1qS5kL7aG3nxxcTEUL7iLkAbLOk2wmY1A= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708682482; c=relaxed/simple; bh=QrjSM/EHC7/0U7gN9AKeC+7N+Kw1P3wZ8JJ9RmDQbl4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=aoQFhxjtRmi7svWzccSr8F1o/JoCYyK43Vtf71M77qaYJqv/M12K1R+Ne9ijcK6XgVpz9Fe0WaTmeYDDBt0IHWAqo21j3btJi+0zYw41mk6bfC8D9mqeeYgzbiNX7Lq7326l8zuN93P9gb1OGWe9yFwk2vwQ3grHkCaVZrFO9yc= 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=WX+3Ns6m; arc=none smtp.client-ip=209.85.218.42 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="WX+3Ns6m" Received: by mail-ej1-f42.google.com with SMTP id a640c23a62f3a-a2f22bfb4e6so29553066b.0 for ; Fri, 23 Feb 2024 02:01:20 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1708682478; x=1709287278; 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=CpkUdAkZAVvGrHgM5DDWapCPHaKSfiO7aVfbUq4TX4I=; b=WX+3Ns6mOcpbWSHpbn2JqR86sXWpHW7M+v6CEHVDx/lwMhomvrh0eBo7zFQEcBEN1O 1RTLrdznlqFNKJSkIayNN/1x9EHqDg3WptjC877Xr14qkKwV/F/LE8VyVUwyDQWVToW7 2Cva5H2c7zFK6zE4NrvkgsRvfPdEvJACn+z/NV9hJ9kVB/x9ihrY4vurMF6No9G+zM3S voJjGERk7eIa70nX4v7aLkenas2ONn8i8O1LIL6HhP4UZ90r89RLTYeDXBbUQI3MS82P k5IFa7faihQksLexqLv6Q7qgN8JsbXY8+gIz56tSq/GSEiex/u4Vh/wGVzbOI4cw6vaN e/Dg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708682478; x=1709287278; 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=CpkUdAkZAVvGrHgM5DDWapCPHaKSfiO7aVfbUq4TX4I=; b=Ze0biG6I68Jhq84TJj3XP6dlmfdVYE+u46R7AJMWnp61gnK8O2QD/wTCNbnjdZPfva dO05UX4j8ioUer7DCtLUWFmiurkaAQpMTVakKB7lnIfa0PV4VarlG3f7gR1JStxl9C3G yb1GoMkqz4n+mgD72IsidrOvwXMM9+k8ySVQOgz+Y1ghwfe4bnzpyFJjRHUamdWkvpvR cbPHacb0VrsGb8LTfkHMzMiqmZU5utcAigmILeUREn/pr34v4XMRwPRc8pg3xGOUvp55 7Ii8wOIkiW+YeSCKaQiVOAZaY03211X190EPAYNSlOtjtSHKBNe48UT8U345wHFlQtdH EowQ== X-Gm-Message-State: AOJu0YyzDRP4C6bcUjzltMRd4EHYmHOZbcUf8iE6cpWziEl8fLMiMX7l cLy480E9qSEFA0BfLtw5fpuD2I1qlFGDPtnq3FAEnjROkfN8X2TJ74xqXVNLdVE= X-Google-Smtp-Source: AGHT+IFHc85YBXiNn4D8rdtozAfwacyNkK/8Q8TRqi7+kDxtvxqzkQ5zrQigSIX0gkckphcFW/XxTg== X-Received: by 2002:a17:906:711:b0:a3e:8f38:8d76 with SMTP id y17-20020a170906071100b00a3e8f388d76mr820403ejb.59.1708682478508; Fri, 23 Feb 2024 02:01:18 -0800 (PST) Received: from laptop.fritz.box ([2a02:2455:826e:4900:660d:e728:4b64:50ff]) by smtp.gmail.com with ESMTPSA id ha1-20020a170906a88100b00a381ca0e589sm6734917ejb.22.2024.02.23.02.01.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 23 Feb 2024 02:01:18 -0800 (PST) From: Karthik Nayak To: git@vger.kernel.org Cc: gitster@pobox.com, ps@pks.im, phillip.wood123@gmail.com, Karthik Nayak Subject: [PATCH v5 3/5] refs: introduce `refs_for_each_include_root_refs()` Date: Fri, 23 Feb 2024 11:01:10 +0100 Message-ID: <20240223100112.44127-4-karthik.188@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240223100112.44127-1-karthik.188@gmail.com> References: <20240119142705.139374-1-karthik.188@gmail.com> <20240223100112.44127-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_ROOT_REFS`, which will be used to iterate over regular refs plus pseudorefs and HEAD. 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. Introduce `refs_for_each_include_root_refs()` which calls `do_for_each_ref()` with this newly introduced flag. In `refs/files-backend.c`, introduce a new function `add_pseudoref_and_head_entries()` to add pseudorefs and HEAD to the `ref_dir`. We then finally call `add_pseudoref_and_head_entries()` whenever the `DO_FOR_EACH_INCLUDE_ROOT_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 | 65 ++++++++++++++++++++++++++++++++++++++++---- refs/refs-internal.h | 6 ++++ 4 files changed, 79 insertions(+), 5 deletions(-) diff --git a/refs.c b/refs.c index 3546d90831..7d58fe1e09 100644 --- a/refs.c +++ b/refs.c @@ -1765,6 +1765,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_include_root_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_ROOT_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 f66cdd731c..5cfaee6229 100644 --- a/refs.h +++ b/refs.h @@ -398,6 +398,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 refs including root refs, i.e. pseudorefs and HEAD. + */ +int refs_for_each_include_root_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 65128821a8..9c1c42fe52 100644 --- a/refs/files-backend.c +++ b/refs/files-backend.c @@ -315,9 +315,59 @@ 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 to the ref dir by parsing the directory for any files + * which follow the pseudoref syntax. + */ +static void add_pseudoref_and_head_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(ref_store, de->d_name) || + is_headref(ref_store, 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 +378,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_ROOT_REFS) + add_pseudoref_and_head_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 +916,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 +1277,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 83e0f0bba3..73a8fa18ad 100644 --- a/refs/refs-internal.h +++ b/refs/refs-internal.h @@ -260,6 +260,12 @@ 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 root refs i.e. HEAD and pseudorefs along with the regular + * refs. + */ + DO_FOR_EACH_INCLUDE_ROOT_REFS = (1 << 3), }; /* From patchwork Fri Feb 23 10:01:11 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: karthik nayak X-Patchwork-Id: 13568829 Received: from mail-ej1-f49.google.com (mail-ej1-f49.google.com [209.85.218.49]) (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 E530F5D461 for ; Fri, 23 Feb 2024 10:01:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.49 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708682484; cv=none; b=YN7o9Tpz3He7WtkKJ0OFN7+enSoFSmU1gyz2vy/t6rLaHvmiU6286oOrbRDCKxyAWRrMCjP+70F+ZPyAxA3n8kPB/cNDSGzlN4VPnPGGO0K3Q/N5GEUrllH4ns9tB3QBR1/rRPWUBQAotIRVVXQTc5rx++/twFBZyDx/J4ZnpNI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708682484; c=relaxed/simple; bh=fdMvcQ3ggkXZxLDXfELYFogbCnL4yMRk39ONX3bUV5g=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=TJRlVJJYBHtfxmUCIiZlskOOt8LhHO4Z/4xEiX8DQdsJs7Xt/7f4XQv9hlpBKUdgkW6Ry7i9eXr7CUczLMe/tXUADe4XDzfoVGmnxoPUCyN2VjUbgUIZZNcm8k6Dkl76qr+jAKMqi4fapHnk9GdL3hghgdE11hoX3gRgwC16ebQ= 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=LiC/N76U; arc=none smtp.client-ip=209.85.218.49 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="LiC/N76U" Received: by mail-ej1-f49.google.com with SMTP id a640c23a62f3a-a3e72ec566aso82189366b.2 for ; Fri, 23 Feb 2024 02:01:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1708682480; x=1709287280; 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=ONUj48aF3C7y99NgU69gpZnZKZCfgGEoHsbwW5vy4ww=; b=LiC/N76UVcPslkxHnGdQWuosfu7jMlR7Nc9X1Ml0iFG+qZCjIRhwGO5r3A8mXD5pUZ qczg2EgdTcHq1x+K8zK4Gay8eWWm43GTwIXBrbchQSf4XwkU8HXLV/ubCuVxE7h/Tz+w dLKBwO8M3xskKGfzaF3chM1+wxImUCEwgXfuvmGhRUXNk+I4ejr58UJSyEgbwbj7iCXv 0YKSULbJm24NaXuQSQgX8SNw9WI8aPrJTMYLAOLSf6Vtpb9Wvj9GfWFNctdxlh5+r1Qv BprFFtDmt1uY4OMi+U9PFmNOIpR5OE+OtbsHhQVT3MlnaYBK1TuSYZo/QXhAJHDrgD7B syIg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708682480; x=1709287280; 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=ONUj48aF3C7y99NgU69gpZnZKZCfgGEoHsbwW5vy4ww=; b=wcHgz6EBLJTCxWolkFcWqN5GLGocLBtWjPnqML0Z2XE3d89TqQr/kc8a2rHzzknUk3 b6aSSFkbCz3Wptq+jJv5wE8Kew0zxLFmmWhkp7G9r265pEVLb0ojFiodFOvZ/Z3lqRzi LvkGvYABf9G8aiLcRg9/rtlUi4gZzVzyxpLqRkKVPuswVwNt5iSdhQM/i9+D4cU6iJF7 qcr8VpgPQGpN9D7geR6jYZGYwRVX0KExevBhw/1uAv+RnIP6aHBPufDNFb273P1K02ck oTaI3cq84oZDXHVTlyKCOEf+PjfCOkDKkm9A3s51JTpO/QeCT+GuyTGJ435vfobqAo91 QBjw== X-Gm-Message-State: AOJu0YyqxFBUOo2l/d9tzEfWxKrozvkkYmYDpME+JTPzRb+nPtxdErU2 LuuG15PoszswwSyTQH4c7yf0g3dBo5FHLn/plJYwqesCHcKMc5mNq4uHdxOR6Uc= X-Google-Smtp-Source: AGHT+IG/vxH9CDmXZU73Gd48Y9oBEW1V9sIQfbA+BOjBDoxZlk9dx8GfC8exV6vARtiJBhnWW8Ndsg== X-Received: by 2002:a17:906:4551:b0:a3f:c114:9674 with SMTP id s17-20020a170906455100b00a3fc1149674mr841551ejq.12.1708682479592; Fri, 23 Feb 2024 02:01:19 -0800 (PST) Received: from laptop.fritz.box ([2a02:2455:826e:4900:660d:e728:4b64:50ff]) by smtp.gmail.com with ESMTPSA id ha1-20020a170906a88100b00a381ca0e589sm6734917ejb.22.2024.02.23.02.01.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 23 Feb 2024 02:01:18 -0800 (PST) From: Karthik Nayak To: git@vger.kernel.org Cc: gitster@pobox.com, ps@pks.im, phillip.wood123@gmail.com, Karthik Nayak Subject: [PATCH v5 4/5] ref-filter: rename 'FILTER_REFS_ALL' to 'FILTER_REFS_REGULAR' Date: Fri, 23 Feb 2024 11:01:11 +0100 Message-ID: <20240223100112.44127-5-karthik.188@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240223100112.44127-1-karthik.188@gmail.com> References: <20240119142705.139374-1-karthik.188@gmail.com> <20240223100112.44127-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 flag 'FILTER_REFS_ALL' is a bit ambiguous, where ALL doesn't specify if it means to contain refs from all worktrees or whether all types of refs (regular, HEAD & pseudorefs) or all of the above. Since here it is actually referring to all refs with the "refs/" prefix, let's rename it to 'FILTER_REFS_REGULAR' to indicate that this is specifically for regular refs. Signed-off-by: Karthik Nayak --- builtin/for-each-ref.c | 2 +- ref-filter.c | 2 +- ref-filter.h | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/builtin/for-each-ref.c b/builtin/for-each-ref.c index 3885a9c28e..23d352e371 100644 --- a/builtin/for-each-ref.c +++ b/builtin/for-each-ref.c @@ -97,7 +97,7 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix) } filter.match_as_path = 1; - filter_and_format_refs(&filter, FILTER_REFS_ALL, sorting, &format); + filter_and_format_refs(&filter, FILTER_REFS_REGULAR, sorting, &format); ref_filter_clear(&filter); ref_sorting_release(sorting); diff --git a/ref-filter.c b/ref-filter.c index be14b56e32..acb960e35c 100644 --- a/ref-filter.c +++ b/ref-filter.c @@ -3047,7 +3047,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_REGULAR) 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..5416936800 100644 --- a/ref-filter.h +++ b/ref-filter.h @@ -19,10 +19,10 @@ #define FILTER_REFS_BRANCHES 0x0004 #define FILTER_REFS_REMOTES 0x0008 #define FILTER_REFS_OTHERS 0x0010 -#define FILTER_REFS_ALL (FILTER_REFS_TAGS | FILTER_REFS_BRANCHES | \ +#define FILTER_REFS_REGULAR (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_KIND_MASK (FILTER_REFS_REGULAR | FILTER_REFS_DETACHED_HEAD) struct atom_value; struct ref_sorting; From patchwork Fri Feb 23 10:01:12 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: karthik nayak X-Patchwork-Id: 13568830 Received: from mail-ej1-f42.google.com (mail-ej1-f42.google.com [209.85.218.42]) (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 EA8565D48A for ; Fri, 23 Feb 2024 10:01:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.42 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708682485; cv=none; b=L8AccK8fZfesKvqoQUlWEoFRcfsnPbnwf2oQSUqiIIHjOY2rwkwEq/LJF6z6h/9m7QML5fdvQyPFrdB1mqR+fIpytACirjt69OfkHeDrAsbz9tyLFho9HxcjxhGHumCGZSceANbv5sg1XhH6yqLUN0Rcl5hjtzrsljD1MceATQc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708682485; c=relaxed/simple; bh=FZugirshiZwDgpBKthHn485F9miESgoJDiqhgbUqfdE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=k0hr0j38ypEpuyHd98eqrt8vLMpxZgc29BgGhoKXeouVMK8SxPT36hQKtnBGCxJS83aI1PezegW91hNTLfVjW9ho4eqYRd69kvGcszDrSzsnOaIh8f5HiBfwyHpN14ExQ9TALjQEK3deeZWhQojbidtgTmtvPXdguGhplCBMoAU= 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=YrsVchnY; arc=none smtp.client-ip=209.85.218.42 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="YrsVchnY" Received: by mail-ej1-f42.google.com with SMTP id a640c23a62f3a-a3e552eff09so65375466b.3 for ; Fri, 23 Feb 2024 02:01:22 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1708682481; x=1709287281; 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=EdBjcIhb72wm2Mqt9yDpGuFQvhq8IxfvpuJF0VfZBYs=; b=YrsVchnYizWrNzqB7n+MdvMoAlpZaslT+z/Own9lwm9B4/O8Yz2u/ViDK8/5byhJql frj7CAxyRr7B/vVJWsZ1rXO1fsg7uLBqpgvZQoLL9h3komptaZgO1GI5qli165gCrQRH CT5YO942+kmHHI9IeO3HAKr2PjkqEMkajcykKsvqXce9Z50BCctj2t5IAoD1BdaZOCn0 4MxfzSRkG+Enx3b+BX7mAk7JbLKjfYhd3gEItV4WorH4DIlch5aO+AiLhxZx+IJNsv7H y5uxQikDhGLjwXt7Ua1G+IRIRnmnYjuVEK40VVnMaGf8WHY6VBEhjpWcuwzZM+CPNR/2 N+3g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708682481; x=1709287281; 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=EdBjcIhb72wm2Mqt9yDpGuFQvhq8IxfvpuJF0VfZBYs=; b=DKCalzUjNDknHN6u350MeEWq9szSUf6WD6HVOIGixRX/YqXUjtFPzru+Pnu3rEc4Tu tqb1zLyKu8+RkMohFRNCpMwdjZ7faukkGDqGEhVZpXB39U73qGdj+KXLpaTaV8LWZwct bziV99U3bdJdoQqr4BCbsLbk7nlr6Sbn3anUOuQZkdJ8S0mxjT+XjQFhr3jxcu2otRPc hmJdK8BXk9wGlOlZcp6ie3msDejELat4Q+ke5NvAHkqzg9qLFpt5kZUia2b2gsnXcMsr rJu2WhZt8rVJl2JXyiTc3cNTwkeN5kDACg4JPCUWVzUEASjGrq9VfQMmQR7yd/HDz44G IOVA== X-Gm-Message-State: AOJu0Yy5ZfPD6p2fuKLrysxgwzDhN+XlbjSx70iRDCNJvNwfOP9xf/VQ olfytRscY7XsKC7rBNSz8Cxtw5NelqmJ8IhNSfTdPHVsy2SjcVjPKxVQ9ksb1wY= X-Google-Smtp-Source: AGHT+IFFa8RWmPLpVjCC1eHolapypUz4gH6+wP+tVTJV3jyRAOA38W4LFigXIm/xTu8gUWhw+jhjMQ== X-Received: by 2002:a17:906:80c5:b0:a3f:d2cc:e8ae with SMTP id a5-20020a17090680c500b00a3fd2cce8aemr598196ejx.63.1708682480422; Fri, 23 Feb 2024 02:01:20 -0800 (PST) Received: from laptop.fritz.box ([2a02:2455:826e:4900:660d:e728:4b64:50ff]) by smtp.gmail.com with ESMTPSA id ha1-20020a170906a88100b00a381ca0e589sm6734917ejb.22.2024.02.23.02.01.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 23 Feb 2024 02:01:20 -0800 (PST) From: Karthik Nayak To: git@vger.kernel.org Cc: gitster@pobox.com, ps@pks.im, phillip.wood123@gmail.com, Karthik Nayak Subject: [PATCH v5 5/5] for-each-ref: add new option to include root refs Date: Fri, 23 Feb 2024 11:01:12 +0100 Message-ID: <20240223100112.44127-6-karthik.188@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240223100112.44127-1-karthik.188@gmail.com> References: <20240119142705.139374-1-karthik.188@gmail.com> <20240223100112.44127-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 git-for-each-ref(1) command doesn't provide a way to print root refs i.e pseudorefs and HEAD with the regular "refs/" prefixed refs. This commit adds a new option "--include-root-refs" to git-for-each-ref(1). When used this would also print pseudorefs and HEAD for the current worktree. Signed-off-by: Karthik Nayak --- Documentation/git-for-each-ref.txt | 5 ++++- builtin/for-each-ref.c | 10 +++++++--- ref-filter.c | 28 +++++++++++++++++++++++++-- ref-filter.h | 5 ++++- refs/reftable-backend.c | 11 +++++++---- t/t6302-for-each-ref-filter.sh | 31 ++++++++++++++++++++++++++++++ 6 files changed, 79 insertions(+), 11 deletions(-) diff --git a/Documentation/git-for-each-ref.txt b/Documentation/git-for-each-ref.txt index 3a9ad91b7a..c1dd12b93c 100644 --- a/Documentation/git-for-each-ref.txt +++ b/Documentation/git-for-each-ref.txt @@ -10,7 +10,7 @@ SYNOPSIS [verse] 'git for-each-ref' [--count=] [--shell|--perl|--python|--tcl] [(--sort=)...] [--format=] - [ --stdin | ... ] + [--include-root-refs] [ --stdin | ... ] [--points-at=] [--merged[=]] [--no-merged[=]] [--contains[=]] [--no-contains[=]] @@ -105,6 +105,9 @@ TAB %(refname)`. any excluded pattern(s) are shown. Matching is done using the same rules as `` above. +--include-root-refs:: + List root refs (HEAD and pseudorefs) apart from regular refs. + FIELD NAMES ----------- diff --git a/builtin/for-each-ref.c b/builtin/for-each-ref.c index 23d352e371..919282e12a 100644 --- a/builtin/for-each-ref.c +++ b/builtin/for-each-ref.c @@ -20,10 +20,10 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix) { struct ref_sorting *sorting; struct string_list sorting_options = STRING_LIST_INIT_DUP; - int icase = 0; + int icase = 0, include_root_refs = 0, from_stdin = 0; struct ref_filter filter = REF_FILTER_INIT; struct ref_format format = REF_FORMAT_INIT; - int from_stdin = 0; + unsigned int flags = FILTER_REFS_REGULAR; struct strvec vec = STRVEC_INIT; struct option opts[] = { @@ -53,6 +53,7 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix) OPT_NO_CONTAINS(&filter.no_commit, N_("print only refs which don't contain the commit")), OPT_BOOL(0, "ignore-case", &icase, N_("sorting and filtering are case insensitive")), OPT_BOOL(0, "stdin", &from_stdin, N_("read reference patterns from stdin")), + OPT_BOOL(0, "include-root-refs", &include_root_refs, N_("also include HEAD ref and pseudorefs")), OPT_END(), }; @@ -96,8 +97,11 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix) filter.name_patterns = argv; } + if (include_root_refs) + flags |= FILTER_REFS_ROOT_REFS; + filter.match_as_path = 1; - filter_and_format_refs(&filter, FILTER_REFS_REGULAR, 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 acb960e35c..0ec29f7385 100644 --- a/ref-filter.c +++ b/ref-filter.c @@ -2628,6 +2628,12 @@ static int for_each_fullref_in_pattern(struct ref_filter *filter, each_ref_fn cb, void *cb_data) { + if (filter->kind == FILTER_REFS_KIND_MASK) { + /* In this case, we want to print all refs including root refs. */ + return refs_for_each_include_root_refs(get_main_ref_store(the_repository), + cb, cb_data); + } + if (!filter->match_as_path) { /* * in this case, the patterns are applied after @@ -2750,6 +2756,9 @@ static int ref_kind_from_refname(const char *refname) return ref_kind[i].kind; } + if (is_pseudoref(get_main_ref_store(the_repository), refname)) + return FILTER_REFS_PSEUDOREFS; + return FILTER_REFS_OTHERS; } @@ -2781,7 +2790,16 @@ 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)) + + /* + * Generally HEAD refs are printed with special description denoting a rebase, + * detached state and so forth. This is useful when only printing the HEAD ref + * But when it is being printed along with other pseudorefs, it makes sense to + * keep the formatting consistent. So we mask the type to act like a pseudoref. + */ + if (filter->kind == FILTER_REFS_KIND_MASK && kind == FILTER_REFS_DETACHED_HEAD) + kind = FILTER_REFS_PSEUDOREFS; + else if (!(kind & filter->kind)) return NULL; if (!filter_pattern_match(filter, refname)) @@ -3049,7 +3067,13 @@ static int do_filter_refs(struct ref_filter *filter, unsigned int type, each_ref ret = for_each_fullref_in("refs/tags/", fn, cb_data); else if (filter->kind & FILTER_REFS_REGULAR) ret = for_each_fullref_in_pattern(filter, fn, cb_data); - if (!ret && (filter->kind & FILTER_REFS_DETACHED_HEAD)) + + /* + * When printing all ref types, HEAD is already included, + * so we don't want to print HEAD again. + */ + if (!ret && (filter->kind != FILTER_REFS_KIND_MASK) && + (filter->kind & FILTER_REFS_DETACHED_HEAD)) head_ref(fn, cb_data); } diff --git a/ref-filter.h b/ref-filter.h index 5416936800..0ca28d2bba 100644 --- a/ref-filter.h +++ b/ref-filter.h @@ -22,7 +22,10 @@ #define FILTER_REFS_REGULAR (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_REGULAR | FILTER_REFS_DETACHED_HEAD) +#define FILTER_REFS_PSEUDOREFS 0x0040 +#define FILTER_REFS_ROOT_REFS (FILTER_REFS_DETACHED_HEAD | FILTER_REFS_PSEUDOREFS) +#define FILTER_REFS_KIND_MASK (FILTER_REFS_REGULAR | FILTER_REFS_DETACHED_HEAD | \ + FILTER_REFS_PSEUDOREFS) struct atom_value; struct ref_sorting; diff --git a/refs/reftable-backend.c b/refs/reftable-backend.c index a14f2ad7f4..c23a516ac2 100644 --- a/refs/reftable-backend.c +++ b/refs/reftable-backend.c @@ -364,12 +364,15 @@ static int reftable_ref_iterator_advance(struct ref_iterator *ref_iterator) break; /* - * The files backend only lists references contained in - * "refs/". We emulate the same behaviour here and thus skip - * all references that don't start with this prefix. + * The files backend only lists references contained in "refs/" unless + * the root refs are to be included. We emulate the same behaviour here. */ - if (!starts_with(iter->ref.refname, "refs/")) + if (!starts_with(iter->ref.refname, "refs/") && + !(iter->flags & DO_FOR_EACH_INCLUDE_ROOT_REFS && + (is_pseudoref(&iter->refs->base, iter->ref.refname) || + is_headref(&iter->refs->base, iter->ref.refname)))) { continue; + } if (iter->prefix && strncmp(iter->prefix, iter->ref.refname, strlen(iter->prefix))) { diff --git a/t/t6302-for-each-ref-filter.sh b/t/t6302-for-each-ref-filter.sh index 82f3d1ea0f..948f1bb5f4 100755 --- a/t/t6302-for-each-ref-filter.sh +++ b/t/t6302-for-each-ref-filter.sh @@ -31,6 +31,37 @@ test_expect_success 'setup some history and refs' ' git update-ref refs/odd/spot main ' +test_expect_success '--include-root-refs pattern prints pseudorefs' ' + 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 + git update-ref ORIG_HEAD main && + git for-each-ref --format="%(refname)" --include-root-refs >actual && + test_cmp expect actual +' + +test_expect_success '--include-root-refs with other patterns' ' + cat >expect <<-\EOF && + HEAD + ORIG_HEAD + EOF + git update-ref ORIG_HEAD main && + git for-each-ref --format="%(refname)" --include-root-refs "*HEAD" >actual && + test_cmp expect actual +' + test_expect_success 'filtering with --points-at' ' cat >expect <<-\EOF && refs/heads/main