-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit_submodule_path.pl
executable file
·78 lines (65 loc) · 1.54 KB
/
git_submodule_path.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/usr/bin/env perl
use warnings;
use strict;
use FindBin;
use lib "$FindBin::Bin";
#push @INC, $FindBin::Bin
use Essent_v2;
use feature qw(say);
use Cwd qw(abs_path cwd);
use File::Basename;
use Data::Dumper;
my $abspath = abs_path(cwd());
my $startdir = $ARGV[0] || $abspath;
## TODO: make faster
my %finallist;
die("$startdir does not exist. $!") unless -d $startdir;
my $exec = "git remote";
`$exec`;
die("$startdir is not a git repo (no .git as subdir) $!") if $? || ! -d "$startdir/.git";
$finallist{$startdir} = 1;
recurse($startdir);
sub recurse {
my @queue = @_;
my @gitstatuslist = ();
while (@queue) {
my $path = shift @queue;
@gitstatuslist = (gitstatuslist($path));
my $abspath = abs_path(cwd());
if (@gitstatuslist) {
# _git submodule status_ always returns relative paths
my @paths = sm_paths(@gitstatuslist);
# convert into absolute paths
@paths = map {$abspath ."/". $_} @paths;
foreach (@paths) {
$finallist{$_} = 1;
}
recurse(@paths);
} else {
$finallist{$path} = 1;
}
}
}
my @finallist;
foreach my $path (keys %finallist) {
push @finallist, $path;
}
print "@finallist";
## END
sub gitstatuslist {
my $dir = shift;
chdir($dir);
my $exec = "git submodule status";
my @statuslist = `$exec`;
@statuslist = map {Data::remove_outerspace($_)} @statuslist;
return @statuslist;
}
sub sm_paths {
my @gitstatus = @_;
my @sm_paths;
foreach my $sm (@gitstatus) {
my $path = (split(/\s/, $sm))[1];
push @sm_paths, $path;
}
return @sm_paths;
}