summaryrefslogtreecommitdiff
path: root/bin/src-register-all
diff options
context:
space:
mode:
authorSean Whitton <spwhitton@spwhitton.name>2019-07-30 09:45:08 +0100
committerSean Whitton <spwhitton@spwhitton.name>2019-07-30 09:45:08 +0100
commit2de3db02bacbc5a9fe4eeb17a94d8cc9a052778c (patch)
tree97095613c96e891abbe3386f976b9d80bea0c6d8 /bin/src-register-all
parentb05657c8a0ffb75441aecfa2f80be2dc137070de (diff)
downloaddotfiles-2de3db02bacbc5a9fe4eeb17a94d8cc9a052778c.tar.gz
rewrite bin/src-*
Diffstat (limited to 'bin/src-register-all')
-rwxr-xr-xbin/src-register-all77
1 files changed, 62 insertions, 15 deletions
diff --git a/bin/src-register-all b/bin/src-register-all
index 5d5bae59..541f196d 100755
--- a/bin/src-register-all
+++ b/bin/src-register-all
@@ -1,21 +1,68 @@
-#!/usr/bin/perl
+#!/usr/bin/env perl
-# Add new all new git/hg repos in ~/src to ~/src/.mrconfig
+# src-register-all -- add new all new git/hg repos in ~/src to ~/.mrconfig
+
+# Copyright (C) 2019 Sean Whitton
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or (at
+# your option) any later version.
+#
+# This program is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+# General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+
+
+# This script must be as portable as possible (inc. its shebang).
use strict;
use warnings;
-use File::Basename;
-use File::Grep "fgrep";
-use File::chdir;
-
-# TODO should recurse into arbitarary subdirectories of ~/src
-
-foreach my $f ( glob "$ENV{'HOME'}/src/*" ) {
- my $short = basename($f);
- next unless ( -d "$f/.git" || -d "$f/.hg" );
- {
- local $CWD = $f;
- system "mr register >/dev/null";
- die "failed to register ~/src/$short" unless ($? >> 8 == 0);
+
+use Cwd;
+use File::Find;
+
+chdir;
+my @known_repos;
+open my $fh, "<", ".mrconfig";
+while (<$fh>) {
+ if (/^\[(src\/.+)\]$/) {
+ push @known_repos, getcwd()."/$1";
}
}
+find({wanted => \&register, preprocess => \&skip}, "src");
+
+sub skip {
+ my $cwd = getcwd();
+ # once we've found a repo, don't search inside it for more repos
+ return () if is_repo($cwd);
+ my @entries;
+ # don't process repos mr already knows about
+ foreach my $entry (@_) {
+ my $entry_path = $cwd."/$entry";
+ push @entries, $entry
+ unless grep /\A$entry_path\z/, @known_repos;;
+ }
+ return @entries;
+}
+sub register {
+ return unless is_repo($_);
+ chdir $_;
+ my $register_out = `mr -c $ENV{HOME}/.mrconfig register 2>&1`;
+ unless ($? == 0) {
+ print "mr register: $File::Find::name\n";
+ print $register_out;
+ die "\n";
+ }
+ chdir "..";
+}
+
+sub is_repo {
+ my $repo = shift;
+ return -d "$repo/.git" || -d "$repo/.hg";
+}