From patchwork Fri Apr 19 09:51:34 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Steinhardt X-Patchwork-Id: 13636073 Received: from wfout5-smtp.messagingengine.com (wfout5-smtp.messagingengine.com [64.147.123.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 F17D576402 for ; Fri, 19 Apr 2024 09:51:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=64.147.123.148 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713520300; cv=none; b=kLwaLHRs97Mq0yhNCCkjzWKeh31b/8OVB3GGNuCs8Ihih8NYoMaZV+0n9DCYtghv3zGeQ+litNRI/lx1se7FLjLvEQx+sgqkWGQVPN7/jBBTdpX5qG2874gjFuE5CfU8jZO0+vFPrvAuZYHwpdQul8VrUQWz4zHzaGAAsbkY2PE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713520300; c=relaxed/simple; bh=PONfmiGeRHTRM7v9AmD3ETWgTxwok7Ks6ZiHcgFPHA0=; h=Date:From:To:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=c2/Qdb3eNkH2l408nEWfVdRGKkE0HKgr648q4IuLKFwMO+gKH8tWZRYGUgLoGD30xGIueBafUGH9754qmPtOcoHS6S6CaBmw8Icb9dIp++wNLl9hm073M+SQ0NGvn/BnlL47m3wFgZ2gsUZElTIfRvxfgx7DPCA89HPbnSGk0Vc= 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=fKtxkdKM; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b=Q4ntT72m; arc=none smtp.client-ip=64.147.123.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="fKtxkdKM"; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="Q4ntT72m" Received: from compute7.internal (compute7.nyi.internal [10.202.2.48]) by mailfout.west.internal (Postfix) with ESMTP id 6B4511C000CE for ; Fri, 19 Apr 2024 05:51:38 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute7.internal (MEProxy); Fri, 19 Apr 2024 05:51:38 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pks.im; h=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=1713520297; x=1713606697; bh=5FVypehmGd HDkCBluyaMY64AfAPbSoZNM/Wr9qpEtvY=; b=fKtxkdKMYhn63fIfEte1ca1M3I Ix5nzWVOBRO0W3PqfFu2TR+qqkCir7FfrFmpg6K3Ye7ej2NquvA58ATVBtzUiyo8 4htxhNOA6oKiSzeTYbeKdgD8nqppg5UKnY6uzHKmYyDNSHkssWMwri4Z3gLKalhW 2B+g6tz/OktqTvh2WjLTBk+rYEG129LVi71YGKK0GuaJBccPybuMyj3v1ftjPtaa IBcSgp2PbsmySF92vcqZ5d5rhrxshvdMq8MzTUQiWks+neuQRSdztz73vA7YoS4Q gYn4EQ+RVRl2pKNqQpd2W8uJK4EFKcDu+vnI5t5Tk55FI+HtXQ/7YZ99w9MA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=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=1713520297; x=1713606697; bh=5FVypehmGdHDkCBluyaMY64AfAPb SoZNM/Wr9qpEtvY=; b=Q4ntT72mVwErpxsXspDSd3EALq17cks0aAb60av/qKoe WGyZpx3Q+17JnIvk3/itJlKBIynSipVSvLmoGILEWajF+L+SLQD53+sN6gjHBVEt 7jYYG+XAOpWNO58Yau82oAq1tSWlT6Sf+DmK7sQtfOFooFVl8UrNyUpAgI9sBFwK 5//5L5OYenbVueJayMrY0l4AUHWsnJJjixOYYCd9l7+fbWTeiOrq3AWtc08FUFxy k3w88/3xoDqx1vWq555Q0BU+U76ZKtXP22fH8RcPs68Mzg8EKwSE7gaHmlKGznlu pyz1usWhWq9Mhxx4lvSTf5yFnKMw9oLRWwkAdsKLZg== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvledrudekvddgvdduucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucenucfjughrpeffhffvuffkfhggtggujgesghdtre ertddtvdenucfhrhhomheprfgrthhrihgtkhcuufhtvghinhhhrghrughtuceophhssehp khhsrdhimheqnecuggftrfgrthhtvghrnhepheeghfdtfeeuffehkefgffduleffjedthf dvjeektdfhhedvlefgtefgvdettdfhnecuvehluhhsthgvrhfuihiivgepudenucfrrghr rghmpehmrghilhhfrhhomhepphhssehpkhhsrdhimh X-ME-Proxy: Feedback-ID: i197146af:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA for ; Fri, 19 Apr 2024 05:51:37 -0400 (EDT) Received: by localhost (OpenSMTPD) with ESMTPSA id 8733714a (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO) for ; Fri, 19 Apr 2024 09:51:09 +0000 (UTC) Date: Fri, 19 Apr 2024 11:51:34 +0200 From: Patrick Steinhardt To: git@vger.kernel.org Subject: [PATCH 06/11] builtin/rev-parse: allow shortening to more than 40 hex characters Message-ID: <9cb7baa50c6df77adfc2017a16883b3cb0c587e2.1713519789.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 `--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