From patchwork Thu Apr 18 12:14:33 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Steinhardt X-Patchwork-Id: 13634607 Received: from fout7-smtp.messagingengine.com (fout7-smtp.messagingengine.com [103.168.172.150]) (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 B85CB15F407 for ; Thu, 18 Apr 2024 12:14:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=103.168.172.150 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713442480; cv=none; b=BOerh/FpWq4hmtP3vd5IEGZ8INrDmjElB/9gLcwn+RxfmBDL0YbesEQGNIdho7P9Nl62VMFQ7Lmg+4lLTnA7N6S58/WCisZctFsnTTt4CeDvu3hCgKmgGYqY6MlE+CtdQEsjOvBAN7cJAFAPtyKu7LEr837ykPD/tRBlKIxxPOM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713442480; c=relaxed/simple; bh=K4RGzecjXDJDpUDoxHbkG2A1Y1Il3mO2CcWX4jEh46U=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=I0MoUqD02Dy1bfz7wAuDr6ICVWuy62SuXFmYAHNqIl/N0J+LvqbqTdzfyOSdwjXQAsblBga5mNBTYLiHvK+VSA6mOriFLtYSXAmr+A5WYqfjQaUm1jBDtOrHQBRSupnJNu/tFcpqGdS5CV1oo5VbaaGGngAUFQ76gc8N3cfcu9g= 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=lpbTyI6z; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b=HTx7Z4qP; arc=none smtp.client-ip=103.168.172.150 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="lpbTyI6z"; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="HTx7Z4qP" Received: from compute7.internal (compute7.nyi.internal [10.202.2.48]) by mailfout.nyi.internal (Postfix) with ESMTP id D9B87138023F; Thu, 18 Apr 2024 08:14:37 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute7.internal (MEProxy); Thu, 18 Apr 2024 08:14:37 -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=1713442477; x=1713528877; bh=PorwP+WNqP ccDtxN72Wzj69yLMQ8Q/cs5BvwLPJzZ1M=; b=lpbTyI6zvDsSQPOOwJRqM5uv48 aCtBPyzbKZNngc3y5aJ6JF9uniWhBPWDkfrftud71yNiSq9R/YSr/Hh40Jng+ml5 3v3yyXsmbO9sudEWnMl9AKvEG8m518tVc26AT96O6clDvOm4hM0cH8SHbFz+LSG3 O4ZW2l5qinXlhu+pzqC1/BPZa0nrKyqfeAx+RMQNWtDjTyry2B2rnKd5gLAbI6kL Fcy55W424c8nz5SYWowtMdnwigXCDYWeu3tUUE11RvF3ASIsVsUklhns+imsSTmY fEvcDRLFAQ3rPCvFTOcXsOpRiED9EmHH4TRYftqTl0Bh7RMqBPu33GmfbGKg== 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=1713442477; x=1713528877; bh=PorwP+WNqPccDtxN72Wzj69yLMQ8 Q/cs5BvwLPJzZ1M=; b=HTx7Z4qPrBzuf6sxaIMss5JTLuv9pz6aLzo4dZkZJcm7 GIX8IZMWOq7lewkiblioaIX4oD4oFrIiihQSW5KiVPs/L5wZ7IyGk24GYOSMjIFD M0sV0ttowLH1Z7iRC2gmzfqLFL/ombc0v7BcsEB71GYdyG3AeWTnM4EEQkmt8Kr3 Jo4toDZcPYtuOwNe5pYfueLnFo/wiCeclMsbGC6qTaW28r5iX4ynHfhKyspwaJ2s cSvCATCDyZuW4rfxKGwM6w+j59rM/cOuSO0kSOdd22ADBE+qet7sRw1XFjVq8n6u 5T4Bj+W2oVIeyxSXMkxDLnSHRyqx8dGKEP49K8alhA== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvledrudektddghedtucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepfffhvfevuffkfhggtggujgesghdtreertddtvdenucfhrhhomheprfgrthhr ihgtkhcuufhtvghinhhhrghrughtuceophhssehpkhhsrdhimheqnecuggftrfgrthhtvg hrnhepueektdevtdffveeljeetgfehheeigeekleduvdeffeeghefgledttdehjeelffet necuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomhepphhsse hpkhhsrdhimh X-ME-Proxy: Feedback-ID: i197146af:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Thu, 18 Apr 2024 08:14:36 -0400 (EDT) Received: by localhost (OpenSMTPD) with ESMTPSA id eab053f2 (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO); Thu, 18 Apr 2024 12:14:11 +0000 (UTC) Date: Thu, 18 Apr 2024 14:14:33 +0200 From: Patrick Steinhardt To: git@vger.kernel.org Cc: Karthik Nayak , Phillip Wood , Junio C Hamano Subject: [PATCH v2 6/6] repository: drop `initialize_the_repository()` 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: Now that we have dropped `the_index`, `initialize_the_repository()` doesn't really do a lot anymore except for setting up the pointer for `the_repository` and then calling `initialize_repository()`. The former can be replaced by statically initializing the pointer though, which basically makes this function moot. Convert callers to instead call `initialize_repository(the_repository)` and drop `initialize_thee_repository()`. Signed-off-by: Patrick Steinhardt --- common-main.c | 2 +- oss-fuzz/fuzz-commit-graph.c | 3 ++- repository.c | 29 +++++++++++++++++++++-------- repository.h | 2 +- t/helper/test-read-cache.c | 2 +- 5 files changed, 26 insertions(+), 12 deletions(-) diff --git a/common-main.c b/common-main.c index 033778b3c5..b86f40600f 100644 --- a/common-main.c +++ b/common-main.c @@ -48,7 +48,7 @@ int main(int argc, const char **argv) setlocale(LC_CTYPE, ""); git_setup_gettext(); - initialize_the_repository(); + initialize_repository(the_repository); attr_start(); diff --git a/oss-fuzz/fuzz-commit-graph.c b/oss-fuzz/fuzz-commit-graph.c index 2992079dd9..fe15e2c225 100644 --- a/oss-fuzz/fuzz-commit-graph.c +++ b/oss-fuzz/fuzz-commit-graph.c @@ -11,7 +11,8 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { struct commit_graph *g; - initialize_the_repository(); + initialize_repository(the_repository); + /* * Initialize the_repository with commit-graph settings that would * normally be read from the repository's gitdir. We want to avoid diff --git a/repository.c b/repository.c index 089edbffa2..2118f563e3 100644 --- a/repository.c +++ b/repository.c @@ -17,22 +17,35 @@ /* The main repository */ static struct repository the_repo; -struct repository *the_repository; +struct repository *the_repository = &the_repo; -static void initialize_repository(struct repository *repo) +void initialize_repository(struct repository *repo) { repo->objects = raw_object_store_new(); repo->remote_state = remote_state_new(); repo->parsed_objects = parsed_object_pool_new(); ALLOC_ARRAY(repo->index, 1); index_state_init(repo->index, repo); -} -void initialize_the_repository(void) -{ - the_repository = &the_repo; - initialize_repository(the_repository); - repo_set_hash_algo(&the_repo, GIT_HASH_SHA1); + /* + * Unfortunately, we need to keep this hack around for the time being: + * + * - Not setting up the hash algorithm for `the_repository` leads to + * crashes because `the_hash_algo` is a macro that expands to + * `the_repository->hash_algo`. So if Git commands try to access + * `the_hash_algo` without a Git directory we crash. + * + * - Setting up the hash algorithm to be SHA1 by default breaks other + * commands when running with SHA256. + * + * This is another point in case why having global state is a bad idea. + * Eventually, we should remove this hack and stop setting the hash + * algorithm in this function altogether. Instead, it should only ever + * be set via our repository setup procedures. But that requires more + * work. + */ + if (repo == the_repository) + repo_set_hash_algo(repo, GIT_HASH_SHA1); } static void expand_base_dir(char **out, const char *in, diff --git a/repository.h b/repository.h index 6f4af15417..41ed22543a 100644 --- a/repository.h +++ b/repository.h @@ -207,7 +207,7 @@ void repo_set_worktree(struct repository *repo, const char *path); void repo_set_hash_algo(struct repository *repo, int algo); void repo_set_compat_hash_algo(struct repository *repo, int compat_algo); void repo_set_ref_storage_format(struct repository *repo, unsigned int format); -void initialize_the_repository(void); +void initialize_repository(struct repository *repo); RESULT_MUST_BE_USED int repo_init(struct repository *r, const char *gitdir, const char *worktree); diff --git a/t/helper/test-read-cache.c b/t/helper/test-read-cache.c index 458efa88a6..e803c43ece 100644 --- a/t/helper/test-read-cache.c +++ b/t/helper/test-read-cache.c @@ -9,7 +9,7 @@ int cmd__read_cache(int argc, const char **argv) int i, cnt = 1; const char *name = NULL; - initialize_the_repository(); + initialize_repository(the_repository); if (argc > 1 && skip_prefix(argv[1], "--print-and-refresh=", &name)) { argc--;