unofficial mirror of meta@public-inbox.org
 help / color / mirror / Atom feed
* [PATCH] public-inbox-index: resolve git directory if run inside one
@ 2015-08-30  1:04 Eric Wong
  0 siblings, 0 replies; only message in thread
From: Eric Wong @ 2015-08-30  1:04 UTC (permalink / raw)
  To: meta

I often forget to pass the correct path to a git directory
or run from inside one.  Fortunately git is script-friendly
and allows easily resolving the correct GIT_DIR path.
---
 public-inbox-index | 35 +++++++++++++++++++++++++++++++++--
 1 file changed, 33 insertions(+), 2 deletions(-)

diff --git a/public-inbox-index b/public-inbox-index
index f39ad9e..44814e3 100755
--- a/public-inbox-index
+++ b/public-inbox-index
@@ -15,11 +15,42 @@ if ($@) {
 	print STDERR "Search::Xapian required for $0\n";
 	exit 1;
 }
+my @dirs;
+
+sub resolve_git_dir {
+	my ($cd) = @_;
+	my @cmd = qw(git rev-parse --git-dir);
+	my $cmd = join(' ', @cmd);
+	my $pid = open my $fh, '-|';
+	defined $pid or die "forking $cmd failed: $!\n";
+	if ($pid == 0) {
+		if (defined $cd) {
+			chdir $cd or die "chdir $cd failed: $!\n";
+		}
+		exec @cmd;
+		die "Failed to exec $cmd: $!\n";
+	} else {
+		my $dir = eval {
+			local $/;
+			<$fh>;
+		};
+		close $fh or die "error in $cmd: $!\n";
+		chomp $dir;
+		return $cd if ($dir eq '.' && defined $cd);
+		$dir;
+	}
+}
+
+if (@ARGV) {
+	@dirs = map { resolve_git_dir($_) } @ARGV;
+} else {
+	@dirs = (resolve_git_dir());
+}
 
 sub usage { print STDERR "Usage: $usage\n"; exit 1 }
-usage() unless @ARGV;
+usage() unless @dirs;
 
-foreach my $dir (@ARGV) {
+foreach my $dir (@dirs) {
 	index_dir($dir);
 }
 
-- 
EW


^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2015-08-30  1:04 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-08-30  1:04 [PATCH] public-inbox-index: resolve git directory if run inside one Eric Wong

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).