All of lore.kernel.org
 help / color / mirror / Atom feed
From: Patrick Steinhardt <ps@pks.im>
To: Calvin Wan <calvinwan@google.com>
Cc: git@vger.kernel.org, Justin Tobler <jltobler@gmail.com>,
	Junio C Hamano <gitster@pobox.com>
Subject: Re: [PATCH v2 05/20] path: stop relying on `the_repository` when reporting garbage
Date: Thu, 15 Aug 2024 07:26:01 +0200	[thread overview]
Message-ID: <Zr2RaQCbi3Tpstz7@tanuki> (raw)
In-Reply-To: <20240814182850.1273188-1-calvinwan@google.com>

On Wed, Aug 14, 2024 at 06:28:50PM +0000, Calvin Wan wrote:
> Patrick Steinhardt <ps@pks.im> writes:
> > We access `the_repository` in `report_linked_checkout_garbage()` both
> > directly and indirectly via `get_git_dir()`. Remove this dependency by
> > instead passing a `struct repository` as parameter.
> > 
> > Signed-off-by: Patrick Steinhardt <ps@pks.im>
> > ---
> >  builtin/count-objects.c | 2 +-
> >  path.c                  | 6 +++---
> >  path.h                  | 2 +-
> >  3 files changed, 5 insertions(+), 5 deletions(-)
> > 
> > diff --git a/path.c b/path.c
> > index 069db6ff8f..97a07fafc7 100644
> > --- a/path.c
> > +++ b/path.c
> > @@ -365,15 +365,15 @@ static void update_common_dir(struct strbuf *buf, int git_dir_len,
> >  		strbuf_addstr(buf, LOCK_SUFFIX);
> >  }
> >  
> > -void report_linked_checkout_garbage(void)
> > +void report_linked_checkout_garbage(struct repository *r)
> >  {
> >  	struct strbuf sb = STRBUF_INIT;
> >  	const struct common_dir *p;
> >  	int len;
> >  
> > -	if (!the_repository->different_commondir)
> > +	if (!r->different_commondir)
> >  		return;
> > -	strbuf_addf(&sb, "%s/", get_git_dir());
> > +	strbuf_addf(&sb, "%s/", r->gitdir);
> >  	len = sb.len;
> >  	for (p = common_list; p->path; p++) {
> >  		const char *path = p->path;
> 
> Callers have two options here for accessing the gitdir: one is including
> environment.h and calling get_git_dir(). The other is passing in `struct
> repository *r` and accessing r->gitdir. It's not entirely clear which
> should be used in what scenario. Sure with the second option the user
> has the option of passing something in that isn't `the_repository` but
> practically speaking that's not happening here and also in the large
> majority of other scenarios.
> 
> I'm OK with this patch for the purposes of the series, but do you think
> in the future we should introduce get_git_dir(struct repository *r) and
> change get_git_dir() into get_env_git_dir() that simply calls
> get_git_dir(the_repository)?

Good question. `get_git_dir()` is implemented like this:

        const char *get_git_dir(void)
        {
            if (!the_repository->gitdir)
                BUG("git environment hasn't been setup");
            return the_repository->gitdir;
        }

We could of course introduce something like `repo_get_git_dir()` that
takes an arbitrary repository as input, like this:

        const char *repo_get_git_dir(struct repository *r)
        {
            if (!r->gitdir)
                BUG("git environment hasn't been setup");
            return r->gitdir;
        }

The only benefit that this has is that we double check whether
`r->gitdir` has actually been set in the first place. I don't really
think that check is all that useful though, because I expect that the
caller either has a repository, and given that a repository always has a
`gitdir` I'd always expect it to be populated. Or they don't have a
gitdir, but in that case they cannot call `repo_get_git_dir()` in the
first place because we'd already segfault before hitting the BUG when
trying to dereference a NULL pointer.

So I'm not entirely sure of the benefit of such a function over just
accessing `r->gitdir` directly.

Patrick

  reply	other threads:[~2024-08-15  5:26 UTC|newest]

Thread overview: 69+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-08-07  6:56 [PATCH 00/20] Stop using `the_repository` in "config.c" Patrick Steinhardt
2024-08-07  6:56 ` [PATCH 01/20] path: expose `do_git_path()` as `repo_git_pathv()` Patrick Steinhardt
2024-08-09 16:58   ` Justin Tobler
2024-08-13  9:25     ` Patrick Steinhardt
2024-08-07  6:56 ` [PATCH 02/20] path: expose `do_git_common_path()` as `strbuf_git_common_pathv()` Patrick Steinhardt
2024-08-09 17:18   ` Justin Tobler
2024-08-09 17:32     ` Junio C Hamano
2024-08-13  9:25       ` Patrick Steinhardt
2024-08-13 15:12         ` Junio C Hamano
2024-08-07  6:56 ` [PATCH 03/20] editor: do not rely on `the_repository` for interactive edits Patrick Steinhardt
2024-08-09 17:36   ` Justin Tobler
2024-08-07  6:57 ` [PATCH 04/20] hooks: remove implicit dependency on `the_repository` Patrick Steinhardt
2024-08-07  6:57 ` [PATCH 05/20] path: stop relying on `the_repository` when reporting garbage Patrick Steinhardt
2024-08-07  6:57 ` [PATCH 06/20] path: stop relying on `the_repository` in `worktree_git_path()` Patrick Steinhardt
2024-08-09 19:02   ` Justin Tobler
2024-08-13  9:25     ` Patrick Steinhardt
2024-08-07  6:57 ` [PATCH 07/20] path: hide functions using `the_repository` by default Patrick Steinhardt
2024-08-09 19:43   ` Justin Tobler
2024-08-13  9:25     ` Patrick Steinhardt
2024-08-07  6:57 ` [PATCH 08/20] config: introduce missing setters that take repo as parameter Patrick Steinhardt
2024-08-09 20:07   ` Justin Tobler
2024-08-13  9:25     ` Patrick Steinhardt
2024-08-07  6:57 ` [PATCH 09/20] config: expose `repo_config_clear()` Patrick Steinhardt
2024-08-07  6:57 ` [PATCH 10/20] config: pass repo to `git_config_get_index_threads()` Patrick Steinhardt
2024-08-07  6:57 ` [PATCH 11/20] config: pass repo to `git_config_get_split_index()` Patrick Steinhardt
2024-08-07  6:57 ` [PATCH 12/20] config: pass repo to `git_config_get_max_percent_split_change()` Patrick Steinhardt
2024-08-07  6:57 ` [PATCH 13/20] config: pass repo to `git_config_get_expiry()` Patrick Steinhardt
2024-08-07  6:57 ` [PATCH 14/20] config: pass repo to `git_config_get_expiry_in_days()` Patrick Steinhardt
2024-08-09 20:21   ` Justin Tobler
2024-08-09 21:14     ` Junio C Hamano
2024-08-07  6:57 ` [PATCH 15/20] config: pass repo to `git_die_config()` Patrick Steinhardt
2024-08-07  6:57 ` [PATCH 16/20] config: pass repo to functions that rename or copy sections Patrick Steinhardt
2024-08-07  6:58 ` [PATCH 17/20] config: don't have setters depend on `the_repository` Patrick Steinhardt
2024-08-07  6:58 ` [PATCH 18/20] config: don't depend on `the_repository` with branch conditions Patrick Steinhardt
2024-08-09 20:47   ` Justin Tobler
2024-08-13  9:25     ` Patrick Steinhardt
2024-08-07  6:58 ` [PATCH 19/20] global: prepare for hiding away repo-less config functions Patrick Steinhardt
2024-08-09 20:57   ` Justin Tobler
2024-08-07  6:58 ` [PATCH 20/20] config: hide functions using `the_repository` by default Patrick Steinhardt
2024-08-09 21:13   ` Justin Tobler
2024-08-07  9:48 ` [PATCH 00/20] Stop using `the_repository` in "config.c" Ghanshyam Thakkar
2024-08-07 13:11   ` Patrick Steinhardt
2024-08-13  9:13 ` [PATCH v2 " Patrick Steinhardt
2024-08-13  9:13   ` [PATCH v2 01/20] path: expose `do_git_path()` as `repo_git_pathv()` Patrick Steinhardt
2024-08-13  9:13   ` [PATCH v2 02/20] path: expose `do_git_common_path()` as `repo_common_pathv()` Patrick Steinhardt
2024-08-13  9:13   ` [PATCH v2 03/20] editor: do not rely on `the_repository` for interactive edits Patrick Steinhardt
2024-08-13  9:13   ` [PATCH v2 04/20] hooks: remove implicit dependency on `the_repository` Patrick Steinhardt
2024-08-13  9:13   ` [PATCH v2 05/20] path: stop relying on `the_repository` when reporting garbage Patrick Steinhardt
2024-08-14 18:28     ` Calvin Wan
2024-08-15  5:26       ` Patrick Steinhardt [this message]
2024-08-13  9:13   ` [PATCH v2 06/20] path: stop relying on `the_repository` in `worktree_git_path()` Patrick Steinhardt
2024-08-13  9:13   ` [PATCH v2 07/20] path: hide functions using `the_repository` by default Patrick Steinhardt
2024-08-13  9:13   ` [PATCH v2 08/20] config: introduce missing setters that take repo as parameter Patrick Steinhardt
2024-08-13  9:13   ` [PATCH v2 09/20] config: expose `repo_config_clear()` Patrick Steinhardt
2024-08-13  9:13   ` [PATCH v2 10/20] config: pass repo to `git_config_get_index_threads()` Patrick Steinhardt
2024-08-13  9:13   ` [PATCH v2 11/20] config: pass repo to `git_config_get_split_index()` Patrick Steinhardt
2024-08-13  9:13   ` [PATCH v2 12/20] config: pass repo to `git_config_get_max_percent_split_change()` Patrick Steinhardt
2024-08-13  9:13   ` [PATCH v2 13/20] config: pass repo to `git_config_get_expiry()` Patrick Steinhardt
2024-08-13  9:14   ` [PATCH v2 14/20] config: pass repo to `git_config_get_expiry_in_days()` Patrick Steinhardt
2024-08-13  9:14   ` [PATCH v2 15/20] config: pass repo to `git_die_config()` Patrick Steinhardt
2024-08-13  9:14   ` [PATCH v2 16/20] config: pass repo to functions that rename or copy sections Patrick Steinhardt
2024-08-13  9:14   ` [PATCH v2 17/20] config: don't have setters depend on `the_repository` Patrick Steinhardt
2024-08-13  9:14   ` [PATCH v2 18/20] config: don't depend on `the_repository` with branch conditions Patrick Steinhardt
2024-08-13  9:14   ` [PATCH v2 19/20] global: prepare for hiding away repo-less config functions Patrick Steinhardt
2024-08-13  9:14   ` [PATCH v2 20/20] config: hide functions using `the_repository` by default Patrick Steinhardt
2024-08-13 17:07   ` [PATCH v2 00/20] Stop using `the_repository` in "config.c" Junio C Hamano
2024-08-14 19:29   ` Calvin Wan
2024-08-15  5:13     ` Patrick Steinhardt
2024-08-15  0:58   ` Justin Tobler

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=Zr2RaQCbi3Tpstz7@tanuki \
    --to=ps@pks.im \
    --cc=calvinwan@google.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=jltobler@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.