user/dev discussion of public-inbox itself
 help / color / mirror / code / Atom feed
From: Eric Wong <e@80x24.org>
To: meta@public-inbox.org
Subject: [PATCH] git: shorten --git-dir= in CLI with chdir in spawn
Date: Wed, 29 Sep 2021 21:25:20 +0000	[thread overview]
Message-ID: <20210929212520.7297-1-e@80x24.org> (raw)

Long pathnames are difficult to read and distinguish in ps(1)
output.  Deep paths can also slow down pathname resolution
when dealing with loose objects, so we put "cat-file --batch"
deeper into the directory tree.

Since v2 processes are in the form of $INBOXDIR/all.git, keep
the basename of $INBOXDIR in --git-dir= so it's easy to
distinguish between processes just by looking at ps(1).

While "git -C" also exists, it's only present in git 1.8.5+.
We also need to keep in mind the "directory" pointed to by
--git-dir= need not be a directory (nor a symlink pointing
to one).

This reduces pathname resolution overhead for v1 and v2 inbox
git processes, but unfortunately not for extindex since that
needs to store alternates as absolute paths.
---
 lib/PublicInbox/Git.pm | 18 ++++++++++++------
 1 file changed, 12 insertions(+), 6 deletions(-)

diff --git a/lib/PublicInbox/Git.pm b/lib/PublicInbox/Git.pm
index d5b1d39d..97c39aad 100644
--- a/lib/PublicInbox/Git.pm
+++ b/lib/PublicInbox/Git.pm
@@ -62,6 +62,8 @@ sub git_quote ($) {
 
 sub new {
 	my ($class, $git_dir) = @_;
+	$git_dir =~ tr!/!/!s;
+	$git_dir =~ s!/*\z!!s;
 	# may contain {-tmp} field for File::Temp::Dir
 	bless { git_dir => $git_dir, alt_st => '', -git_path => {} }, $class
 }
@@ -110,18 +112,22 @@ sub _bidi_pipe {
 		}
 		return;
 	}
-	my ($out_r, $out_w);
-	pipe($out_r, $out_w) or $self->fail("pipe failed: $!");
-	my @cmd = (qw(git), "--git-dir=$self->{git_dir}",
+	pipe(my ($out_r, $out_w)) or $self->fail("pipe failed: $!");
+	my $rdr = { 0 => $out_r };
+	my $gd = $self->{git_dir};
+	if ($gd =~ s!/([^/]+/[^/]+)\z!/!) {
+		$rdr->{-C} = $gd;
+		$gd = $1;
+	}
+	my @cmd = (qw(git), "--git-dir=$gd",
 			qw(-c core.abbrev=40 cat-file), $batch);
-	my $redir = { 0 => $out_r };
 	if ($err) {
 		my $id = "git.$self->{git_dir}$batch.err";
 		my $fh = tmpfile($id) or $self->fail("tmpfile($id): $!");
 		$self->{$err} = $fh;
-		$redir->{2} = $fh;
+		$rdr->{2} = $fh;
 	}
-	my ($in_r, $p) = popen_rd(\@cmd, undef, $redir);
+	my ($in_r, $p) = popen_rd(\@cmd, undef, $rdr);
 	$self->{$pid} = $p;
 	$self->{"$pid.owner"} = $$;
 	$out_w->autoflush(1);

                 reply	other threads:[~2021-09-29 21:25 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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

  List information: https://public-inbox.org/README

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

  git send-email \
    --in-reply-to=20210929212520.7297-1-e@80x24.org \
    --to=e@80x24.org \
    --cc=meta@public-inbox.org \
    --subject='Re: [PATCH] git: shorten --git-dir= in CLI with chdir in spawn' \
    /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

Code repositories for project(s) associated with this inbox:

	https://80x24.org/public-inbox.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).