merge git log from multiple repositories by date
[dell-switch] / git-log-merge.pl
1 #!/usr/bin/perl
2 use warnings;
3 use strict;
4 use autodie;
5 use Data::Dump qw(dump);
6
7 my @repos = qw(
8 /home/dpavlin/dell-switch/running-config/
9 /home/dpavlin/mikrotik-switch/backup/
10 /home/dpavlin/tilera/backup/
11 );
12
13 my $debug = $ENV{DEBUG} || 0;
14 $| = 1 if $debug;
15
16 open(my $less, '|-', "less -R -S");
17
18 my $commit;
19 my $commit_next;
20
21 our $fh;
22 my $date_commit;
23
24 sub get_commit {
25         my $repo = shift;
26         my $r = $fh->{$repo};
27         die "ERROR on $repo in ",dump($fh) unless $r;
28         while(<$r>) {
29                 if ( m/(\e\[\d*m)?commit [0-9a-f]+/ ) {
30                 #if ( m/commit [0-9a-f]+/ ) {
31                         if ( ! defined $commit->{$repo} ) { # first time, read commit
32                                 $commit->{$repo} = $_;
33                                 warn "## first commit ",dump($_) if $debug;
34                         } else {
35                                 $commit_next->{$repo} = $_;
36                                 warn "## --------------" if $debug;
37                                 last;
38                         }
39                 } elsif (m/Date:\s+([0-9-\+: ]+)/ ) {
40                         $date_commit->{$1} = $repo;
41                         warn "# $repo $1" if $debug;
42                         $commit->{$repo} .= $_;
43                 } else {
44                         $commit->{$repo} .= $_;
45                         warn "## $repo $. ",dump($_) if $debug;
46                 }
47         }
48 }
49
50 foreach my $repo ( @repos ) {
51         $ENV{PAGER} = 'cat';
52         open(my $r, '-|', "git -C $repo log --date=iso --color @ARGV");
53         $fh->{$repo} = $r;
54         get_commit $repo;
55 }
56
57
58 while(1) {
59         #warn "# date_commit = ",dump($date_commit);
60         my $date = ( sort { $b cmp $a } keys %$date_commit )[0];
61         my $repo = $date_commit->{$date};
62         print "# $date $repo\n$commit->{$repo}";
63         $commit->{$repo} = $commit_next->{$repo};
64         delete $date_commit->{$date};
65         get_commit $repo;
66 }