File: GitDirectory.pm

package info (click to toggle)
prt 0.22-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 404 kB
  • sloc: perl: 1,185; makefile: 3
file content (62 lines) | stat: -rw-r--r-- 1,370 bytes parent folder | download | duplicates (5)
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
package App::PRT::Collector::GitDirectory;
use strict;
use warnings;
use Path::Class;

# find git directory from specified directory path
# returns:
#   directory path (when found)
#   undef          (when not found)
sub find_git_root_directory {
    my ($class, $directory) = @_;

    die "$directory does not exist" unless -d $directory;

    my $current = dir($directory);
    while (1) {
        if (-d $current->subdir('.git')) {
            return $current->stringify;
        }

        if ($current eq $current->parent) {
            # not found
            return;
        }
        $current = $current->parent;
    }
}

sub new {
    my ($class, $directory) = @_;

    die 'directory requird' unless defined $directory;

    die "directory $directory does not exist" unless -d $directory;

    bless {
        directory     => $directory,
    }, $class;
}

sub collect {
    my ($self) = @_;
    my $git_directory = dir($self->directory)->subdir('.git');

    my $output = `git --no-pager --git-dir $git_directory ls-files --full-name 2> /dev/null`;

    if ($?) {
        # when success, exit status is 0
        die "directory @{[ $self->directory ]} seems not a git repository";
    }

    my $dir = dir($self->directory);
    [ map { $dir->file($_)->stringify } split /\n/, $output ];
}

sub directory {
    my ($self) = @_;

    $self->{directory};
}

1;