From patchwork Tue Apr 23 05:07:26 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Steinhardt X-Patchwork-Id: 13639342 Received: from fout5-smtp.messagingengine.com (fout5-smtp.messagingengine.com [103.168.172.148]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1C3591CA82 for ; Tue, 23 Apr 2024 05:07:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=103.168.172.148 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713848853; cv=none; b=M0zPEsq8jiW+ZlBs/EYUMYuY9NTnAQc+6P6kIAzlF1WX0MmdmqfX8ktp2939MdG09xKsDBhlOknuRlTs2Pohz4tXnexXGjY8RfB6VbSaz2RMD/sEhYoNfLmCld6sofDCSzdswHm9QHJYwztHBBAgWs2sQmvKZPgmelVPhaZ9Hmc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713848853; c=relaxed/simple; bh=VAMRxG/By8Ho5fxEvNM2Ekl6yfflAcG1nfUP4LkoWXY=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=X8PVJrLgxztqC/v9SgxPS8APJMQIqOtTAbmBschv3+YBJMvuOHaTDj4l0tfg7VuNTWyNoQeglydM7tTvLopPRGjcjLsnKxrOdLt4tTC1msDK3kt5I7st8l6XV2a7sP2B18/XQPyo4XsdTEROEQmfS57CiuycA5lQ28AAJIFRaUM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pks.im; spf=pass smtp.mailfrom=pks.im; dkim=pass (2048-bit key) header.d=pks.im header.i=@pks.im header.b=RNCr5MW2; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b=IveJhPhS; arc=none smtp.client-ip=103.168.172.148 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pks.im Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pks.im Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=pks.im header.i=@pks.im header.b="RNCr5MW2"; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="IveJhPhS" Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailfout.nyi.internal (Postfix) with ESMTP id 0ED761380253; Tue, 23 Apr 2024 01:07:30 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute4.internal (MEProxy); Tue, 23 Apr 2024 01:07:30 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pks.im; h=cc:cc :content-type:content-type:date:date:from:from:in-reply-to :in-reply-to:message-id:mime-version:references:reply-to:subject :subject:to:to; s=fm3; t=1713848850; x=1713935250; bh=qVKHFDjepx DSXEZQlBYkSKHDX24eH2v0Clw48bFhSpg=; b=RNCr5MW2K8pH/1MDrB1FndwBNq aG17/IuTrcY6/acJ4UFHtjnbOWfZU56wtmApb3wnzhTE0LAkJZol8SguurTSXmZX zVa5IbV1C1Y13/tFOVWgRPAfRsSy36oxTYmtQU7bCMrgzT/iT9qMsFqPFOraJRZc HYOx1l0RLjtDiMwuYDWcnrk6hj4kFnUMd6Duk2dzxaq6VOpvsqWMJL01TxzUbbIM fUDtEF7ewpBMT7SSxOKTkAvmG61ntBZWLFsJ6/Xglvd4+qBeGslNAVEj7rbwnUf9 xWPYkEGB9p8Y3bMxrPuzE+njr+gtCmRkV7jKHBatIe6KZTZxhRfuHf1peb9g== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:subject:subject:to :to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm3; t=1713848850; x=1713935250; bh=qVKHFDjepxDSXEZQlBYkSKHDX24e H2v0Clw48bFhSpg=; b=IveJhPhSFOAp7u6jueZomt8ut6gT4mki4/DAOpqA/b8O uLG03qFgtCaznBifKVDCYIQHfJ20piAytq/YzGm/uzBqC0fyfJV5beW0mK9FkUVT L4SeHNJEr+8G08Hs5gbzPzft047nf/n7zi/YzO6sECfRB59MXcZoKRZGrvD6qOEg FU5Vljycg+ltY3gMWvFhWJoHOKBqJgOm4Z21IwdJviH9zhLhku8PKn8y/mCDe7ff W+JAgu3IA+c7M2775/Q4AouxXYNiLRM2GZHVb8Tjag/kIdV8c98+FWkrQobCZfRP SRNHJIllKVcA61TIZVH51vhrKZLwIfWtYwN6JfbrNg== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvledrudeltddgleegucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepfffhvfevuffkfhggtggujgesghdtreertddtvdenucfhrhhomheprfgrthhr ihgtkhcuufhtvghinhhhrghrughtuceophhssehpkhhsrdhimheqnecuggftrfgrthhtvg hrnhepueektdevtdffveeljeetgfehheeigeekleduvdeffeeghefgledttdehjeelffet necuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomhepphhsse hpkhhsrdhimh X-ME-Proxy: Feedback-ID: i197146af:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Tue, 23 Apr 2024 01:07:28 -0400 (EDT) Received: by localhost (OpenSMTPD) with ESMTPSA id 9aa70d36 (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO); Tue, 23 Apr 2024 05:07:21 +0000 (UTC) Date: Tue, 23 Apr 2024 07:07:26 +0200 From: Patrick Steinhardt To: git@vger.kernel.org Cc: Junio C Hamano , "brian m. carlson" , Justin Tobler Subject: [PATCH v2 01/12] path: harden validation of HEAD with non-standard hashes Message-ID: References: Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: The `validate_headref()` function takes a path to a supposed "HEAD" file and checks whether its format is something that we understand. It is used as part of our repository discovery to check whether a specific directory is a Git directory or not. Part of the validation is a check for a detached HEAD that contains a plain object ID. To do this validation we use `get_oid_hex()`, which relies on `the_hash_algo`. At this point in time the hash algo cannot yet be initialized though because we didn't yet read the Git config. Consequently, it will always be the SHA1 hash algorithm. In practice this works alright because `get_oid_hex()` only ends up checking whether the prefix of the buffer is a valid object ID. And because SHA1 is shorter than SHA256, the function will successfully parse SHA256 object IDs, as well. It is somewhat fragile though and not really the intent to only check for SHA1. With this in mind, harden the code to use `get_oid_hex_any()` to check whether the "HEAD" file parses as any known hash. One might be hard pressed to tighten the check even further and fully validate the file contents, not only the prefix. In practice though that wouldn't make a lot of sense as it could be that the repository uses a hash function that produces longer hashes than SHA256, but which the current version of Git doesn't understand yet. We'd still want to detect the repository as proper Git repository in that case, and we will fail eventually with a proper error message that the hash isn't understood when trying to set up the repository format. It follows that we could just leave the current code intact, as in practice the code change doesn't have any user visible impact. But it also prepares us for `the_hash_algo` being unset when there is no repository. Signed-off-by: Patrick Steinhardt --- path.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/path.c b/path.c index 67229edb9c..cc02165530 100644 --- a/path.c +++ b/path.c @@ -693,7 +693,7 @@ int validate_headref(const char *path) /* * Is this a detached HEAD? */ - if (!get_oid_hex(buffer, &oid)) + if (get_oid_hex_any(buffer, &oid) != GIT_HASH_UNKNOWN) return 0; return -1; From patchwork Tue Apr 23 05:07:32 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Steinhardt X-Patchwork-Id: 13639343 Received: from fhigh2-smtp.messagingengine.com (fhigh2-smtp.messagingengine.com [103.168.172.153]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 721761CA82 for ; Tue, 23 Apr 2024 05:07:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=103.168.172.153 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713848859; cv=none; b=toDiG1w755pOBIxmrPp/6rf3MPksFo5jZ0ux+T60IsqPQJyvFVRE/S/nI6L0Z5qh84/Eyfiji1i7qhJwUSpVsKaSpOa/m0RNim3iG4rvG1V9suqLiIIlIIBSs6DTUHYvHJLHaSnVIx3Jsn2PJwSL5vw4WipA3kOi4kCpVCqTwKQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713848859; c=relaxed/simple; bh=5Vt8BOrRcDEeufSce5pMHcdhHZXNyEF8Dcc0g/YyxOk=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=ifY06Rd8GQTy//aWRNd9kIkenGsATRqWu5YAe8RBASovhFjmrAkgHNA3tTSzA/HYwbPl/DEHtMAHWaOkZIB5jqm16YzLAC93GRorLRHXVIwAkcUKWbkmABir3GFDqdMFAbL6GI5gE4ekX+kJG7sBRExSU6ihVfIBzbnWOWgfU28= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pks.im; spf=pass smtp.mailfrom=pks.im; dkim=pass (2048-bit key) header.d=pks.im header.i=@pks.im header.b=YTjsk6iV; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b=dafL+gqP; arc=none smtp.client-ip=103.168.172.153 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pks.im Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pks.im Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=pks.im header.i=@pks.im header.b="YTjsk6iV"; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="dafL+gqP" Received: from compute5.internal (compute5.nyi.internal [10.202.2.45]) by mailfhigh.nyi.internal (Postfix) with ESMTP id 64F79114017C; Tue, 23 Apr 2024 01:07:36 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute5.internal (MEProxy); Tue, 23 Apr 2024 01:07:36 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pks.im; h=cc:cc :content-type:content-type:date:date:from:from:in-reply-to :in-reply-to:message-id:mime-version:references:reply-to:subject :subject:to:to; s=fm3; t=1713848856; x=1713935256; bh=DX+5/JiZZ/ Lhnrx9jIYNZb9TuWi/IQnto78432RzjIU=; b=YTjsk6iVBNrBcbowHyBbEnjOur po2nV7sngZyEKfb0jEtTfkK9BLknbET+/pld9fMRvY/dCKIMgAeCSQJCxcI2U2Qt 7ipVcSuHqNRzbu7f6rRbfwLk2FmKbKWjrcMonW7x82LCCU8FqMj8jEa6vmLZjdYN TdNAb1eWBtuZh7Qvb04ZnjSy9r+O227eP7J1p4x9Rhs6s3NmZp3+UI+uYUWKxs7H ePQi3j5JNlT1AS2+nnlQ3u8aSPG7eVqOFvN0U8DZttiS8N15xx/b0QHAty6xDm8w +188eKt9VUuCekjBhk0r8LXtuRd8rifcZK6dKLaeqLg8yy9Tm+So4fKurBBw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:subject:subject:to :to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm3; t=1713848856; x=1713935256; bh=DX+5/JiZZ/Lhnrx9jIYNZb9TuWi/ IQnto78432RzjIU=; b=dafL+gqPzuhGnxjYpeAh7vyE76jNfbCDZBR9L9sgkOU/ 3K7ATMufUs8BxCvAFvK61YreVBQAtXaxkkQwd5ZSyhRVLMpEdBvX1AA4ptawgrLI Pwoe3ULjReTxhWO6VBqULd5UpJl9+lWcAyE8OPp0RYd75qzXkXNZjQRTl8wTeE58 wS9muV2ByLO4cKCxCWDMVBsYuwGqieZ2BhcbmGOlXAFIMjudiYTHCv1JKYeL49V3 7TGDHZCdN5Fu5DYTiRg61bJVpw46VoDhdvJhWT56aJCjlCrvadtpRCIWEW0fq4Rn +54ecyIhG7xA7XTeJMo5CpYRzhypBfQ4axTHb01oSQ== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvledrudeltddgleegucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepfffhvfevuffkfhggtggujgesghdtreertddtvdenucfhrhhomheprfgrthhr ihgtkhcuufhtvghinhhhrghrughtuceophhssehpkhhsrdhimheqnecuggftrfgrthhtvg hrnhepueektdevtdffveeljeetgfehheeigeekleduvdeffeeghefgledttdehjeelffet necuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomhepphhsse hpkhhsrdhimh X-ME-Proxy: Feedback-ID: i197146af:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Tue, 23 Apr 2024 01:07:35 -0400 (EDT) Received: by localhost (OpenSMTPD) with ESMTPSA id 69d16cfa (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO); Tue, 23 Apr 2024 05:07:27 +0000 (UTC) Date: Tue, 23 Apr 2024 07:07:32 +0200 From: Patrick Steinhardt To: git@vger.kernel.org Cc: Junio C Hamano , "brian m. carlson" , Justin Tobler Subject: [PATCH v2 02/12] path: move `validate_headref()` to its only user Message-ID: References: Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: While `validate_headref()` is only called from `is_git_directory()` in "setup.c", it is currently implemented in "path.c". Move it over such that it becomes clear that it is only really used during setup in order to discover repositories. Signed-off-by: Patrick Steinhardt --- path.c | 53 ----------------------------------------------------- path.h | 1 - setup.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+), 54 deletions(-) diff --git a/path.c b/path.c index cc02165530..bd6e25245d 100644 --- a/path.c +++ b/path.c @@ -5,7 +5,6 @@ #include "abspath.h" #include "environment.h" #include "gettext.h" -#include "hex.h" #include "repository.h" #include "strbuf.h" #include "string-list.h" @@ -647,58 +646,6 @@ void strbuf_git_common_path(struct strbuf *sb, va_end(args); } -int validate_headref(const char *path) -{ - struct stat st; - char buffer[256]; - const char *refname; - struct object_id oid; - int fd; - ssize_t len; - - if (lstat(path, &st) < 0) - return -1; - - /* Make sure it is a "refs/.." symlink */ - if (S_ISLNK(st.st_mode)) { - len = readlink(path, buffer, sizeof(buffer)-1); - if (len >= 5 && !memcmp("refs/", buffer, 5)) - return 0; - return -1; - } - - /* - * Anything else, just open it and try to see if it is a symbolic ref. - */ - fd = open(path, O_RDONLY); - if (fd < 0) - return -1; - len = read_in_full(fd, buffer, sizeof(buffer)-1); - close(fd); - - if (len < 0) - return -1; - buffer[len] = '\0'; - - /* - * Is it a symbolic ref? - */ - if (skip_prefix(buffer, "ref:", &refname)) { - while (isspace(*refname)) - refname++; - if (starts_with(refname, "refs/")) - return 0; - } - - /* - * Is this a detached HEAD? - */ - if (get_oid_hex_any(buffer, &oid) != GIT_HASH_UNKNOWN) - return 0; - - return -1; -} - static struct passwd *getpw_str(const char *username, size_t len) { struct passwd *pw; diff --git a/path.h b/path.h index ea96487b29..c3bc8617bd 100644 --- a/path.h +++ b/path.h @@ -173,7 +173,6 @@ const char *git_path_fetch_head(struct repository *r); const char *git_path_shallow(struct repository *r); int ends_with_path_components(const char *path, const char *components); -int validate_headref(const char *ref); int calc_shared_perm(int mode); int adjust_shared_perm(const char *path); diff --git a/setup.c b/setup.c index f4b32f76e3..7c996659bd 100644 --- a/setup.c +++ b/setup.c @@ -4,6 +4,7 @@ #include "environment.h" #include "exec-cmd.h" #include "gettext.h" +#include "hex.h" #include "object-name.h" #include "refs.h" #include "repository.h" @@ -341,6 +342,58 @@ int get_common_dir_noenv(struct strbuf *sb, const char *gitdir) return ret; } +static int validate_headref(const char *path) +{ + struct stat st; + char buffer[256]; + const char *refname; + struct object_id oid; + int fd; + ssize_t len; + + if (lstat(path, &st) < 0) + return -1; + + /* Make sure it is a "refs/.." symlink */ + if (S_ISLNK(st.st_mode)) { + len = readlink(path, buffer, sizeof(buffer)-1); + if (len >= 5 && !memcmp("refs/", buffer, 5)) + return 0; + return -1; + } + + /* + * Anything else, just open it and try to see if it is a symbolic ref. + */ + fd = open(path, O_RDONLY); + if (fd < 0) + return -1; + len = read_in_full(fd, buffer, sizeof(buffer)-1); + close(fd); + + if (len < 0) + return -1; + buffer[len] = '\0'; + + /* + * Is it a symbolic ref? + */ + if (skip_prefix(buffer, "ref:", &refname)) { + while (isspace(*refname)) + refname++; + if (starts_with(refname, "refs/")) + return 0; + } + + /* + * Is this a detached HEAD? + */ + if (get_oid_hex_any(buffer, &oid) != GIT_HASH_UNKNOWN) + return 0; + + return -1; +} + /* * Test if it looks like we're at a git directory. * We want to see: From patchwork Tue Apr 23 05:07:37 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Steinhardt X-Patchwork-Id: 13639344 Received: from fhigh2-smtp.messagingengine.com (fhigh2-smtp.messagingengine.com [103.168.172.153]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E17C71CA82 for ; Tue, 23 Apr 2024 05:07:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=103.168.172.153 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713848864; cv=none; b=iVIPxs2oA9xm8CnDzq83IEiGrulTnQjVmnfLNXNq6g3tMR5VmjIXWeuYZ8tbhKU9CB9QoQku2g44GrgsD8RBr1hBc7DrV+FloRcz6V5GyNRo17RnhH8NDsGVatmIzCmK0xn5a9ak3gGtALV9HVJHjFfNZBHQkY1Hz7cQDB6e/cE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713848864; c=relaxed/simple; bh=X1mR1swojSrf9rVYpv/Xb58qH6w0FbF6e9l92U8qu9M=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=k0yq1tIVXbYSUzNAxwVVAfLd1ldIb+1kjgiiv73RnIVxK7oZoM5F99wORyhG1jMjZ3ccSrXfxuK7/gUMccx4z1CPS2NDQEDrN257G9orY+z8h3lRAr+v3hTQY9JVIiY5dQ0Oremchc1woouml2U71MABeDH+1kX7OI3ACBEIpEU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pks.im; spf=pass smtp.mailfrom=pks.im; dkim=pass (2048-bit key) header.d=pks.im header.i=@pks.im header.b=TkWN/NGx; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b=ks1DbnOh; arc=none smtp.client-ip=103.168.172.153 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pks.im Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pks.im Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=pks.im header.i=@pks.im header.b="TkWN/NGx"; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="ks1DbnOh" Received: from compute1.internal (compute1.nyi.internal [10.202.2.41]) by mailfhigh.nyi.internal (Postfix) with ESMTP id E336B114017D; Tue, 23 Apr 2024 01:07:41 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute1.internal (MEProxy); Tue, 23 Apr 2024 01:07:41 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pks.im; h=cc:cc :content-type:content-type:date:date:from:from:in-reply-to :in-reply-to:message-id:mime-version:references:reply-to:subject :subject:to:to; s=fm3; t=1713848861; x=1713935261; bh=+goAfq/ig2 Ixx63alixTfrGeGeORuU+Wn9xNxn626AA=; b=TkWN/NGx3I3rP3ncYhTNJT84ID 66iSzdhnsNqyMU4tloDDkbDF+AEkPkNHUa/q1bfBe+93+Uy2YuNoZgd95ItVcapP fGdjdahKXmvc8eHoCZOTbymIO7uDyj6HNhXAUWYX5bJcMlf1lc/ewB1azWd5AoTo pnPJuSmJSoKqd7EJbZx4O4gCX7rWs3lxRCL8guPFgMreFEgEod+YfqCooM4Vz5JF ABGpCZKhAwlbqJdZ5BHiTWq2AG6FdGU64idd4RrxgO5GLnezLcZsndhX9mijyrQU +O0V8LN1sKYSNH1y4Ut+TIrpExbGZZR+VJhWE4zrEcXtK9J4CjDRF5898xNw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:subject:subject:to :to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm3; t=1713848861; x=1713935261; bh=+goAfq/ig2Ixx63alixTfrGeGeOR uU+Wn9xNxn626AA=; b=ks1DbnOhhRPx7k0nYprzJmZgeTRA34/yxinb/SrwATtG +bxEsgqBhFlyGeShlmEN4dP6nyC0XN5lDvMExSa63jhy66kCazhptBfEhetz/MAH rpSl+IIC0s9I1sQSX1DvNeqz4zjgucdu23nLZdxH6tYquVB9XeWh6xWaB0f/XpTf hKwsznY5x86teCCehgmJSbhNk8loQsCjGfd08j0m1dU6QWFwu5LqYTvKQ3mr/qsf YU4Lf6jc5sVxls/sAkFGnB6QEFb894m5A92exVRqrnENs0CWcDgR7STTg+pckx78 dMUzkTiF55pMGzcXhjObZ6miN5T8MpI8FfxWsgm64g== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvledrudeltddgleegucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepfffhvfevuffkfhggtggujgesghdtreertddtvdenucfhrhhomheprfgrthhr ihgtkhcuufhtvghinhhhrghrughtuceophhssehpkhhsrdhimheqnecuggftrfgrthhtvg hrnhepueektdevtdffveeljeetgfehheeigeekleduvdeffeeghefgledttdehjeelffet necuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomhepphhsse hpkhhsrdhimh X-ME-Proxy: Feedback-ID: i197146af:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Tue, 23 Apr 2024 01:07:40 -0400 (EDT) Received: by localhost (OpenSMTPD) with ESMTPSA id 2c2e1e5e (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO); Tue, 23 Apr 2024 05:07:33 +0000 (UTC) Date: Tue, 23 Apr 2024 07:07:37 +0200 From: Patrick Steinhardt To: git@vger.kernel.org Cc: Junio C Hamano , "brian m. carlson" , Justin Tobler Subject: [PATCH v2 03/12] parse-options-cb: only abbreviate hashes when hash algo is known Message-ID: References: Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: The `OPT__ABBREV()` option can be used to add an option that abbreviates object IDs. When given a length longer than `the_hash_algo->hexsz`, then it will instead set the length to that maximum length. It may not always be guaranteed that we have `the_hash_algo` initialized properly as the hash algorithm can only be set up after we have set up `the_repository`. In that case, the hash would always be truncated to the hex length of SHA1, which may not be what the user desires. In practice it's not a problem as all commands that use `OPT__ABBREV()` also have `RUN_SETUP` set and thus cannot work without a repository. Consequently, both `the_repository` and `the_hash_algo` would be properly set up. Regardless of that, harden the code to not truncate the length when we didn't set up a repository. Signed-off-by: Patrick Steinhardt --- parse-options-cb.c | 3 ++- t/t0040-parse-options.sh | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/parse-options-cb.c b/parse-options-cb.c index bdc7fae497..d99d688d3c 100644 --- a/parse-options-cb.c +++ b/parse-options-cb.c @@ -7,6 +7,7 @@ #include "environment.h" #include "gettext.h" #include "object-name.h" +#include "setup.h" #include "string-list.h" #include "strvec.h" #include "oid-array.h" @@ -29,7 +30,7 @@ int parse_opt_abbrev_cb(const struct option *opt, const char *arg, int unset) opt->long_name); if (v && v < MINIMUM_ABBREV) v = MINIMUM_ABBREV; - else if (v > the_hash_algo->hexsz) + else if (startup_info->have_repository && v > the_hash_algo->hexsz) v = the_hash_algo->hexsz; } *(int *)(opt->value) = v; diff --git a/t/t0040-parse-options.sh b/t/t0040-parse-options.sh index 8bb2a8b453..45a773642f 100755 --- a/t/t0040-parse-options.sh +++ b/t/t0040-parse-options.sh @@ -176,6 +176,23 @@ test_expect_success 'long options' ' test_cmp expect output ' +test_expect_success 'abbreviate to something longer than SHA1 length' ' + cat >expect <<-EOF && + boolean: 0 + integer: 0 + magnitude: 0 + timestamp: 0 + string: (not set) + abbrev: 100 + verbose: -1 + quiet: 0 + dry run: no + file: (not set) + EOF + test-tool parse-options --abbrev=100 >output && + test_cmp expect output +' + test_expect_success 'missing required value' ' cat >expect <<-\EOF && error: switch `s'\'' requires a value From patchwork Tue Apr 23 05:07:42 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Steinhardt X-Patchwork-Id: 13639345 Received: from fhigh2-smtp.messagingengine.com (fhigh2-smtp.messagingengine.com [103.168.172.153]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E30D020323 for ; Tue, 23 Apr 2024 05:07:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=103.168.172.153 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713848868; cv=none; b=WzfDb3AwCy8kdRR4Tq/tWS4fh/7TN2YqN5EkRENLvoLYeC+04t6KKmT7aDwbEOPAdSDlG0YDcVmeByzsh3mUhp9ZxVrvla0YhJ+nJzKcs2HXTpZJHw0rT80/rynFH80vMF8p0Sgj186stgTQXY9bOTs5E11wQ7masy9koirHgT8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713848868; c=relaxed/simple; bh=bgDgmJ5p9yF3x4jbsTXocZQAwRTUOP7VWYuBXFnBE1Y=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=Cj2uyUZabVTsE8wQf7m5tSqPYXouJqpTrPdw1Zlrc5gGOv9yIujcx2HlTFg2NtGcPIibXHStZAiTWsdgyrWs6KcFiiDRwxdhz/6koAfowjLg+CS1eNZXGuQc5f88zOcNpVXnkUP714HRBIvHBm3Ev/WT4TY7u8y8KVGR4NExd3A= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pks.im; spf=pass smtp.mailfrom=pks.im; dkim=pass (2048-bit key) header.d=pks.im header.i=@pks.im header.b=mucgMfhs; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b=T6UD96kW; arc=none smtp.client-ip=103.168.172.153 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pks.im Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pks.im Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=pks.im header.i=@pks.im header.b="mucgMfhs"; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="T6UD96kW" Received: from compute5.internal (compute5.nyi.internal [10.202.2.45]) by mailfhigh.nyi.internal (Postfix) with ESMTP id 42EAB1140163; Tue, 23 Apr 2024 01:07:46 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute5.internal (MEProxy); Tue, 23 Apr 2024 01:07:46 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pks.im; h=cc:cc :content-type:content-type:date:date:from:from:in-reply-to :in-reply-to:message-id:mime-version:references:reply-to:subject :subject:to:to; s=fm3; t=1713848866; x=1713935266; bh=AegkeGNK2M 5e7Zq8fP0jrG9qLNF+ku7wE5Cyik17kOQ=; b=mucgMfhs4oZ9brmymewl0Mp5ib En5XlrDfU2hjDmbNSd6zF28zycuPiqcepuW6G8FjDGEkkqV38D1ApAWjY9Ikvhmd LoqDlnAWystrQ9h3hIMMlQzQSSWHTo6xnC0dqysu1/UpkfHFivxUA2OVrIA0O/0H WHlr4iymH5KpbWwmr8e14H1YfUe754EuTSJ0Z+6IRbdF7lLhfkgwgYDYCNN4ObxY AIL98kqoIyz1AnL9MJZkHVs1H02j5i52HdZvjMa2hdojl0q1Vkwb6zusRwh1sZLC WE+bbBOVIAgQTHtvXbW9hYjD9pLykwQIR2yulCF4h8CAYOj/yiziwPE0mGfA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:subject:subject:to :to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm3; t=1713848866; x=1713935266; bh=AegkeGNK2M5e7Zq8fP0jrG9qLNF+ ku7wE5Cyik17kOQ=; b=T6UD96kWhpmFRuT5vRhI8svSGdjMSevpq3c09enSC3fa Qhow7KNXPrO+FRnjIbAWemkj+yb+HHcZEuQH1KCwR9o/okiHoDgbdrR5fyQszWkF E9dWjcoIJEoMv6u/w9IrCQlPqLtvzFj1nFcratFB5isECfLmgGuAfl3aHv/OMJeK IdotHMrSz20d7u0v2054qpSyYt5ic7zd110mDf6/PH/Ymf5MfiM5l0YaNC4wbuus 4n2KOjOEgtQHZ4FBme6/C7AQBOE+QiNecXNGc9o/ijDHDXeqnNjjqaGxCmqICGuN cMTOitxDgdA+Rupb8dCg8Kq0mK3AFB65W2WX46jsjw== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvledrudeltddgleegucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepfffhvfevuffkfhggtggujgesghdtreertddtvdenucfhrhhomheprfgrthhr ihgtkhcuufhtvghinhhhrghrughtuceophhssehpkhhsrdhimheqnecuggftrfgrthhtvg hrnhepueektdevtdffveeljeetgfehheeigeekleduvdeffeeghefgledttdehjeelffet necuvehluhhsthgvrhfuihiivgepudenucfrrghrrghmpehmrghilhhfrhhomhepphhsse hpkhhsrdhimh X-ME-Proxy: Feedback-ID: i197146af:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Tue, 23 Apr 2024 01:07:45 -0400 (EDT) Received: by localhost (OpenSMTPD) with ESMTPSA id 4b6e6074 (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO); Tue, 23 Apr 2024 05:07:37 +0000 (UTC) Date: Tue, 23 Apr 2024 07:07:42 +0200 From: Patrick Steinhardt To: git@vger.kernel.org Cc: Junio C Hamano , "brian m. carlson" , Justin Tobler Subject: [PATCH v2 04/12] attr: don't recompute default attribute source Message-ID: <1b5f904eedfd1bd3bc5d79cd43baf5e7a83b96aa.1713848619.git.ps@pks.im> References: Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: The `default_attr_source()` function lazily computes the attr source supposedly once, only. This is done via a static variable `attr_source` that contains the resolved object ID of the attr source's tree. If the variable is the null object ID then we try to look up the attr source, otherwise we skip over it. This approach is flawed though: the variable will never be set to anything else but the null object ID in case there is no attr source. Consequently, we re-compute the information on every call. And in the worst case, when we silently ignore bad trees, this will cause us to try and look up the treeish every single time. Improve this by introducing a separate variable `has_attr_source` to track whether we already computed the attr source and, if so, whether we have an attr source or not. This also allows us to convert the `ignore_bad_attr_tree` to not be static anymore as the code will only be executed once anyway. Signed-off-by: Patrick Steinhardt --- attr.c | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/attr.c b/attr.c index 679e42258c..9d911aeb31 100644 --- a/attr.c +++ b/attr.c @@ -1206,15 +1206,16 @@ static void collect_some_attrs(struct index_state *istate, } static const char *default_attr_source_tree_object_name; -static int ignore_bad_attr_tree; void set_git_attr_source(const char *tree_object_name) { default_attr_source_tree_object_name = xstrdup(tree_object_name); } -static void compute_default_attr_source(struct object_id *attr_source) +static int compute_default_attr_source(struct object_id *attr_source) { + int ignore_bad_attr_tree = 0; + if (!default_attr_source_tree_object_name) default_attr_source_tree_object_name = getenv(GIT_ATTR_SOURCE_ENVIRONMENT); @@ -1230,22 +1231,28 @@ static void compute_default_attr_source(struct object_id *attr_source) ignore_bad_attr_tree = 1; } - if (!default_attr_source_tree_object_name || !is_null_oid(attr_source)) - return; + if (!default_attr_source_tree_object_name) + return 0; if (repo_get_oid_treeish(the_repository, default_attr_source_tree_object_name, - attr_source) && !ignore_bad_attr_tree) - die(_("bad --attr-source or GIT_ATTR_SOURCE")); + attr_source)) { + if (!ignore_bad_attr_tree) + die(_("bad --attr-source or GIT_ATTR_SOURCE")); + return 0; + } + + return 1; } static struct object_id *default_attr_source(void) { static struct object_id attr_source; + static int has_attr_source = -1; - if (is_null_oid(&attr_source)) - compute_default_attr_source(&attr_source); - if (is_null_oid(&attr_source)) + if (has_attr_source < 0) + has_attr_source = compute_default_attr_source(&attr_source); + if (!has_attr_source) return NULL; return &attr_source; } From patchwork Tue Apr 23 05:07:47 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Steinhardt X-Patchwork-Id: 13639346 Received: from fout5-smtp.messagingengine.com (fout5-smtp.messagingengine.com [103.168.172.148]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 76D18200C7 for ; Tue, 23 Apr 2024 05:07:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=103.168.172.148 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713848874; cv=none; b=SF2y7b+iT3W+4Y8iv951vSbn7ouREIedtCMcHh0OJrrb1vx2tUoZQbtZWwPo5z9ew9jTsj42zFVQldIc5BXo1v8dVoxruAkQuDaLSBvjpAEI87y9LY1/F04tp01QUCNJJKAUquQBOds9XWWlKS4HFcHwS5071XzbIbk7HVw+jzk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713848874; c=relaxed/simple; bh=BQ8ca6VzY8GiMlHg9ntpCgTmC8hg/Y692xya8P+o7pE=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=OEi21KTQh+Eb7/yWlUDRluuCqEE4oixF1XCJKIlJOs0t00ghYShok9kRNyjY8O/vz55ynlxQwDMae+wc0SutrSYuyiEx3VVvLPUl/a330QYl00a+3wBcwdggkeG4hp9ibYYZsemwgUV48JDk6ijWTdpCbmMQLpDV1k3nqI0pqTQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pks.im; spf=pass smtp.mailfrom=pks.im; dkim=pass (2048-bit key) header.d=pks.im header.i=@pks.im header.b=L8f6vWBt; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b=NPwujozO; arc=none smtp.client-ip=103.168.172.148 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pks.im Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pks.im Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=pks.im header.i=@pks.im header.b="L8f6vWBt"; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="NPwujozO" Received: from compute6.internal (compute6.nyi.internal [10.202.2.47]) by mailfout.nyi.internal (Postfix) with ESMTP id A4B15138024A; Tue, 23 Apr 2024 01:07:51 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute6.internal (MEProxy); Tue, 23 Apr 2024 01:07:51 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pks.im; h=cc:cc :content-type:content-type:date:date:from:from:in-reply-to :in-reply-to:message-id:mime-version:references:reply-to:subject :subject:to:to; s=fm3; t=1713848871; x=1713935271; bh=balkgB/F5Q Gpx7Jlqa00ksChOu/OiszgfGZ/LosQtVw=; b=L8f6vWBt8MQKqVYMSJDcsJ/ldL 1ctuYEWfBaL4kO0j8Q7LF163EGIyheR2aOtxf4QNcaJwSZEc0oSMWnqa4BlhW2s3 AX0piho0zpJ1NQd57f6m1pXzhWJF8AKcZ7GesOAm04Y2uuSDuEO4TErQ5xtEEtjn NunkpMzssmyMk8jpVRiIW0El8GsYXLlxKbppAfCdOC8I+PjzABTjj/V0IphXlbh7 eyFE1dwbOQvThHfRZhXRMybCc2i7OZSc56wF1yHi9XCkz20a289w4uQE1go7fG7o u0KV8YmExvXCm46TQty47tzaghDmZf6aP5BILmxiTqYbLCf2QMKUxYLxBRwg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:subject:subject:to :to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm3; t=1713848871; x=1713935271; bh=balkgB/F5QGpx7Jlqa00ksChOu/O iszgfGZ/LosQtVw=; b=NPwujozO0QtDNVQG+6Nf952noaM6ZkxUsv0uq1Uez2fH dEEqetkQbIUu+BJk6rHvT3GSb07xwlZ3iDe4cPrVJdVM5X4LJhK0ymI6cUB62wNK lf2YqnfKZQRcMWct3EIHRGJ5s0pZhkFQUoOuhvFk2EWuY6rbUowGPkr+jy4yizYQ 8AOlML/3FmgCt7OEOamEfVc8XRzn0JdfufFZuzL1fINcGF64Tq+dzNph1q5T83va FNa2PHcFl+xcTyQp1jriNGIZcGLOvn2FmXiEigkZDVNGtxm651TODO3zie/twjsY gjcWA8U76zVe2sux59ZIyQcGQLy24dOIFnoKneiT/Q== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvledrudeltddgleegucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepfffhvfevuffkfhggtggujgesghdtreertddtvdenucfhrhhomheprfgrthhr ihgtkhcuufhtvghinhhhrghrughtuceophhssehpkhhsrdhimheqnecuggftrfgrthhtvg hrnhepueektdevtdffveeljeetgfehheeigeekleduvdeffeeghefgledttdehjeelffet necuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomhepphhsse hpkhhsrdhimh X-ME-Proxy: Feedback-ID: i197146af:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Tue, 23 Apr 2024 01:07:50 -0400 (EDT) Received: by localhost (OpenSMTPD) with ESMTPSA id fbae916f (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO); Tue, 23 Apr 2024 05:07:42 +0000 (UTC) Date: Tue, 23 Apr 2024 07:07:47 +0200 From: Patrick Steinhardt To: git@vger.kernel.org Cc: Junio C Hamano , "brian m. carlson" , Justin Tobler Subject: [PATCH v2 05/12] attr: fix BUG() when parsing attrs outside of repo Message-ID: <26909daca4ad416b1f3d0738751facf2d66d1aba.1713848619.git.ps@pks.im> References: Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: If either the `--attr-source` option or the `GIT_ATTR_SOURCE` envvar are set, then `compute_default_attr_source()` will try to look up the value as a treeish. It is possible to hit that function while outside of a Git repository though, for example when using `git grep --no-index`. In that case, Git will hit a bug because we try to look up the main ref store outside of a repository. Handle the case gracefully and detect when we try to look up an attr source without a repository. Signed-off-by: Patrick Steinhardt --- attr.c | 6 ++++++ t/t0003-attributes.sh | 15 +++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/attr.c b/attr.c index 9d911aeb31..4bd09bcb4b 100644 --- a/attr.c +++ b/attr.c @@ -1234,6 +1234,12 @@ static int compute_default_attr_source(struct object_id *attr_source) if (!default_attr_source_tree_object_name) return 0; + if (!startup_info->have_repository) { + if (!ignore_bad_attr_tree) + die(_("cannot use --attr-source or GIT_ATTR_SOURCE without repo")); + return 0; + } + if (repo_get_oid_treeish(the_repository, default_attr_source_tree_object_name, attr_source)) { diff --git a/t/t0003-attributes.sh b/t/t0003-attributes.sh index 774b52c298..3efdec54dd 100755 --- a/t/t0003-attributes.sh +++ b/t/t0003-attributes.sh @@ -428,6 +428,21 @@ test_expect_success 'precedence of --attr-source, GIT_ATTR_SOURCE, then attr.tre ) ' +test_expect_success 'diff without repository with attr source' ' + mkdir -p "$TRASH_DIRECTORY/outside/nongit" && + ( + cd "$TRASH_DIRECTORY/outside/nongit" && + GIT_CEILING_DIRECTORIES="$TRASH_DIRECTORY/outside" && + export GIT_CEILING_DIRECTORIES && + touch file && + cat >expect <<-EOF && + fatal: cannot use --attr-source or GIT_ATTR_SOURCE without repo + EOF + test_must_fail env GIT_ATTR_SOURCE=HEAD git grep --no-index foo file 2>err && + test_cmp expect err + ) +' + test_expect_success 'bare repository: with --source' ' ( cd bare.git && From patchwork Tue Apr 23 05:07:52 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Steinhardt X-Patchwork-Id: 13639347 Received: from fout5-smtp.messagingengine.com (fout5-smtp.messagingengine.com [103.168.172.148]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DAF5C20DC3 for ; Tue, 23 Apr 2024 05:07:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=103.168.172.148 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713848878; cv=none; b=cnJwaMhmAWWak91UV52ATk22w4vJTo8qQEa7PnEIoqsh5ulZMJRq95aVdcRvdWUZchVi+6G7DfusujgbqHtOgHv47rhv5zylDkRX0m5+dJnqSIU6C5aGGYxgcibXgGlHWeDvhgWR3LROeJXU1wABkvnVb2/1HCAqN1W35UPKGoE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713848878; c=relaxed/simple; bh=qHOxt3x+if9WEx2n/yXN6t2ZDi/B1Q7v5jhBpeC4xII=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=RwqC1uQJc+BoKoNKTK/cDOiinxWY5bjsGfNuzomGr6v8R4UZdSZwuofZLZ/5+SDlu4wLz5fO0L9xB5mPQYBQOmXbrE7Kr8t+xyVuUGDZwwcckuiWSP5pjTLOvoJXxVbalYkpLt6MlRmMDoblPuC3Oxv9tWTysf46l3tTJnHJpAo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pks.im; spf=pass smtp.mailfrom=pks.im; dkim=pass (2048-bit key) header.d=pks.im header.i=@pks.im header.b=ubHXAM7k; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b=G9SXxgtg; arc=none smtp.client-ip=103.168.172.148 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pks.im Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pks.im Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=pks.im header.i=@pks.im header.b="ubHXAM7k"; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="G9SXxgtg" Received: from compute7.internal (compute7.nyi.internal [10.202.2.48]) by mailfout.nyi.internal (Postfix) with ESMTP id 12D65138024A; Tue, 23 Apr 2024 01:07:56 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute7.internal (MEProxy); Tue, 23 Apr 2024 01:07:56 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pks.im; h=cc:cc :content-type:content-type:date:date:from:from:in-reply-to :in-reply-to:message-id:mime-version:references:reply-to:subject :subject:to:to; s=fm3; t=1713848876; x=1713935276; bh=tb4QRUprRU DD7/A8Fskd9jH9xMQwdTtV/kLK/FihCg4=; b=ubHXAM7kAUMLz0PvVHD5L1JM+q 3YcxwieZKeIMECL+GNs+oAKSn6XDneWJPy9lBNOVfqKsMDRrUsYkHhqqCjFdkcQn Wepr5mnL7XsdJ35qGnh2vv4A8SSFytfUyrkp77tIWiYypOW087VqOHNUOeKBsBWf RMOmJ3+PXqFw0iGG0tfIT89CEPJ0KYuE78PM18ETloI3Z812YKGkTnyFau4dKhEO v0rHQBBuqcDHE1CP26ob+I0o/jxJPORO1vN5S0uFqVTgO80LZJSSvIn2rigL49AM isNqT1mNBX2X0HxLaNSRtCuN+E+GjcfjnETuELdhrxUHOao+hlbpNuiTSjww== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:subject:subject:to :to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm3; t=1713848876; x=1713935276; bh=tb4QRUprRUDD7/A8Fskd9jH9xMQw dTtV/kLK/FihCg4=; b=G9SXxgtgZY/KkJIeXIK/+hKokKbdFDVbjYLMWYZWLw7K nRLgbyxjQonUv1g1u7uBwN86hNtBOrmMeNjMLzsA63RYJ6eTUqZ10CtqrT+54WuX ANiKNHEOlUCPgvl/YzETiMhcrDv/w3l8PBj514LY+4vokVamFK7hqXK5lfStwnZs x7vwmCDTlMMyupRlrt2W2WZ7MclRThtdlYyiYdkNdSb+l1eSTrGyOhb9A6IS6D0r T4LOCLNY3I6zjypW5/fMu6Qx0XaaoviPMZrhhCdawnk2ATA1HBUTC2nCeiIWYg4N O70kHi9UZMRwLErm4JEUL2P6w2WD+si5T2x6ehEXvQ== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvledrudeltddgleegucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepfffhvfevuffkfhggtggujgesghdtreertddtvdenucfhrhhomheprfgrthhr ihgtkhcuufhtvghinhhhrghrughtuceophhssehpkhhsrdhimheqnecuggftrfgrthhtvg hrnhepjedvjedukedtvddvleejjeefudefgeettdegffdtleekgeehvdfffeeitedviedv necuffhomhgrihhnpehhthhtphdqfhgvthgthhdqughumhgsrdhshhdphhhtthhpthhrrg hnshhpohhrthdrrghsnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghi lhhfrhhomhepphhssehpkhhsrdhimh X-ME-Proxy: Feedback-ID: i197146af:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Tue, 23 Apr 2024 01:07:54 -0400 (EDT) Received: by localhost (OpenSMTPD) with ESMTPSA id a09528bb (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO); Tue, 23 Apr 2024 05:07:47 +0000 (UTC) Date: Tue, 23 Apr 2024 07:07:52 +0200 From: Patrick Steinhardt To: git@vger.kernel.org Cc: Junio C Hamano , "brian m. carlson" , Justin Tobler Subject: [PATCH v2 06/12] remote-curl: fix parsing of detached SHA256 heads Message-ID: <0b99184f504bdab56b6d2d49f609b74a3e5c9c40.1713848619.git.ps@pks.im> References: Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: The dumb HTTP transport tries to read the remote HEAD reference by downloading the "HEAD" file and then parsing it via `http_fetch_ref()`. This function will either parse the file as an object ID in case it is exactly `the_hash_algo->hexsz` long, or otherwise it will check whether the reference starts with "ref :" and parse it as a symbolic ref. This is broken when parsing detached HEADs of a remote SHA256 repository because we never update `the_hash_algo` to the discovered remote object hash. Consequently, `the_hash_algo` will always be the fallback SHA1 hash algorithm, which will cause us to fail parsing HEAD altogteher when it contains a SHA256 object ID. Fix this issue by setting up `the_hash_algo` via `repo_set_hash_algo()`. While at it, let's make the expected SHA1 fallback explicit in our code, which also addresses an upcoming issue where we are going to remove the SHA1 fallback for `the_hash_algo`. Signed-off-by: Patrick Steinhardt --- remote-curl.c | 19 ++++++++++++++++++- t/t5550-http-fetch-dumb.sh | 15 +++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/remote-curl.c b/remote-curl.c index 0b6d7815fd..004b707fdf 100644 --- a/remote-curl.c +++ b/remote-curl.c @@ -266,12 +266,23 @@ static struct ref *parse_git_refs(struct discovery *heads, int for_push) return list; } +/* + * Try to detect the hash algorithm used by the remote repository when using + * the dumb HTTP transport. As dumb transports cannot tell us the object hash + * directly have to derive it from the advertised ref lengths. + */ static const struct git_hash_algo *detect_hash_algo(struct discovery *heads) { const char *p = memchr(heads->buf, '\t', heads->len); int algo; + + /* + * In case the remote has no refs we have no way to reliably determine + * the object hash used by that repository. In that case we simply fall + * back to SHA1, which may or may not be correct. + */ if (!p) - return the_hash_algo; + return &hash_algos[GIT_HASH_SHA1]; algo = hash_algo_by_length((p - heads->buf) / 2); if (algo == GIT_HASH_UNKNOWN) @@ -295,6 +306,12 @@ static struct ref *parse_info_refs(struct discovery *heads) "is this a git repository?", transport_anonymize_url(url.buf)); + /* + * Set the repository's hash algo to whatever we have just detected. + * This ensures that we can correctly parse the remote references. + */ + repo_set_hash_algo(the_repository, hash_algo_by_ptr(options.hash_algo)); + data = heads->buf; start = NULL; mid = data; diff --git a/t/t5550-http-fetch-dumb.sh b/t/t5550-http-fetch-dumb.sh index 4c3b32785d..5f16cbc58d 100755 --- a/t/t5550-http-fetch-dumb.sh +++ b/t/t5550-http-fetch-dumb.sh @@ -55,6 +55,21 @@ test_expect_success 'list refs from outside any repository' ' test_cmp expect actual ' + +test_expect_success 'list detached HEAD from outside any repository' ' + git clone --mirror "$HTTPD_DOCUMENT_ROOT_PATH/repo.git" \ + "$HTTPD_DOCUMENT_ROOT_PATH/repo-detached.git" && + git -C "$HTTPD_DOCUMENT_ROOT_PATH/repo-detached.git" \ + update-ref --no-deref HEAD refs/heads/main && + git -C "$HTTPD_DOCUMENT_ROOT_PATH/repo-detached.git" update-server-info && + cat >expect <<-EOF && + $(git rev-parse main) HEAD + $(git rev-parse main) refs/heads/main + EOF + nongit git ls-remote "$HTTPD_URL/dumb/repo-detached.git" >actual && + test_cmp expect actual +' + test_expect_success 'create password-protected repository' ' mkdir -p "$HTTPD_DOCUMENT_ROOT_PATH/auth/dumb/" && cp -Rf "$HTTPD_DOCUMENT_ROOT_PATH/repo.git" \ From patchwork Tue Apr 23 05:07:57 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Steinhardt X-Patchwork-Id: 13639348 Received: from fout5-smtp.messagingengine.com (fout5-smtp.messagingengine.com [103.168.172.148]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2EE27208A8 for ; Tue, 23 Apr 2024 05:08:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=103.168.172.148 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713848883; cv=none; b=qBEyE3At/1psj6fKWMZHlkp5OM/vtYajPT+1aR4xph5wHbzq740I2dMJKqoPMfnlCiLnkgcUso6hae3/9COM2NVRZlrDC+//FZU2iLaOtbsrSKIMUHf4vP9AMuk/2Uqs8c7hYr5zRTBqQlBD7tHgl+9v6U4tH9S/Vjzd4bWFZQM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713848883; c=relaxed/simple; bh=5lGQGYybA9/RznX/ZXkSG5k0aks2Qnv7d1hrYIOHQvE=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=muOqB5qDR76SzRDLWudP83AqY1UO4i3Q//5Gl0vZeFBsD6WxB916Y6eQwGCDuDJmMz5wR+9bLgkSLOMBhKIGZSTYgG4NFXDtWmzMasjweQaJb9QEZ0XHOpGqQIDoG7O/OqwgMET5d+yKNioqnsozQiq09ijGYV39vSLfH4VknPk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pks.im; spf=pass smtp.mailfrom=pks.im; dkim=pass (2048-bit key) header.d=pks.im header.i=@pks.im header.b=mlToV19b; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b=Nq2UYXsD; arc=none smtp.client-ip=103.168.172.148 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pks.im Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pks.im Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=pks.im header.i=@pks.im header.b="mlToV19b"; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="Nq2UYXsD" Received: from compute7.internal (compute7.nyi.internal [10.202.2.48]) by mailfout.nyi.internal (Postfix) with ESMTP id 710E21380254; Tue, 23 Apr 2024 01:08:01 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute7.internal (MEProxy); Tue, 23 Apr 2024 01:08:01 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pks.im; h=cc:cc :content-type:content-type:date:date:from:from:in-reply-to :in-reply-to:message-id:mime-version:references:reply-to:subject :subject:to:to; s=fm3; t=1713848881; x=1713935281; bh=xgP4stn3/D 5OeFhn35AhHN/LOpQVSH3O+jgUKM8DEOs=; b=mlToV19bcDHRceQ+yuvOx3/vM0 yL6ORUYxynKJ+WYIcTOpb4fE6AnQQ1iKwuBPY5dju4SmuNZ91tGvuxmPeX7HyqQN BDs4LsS6VGMLOs7uBErGzdUyWXOZkAn/vhzTzZOB25jIFH764whRrT0De4ne2/+l EZxPwFvrRID+3id8GsDxdHuSjX5oJzltPf5pYr8qRkEbCMSlWlRiNHEFvHUajl4R 7AESz9JB4l2RE0pLuxlOW79uhxSwbLB16fn/czPVYVIe9K5pFZRnUKf8BpyXfshZ o1N4RQUxdOW30WEH4/5G71TpL41kwuo6lzBTM96LXer6eO95CW6IyzCvU07w== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:subject:subject:to :to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm3; t=1713848881; x=1713935281; bh=xgP4stn3/D5OeFhn35AhHN/LOpQV SH3O+jgUKM8DEOs=; b=Nq2UYXsDGNzc4yVziIhl7Z3Wg1XxlFiAH7yRlrh+HRqF 5IQD77G3/qOdV+Pu0S0+yVDGvc4G/JlQQXYUoguc/j+YnwOmcDwmufsdjA/Q/av0 +/ZszJMHDuneFt4GzAgfextW3eQzvH7H/7YMgkOdCWbEDUSdFQyvV83vFl3nqk8W /2pytTVW4/HzQ1SRe7A8+Rrmtu7480wI5ZCc5omQ3F5l/dW96PzwKoZYl2Kerm4z PikMu0HlFU42B270SzGqutYNQgor4ZhqAfZfTGBRfQqDoRjAEmukOFYxIlj9Gkyh FYmwEzCVMIk7LYShwPxjgLkHVO96srfMrVlds12Djg== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvledrudeltddgleegucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepfffhvfevuffkfhggtggujgesghdtreertddtvdenucfhrhhomheprfgrthhr ihgtkhcuufhtvghinhhhrghrughtuceophhssehpkhhsrdhimheqnecuggftrfgrthhtvg hrnhepueektdevtdffveeljeetgfehheeigeekleduvdeffeeghefgledttdehjeelffet necuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomhepphhsse hpkhhsrdhimh X-ME-Proxy: Feedback-ID: i197146af:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Tue, 23 Apr 2024 01:08:00 -0400 (EDT) Received: by localhost (OpenSMTPD) with ESMTPSA id 4c2dd633 (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO); Tue, 23 Apr 2024 05:07:52 +0000 (UTC) Date: Tue, 23 Apr 2024 07:07:57 +0200 From: Patrick Steinhardt To: git@vger.kernel.org Cc: Junio C Hamano , "brian m. carlson" , Justin Tobler Subject: [PATCH v2 07/12] builtin/rev-parse: allow shortening to more than 40 hex characters Message-ID: References: Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: The `--short=` option for git-rev-parse(1) allows the user to specify to how many characters object IDs should be shortened to. The option is broken though for SHA256 repositories because we set the maximum allowed hash size to `the_hash_algo->hexsz` before we have even set up the repo. Consequently, `the_hash_algo` will always be SHA1 and thus we truncate every hash after at most 40 characters. Fix this by accessing `the_hash_algo` only after we have set up the repo. Signed-off-by: Patrick Steinhardt --- builtin/rev-parse.c | 5 ++--- t/t1500-rev-parse.sh | 6 ++++++ 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/builtin/rev-parse.c b/builtin/rev-parse.c index 624182e507..d7b87c605e 100644 --- a/builtin/rev-parse.c +++ b/builtin/rev-parse.c @@ -687,7 +687,6 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix) const char *name = NULL; struct object_context unused; struct strbuf buf = STRBUF_INIT; - const int hexsz = the_hash_algo->hexsz; int seen_end_of_options = 0; enum format_type format = FORMAT_DEFAULT; @@ -863,8 +862,8 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix) abbrev = strtoul(arg, NULL, 10); if (abbrev < MINIMUM_ABBREV) abbrev = MINIMUM_ABBREV; - else if (hexsz <= abbrev) - abbrev = hexsz; + else if ((int)the_hash_algo->hexsz <= abbrev) + abbrev = the_hash_algo->hexsz; continue; } if (!strcmp(arg, "--sq")) { diff --git a/t/t1500-rev-parse.sh b/t/t1500-rev-parse.sh index a669e592f1..30c31918fd 100755 --- a/t/t1500-rev-parse.sh +++ b/t/t1500-rev-parse.sh @@ -304,4 +304,10 @@ test_expect_success 'rev-parse --bisect includes bad, excludes good' ' test_cmp expect actual ' +test_expect_success '--short= truncates to the actual hash length' ' + git rev-parse HEAD >expect && + git rev-parse --short=100 HEAD >actual && + test_cmp expect actual +' + test_done From patchwork Tue Apr 23 05:08:02 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Steinhardt X-Patchwork-Id: 13639349 Received: from fout5-smtp.messagingengine.com (fout5-smtp.messagingengine.com [103.168.172.148]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7A042208A8 for ; Tue, 23 Apr 2024 05:08:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=103.168.172.148 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713848887; cv=none; b=GI/zPtemiqTbJ/m/JlEZpnC9XtGJHk0Kd9Q677RkTVGreS7sxFWwZg1jKG3QDWyTIvCfi+f7jFUZbjVo/6O4bBT2H1CszlIhgLCI1nEvxVwAFGYLTJ/lTi3TbyXwZu4CXVol4TBi1Hrp5gCzQhL4rjSdchO05wngTpahEi56v34= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713848887; c=relaxed/simple; bh=vaUlDc8inW0vv9csyVcCwIm727xMjw+JngAqbfAQD5I=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=MWYhDw2Xl0RnKpQ+Ok/G6ZMNtk+C/cZkmt22kYhR8SFGWJ0ZQBFpQHOVoKKmaSoFdE5xyFbfHxk9Q+WMjxBRvVEGFdBeP3TJYEH1Rly1kBySEC2p25+v9srp80HcU5+sOXDRv3CndPRoe/AHfVDAOQxSk7CEIHFir3N0MtySO6s= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pks.im; spf=pass smtp.mailfrom=pks.im; dkim=pass (2048-bit key) header.d=pks.im header.i=@pks.im header.b=G3jboM6z; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b=Bhw8/3aw; arc=none smtp.client-ip=103.168.172.148 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pks.im Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pks.im Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=pks.im header.i=@pks.im header.b="G3jboM6z"; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="Bhw8/3aw" Received: from compute5.internal (compute5.nyi.internal [10.202.2.45]) by mailfout.nyi.internal (Postfix) with ESMTP id CCE3D1380252; Tue, 23 Apr 2024 01:08:05 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute5.internal (MEProxy); Tue, 23 Apr 2024 01:08:05 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pks.im; h=cc:cc :content-type:content-type:date:date:from:from:in-reply-to :in-reply-to:message-id:mime-version:references:reply-to:subject :subject:to:to; s=fm3; t=1713848885; x=1713935285; bh=aenadhVcZp +djtsKnBa7hgEUVlc3KuN4woe7JVIEckw=; b=G3jboM6z4YNNRa5hbI7lI0NIcH vIoRrm4+jf+YZ0C6tbz6uVSvp3Zh2C0mwtAiTrVH++tpnH82s/y00dxbojcfKGpU UeHThhR0qe1bTcrwrLEGejC4QJyIHAB4X1zFu3jQYJIFGs96oiFQeYkbklKH5miG Z3JgQI3002/mPTzQQ+DX1nQ+hS3jPLL93CIJf7+nwGnuPNZXe/raRPA9QZlFla+C xZn/j408hEBM2xy9NRTfPgyHXxftOMYYnpIgqxxWadTzaSmYM1WlyEnTzaD1e0py JvPn/Gpt7Zr6PSb7QLj+GByjI9uVF1R3cnd4S7ewbsVsn5XCKDSCB2WKXrCw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:subject:subject:to :to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm3; t=1713848885; x=1713935285; bh=aenadhVcZp+djtsKnBa7hgEUVlc3 KuN4woe7JVIEckw=; b=Bhw8/3awu+7Fy9ozap6RtcvfX1lIKGn1wp8cZ/M2vgoB 5jW9MbEMuqovf3ScJYNZi2v3ZYHZ9A3AJmN/kkU+0GgMrnNOepEE4AlB7Imte0vX x8/W3Hbca/WeF2YWt8+47IlYP73qRwH7CZABsGJgWBLTYZXqd7CbBB5GTjPTFZvy QUE/8NJ9qaE12ipEgCZaTtuEU0APiGs2PyKk1z5gTBU81pg7FNJd2ZB4AB593o6W gyVGu6UJYFc+XJyUu0R/CIgVPw404aTAjBxEaTTds6ZcZcbIW6Tr0v4PjUmE6xGC Z31o9XNhioV48UvA0z6XmA5lWgZ/q0I+1LYxvZve4Q== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvledrudeltddgleegucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepfffhvfevuffkfhggtggujgesghdtreertddtvdenucfhrhhomheprfgrthhr ihgtkhcuufhtvghinhhhrghrughtuceophhssehpkhhsrdhimheqnecuggftrfgrthhtvg hrnhepueektdevtdffveeljeetgfehheeigeekleduvdeffeeghefgledttdehjeelffet necuvehluhhsthgvrhfuihiivgepvdenucfrrghrrghmpehmrghilhhfrhhomhepphhsse hpkhhsrdhimh X-ME-Proxy: Feedback-ID: i197146af:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Tue, 23 Apr 2024 01:08:04 -0400 (EDT) Received: by localhost (OpenSMTPD) with ESMTPSA id 1b24aeea (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO); Tue, 23 Apr 2024 05:07:57 +0000 (UTC) Date: Tue, 23 Apr 2024 07:08:02 +0200 From: Patrick Steinhardt To: git@vger.kernel.org Cc: Junio C Hamano , "brian m. carlson" , Justin Tobler Subject: [PATCH v2 08/12] builtin/blame: don't access potentially unitialized `the_hash_algo` Message-ID: <1813e7eb5c2a9e136f828a74c53843be2f13f90c.1713848619.git.ps@pks.im> References: Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: We access `the_hash_algo` in git-blame(1) before we have executed `parse_options_start()`, which may not be properly set up in case we have no repository. This is fine for most of the part because all the call paths that lead to it (git-blame(1), git-annotate(1) as well as git-pick-axe(1)) specify `RUN_SETUP` and thus require a repository. There is one exception though, namely when passing `-h` to print the help. Here we will access `the_hash_algo` even if there is no repo. This works fine right now because `the_hash_algo` gets sets up to point to the SHA1 algorithm via `initialize_repository()`. But we're about to stop doing this, and thus the code would lead to a `NULL` pointer exception. Prepare the code for this and only access `the_hash_algo` after we are sure that there is a proper repository. Signed-off-by: Patrick Steinhardt --- builtin/blame.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/builtin/blame.c b/builtin/blame.c index 9aa74680a3..e325825936 100644 --- a/builtin/blame.c +++ b/builtin/blame.c @@ -915,7 +915,6 @@ int cmd_blame(int argc, const char **argv, const char *prefix) struct range_set ranges; unsigned int range_i; long anchor; - const int hexsz = the_hash_algo->hexsz; long num_lines = 0; const char *str_usage = cmd_is_annotate ? annotate_usage : blame_usage; const char **opt_usage = cmd_is_annotate ? annotate_opt_usage : blame_opt_usage; @@ -973,11 +972,11 @@ int cmd_blame(int argc, const char **argv, const char *prefix) } else if (show_progress < 0) show_progress = isatty(2); - if (0 < abbrev && abbrev < hexsz) + if (0 < abbrev && abbrev < (int)the_hash_algo->hexsz) /* one more abbrev length is needed for the boundary commit */ abbrev++; else if (!abbrev) - abbrev = hexsz; + abbrev = the_hash_algo->hexsz; if (revs_file && read_ancestry(revs_file)) die_errno("reading graft file '%s' failed", revs_file); From patchwork Tue Apr 23 05:08:07 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Steinhardt X-Patchwork-Id: 13639350 Received: from fout5-smtp.messagingengine.com (fout5-smtp.messagingengine.com [103.168.172.148]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 36FFB208A0 for ; Tue, 23 Apr 2024 05:08:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=103.168.172.148 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713848893; cv=none; b=IrqjxpPJMYfuyAw11PKNIptMN9q1UIMUfdzGaUoi4EhSrv1muoCzeAt3py5206TI2fIjF42mmS/Z5eJdRQ7z0gsD1H/BFJfchmgwOB6CRqF6P1rjmW3yrapphE3RCiTt8QKILPVyauK+mDHTTK8wJXGf2aSkWByJ0M0SnoX5uQU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713848893; c=relaxed/simple; bh=UKUB9B/V9JsZYKzwN0BFyVIxebH+1vn1kxQ8Lv/A0wg=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=ofmal7W8olZepgOd2QJwSf1OAP6WXItgkbYDMTVzp5HNdp7F1QNfK1jo5ehU+mfqH8sCYcMQO4kbJCSRpUgk2GSQbmlVTNirQGTAJqV3TT+ilsNhsszVBVaDjWX8wmy4H+JML58JzrZh8W7FfW66i+IyUlHpLFNpeROnzdmEMP8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pks.im; spf=pass smtp.mailfrom=pks.im; dkim=pass (2048-bit key) header.d=pks.im header.i=@pks.im header.b=azj+s9P9; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b=D1DVqzbm; arc=none smtp.client-ip=103.168.172.148 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pks.im Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pks.im Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=pks.im header.i=@pks.im header.b="azj+s9P9"; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="D1DVqzbm" Received: from compute1.internal (compute1.nyi.internal [10.202.2.41]) by mailfout.nyi.internal (Postfix) with ESMTP id 46CB21380252; Tue, 23 Apr 2024 01:08:11 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute1.internal (MEProxy); Tue, 23 Apr 2024 01:08:11 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pks.im; h=cc:cc :content-type:content-type:date:date:from:from:in-reply-to :in-reply-to:message-id:mime-version:references:reply-to:subject :subject:to:to; s=fm3; t=1713848891; x=1713935291; bh=HhU6cn4JKp YNWYgHZ3V5HAK3yx6kK/Fspr9w9tB7KaI=; b=azj+s9P9StK2XQKjDolkWErCrU 3cLSVoHAZ6G2wIVI5KirJptosKmPKGiWsBDyng5HmqCMGWC2zhRnnnGH3GuONxVl 1DQtwZ8x5RaC02wuMVV+nDTo1BveMqbp1WQI+xhUwVejHuErni0voWaxGewtnhNU 1N49k7BuAazyPBJXMkKeaIwMEuMEOR/uti2LfCn18xHX7NTZ9KfSkaIO23SPTH3q V7xpUyCvv+BIcsBQ9Gb9s2m6CstK3sk2o3IWjn4B6OtuXrgpTq43gRCkVbOcFOPV agQ10J9OrjxXil1yJlkRDwxJNqZ9Q3doBgB48oENCKvvWDNsHUTCLS7vsHTw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:subject:subject:to :to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm3; t=1713848891; x=1713935291; bh=HhU6cn4JKpYNWYgHZ3V5HAK3yx6k K/Fspr9w9tB7KaI=; b=D1DVqzbmJ7ySOh5jBeN1SjKe4FRSSqejdQhx+MfVQ8O9 vo3I+hO4eNhI13lnh9oDoq/qrqn4UPmy/GZdMfwcZKnyKKoWPW90c4olkpFfJnkG fOLnPbZXIuCQI2t2fZNf5VriPWA1Ud5YnYWO3ZaRrgjog/2srD8VsQ0BioDCNJWS M6qI97a+oYeufwGBdjN+aUTSAGvOUBpiJ8l0A7gA0zeU5Zr89DmVwDvquvPsJ5vs VM/difUXQPuXGQyl1/bjIQxvShNO+r5gL7Kbi/LhOK6FbLe6YvF+9U3eIsVSx1sc mTys9neGZEMmQyJyBFlUP0t4MeEqzGGbf0Fz5soTgQ== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvledrudeltddgleegucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepfffhvfevuffkfhggtggujgesghdtreertddtvdenucfhrhhomheprfgrthhr ihgtkhcuufhtvghinhhhrghrughtuceophhssehpkhhsrdhimheqnecuggftrfgrthhtvg hrnhepueektdevtdffveeljeetgfehheeigeekleduvdeffeeghefgledttdehjeelffet necuvehluhhsthgvrhfuihiivgepudenucfrrghrrghmpehmrghilhhfrhhomhepphhsse hpkhhsrdhimh X-ME-Proxy: Feedback-ID: i197146af:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Tue, 23 Apr 2024 01:08:09 -0400 (EDT) Received: by localhost (OpenSMTPD) with ESMTPSA id 0bf4843e (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO); Tue, 23 Apr 2024 05:08:02 +0000 (UTC) Date: Tue, 23 Apr 2024 07:08:07 +0200 From: Patrick Steinhardt To: git@vger.kernel.org Cc: Junio C Hamano , "brian m. carlson" , Justin Tobler Subject: [PATCH v2 09/12] builtin/bundle: abort "verify" early when there is no repository Message-ID: <31182a1fc6a0123dd94fbfc3dce2bc49d7ff6086.1713848619.git.ps@pks.im> References: Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: Verifying a bundle requires us to have a repository. This is encoded in `verify_bundle()`, which will return an error if there is no repository. We call `open_bundle()` before we call `verify_bundle()` though, which already performs some verifications even though we may ultimately abort due to a missing repository. This is problematic because `open_bundle()` already reads the bundle header and verifies that it contains a properly formatted hash. When there is no repository we have no clue what hash function to expect though, so we always end up assuming SHA1 here, which may or may not be correct. Furthermore, we are about to stop initializing `the_hash_algo` when there is no repository, which will lead to segfaults. Check early on whether we have a repository to fix this issue. Signed-off-by: Patrick Steinhardt --- builtin/bundle.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/builtin/bundle.c b/builtin/bundle.c index 3ad11dc5d0..d5d41a8f67 100644 --- a/builtin/bundle.c +++ b/builtin/bundle.c @@ -140,6 +140,11 @@ static int cmd_bundle_verify(int argc, const char **argv, const char *prefix) { builtin_bundle_verify_usage, options, &bundle_file); /* bundle internals use argv[1] as further parameters */ + if (!startup_info->have_repository) { + ret = error(_("need a repository to verify a bundle")); + goto cleanup; + } + if ((bundle_fd = open_bundle(bundle_file, &header, &name)) < 0) { ret = 1; goto cleanup; From patchwork Tue Apr 23 05:08:11 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Steinhardt X-Patchwork-Id: 13639351 Received: from fhigh2-smtp.messagingengine.com (fhigh2-smtp.messagingengine.com [103.168.172.153]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8989B208A0 for ; Tue, 23 Apr 2024 05:08:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=103.168.172.153 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713848897; cv=none; b=Gkkt9UaHM0eYKtlXoQ32sglM32ryXN/Ll0cq54INCf4Fw5BDv7j7CJUK+pYqrX5AvqcCC+0YTR8uDQOMLxWQPSmGXZxgJrcfBqDpMvHfEkqAgi5ctpxX6Ixn6+mP89qhr9C86B7odEsGXTdUxRWuciOavFto17tdt/dbbUI1g+I= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713848897; c=relaxed/simple; bh=e4J8zUysxNyUCW68YU1kTKiHcbNYIfz2tshcxJqJq/M=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=oXpwt2JJv5rVX8OzudvQntzg4WxKyrE0fiEtl0BgB2pn/0DskaopjIeRLh/dANOaTnSxgjNAAXzHKFeArU1WJ/5EXNu4oAzhEKlB2txeTQxk5v2cHTz/OIuCO5mOzYrvXx+Bi9mJvYLA3NMMjYQKO7NAFJNDWA++yCVgxIR7D3c= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pks.im; spf=pass smtp.mailfrom=pks.im; dkim=pass (2048-bit key) header.d=pks.im header.i=@pks.im header.b=lTexjO3H; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b=WSTEJhPX; arc=none smtp.client-ip=103.168.172.153 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pks.im Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pks.im Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=pks.im header.i=@pks.im header.b="lTexjO3H"; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="WSTEJhPX" Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailfhigh.nyi.internal (Postfix) with ESMTP id A134A114017C; Tue, 23 Apr 2024 01:08:15 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute4.internal (MEProxy); Tue, 23 Apr 2024 01:08:15 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pks.im; h=cc:cc :content-type:content-type:date:date:from:from:in-reply-to :in-reply-to:message-id:mime-version:references:reply-to:subject :subject:to:to; s=fm3; t=1713848895; x=1713935295; bh=jRRx9mCtVN 0flKOebi2Q5/wwTwYAcXmngOCpuNLbOV4=; b=lTexjO3HAwex7Ig9jRmNOcqCZc bU6C7lt4qN5jiXr/zR8KSSBBkJ1v9K7QauH0R0+sGPmm2/tjBxVM5m8PI9U5qNfj YK6WVcDurh5pujIq568/yT7tJDnd+5cJE/GN+kvPzb0USKROAWwUWPBR7R59LhZA ztvXzG9AoNKvUebOUSERrgGkhggAIrQEIu0i5XMkMM8+0MIkAex5liSIEeKEpzzT CKoloRoUkFXi/dzcvPK8lOnjm2QAmoAAumHDBKbcmTLQCk0c7+JQcJoWdUF4CBpP jfCMJL50rExP48fcqKeFstqs3WvjDglwf6LzBzZWTmz0wyar5FJVX/5bUimQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:subject:subject:to :to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm3; t=1713848895; x=1713935295; bh=jRRx9mCtVN0flKOebi2Q5/wwTwYA cXmngOCpuNLbOV4=; b=WSTEJhPXGL4g3IEsl4QX7Us9es2YKbhsCFqCtScKSPQ5 cBy4HMbNhUCRCCpwZartu/avfZNVAd/xaoDI0xKObE3/2oIZGhsRPF/m2IWuECMw VSieN+lHQeJ2lLrC9ZT6bO3OHAL4V6iRFKqGJFU2hIhC3j5vpZNlT5JNIwnddMwH 2uQYdFfLZcCTpvXMQT0ZCCebpNqy7Ht/bToB8m6nF4afTfs09kwxRY5wmcDHcAFm CYmFUnxS6n4jEy1bO38AHsV9BizN52mriOGTgEMPOaREX4oI7u12yqj9CqnfoFMj xujcNfmbbiJRIwBNJasm1uubidsLD5pp8S/broSTkw== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvledrudeltddgleegucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepfffhvfevuffkfhggtggujgesghdtreertddtvdenucfhrhhomheprfgrthhr ihgtkhcuufhtvghinhhhrghrughtuceophhssehpkhhsrdhimheqnecuggftrfgrthhtvg hrnhepueektdevtdffveeljeetgfehheeigeekleduvdeffeeghefgledttdehjeelffet necuvehluhhsthgvrhfuihiivgepudenucfrrghrrghmpehmrghilhhfrhhomhepphhsse hpkhhsrdhimh X-ME-Proxy: Feedback-ID: i197146af:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Tue, 23 Apr 2024 01:08:14 -0400 (EDT) Received: by localhost (OpenSMTPD) with ESMTPSA id cc316a7e (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO); Tue, 23 Apr 2024 05:08:07 +0000 (UTC) Date: Tue, 23 Apr 2024 07:08:11 +0200 From: Patrick Steinhardt To: git@vger.kernel.org Cc: Junio C Hamano , "brian m. carlson" , Justin Tobler Subject: [PATCH v2 10/12] builtin/diff: explicitly set hash algo when there is no repo Message-ID: <78e19d0a1b09ed36bfd73fe128d2f78addcd1134.1713848619.git.ps@pks.im> References: Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: The git-diff(1) command can be used outside repositories to diff two files with each other. But even if there is no repository we will end up hashing the files that we are diffing so that we can print the "index" line: ``` diff --git a/a b/b index 7898192..6178079 100644 --- a/a +++ b/b @@ -1 +1 @@ -a +b ``` We implicitly use SHA1 to calculate the hash here, which is because `the_repository` gets initialized with SHA1 during the startup routine. We are about to stop doing this though such that `the_repository` only ever has a hash function when it was properly initialized via a repo's configuration. To give full control to our users, we would ideally add a new switch to git-diff(1) that allows them to specify the hash function when executed outside of a repository. But for now, we only convert the code to make this explicit such that we can stop setting the default hash algorithm for `the_repository`. Signed-off-by: Patrick Steinhardt --- builtin/diff.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/builtin/diff.c b/builtin/diff.c index 6e196e0c7d..58ec7e5da2 100644 --- a/builtin/diff.c +++ b/builtin/diff.c @@ -465,6 +465,15 @@ int cmd_diff(int argc, const char **argv, const char *prefix) no_index = DIFF_NO_INDEX_IMPLICIT; } + /* + * When operating outside of a Git repository we need to have a hash + * algorithm at hand so that we can generate the blob hashes. We + * default to SHA1 here, but may eventually want to change this to be + * configurable via a command line option. + */ + if (nongit) + repo_set_hash_algo(the_repository, GIT_HASH_SHA1); + init_diff_ui_defaults(); git_config(git_diff_ui_config, NULL); prefix = precompose_argv_prefix(argc, argv, prefix); From patchwork Tue Apr 23 05:08:16 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Steinhardt X-Patchwork-Id: 13639352 Received: from fhigh2-smtp.messagingengine.com (fhigh2-smtp.messagingengine.com [103.168.172.153]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DE001200C7 for ; Tue, 23 Apr 2024 05:08:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=103.168.172.153 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713848902; cv=none; b=CT42n5TCEEr7Ko4jysmYtOOXhCgYM7fjMheKp2lC4KsyrA+mU105bK0uZgVEjrTx0XUT486t6YR96Sik7fdCO0mavUWv4tp6aiJcOQTbUMxn3RaZ8gm5BC7NI8nMSdoHVeQgSLUlsVmF7iMUkRkpF7kvqsNlbJuquDtkdgYW0VU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713848902; c=relaxed/simple; bh=F10c1hZSNcdvG2NM7sfuQxD29spwvx5z6HbxagdqOSM=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=EL7HoLEXkAv1BIGmNOGCQdi3+gPAMhQULoNAyWQlSAprnfY7pXnu7O16GqMpWNOD3eFWtr34tbwL9uSIZ20GWMN1kF1zHfv5Dv8cW19a8GNpLZfLvpfYOWrj/CWZkCmh7kabH/DKorai4JMNuqIRFwA+3M4Do0/wu2dyt9nH1ac= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pks.im; spf=pass smtp.mailfrom=pks.im; dkim=pass (2048-bit key) header.d=pks.im header.i=@pks.im header.b=rwN7LOaX; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b=LLTVx9aM; arc=none smtp.client-ip=103.168.172.153 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pks.im Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pks.im Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=pks.im header.i=@pks.im header.b="rwN7LOaX"; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="LLTVx9aM" Received: from compute6.internal (compute6.nyi.internal [10.202.2.47]) by mailfhigh.nyi.internal (Postfix) with ESMTP id 0E62B114017D; Tue, 23 Apr 2024 01:08:20 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute6.internal (MEProxy); Tue, 23 Apr 2024 01:08:20 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pks.im; h=cc:cc :content-type:content-type:date:date:from:from:in-reply-to :in-reply-to:message-id:mime-version:references:reply-to:subject :subject:to:to; s=fm3; t=1713848900; x=1713935300; bh=ZOyGw/y0N1 7fnEeFwIGyUar8sVv57Tqsl8yY7lgEt8c=; b=rwN7LOaXgz9Vw4quJDHrrMdbSL jBxIljMGLb80rW7ghvmqW3SiQJKNN165bZDinGOD4CWpsu56Dz3oMdnD1B4JB+o6 3t+/cGDAuvFS4LX3gbdTbV0BYr7dg8ykztFJUSc8/SHSsji/1dSLRDgQiyK539oT J0K98iIgf6CxzGJ5mByPysMyDRqvLwPJMtK9KTXEVhDn2J36bhR7OHqDqUT8T8Nx 6TJOmVIRMPISKeoaPM75tw5m6zzoNKLjtAPGVouKG/ZuqxPcfSyGFOc2PSJ+csEX waEMhzHMmgOqK102KC1d36jrWsVRIc8U9IqLMkFRXWD8cnIU+0M17dc+60ow== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:subject:subject:to :to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm3; t=1713848900; x=1713935300; bh=ZOyGw/y0N17fnEeFwIGyUar8sVv5 7Tqsl8yY7lgEt8c=; b=LLTVx9aMMvEmYa6cBjN6kYbcWWkMdu3kCg4ZrqrqrwWG rvD2ITIMsA4iNzEoTCYlyBy+MqVv2kWi1TeHouJTy7z7RrERla8gi5ldQ3FM7u13 70jgcVUQohqhcOXGi97LCxJc5whHmxy4i7f6TwPGBUFnDPROleozacMTsg78AL0n 4H6PIGgSp/kqvmbyurHC9oOa//Jru0mCp1D95GxD0gKYjSCJvV1Q3hDfY4ysVmaF 9p2oQx51tSrpSMZeVTmt4tXLQtKC3QH1ElQjBpUKMdUA6h/8u2ZZqBkd5hbZtVTZ BoIh0jTIl84wTEp1tg5smfd0oyHES1p400yqvRycjw== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvledrudeltddgleegucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepfffhvfevuffkfhggtggujgesghdtreertddtvdenucfhrhhomheprfgrthhr ihgtkhcuufhtvghinhhhrghrughtuceophhssehpkhhsrdhimheqnecuggftrfgrthhtvg hrnhepueektdevtdffveeljeetgfehheeigeekleduvdeffeeghefgledttdehjeelffet necuvehluhhsthgvrhfuihiivgepudenucfrrghrrghmpehmrghilhhfrhhomhepphhsse hpkhhsrdhimh X-ME-Proxy: Feedback-ID: i197146af:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Tue, 23 Apr 2024 01:08:18 -0400 (EDT) Received: by localhost (OpenSMTPD) with ESMTPSA id 86b9790f (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO); Tue, 23 Apr 2024 05:08:12 +0000 (UTC) Date: Tue, 23 Apr 2024 07:08:16 +0200 From: Patrick Steinhardt To: git@vger.kernel.org Cc: Junio C Hamano , "brian m. carlson" , Justin Tobler Subject: [PATCH v2 11/12] builtin/shortlog: don't set up revisions without repo Message-ID: <51bcddbc3154addac208ba15866ff70becf90480.1713848619.git.ps@pks.im> References: Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: It is possible to run git-shortlog(1) outside of a repository by passing it output from git-log(1) via standard input. Obviously, as there is no repository in that context, it is thus unsupported to pass any revisions as arguments. Regardless of that we still end up calling `setup_revisions()`. While that works alright, it is somewhat strange. Furthermore, this is about to cause problems when we unset the default object hash. Refactor the code to only call `setup_revisions()` when we have a repository. This is safe to do as we already verify that there are no arguments when running outside of a repository anyway. Signed-off-by: Patrick Steinhardt --- builtin/shortlog.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/builtin/shortlog.c b/builtin/shortlog.c index 3c7cd2d6ef..d4daf31e22 100644 --- a/builtin/shortlog.c +++ b/builtin/shortlog.c @@ -435,7 +435,7 @@ int cmd_shortlog(int argc, const char **argv, const char *prefix) usage_with_options(shortlog_usage, options); } - if (setup_revisions(argc, argv, &rev, NULL) != 1) { + if (!nongit && setup_revisions(argc, argv, &rev, NULL) != 1) { error(_("unrecognized argument: %s"), argv[1]); usage_with_options(shortlog_usage, options); } From patchwork Tue Apr 23 05:08:21 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Steinhardt X-Patchwork-Id: 13639353 Received: from fhigh2-smtp.messagingengine.com (fhigh2-smtp.messagingengine.com [103.168.172.153]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7A496200CB for ; Tue, 23 Apr 2024 05:08:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=103.168.172.153 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713848908; cv=none; b=TzJAhEp5Y++0YtwMExBU7A1YG+1PQI4y/0FT4qrcEM9WVKuvg8xJEyY75c0IdjKdpX7cW1RaH4NLBrVRD7GRsuqjFIHAKmGP9TaAwW1lBQx1Dxwev7cKNdxNAoAp7oDKJ9orXLDb4kKYG0upuvS2VMyBSckDuWcoL+awXQGqg+M= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713848908; c=relaxed/simple; bh=LkFvzQ31gFEPKkDyg/BDaAYd014sozEQG4EVzp1sttU=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=nXuLipHsavP3LbC0uceBzADlDHJbM9mbhe3DgoYHZsA00q/bDrQAlVbyFnhWwPpnFfDbNQ3IfNsEv+GJRHDA4nv0wZGX4gVrtYrG/epmo4d6/GxEaLup+YWjeVSggTuVNMLSyKMLS7MXs78I/G+fTAOp4GClu2l/j0aHFEejwqY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pks.im; spf=pass smtp.mailfrom=pks.im; dkim=pass (2048-bit key) header.d=pks.im header.i=@pks.im header.b=twGUd5+D; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b=KtI8wYsN; arc=none smtp.client-ip=103.168.172.153 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pks.im Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pks.im Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=pks.im header.i=@pks.im header.b="twGUd5+D"; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="KtI8wYsN" Received: from compute6.internal (compute6.nyi.internal [10.202.2.47]) by mailfhigh.nyi.internal (Postfix) with ESMTP id 74106114017D; Tue, 23 Apr 2024 01:08:25 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute6.internal (MEProxy); Tue, 23 Apr 2024 01:08:25 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pks.im; h=cc:cc :content-type:content-type:date:date:from:from:in-reply-to :in-reply-to:message-id:mime-version:references:reply-to:subject :subject:to:to; s=fm3; t=1713848905; x=1713935305; bh=qeA6lTHc3u pUQjo5QJeJ2VFY+gC/Vpk/hsdAwIYEDko=; b=twGUd5+DkNSictRRZlxQK3K/bb B4N+zG9PM9rnVElxguPoL3gj3qeauIZTeZ9e8aVEcQ9IslAwzn/1Y/6cZCcR8jhe 7aB+iSodViB6yWeU0Q1I078BMnsO4lHaWqk0nQK6b1IsSZreI1uWlUEccG+n5szX aZTNDi8bbkB7M5kaVO1VGNllMwIHmueHjXgi3TvPKlKD8X1OAO4RKwrvcx4tP9OK tNN4B2NKnYZpsEgZDrV+wtvqviYS4VrzlD+yF5HpO+OZCoxJQdN0ak822vSLWN/X VqVw9d5je4UbG3wzz8U4uuu7Eg/VD4McuKCNBRjWjd2BmRlZCYFIs4vjMocA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:subject:subject:to :to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm3; t=1713848905; x=1713935305; bh=qeA6lTHc3upUQjo5QJeJ2VFY+gC/ Vpk/hsdAwIYEDko=; b=KtI8wYsNawjqVkOJZQjlARf3Its7PFY6bjVtntKB94Av NcwSItuk/6xVZkojdAsn2qvBEvDojxNp/e658bQBpwGZaiuy4G0s4GKKlyYcq8oP gncu9MtvuCQMM/vrNe7L1TzdoRIW5eFtnP5pR04ElkhmIVcLnFEuymdXCA7xcC44 7mHgCTWUZx9G2aMD8EfNZVauFuGg7v2v0CyHPCxfAql3vMRnEiV+YonuHtJsQnTH 65XGAzyGMWYGuVXW1XAPhMwEQhSCCkDy5PnTJu4Uz5G00whY7VL/fpcahgrkoNiP KbGxXU2ajti035KFEwPd8IT2Y5cas+w12X5tG31zVA== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvledrudeltddgleegucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepfffhvfevuffkfhggtggujgesghdtreertddtvdenucfhrhhomheprfgrthhr ihgtkhcuufhtvghinhhhrghrughtuceophhssehpkhhsrdhimheqnecuggftrfgrthhtvg hrnhepueektdevtdffveeljeetgfehheeigeekleduvdeffeeghefgledttdehjeelffet necuvehluhhsthgvrhfuihiivgepvdenucfrrghrrghmpehmrghilhhfrhhomhepphhsse hpkhhsrdhimh X-ME-Proxy: Feedback-ID: i197146af:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Tue, 23 Apr 2024 01:08:24 -0400 (EDT) Received: by localhost (OpenSMTPD) with ESMTPSA id 61d4392e (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO); Tue, 23 Apr 2024 05:08:17 +0000 (UTC) Date: Tue, 23 Apr 2024 07:08:21 +0200 From: Patrick Steinhardt To: git@vger.kernel.org Cc: Junio C Hamano , "brian m. carlson" , Justin Tobler Subject: [PATCH v2 12/12] repository: stop setting SHA1 as the default object hash Message-ID: References: Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: During the startup of Git, we call `initialize_the_repository()` to set up `the_repository` as well as `the_index`. Part of this setup is also to set the default object hash of the repository to SHA1. This has the effect that `the_hash_algo` is getting initialized to SHA1, as well. This default hash algorithm eventually gets overridden by most Git commands via `setup_git_directory()`, which also detects the actual hash algorithm used by the repository. There are some commands though that don't access a repository at all, or at a later point only, and thus retain the default hash function for some amount of time. As some of the the preceding commits demonstrate, this can lead to subtle issues when we access `the_hash_algo` when no repository has been set up. Address this issue by dropping the set up of the default hash algorithm completely. The effect of this is that `the_hash_algo` will map to a `NULL` pointer and thus cause Git to crash when something tries to access the hash algorithm without it being properly initialized. It thus forces all Git commands to explicitly set up the hash algorithm in case there is no repository. Signed-off-by: Patrick Steinhardt --- repository.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/repository.c b/repository.c index e15b416944..b65b1a8c8b 100644 --- a/repository.c +++ b/repository.c @@ -35,8 +35,6 @@ void initialize_the_repository(void) the_repo.parsed_objects = parsed_object_pool_new(); index_state_init(&the_index, the_repository); - - repo_set_hash_algo(&the_repo, GIT_HASH_SHA1); } static void expand_base_dir(char **out, const char *in,