From patchwork Thu Oct 18 18:37:57 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Beller X-Patchwork-Id: 10647807 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id A7DC7157A for ; Thu, 18 Oct 2018 18:38:48 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 9B5BE29024 for ; Thu, 18 Oct 2018 18:38:48 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 8F5D029015; Thu, 18 Oct 2018 18:38:48 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.5 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI, USER_IN_DEF_DKIM_WL autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 24B5629015 for ; Thu, 18 Oct 2018 18:38:48 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728533AbeJSClE (ORCPT ); Thu, 18 Oct 2018 22:41:04 -0400 Received: from mail-oi1-f201.google.com ([209.85.167.201]:39767 "EHLO mail-oi1-f201.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726424AbeJSClD (ORCPT ); Thu, 18 Oct 2018 22:41:03 -0400 Received: by mail-oi1-f201.google.com with SMTP id d23-v6so21259778oib.6 for ; Thu, 18 Oct 2018 11:38:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=40tLCCS66N059thyNV0K+9T6RZVgjK1KcJeEPbkQFrc=; b=TQGmqJg+vnMcvNSJiEiV+qznWy18hI71ojuNZ6QjL+dpQrE6KcRJjhFtaA+DO+mwZC aosfJant6Oq9Yh/aN9g6IBbgeynTXKQ0eVDCFVj4hcHn7uaqEkCqXNkbnqdS4NiozsEY 7YYK//n5eapfDcZynTOIhONK4RKYeMDZWHGspUTBoutjOQtRXoGymtYibxeM3+/muwzG VCQrVqjVGGPJmBZMIwMYYVfZpDQ0/C8tabTo6o0+WncpRWEC2ZiW6PkOlAC1ewlbxFDy onDI3Cc4xWIlq5y1/prKFSpMLL0LY6mXrsbwB+8GRiwiTamIk52thYa3FdBzjeUvVyjz jcLw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=40tLCCS66N059thyNV0K+9T6RZVgjK1KcJeEPbkQFrc=; b=aO8EBFQYgK4hnaOMjliodRY/Mwq6u5YifRUqee2ZE7GMeo+iAjs8vLFzJr1ScR2c4R OKSNhz8AGrPBPFjo3/y3x3To1Qk7szLapdDTYkQZQtvOcMOEcnMRkxqIQ9HcSFCD2BJY 9MERxCbqCK93YiFlhuNJ54QjTdWo5+xWkpeIhmPJ8kjofoQVa9JwgR4lJz4iTV1+a/MQ FujmMSZk2oEeMS9NOJPiZvsdNIQHu+UVBOucd7+twgW32zae1i758X1D9dBpDgdpXP/N h4UHRaFu5pnxIm+fgFnpe7Nz2/ahhC/luFF+pppZNSn7Y/WixE0kxpJDTX27C63bNFFC 97IQ== X-Gm-Message-State: ABuFfojNWkgLbuhrNM28VZphFSHHzyZnvuL/bh1OXaXu5PLlNZLdlnDS Wh/YzNHKx4pvFj0dYk7ZsMjy+dB839Jm X-Google-Smtp-Source: ACcGV62bf00xo/gGwj3kcscw9CL6RMby6MnUsK0nbUg/KlWIzO+qlAnZBDtu8UCuG2wjjszeK8jsA8h6LdW4 X-Received: by 2002:aca:d407:: with SMTP id l7-v6mr25788112oig.41.1539887925900; Thu, 18 Oct 2018 11:38:45 -0700 (PDT) Date: Thu, 18 Oct 2018 11:37:57 -0700 In-Reply-To: <20181018183758.81186-1-sbeller@google.com> Message-Id: <20181018183758.81186-2-sbeller@google.com> Mime-Version: 1.0 References: <20181018183758.81186-1-sbeller@google.com> X-Mailer: git-send-email 2.19.0 Subject: [RFC PATCH 1/2] repository: have get_the_repository() to remove the_repository dependency From: Stefan Beller To: sbeller@google.com Cc: git@vger.kernel.org, jonathantanmy@google.com, stolee@gmail.com Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP The struct 'the_repo' contains all data that of the main repository. As we move more and more globals into this struct, the usual way of accessing these is using 'the_repository', which can be used as a drop in replacement for accessing the migrated globals. However during the migration of globals into the repository object, it is not clear if some code path rely on the_repository or can work on an arbitrary repository (as we'd eventually want for submodules) due to the excessive use of the_repository throughout the code base. To address this, introduce a function 'get_the_repository(void)' which will return the main repository and set the_repository to NULL when the environment variable GIT_NO_THE_REPOSITORY is set. This function is to be strictly used only at the beginning of builtin command to assign it to a local repository pointer that we'll use to pass through the code base. By having the possibility to set the_repository to NULL, we'll get a segfault when we try to access the_repository instead of using the handle that we pass around. This approach let's us have the_repository in the setup code, which in its current form is not yet able to transition into a world where the repository handle is passed around and only test the passing around of the repository handle for later stage code. Eventually in the future the setup code will produce the repository handle and each 'cmd_foo(int argc, char **argv)' builtin would get the repository via an additional parameter. Signed-off-by: Stefan Beller --- repository.c | 10 ++++++++++ repository.h | 13 ++++++++++++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/repository.c b/repository.c index 5dd1486718..aaaababb88 100644 --- a/repository.c +++ b/repository.c @@ -20,6 +20,16 @@ void initialize_the_repository(void) repo_set_hash_algo(&the_repo, GIT_HASH_SHA1); } +struct repository *get_the_repository(void) +{ + struct repository *r = the_repository; + + if (getenv("GIT_NO_THE_REPOSITORY")) + the_repository = NULL; + + return r; +} + static void expand_base_dir(char **out, const char *in, const char *base_dir, const char *def_in) { diff --git a/repository.h b/repository.h index 9f16c42c1e..26f5d64f68 100644 --- a/repository.h +++ b/repository.h @@ -114,13 +114,24 @@ void repo_set_gitdir(struct repository *repo, const char *root, const struct set_gitdir_args *extra_args); void repo_set_worktree(struct repository *repo, const char *path); void repo_set_hash_algo(struct repository *repo, int algo); -void initialize_the_repository(void); int repo_init(struct repository *r, const char *gitdir, const char *worktree); int repo_submodule_init(struct repository *submodule, struct repository *superproject, const char *path); void repo_clear(struct repository *repo); +/* + * Initializes the repository 'the_repository', which is used in the transition + * phase of moving globals into the repository struct. + */ +void initialize_the_repository(void); + +/* + * To be called once; after the call use only returned repository, and do not + * use the_repository any more + */ +struct repository *get_the_repository(void); + /* * Populates the repository's index from its index_file, an index struct will * be allocated if needed. From patchwork Thu Oct 18 18:37:58 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Beller X-Patchwork-Id: 10647809 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 79656157A for ; Thu, 18 Oct 2018 18:38:51 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 6BBA029015 for ; Thu, 18 Oct 2018 18:38:51 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 5F8AF29037; Thu, 18 Oct 2018 18:38:51 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.5 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI, USER_IN_DEF_DKIM_WL autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id C060929015 for ; Thu, 18 Oct 2018 18:38:50 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728569AbeJSClG (ORCPT ); Thu, 18 Oct 2018 22:41:06 -0400 Received: from mail-pf1-f201.google.com ([209.85.210.201]:40656 "EHLO mail-pf1-f201.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726424AbeJSClG (ORCPT ); Thu, 18 Oct 2018 22:41:06 -0400 Received: by mail-pf1-f201.google.com with SMTP id l1-v6so30024179pfb.7 for ; Thu, 18 Oct 2018 11:38:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=LugnInQedrMjFJfLv+/TgWYNu5YSothsN6trdeve0lQ=; b=A3BeLkzRDdqHr4cUGG8292qj3Gpl4oeF5P1m3Kv+rjckLobgpI3zmMNgQUJWPuvb40 BjT+aNhxAS0kh2mRmtBDvphJYQ8I/8nUQI62CsfCFwxQYWGc6xwkD6Hr5SqqKT0ktf7W G9MmosX/zGyjoQmgnQ0soQ9W1wehM4XoJabb3f8lPOpEW4CJvfcGG0YYjACPZ/XFYbDO gA1s1S/UWFsl181HaaWNFFFbu7xvSfv/aFvMw91qPVRz4/BNC5W5Y/fARf1K8BnpPsb2 52KYxgDrNQf0oYX7HD8uKlxByPqiIhV8XMldn95KECTzPYXxVunRVqVAq/MFu668gUKz DkKQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=LugnInQedrMjFJfLv+/TgWYNu5YSothsN6trdeve0lQ=; b=S42feXcJNrhvAtp7f5SfBcoM1AeJHmmxAa/drlnRZWtgNuFXrM+JIegeO84J0RPpbP e9JqgWNz0htJgU0Qra+4DnJm65ARy6Mv6DS/WlPt6E2ZSMqAr48c3J/3Fd/JsYVqHTRw 2nia1gJTyZ1ZbWuC5Gp7eXg3IRN+ABl+YDRaBo2ZSDvdr1X0VjnB55uDYpZYY0A4wbN2 ao4LMEubyrPeb0ZVokMTYHNYG7gVFWeBA9jEeuxQ2Cwn4ZUfji/JiZAoFtOA9xu4K8LM L1BgR4cNS27EMjJo7FOiBWkQIOLvb+ZBmamU3xsOaVVhsDVyLbn6Gqxf5LrRdAivPwSb QVaQ== X-Gm-Message-State: ABuFfoh69lDPdvZypvdDf3mXEyssRwGmLvSKE4ZNeC/GVHNXidIuvF7H TYzd4suClHDcwasfK5qgcVF8CE7vLzwl X-Google-Smtp-Source: ACcGV60mtsC+cetNKdkUIkiq+0PNT/W97Et5JArtC+2sJuOO2bKrTUtkZSWnT062YHBvTYPe37nCXSjx+miV X-Received: by 2002:a63:4924:: with SMTP id w36-v6mr14607572pga.77.1539887928193; Thu, 18 Oct 2018 11:38:48 -0700 (PDT) Date: Thu, 18 Oct 2018 11:37:58 -0700 In-Reply-To: <20181018183758.81186-1-sbeller@google.com> Message-Id: <20181018183758.81186-3-sbeller@google.com> Mime-Version: 1.0 References: <20181018183758.81186-1-sbeller@google.com> X-Mailer: git-send-email 2.19.0 Subject: [RFC PATCH 2/2 (BREAKS BUILD)] builtin/merge-base.c: do not rely on the_repository any more From: Stefan Beller To: sbeller@google.com Cc: git@vger.kernel.org, jonathantanmy@google.com, stolee@gmail.com Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP To avoid creeping in the dependency of the_repository, use GIT_NO_THE_REPOSITORY in the test to prove it still works. Signed-off-by: Stefan Beller --- This doesn't work yet, as we have not converted get_oid, yet. It proves that GIT_NO_THE_REPOSITORY works, though. Stefan builtin/merge-base.c | 67 ++++++++++++++++++++++++++----------------- t/t6010-merge-base.sh | 3 +- 2 files changed, 43 insertions(+), 27 deletions(-) diff --git a/builtin/merge-base.c b/builtin/merge-base.c index 1c92099070..29341f8839 100644 --- a/builtin/merge-base.c +++ b/builtin/merge-base.c @@ -1,3 +1,4 @@ +#define NO_THE_REPOSITORY_COMPATIBILITY_MACROS #include "builtin.h" #include "cache.h" #include "config.h" @@ -37,27 +38,28 @@ static const char * const merge_base_usage[] = { NULL }; -static struct commit *get_commit_reference(const char *arg) +static struct commit *get_commit_reference(struct repository *r, + const char *arg) { struct object_id revkey; - struct commit *r; + struct commit *ref; if (get_oid(arg, &revkey)) die("Not a valid object name %s", arg); - r = lookup_commit_reference(the_repository, &revkey); - if (!r) + ref = lookup_commit_reference(r, &revkey); + if (!ref) die("Not a valid commit name %s", arg); - return r; + return ref; } -static int handle_independent(int count, const char **args) +static int handle_independent(struct repository *r, int count, const char **args) { struct commit_list *revs = NULL, *rev; int i; for (i = count - 1; i >= 0; i--) - commit_list_insert(get_commit_reference(args[i]), &revs); + commit_list_insert(get_commit_reference(r, args[i]), &revs); reduce_heads_replace(&revs); @@ -71,14 +73,16 @@ static int handle_independent(int count, const char **args) return 0; } -static int handle_octopus(int count, const char **args, int show_all) +static int handle_octopus(struct repository *r, + int count, const char **args, + int show_all) { struct commit_list *revs = NULL; struct commit_list *result, *rev; int i; for (i = count - 1; i >= 0; i--) - commit_list_insert(get_commit_reference(args[i]), &revs); + commit_list_insert(get_commit_reference(r, args[i]), &revs); result = get_octopus_merge_bases(revs); free_commit_list(revs); @@ -97,15 +101,15 @@ static int handle_octopus(int count, const char **args, int show_all) return 0; } -static int handle_is_ancestor(int argc, const char **argv) +static int handle_is_ancestor(struct repository *r, int argc, const char **argv) { struct commit *one, *two; if (argc != 2) die("--is-ancestor takes exactly two commits"); - one = get_commit_reference(argv[0]); - two = get_commit_reference(argv[1]); - if (in_merge_bases(one, two)) + one = get_commit_reference(r, argv[0]); + two = get_commit_reference(r, argv[1]); + if (repo_in_merge_bases(r, one, two)) return 0; else return 1; @@ -116,19 +120,22 @@ struct rev_collect { int nr; int alloc; unsigned int initial : 1; + struct repository *repo; }; -static void add_one_commit(struct object_id *oid, struct rev_collect *revs) +static void add_one_commit(struct repository *r, + struct object_id *oid, + struct rev_collect *revs) { struct commit *commit; if (is_null_oid(oid)) return; - commit = lookup_commit(the_repository, oid); + commit = lookup_commit(r, oid); if (!commit || (commit->object.flags & TMP_MARK) || - parse_commit(commit)) + repo_parse_commit(r, commit)) return; ALLOC_GROW(revs->commit, revs->nr + 1, revs->alloc); @@ -144,13 +151,13 @@ static int collect_one_reflog_ent(struct object_id *ooid, struct object_id *noid if (revs->initial) { revs->initial = 0; - add_one_commit(ooid, revs); + add_one_commit(revs->repo, ooid, revs); } - add_one_commit(noid, revs); + add_one_commit(revs->repo, noid, revs); return 0; } -static int handle_fork_point(int argc, const char **argv) +static int handle_fork_point(struct repository *r, int argc, const char **argv) { struct object_id oid; char *refname; @@ -173,13 +180,14 @@ static int handle_fork_point(int argc, const char **argv) if (get_oid(commitname, &oid)) die("Not a valid object name: '%s'", commitname); - derived = lookup_commit_reference(the_repository, &oid); + derived = lookup_commit_reference(r, &oid); memset(&revs, 0, sizeof(revs)); revs.initial = 1; + revs.repo = r; for_each_reflog_ent(refname, collect_one_reflog_ent, &revs); if (!revs.nr && !get_oid(refname, &oid)) - add_one_commit(&oid, &revs); + add_one_commit(r, &oid, &revs); for (i = 0; i < revs.nr; i++) revs.commit[i]->object.flags &= ~TMP_MARK; @@ -217,6 +225,7 @@ int cmd_merge_base(int argc, const char **argv, const char *prefix) int rev_nr = 0; int show_all = 0; int cmdmode = 0; + struct repository *r; struct option options[] = { OPT_BOOL('a', "all", &show_all, N_("output all common ancestors")), @@ -234,27 +243,33 @@ int cmd_merge_base(int argc, const char **argv, const char *prefix) git_config(git_default_config, NULL); argc = parse_options(argc, argv, prefix, options, merge_base_usage, 0); + /* + * TODO: once the config machinery can cope without proper setup of + * the_repository, move this call up + */ + r = get_the_repository(); + if (cmdmode == 'a') { if (argc < 2) usage_with_options(merge_base_usage, options); if (show_all) die("--is-ancestor cannot be used with --all"); - return handle_is_ancestor(argc, argv); + return handle_is_ancestor(r, argc, argv); } if (cmdmode == 'r' && show_all) die("--independent cannot be used with --all"); if (cmdmode == 'o') - return handle_octopus(argc, argv, show_all); + return handle_octopus(r, argc, argv, show_all); if (cmdmode == 'r') - return handle_independent(argc, argv); + return handle_independent(r, argc, argv); if (cmdmode == 'f') { if (argc < 1 || 2 < argc) usage_with_options(merge_base_usage, options); - return handle_fork_point(argc, argv); + return handle_fork_point(r, argc, argv); } if (argc < 2) @@ -262,6 +277,6 @@ int cmd_merge_base(int argc, const char **argv, const char *prefix) ALLOC_ARRAY(rev, argc); while (argc-- > 0) - rev[rev_nr++] = get_commit_reference(*argv++); + rev[rev_nr++] = get_commit_reference(r, *argv++); return show_merge_base(rev, rev_nr, show_all); } diff --git a/t/t6010-merge-base.sh b/t/t6010-merge-base.sh index 44c726ea39..ae1c85ee8d 100755 --- a/t/t6010-merge-base.sh +++ b/t/t6010-merge-base.sh @@ -15,7 +15,8 @@ GIT_COMMITTER_EMAIL=git@comm.iter.xz GIT_COMMITTER_NAME='C O Mmiter' GIT_AUTHOR_NAME='A U Thor' GIT_AUTHOR_EMAIL=git@au.thor.xz -export GIT_COMMITTER_EMAIL GIT_COMMITTER_NAME GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL +GIT_NO_THE_REPOSITORY=1 +export GIT_COMMITTER_EMAIL GIT_COMMITTER_NAME GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_NO_THE_REPOSITORY doit () { OFFSET=$1 &&