1 package System::Introspector::Probe::Repositories::Git;
4 use System::Introspector::Util qw(
12 default => sub { '/' },
17 return transform_exceptions {
18 my $pipe = $self->_open_locate_git_config_pipe;
20 while (defined( my $line = <$pipe> )) {
22 next unless $line =~ m{^(.+)/\.git/config$};
24 $location{ $base } = $self->_gather_git_info($line);
26 return { git => \%location };
30 sub _gather_git_info {
31 my ($self, $config) = @_;
33 config_file => $config,
34 config => transform_exceptions {
35 $self->_gather_git_config($config);
37 tracked => transform_exceptions {
38 $self->_gather_track_info($config);
43 sub _gather_track_info {
44 my ($self, $config) = @_;
45 (my $git_dir = $config) =~ s{/config$}{};
46 return $self->_find_tracking($git_dir);
50 my ($self, $dir) = @_;
51 local $ENV{GIT_DIR} = $dir;
52 my @lines = lines_from_command
53 ['git', 'for-each-ref',
54 '--format', q{OK %(refname:short) %(upstream:short)},
58 for my $line (@lines) {
59 if ($line =~ m{^OK\s+(\S+)\s+(\S+)?$}) {
60 my ($local, $remote) = ($1, $2);
63 changed_files => transform_exceptions {
64 $self->_find_changes($dir, $local, $remote);
66 local_commit_count => transform_exceptions {
67 $self->_find_commits($dir, $local, $remote);
72 return { error => join "\n", @lines };
75 return { branches => \%branch };
79 my ($self, $dir, $local, $remote) = @_;
80 return { error => "No remote" }
81 unless defined $remote;
82 local $ENV{GIT_DIR} = $dir;
83 my @lines = lines_from_command
84 ['git', 'log', '--oneline', "$remote..$local"];
85 return { count => scalar @lines };
89 my ($self, $dir, $local, $remote) = @_;
90 return { error => "No remote" }
91 unless defined $remote;
92 local $ENV{GIT_DIR} = $dir;
93 my @lines = lines_from_command
94 ['git', 'diff', '--name-only', $local, $remote];
95 return { list => \@lines };
98 sub _gather_git_config {
99 my ($self, $config) = @_;
100 my $pipe = $self->_open_git_config_pipe($config);
102 while (defined( my $line = <$pipe> )) {
104 my ($name, $value) = split m{=}, $line, 2;
105 $config{ $name } = $value;
107 return { contents => \%config };
110 sub _open_git_config_pipe {
111 my ($self, $config) = @_;
112 return handle_from_command "git config --file $config --list";
115 sub _open_locate_git_config_pipe {
117 (my $root = $self->root) =~ s{/$}{};
118 return handle_from_command sprintf
119 q{locate --regex '^%s/.*\\.git/config$'}, $root;
128 System::Introspector::Repositories::Git - Gather Git repository info
132 Find Git repositories and gathers their information.
138 This is the root path for the search of git directories. Defaults to C</>.
144 =item L<System::Introspector>