about summary refs log tree commit homepage
path: root/public-inbox-index
diff options
context:
space:
mode:
authorEric Wong <e@80x24.org>2015-08-30 01:04:31 +0000
committerEric Wong <e@80x24.org>2015-08-30 01:10:45 +0000
commitc94fbc9ff69ca6e88984b1b96be25355c3098be3 (patch)
tree718ac8e5a277a728f89ee2bcc480f64de22f6d70 /public-inbox-index
parent8ea217f4452e34776ba294b5090827c99656cada (diff)
downloadpublic-inbox-c94fbc9ff69ca6e88984b1b96be25355c3098be3.tar.gz
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.
Diffstat (limited to 'public-inbox-index')
-rwxr-xr-xpublic-inbox-index35
1 files changed, 33 insertions, 2 deletions
diff --git a/public-inbox-index b/public-inbox-index
index f39ad9e1..44814e3f 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);
 }