aboutsummaryrefslogtreecommitdiff
path: root/perl/git-construct.pl
blob: 4024ef188b78e277f4dbe4af9a2c768a2f58eaf2 (plain) (blame)
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
#!/usr/bin/perl

use strict;
use warnings;
use Git::Repository;
use Cwd 'realpath';
use File::Basename;

use Data::Dumper;

sub new_repository {
    my ($path) = @_;
    $path = realpath($path);

    my $name = basename($path);
    my $repo = Git::Repository->new(work_tree => $path);

    my @raw_branches = $repo->run('branch');
    my @branches;

    for my $b (@raw_branches) {
        chomp $b;
        my $is_active = 0;

        if ($b =~ /^\* /) {
            $b =~ s/^\* //;
            $is_active = 1;
        }

        my @commits;
        my @logs = $repo->run(log => $b, '--pretty=format:%H;%an;%s');
        for my $line (@logs) {
            my ($hash, $author, $message) = split /;/, $line, 3;
            push @commits, {
                hash    => $hash,
                author  => $author,
                message => $message,
            };
        }

        push @branches, {
            name      => $b,
            is_active => $is_active,
            commits   => \@commits,
        };
    }

    # first commiter as a fallback
    my $owner = ($repo->run('log', '--reverse', '--pretty=format:%an'))[0];

    return {
        name     => $name,
        owner    => $owner,
        branches => \@branches,
    };
}

print Dumper( new_repository('./') );