From patchwork Sun Feb 11 18:39:19 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: karthik nayak X-Patchwork-Id: 13552561 Received: from mail-ej1-f46.google.com (mail-ej1-f46.google.com [209.85.218.46]) (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 30F081E4B8 for ; Sun, 11 Feb 2024 18:39:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.46 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707676770; cv=none; b=EsMCs2lE2Gk+XhS+MLTpj3Rz4eyy3pNYLy4wWHlSE2AHnmKDUwx4WZI4QijytMO5rGJULgRlpmGAlqkU5psKYZb41eetIoQCTXB1SxX5PUSOaMDjsdukDjyDnU5zl4bqzLjd+k1ZpIpwoDQOjTtWJRpLFgWOH+IlB8vtpRwNIzg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707676770; c=relaxed/simple; bh=CvOaKPfvQP+0K6egEHgQKPOab/UC67lLwrXW0phiMJo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=e7dhTqMSpx8t1uri3wkDGWGDFgrft35L8F7h6J5Um3NOyPfkMCiiVJVGxZa9O/tPfDtPE8VSSDr1JhFvnJ6HWh4q1kjtA3KwEqS8Q4Aw7ly8hCfS8Jvyznf16lRM8WbQgH3woxpaPa5eavKQxv5JT+X3D1RUwdfkotp3arPkZ38= 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=NGiPHAO2; arc=none smtp.client-ip=209.85.218.46 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="NGiPHAO2" Received: by mail-ej1-f46.google.com with SMTP id a640c23a62f3a-a38392b9917so321048966b.1 for ; Sun, 11 Feb 2024 10:39:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1707676767; x=1708281567; 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=Nk2qVT806rDHDYk4aclkk1FWi54V2TibjmD9T2J6Sok=; b=NGiPHAO2JKluDfpXhUS2V8H8fhVoB9iwepLBWzVCUyG8gOZVLf3cURnPdkwzpzotWk Zc9IsA/YLmbH/KWzNbEIXypQS7RZZHpMcaG4FWR9wcI8q7SjCZA0EgKiJNpbVoYdwYDW 9VGQwGkRvKaaqRpwZCWzXVu8VWOuhC1hPIruzZV2nbxPJZ/+Z2WSl79aQ/48Awe6bhkG ttl6uLvFP2yNCZZ345TmP3PUiea6aPiAJYNOJ4K+i49/ivwMCJvXkuF/CKEjS0fTr0Zx ehjjAcHwaDC3feXOPF7LNBC0/tneJRqXcbHVZr1YvnGpUr/4/FLL33NpsuqIzK/e3RXQ VlIw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707676767; x=1708281567; 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=Nk2qVT806rDHDYk4aclkk1FWi54V2TibjmD9T2J6Sok=; b=L4LecPI3+4KJrhKp7IIMHNNpCefHzwrC6CeMFIQiqZITrmUAWfkwbI5qv/TuV4MgtU YZA5mP9wvgFKZBdD3nfdooxfsJcAfqTBOQjfzqhfrumoMhfREu/5/1zxYlFuve8Lvl+4 ATyLw57AxyK1D4Go4aGyj5eJvFajS39lNQaEkcuVZFV5InqhOb2q9xW0ClDNbdUWdkwL rCK4zQnWTaAaoUNvI46r9ubYsdd+PgUGd4LgcXnfTYRktFLvTCTIsTaERqAVy/6RlgWV b7vIgAav2hgRm5E2CoJaXUyppIh1L4eQR00mjcN3gfC3X/XII7njmvYVDiAWPQEzXA7T BpOg== X-Gm-Message-State: AOJu0Yx8uhJmDJ2GwwvYHPQqSa+M0LjZk3OQHgHcWLBmWX2hjhlHQCg4 KT/iTonHb/ayZaxW3Z1xaFWD46DRByFPFLk9zyGlXy7VeAfASEdTfY7Ylpwz X-Google-Smtp-Source: AGHT+IEKP+Znl0qhF9JxyFxUCa9uM+O3J2n7G3Ucnn6+pdYy9i8SrqO5m7HfA2z0eMAiEevSMFUXSA== X-Received: by 2002:a17:906:a92:b0:a3c:177d:cb1b with SMTP id y18-20020a1709060a9200b00a3c177dcb1bmr3258067ejf.10.1707676766955; Sun, 11 Feb 2024 10:39:26 -0800 (PST) X-Forwarded-Encrypted: i=1; AJvYcCUOHiIQ3v1GPji7hcXECt/9Z7ADbdVM89MRmr+/9XEN5YUIEEb3u8fYMtmsCReLn39G5Fr26lmDfmNocAZcolG6vso30BRTZXfkQqumk24QJYw+tZbgqomfXLGeve2sOPH+NWY03+equraQz7VJDLJOoAp95be22+PTvQ== Received: from localhost.localdomain (h-213.61.124.196.host.de.colt.net. [213.61.124.196]) by smtp.gmail.com with ESMTPSA id w12-20020a170906130c00b00a3716299b40sm3069808ejb.155.2024.02.11.10.39.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 11 Feb 2024 10:39:26 -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 v4 1/5] refs: introduce `is_pseudoref()` and `is_headref()` Date: Sun, 11 Feb 2024 19:39:19 +0100 Message-ID: <20240211183923.131278-2-karthik.188@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240211183923.131278-1-karthik.188@gmail.com> References: <20240119142705.139374-1-karthik.188@gmail.com> <20240211183923.131278-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..d8e4cf9a11 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 Sun Feb 11 18:39:20 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: karthik nayak X-Patchwork-Id: 13552562 Received: from mail-ej1-f43.google.com (mail-ej1-f43.google.com [209.85.218.43]) (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 63B641E4BE for ; Sun, 11 Feb 2024 18:39:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.43 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707676772; cv=none; b=mV6cz2luVNc95sZpjSfDNzj4d9WBpSHLfBTXflFFOSjFsSgdpd51sRCXdHtnwiaIbYV2mdoroq7m7oMsDVzTgOXJjDl2hjEa/rahyTmhlLEOvrXynXvdVmQ4uvcdplFLnSPfxi/XhvC6lwu9CHAnRU69KS15FcWouxiIq+ZQXPc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707676772; c=relaxed/simple; bh=VfltkTB10ZIBhlAE6ooGiSF5wKaiCn9vjFBaF9tYY0A=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=GV2luOOvX86qc2b5pIV8RSb/W67/Ms7ZjHDnxadAvNjaZlzQ+LO9veDkbnyzguwcG1D8u9ocmBjR+xihyQQQ7rbiLLu1+GkxJ6C2pwOH4/cHkB/NMhAZO0EobyCbCk595x2ktabZxK+MigXcSos9zIo+Jh/PUQwqj+HtbwmOVp4= 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=bfkVGgqd; arc=none smtp.client-ip=209.85.218.43 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="bfkVGgqd" Received: by mail-ej1-f43.google.com with SMTP id a640c23a62f3a-a29c4bbb2f4so320324466b.1 for ; Sun, 11 Feb 2024 10:39:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1707676768; x=1708281568; 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=bfkVGgqdIbWRFnlSmxD/c0XuD/Bb+tg+xVeuQerD4QQ782ZHOP6ajiCi8rZsQt2ZG1 rKccYSlNwTZaH0z1GBb0BtcCqeF/fJw67NtTjpiWhW0ZjXFEDQ+yTZNw023viVVMNJf+ WRKEb+s7KDicKYkrv2YDwxA8y7ZWNBNpHqLRv+1V2KO7OzNwV0TgswgIkBKggYQbsTpN rAUyR4vFQ+/f+OMz2sksVxEXnEidEICaxyJxKAMeB+anlU9UpTu9icmV8dbN86knkl/8 zNSe9QX3fLit/YjF/k2BNz0hHsWcJ5aqHEGwTTCJ7TlI6zT1WElT70/i+Z60Pft0c80/ GDow== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707676768; x=1708281568; 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=P3QGG7ZbNCEuE/i27DWs+5AHFd0uvZ3+bJCYxZcIA6wnB1z5Sk1KV9eVfxkDWs8W7n Zc+0hbCT9/A8S09wDmL8VPfw74Xtp1dbhXf3S/+R36odXIicwunnHULWVJbNHQQyzDNd hy9u+mOs5uNJoW24uWxzbrdo42AodnJ/+sPrC4XsnBpYtBBvTJUmZwv88C0WQd0us81J PxKD4BF/4BBSTRkoikyebV+RMWYCw3KlEymiLqGb9qPba3uOrrNLyVCqwAQSOejqE+Uq cZ1/7x+2+CR/NpiPdKezoX5jKHgk/w0WM2OHPUR6KsIN3+Xi/Gzgl/Dzcol5XV9+f3UA u8yQ== X-Gm-Message-State: AOJu0YyxFTseMoBkH6ByZ7Tfjlag3SZJCbrJjQu6DoB9GIO5Gn20n3Q2 6AZHQ8J5UMyfeoHX3dMB9ovyJ667ARiRHc8v4HYmpVASle0cj6WsUuSUB04L X-Google-Smtp-Source: AGHT+IH1ByVVIVDnUjhbHzr9NfSk74mNUusKFRDM/4Dwxsiu+aSg06I1OpIhuifMGXBhNis0U3fTgg== X-Received: by 2002:a17:906:560e:b0:a3c:2f9a:71d1 with SMTP id f14-20020a170906560e00b00a3c2f9a71d1mr2664995ejq.10.1707676768155; Sun, 11 Feb 2024 10:39:28 -0800 (PST) X-Forwarded-Encrypted: i=1; AJvYcCXujO/mfxcOixX8Q/Q1UyX4QVLdY62uU93Bho6WRxrYfGhf8/zDM88vEQVDzQgRx3diV0u7DJKFzqFrab9woe5KZx/klIZJnapDXdkkA+YCiA1gRSYXMklM+IkIVlrau8cFMWdm8Ax/1eXc5Q== Received: from localhost.localdomain (h-213.61.124.196.host.de.colt.net. [213.61.124.196]) by smtp.gmail.com with ESMTPSA id w12-20020a170906130c00b00a3716299b40sm3069808ejb.155.2024.02.11.10.39.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 11 Feb 2024 10:39:27 -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 v4 2/5] refs: extract out `loose_fill_ref_dir_regular_file()` Date: Sun, 11 Feb 2024 19:39:20 +0100 Message-ID: <20240211183923.131278-3-karthik.188@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240211183923.131278-1-karthik.188@gmail.com> References: <20240119142705.139374-1-karthik.188@gmail.com> <20240211183923.131278-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 Sun Feb 11 18:39:21 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: karthik nayak X-Patchwork-Id: 13552563 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 09C6B5D49D for ; Sun, 11 Feb 2024 18:39:31 +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=1707676773; cv=none; b=LZbyU6LZm0zR/XmxJid7/JwPvph/Z1Hi8sMDfpSX4A+4dAhV7alKrfhIkshRw5zem2k7o28DrDd+6WslkCSRmLVFLvlxa9RphOcF5QvJOYyYIBZRSc/Pq6dEtnb0s2UIjtZO66XtjjiB3d3EQJcgbyQhgkX2C1AVlWuQ2lOecTk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707676773; c=relaxed/simple; bh=XdHksjn/cdzBpsfKt6RXWgzaqPjs6g/AgtRaahE7OmE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Y/MatAoy4Dh2zDVjJrXT+iMRrwlyfw8N6NgFQGG+iZOzv8UU+U0oPKxNpbfN+oc9SegkOWxGJ73Qx2l7rhnmrfmvGHo99ToKsVqSdRx5rIbvbbagQprwqPuSvDQzBwpjDbSgP3HVyqIFsdEaJolh3R5pIkVgWAkWemBydjWP4W0= 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=etSYJaJZ; 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="etSYJaJZ" Received: by mail-ej1-f45.google.com with SMTP id a640c23a62f3a-a26ed1e05c7so338688066b.2 for ; Sun, 11 Feb 2024 10:39:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1707676770; x=1708281570; 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=MMan9LFdGitwOzGBOks4QduvHVZgtY9qYqaDjMNW1IE=; b=etSYJaJZ21MDLKrr+Y06H1A6Xz5R0ODOBXk20VJaLPmA8plKyh7IoVWt5Hn1n2b8C+ 4B9NEM9dsj8hwW0svK/NBqHrUqWnfpVgLx/7qmR8USMzNehoOg1KW6W80l4zqjvokxgP yEtLmi8u4ALyD8jdjjSqP/LizLvFv96V0kZq4yGeF+XHumymRAovy/jAFasIPJQA7BDf 95jNAQq27/JNtkLoUYFBzB0LvS7DkwS98AZKOc1zQ189fXrgcllzOLD3pNKZRGHpBdF8 Nm1w5KJaRqATHr7dk4X7ObA/O6vvv+HuIneBR3wb6/MVkOHRstuoTkP72JQ3mqpp3IrQ 3cZw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707676770; x=1708281570; 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=MMan9LFdGitwOzGBOks4QduvHVZgtY9qYqaDjMNW1IE=; b=RfKlplVEl274jdTMNEzI4t28lODvF8Qm8+QTlAji7NmvH62j2ISfmLDSQMvmwMFfyb glx6Qsjq4k8n/9Fh3hcvOh4n11eW4rFbfYLlvynJnIWnuiglVbQPgwmAUYNgvF+VZlh8 4g0/ogoRzeoZWxaeI2f/f4Dy1bxPsw8PA5zudvC28XzXJezB9ZDzX8bU9HwlBnjWTNdE zXfz/5fUDQKUd14wGcM+eExTUQ1iK/lNUks8Bg2uCUFbO8efajvkQx+kygwo3cBUJS+R CS7CfsdoEc7VPeY585fhnsn77UQTmnhdtZyHXoSuIe19CNfWwKv2XbODvFNFPZWDh2PZ bsDw== X-Gm-Message-State: AOJu0YyuIHsEIwIRDUGqOOQqsfPB0T7vBhD4RZ/GgAjAz4Vy1EFgOsQn JXEDTiwmknGN0uzV2vxwYu0K1GLy0rh1O8KJrQkL4Ou/qQymDAc6NrK55sBD X-Google-Smtp-Source: AGHT+IH5XBEEo0DhdJPW8a5Zgs8kzICnw/1oGoTt2Z9gc1/lUU7q0jb9JwGWVXdHmT6+2ckypO1vgQ== X-Received: by 2002:a17:906:cf9b:b0:a37:4765:658 with SMTP id um27-20020a170906cf9b00b00a3747650658mr2664254ejb.34.1707676769773; Sun, 11 Feb 2024 10:39:29 -0800 (PST) X-Forwarded-Encrypted: i=1; AJvYcCVybj5p46gNLIG/gsKlnCv4P5K2z8CNnfAaCdEN8ojpfXWgajKe1JIQTUmjX0rLTASjaihlD1Pezm8cEAHOGAhBPwZoNJnYty01lshaCHhNAI4llDrkixxeKz1IZZibGAI6aPwOQ2zHEBoUAA== Received: from localhost.localdomain (h-213.61.124.196.host.de.colt.net. [213.61.124.196]) by smtp.gmail.com with ESMTPSA id w12-20020a170906130c00b00a3716299b40sm3069808ejb.155.2024.02.11.10.39.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 11 Feb 2024 10:39:28 -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 v4 3/5] refs: introduce `refs_for_each_include_root_refs()` Date: Sun, 11 Feb 2024 19:39:21 +0100 Message-ID: <20240211183923.131278-4-karthik.188@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240211183923.131278-1-karthik.188@gmail.com> References: <20240119142705.139374-1-karthik.188@gmail.com> <20240211183923.131278-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 d8e4cf9a11..77f4c1e4c2 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 Sun Feb 11 18:39:22 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: karthik nayak X-Patchwork-Id: 13552564 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 027985D74A for ; Sun, 11 Feb 2024 18:39:32 +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=1707676774; cv=none; b=qF+WFtwDs7T/KQr5IE/okWIzJcebVK4LCqxBN3Mo0fGpid5EN5gNK6pwvHyTzxgI5nnhJ1lm3Zj9gZ/0zT4+1JwtGHpcsoBRniqMiLcI0LqFDecFsNvxttMZyJQ3gKXSOLoyyuJGBg1NR6qII52YePK2kgOog+UiIFww9uH0nBw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707676774; c=relaxed/simple; bh=fdMvcQ3ggkXZxLDXfELYFogbCnL4yMRk39ONX3bUV5g=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Sv/DczQGKl7idDV9kRYcaeS2waDx1VY9Ov+d+/rmZF6XzzAhOR9EYEGIBwxBC/ZsubHDmGrB+LxvLNC5QlK2i1YOG95HJpY0RXH4IvUlfmFhg4OX/vgaMvi+0dEL6o+F/T4Y9rKaIsN51VIctIrdPyJQMFmHEVbWHR5fd2sopm4= 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=cx9J15SJ; 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="cx9J15SJ" Received: by mail-ej1-f49.google.com with SMTP id a640c23a62f3a-a3c0efb9223so266476466b.1 for ; Sun, 11 Feb 2024 10:39:32 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1707676771; x=1708281571; 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=cx9J15SJzdNDBOdCQeTFPsum7kId89CAvoB+4TgfKoVzOS7YfnpWznUl8snT1flf2x ANga1fSmxga2YwR8mBoGz66w8zSqLUBOyfRV79BpCrR/jSyXL2cEMkkZ1aNN+icI94jS etGfbPlZWObKskNDOEWQGoVYAvPkS+cnIDFr+U1VBAsXMJXRwa+8YKSOBbYmlM+zB0L5 9eLklQ7bkYNHkQdAkUVofiDR1xJRxlYo/JRxp0f7+0ebowarYLYiHY1AcZT8RSlcUTch nSrFLT9l6gia0JQIJQIUySsnygo7+b+q9yNo/Yh1H85xDVeFez75GbHr9GPo4HpuxbyZ q+mg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707676771; x=1708281571; 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=nvBSvoATZZDZm0Nwkh06trWVDI+UyvKkV5+uv9dppBn+LfbxVPCaOPKl3WxQ5RKoRh ubhcxxERq/+ZYkjd7H9IkBBnUEokY1bXIGTso6ltZ1exoQcymdfj1wyofUdVVOy3Ayuh MGKDSg1WKknxtnuhOfHLOiEzPJ8uSlJj4ybAauTNmT5AQohvuS9SVnL7tTDx9SiuQWDg SKBTfDczBD9TYci/N0WUtqwOaBn/I8c/Q4nkDh5SNz4YYXdlizMPY2Q3TQZ4cKy+2/sU YUDJ5X1NJSBDj3+XboLPEZmfk294rrHreGDZZop4VKqMrSKVFzLYvUMvV4ijET3xEzHf M8+g== X-Gm-Message-State: AOJu0YxJq2Ks/4bovi84SXxvMIyjwzaXQ3vvDGjSwiW0dC8Te8QEf+FW MGTwbZ1thTRffS9ugcJyLn1y/bdCF20wlQ61nhCdOrCR/hzR3uStTYrLgSOJ X-Google-Smtp-Source: AGHT+IEbbTT/Tt5Ibi9yU1ntYIToFIU8+883pZcMsAnYdiIYj6ZWPzOBqhqX0mq5vX7sD5qP0NTN5A== X-Received: by 2002:a17:907:7295:b0:a3b:fe38:343f with SMTP id dt21-20020a170907729500b00a3bfe38343fmr6937230ejc.29.1707676770958; Sun, 11 Feb 2024 10:39:30 -0800 (PST) X-Forwarded-Encrypted: i=1; AJvYcCXa3869o9kGB9G/fnsWOp6TBSnZsRu9bknc8YpZzdq3UAcCAhcN1u/dPe5+Z6HO2P899J7iqbpVAh+SOqScKS+XwWthRoci92BTtaGkya9p4/mkLpPRaXhA6BgxGhIX0HLqZ9Z/sZ3OlkHqsA== Received: from localhost.localdomain (h-213.61.124.196.host.de.colt.net. [213.61.124.196]) by smtp.gmail.com with ESMTPSA id w12-20020a170906130c00b00a3716299b40sm3069808ejb.155.2024.02.11.10.39.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 11 Feb 2024 10:39:30 -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 v4 4/5] ref-filter: rename 'FILTER_REFS_ALL' to 'FILTER_REFS_REGULAR' Date: Sun, 11 Feb 2024 19:39:22 +0100 Message-ID: <20240211183923.131278-5-karthik.188@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240211183923.131278-1-karthik.188@gmail.com> References: <20240119142705.139374-1-karthik.188@gmail.com> <20240211183923.131278-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 Sun Feb 11 18:39:23 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: karthik nayak X-Patchwork-Id: 13552565 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 1F73F5D759 for ; Sun, 11 Feb 2024 18:39:33 +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=1707676775; cv=none; b=FsSNpIq1ieeE3Xg8MRjse2FDom/62wVts4ndec+NZHei6AdNBX1dm2O8sBt7xulOG+0DiL094iuljV673y928eu/wNUFGz0P947++K6sFC7pKePyUhEO8umx010SI8xpHYr2mP/7aLbKgpaTLR51vhMQhgMjVkfbxxN4k0s8EWA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707676775; c=relaxed/simple; bh=jRAc/WMMEIPT4uWq9Kh7vBImODiVTXJTaRKuyGdIJyA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=s0itaOmQQ+3dZHSC+SgRizpdUY5n042WnzDp3uplaNRxoQi1SDgRPz60J7DerJ7LOLfJOAQtPVuJeVuLdqoqQaGjflos2BY027AbOPw4a55sA9LGHxrozRHZUtkB8yCzIFywUbkz7Y9zTQnLMfrf4kwtMkcbtnlfPfFtui59COA= 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=PREqzbJa; 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="PREqzbJa" Received: by mail-ej1-f47.google.com with SMTP id a640c23a62f3a-a34c5ca2537so311045766b.0 for ; Sun, 11 Feb 2024 10:39:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1707676772; x=1708281572; 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=7RTpPQlVpONZILhTuiLZqVC72IculsaBZbyoIbQ0HIU=; b=PREqzbJazB3ogTeS/A6gDLHcKZuTWAdJDbwjZbONGLIVCKwW5IHryIcE3NLvKTrZJe 8fn0Ex1X1r0ar2HplTF7ksLNFUw8NTZXNkPqpqB7z5tXuBV99C2C1MqMAUrJhcG8qDkm 6bhgJBGxRxV9i9M/8M9mSQOQU50L0SknHURu25nkg9SUE9uBSrrC9LRRUvIk1FUqNd6a DiylJSiqN6Qdneuu6b/3YoE8pOeyLcEWXYm8qUUey7QU7iCsDYI80D7HXzpIw9kypyE7 WUIrkM84cjm3IriPubpab79R6CULNWme8TM04woNgwzKRsciVegsvPP1gy0nvwrnWN/s f6jA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707676772; x=1708281572; 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=7RTpPQlVpONZILhTuiLZqVC72IculsaBZbyoIbQ0HIU=; b=Tx0DDSsGZlzOvfz0e0SRjbXwYP+BngdgXi/XHbH7LlrFpfaX9k5JgkWmPzdwIWyd+w X0oXt416O6kJIsq09M7lTu40ORznwr2Tq2dHgSvsikuyq9otf0DBOdzIaW8vnqtxk5fi En8H3iJzihWF6tCVequblZ3z7HgwN6x6Z3kQkeGQYCxiQhEwfGXWA9hGmbbO2063zCts WN0U2VM6QEf4Ud1eFssavIIQrzddlMGTjceU41ioHwdYO7l1t7BPVuFmy2LIwxa37bvt uSuFbCKzh7pB4h97bbhD/6Ma6HO84pnXMDGklplLm2ozOwi9MPhUgW6Fxa11PgCuBJLc AK4w== X-Gm-Message-State: AOJu0YyWvF13OIDAyseydDRz54fFQWu2oUmJSrPQ4NcPZ7viOluCfk0Z Dn2mojbI9whD2ihmyZLVmaqaCmz9U1xRoSBpyu197OJWdFq59Oga2tt8V8WE X-Google-Smtp-Source: AGHT+IF/Bo2I7tI0qyH/CTWsTBjxYTagAvVyUKdiAvNRe8IWWl+xIcLThOX4EPug7b1hClOq1F3RYw== X-Received: by 2002:a17:906:264a:b0:a3c:4dfd:1040 with SMTP id i10-20020a170906264a00b00a3c4dfd1040mr2118258ejc.12.1707676771864; Sun, 11 Feb 2024 10:39:31 -0800 (PST) X-Forwarded-Encrypted: i=1; AJvYcCWOiip6S1gXeXWVKuw562thQYwkfHHLFqYG35gfkuaAuKvoIpgpeKRFhEym4CzIiVihMhotIcksMRhDTqSAEcZyRVJle4JWFzxhQqZXzavFspLtMsO7msX9o4supkKhPzkH/QoEMOfnoN/o0A== Received: from localhost.localdomain (h-213.61.124.196.host.de.colt.net. [213.61.124.196]) by smtp.gmail.com with ESMTPSA id w12-20020a170906130c00b00a3716299b40sm3069808ejb.155.2024.02.11.10.39.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 11 Feb 2024 10:39:31 -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 v4 5/5] for-each-ref: add new option to include root refs Date: Sun, 11 Feb 2024 19:39:23 +0100 Message-ID: <20240211183923.131278-6-karthik.188@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240211183923.131278-1-karthik.188@gmail.com> References: <20240119142705.139374-1-karthik.188@gmail.com> <20240211183923.131278-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 | 11 ++++++++--- ref-filter.c | 27 +++++++++++++++++++++++++- ref-filter.h | 5 ++++- refs/reftable-backend.c | 11 +++++++---- t/t6302-for-each-ref-filter.sh | 31 ++++++++++++++++++++++++++++++ 6 files changed, 80 insertions(+), 10 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..9ed146dad3 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,12 @@ 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..0e83e29390 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,6 +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); + + /* + * When printing HEAD with all other refs, we want to apply the same formatting + * rules as the other refs, so we simply ask it to be treated as 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 (!(kind & filter->kind)) return NULL; @@ -3049,7 +3068,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