aboutsummaryrefslogtreecommitdiff
path: root/helpers/git-hist
blob: 8feed741e80ead240b0aa1fcf3ec51db86286824 (plain)
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#!/usr/bin/env perl

use strict;
use warnings;
use Cwd;

my (@data, @authors, @repos, @changed) = ((), (), (), ());
# Syntax: git-hist [author (default: whoami)] [number of weeks (default 10 / all 0)]";
my $author = `whoami`;
my $weeks = 10;
my $rootdir = getcwd;
my $totalcommits = 0;
if (scalar @ARGV) {
  foreach my $arg (@ARGV) {
    if ($arg =~ /^\d+$/) {
      $weeks = $arg;
    } else {
      $author = $arg;
    }
  }
}
chomp($author);

# Make sure there are repositories to use
if (-d ".git") {
  push @repos, ".";
} else {
  opendir(my $dh, ".") || die "can't open local dir: $!";
  @repos = grep { /^[^\.]/ && -d "$_/.git" } readdir($dh);
  closedir $dh;
}
die "Directory (or subs) not git repo\n" unless (scalar @repos);
&main();
exit;

#####################################################
sub main() {
  # Each week
  my $week = 0;
  while ($week <= $weeks) {
    # Adjust for non-zero week list
    $week++ if ($weeks != 0 and $week == 0);
    print "\rParsing week: $week" if $week;
    my $commits = 0;
    # For each repo
    for my $repo (@repos) {
      #print "REPO: $repo\n";
      my $log = &get_weeklog($repo, $author, $week);
      #print "LOG: [$log]\n";
      my $this = &get_commits($log);
      #print "COMMITS: $this\n";
      if ($this and ! &find($repo, \@changed)) {
        #print "ADDING REPO\n";
        push @changed, $repo;
      }
      &get_authors($log);
      $commits += $this;
    }
    push @data, $commits;
    last if ($weeks == 0);
    $week++;
  }
  print "\n\n";

  # Log
  &print_report();
}

######################
sub print_report() {
  return if ($weeks > 0 && scalar @data != $weeks);

  # Weekly histogram
  for my $week (0..$weeks-1) {
    printf("%02d week [%02d]: ", $week, $data[$week]);
    for (1..$data[$week]) {
      print "*";
    }
    print "\n";
  }

  # Summary
  print "\nTotal commits: ";
  printf("%3d", $totalcommits);
  print "\n Repositories: ";
  printf("%3d ", scalar @changed);
  &dump(\@changed);
  print "\n      Authors: ";
  printf("%3d ", scalar @authors);
  &dump(\@authors);
  print "\n";
}

######################
sub get_weeklog() {
  my ($repo, $author, $week) = @_;
  # Author
  my $cmd = "git shortlog -s -n ".
            "--author='$author' ";
  # And week (if available)
  if ($week) {
    my $prev = $week-1;
    $cmd .= "--since='$week.week' ".
            "--until='$prev.week'";
  }
  # Run command
  chdir($repo);
  my $log = `$cmd`;
  chdir($rootdir);
  return $log;
}

######################
sub get_commits() {
  my ($log) = @_;
  my $commits = 0;
  foreach my $line (split(/\n/, $log)) {
    my ($this) = ($line =~ /(\d+)/);
    $commits += $this;
  }
  $totalcommits += $commits;
  return $commits;
}

######################
sub get_authors() {
  my ($log) = @_;
  foreach my $line (split(/\n/, $log)) {
    my ($author) = ($line =~ /\d+\s+(\w.*)/);
    if (!&find($author, \@authors)) {
      push @authors, $author;
    }
  }
}

######################
sub find() {
  my ($elm, $array) = @_;
  foreach my $e (@$array) {
    return 1 if ($e eq $elm);
  }
  return 0;
}

######################
sub dump() {
  my ($array) = @_;
  return if (!scalar @{$array});
  my $out = "";
  foreach my $i (@$array) {
    $out .= "$i, ";
  }
  $out =~ s/, $//;
  print $out;
}