user/dev discussion of public-inbox itself
 help / color / mirror / code / Atom feed
Search results ordered by [date|relevance]  view[summary|nested|Atom feed]
thread overview below | download mbox.gz: |
* [PATCH] public-inbox-index: resolve git directory if run inside one
@ 2015-08-30  1:04  7% Eric Wong
  0 siblings, 0 replies; 1+ results
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	[relevance 7%]

Results 1-1 of 1 | reverse | options above
-- pct% links below jump to the message on this page, permalinks otherwise --
2015-08-30  1:04  7% [PATCH] public-inbox-index: resolve git directory if run inside one Eric Wong

Code repositories for project(s) associated with this public 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).