123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373 |
- use strict;
- use POSIX qw(strftime);
- use Text::Wrap qw(wrap);
- use FileHandle;
- use constant EMPTY_LOG_MESSAGE => '*** empty log message ***';
- my %month = (
- 'Jan'=>0,
- 'Feb'=>1,
- 'Mar'=>2,
- 'Apr'=>3,
- 'May'=>4,
- 'Jun'=>5,
- 'Jul'=>6,
- 'Aug'=>7,
- 'Sep'=>8,
- 'Oct'=>9,
- 'Nov'=>10,
- 'Dec'=>11,
- );
- my $fh = new FileHandle;
- sub key_ready
- {
- my ($rin, $nfd);
- vec($rin, fileno(STDIN), 1) = 1;
- return $nfd = select($rin, undef, undef, 0);
- }
- sub strptime {
- my $str = shift;
- return undef if not defined $str;
-
-
-
-
-
- my @date;
- if ($str =~ /([[:alpha:]]{3})\s+([[:alpha:]]{3})\s+([[:digit:]]{1,2})\s+([[:digit:]]{1,2}):([[:digit:]]{1,2}):([[:digit:]]{1,2})\s+([[:digit:]]{4})/){
- push(@date,$6,$5,$4,$3,$month{$2},($7 - 1900),-1,-1,-1);
- } else {
- die ("Cannot parse date '$str'\n'");
- }
- return @date;
- }
- sub mywrap {
- my ($indent1, $indent2, @text) = @_;
-
- my $text = Text::Wrap::wrap($indent1, $indent2, @text);
- if ( grep /^\s+/m, @text ) {
- return $text;
- }
- my @lines = split /\n/, $text;
- $indent2 =~ s!^((?: {8})+)!"\t" x (length($1)/8)!e;
- $lines[0] =~ s/^$indent1\s+/$indent1/;
- s/^$indent2\s+/$indent2/
- for @lines[1..$#lines];
- my $newtext = join "\n", @lines;
- $newtext .= "\n"
- if substr($text, -1) eq "\n";
- return $newtext;
- }
- sub last_line_len {
- my $files_list = shift;
- my @lines = split (/\n/, $files_list);
- my $last_line = pop (@lines);
- return length ($last_line);
- }
- sub wrap_log_entry {
- my $text = shift;
- my $left_pad_str = shift;
-
- my $length_remaining = shift;
- my $max_line_length = shift;
- my $wrapped_text = '';
- my $user_indent = '';
- my $first_time = 1;
- my $suppress_line_start_match = 0;
- my @lines = split (/\n/, $text);
- while (@lines)
- {
- my $this_line = shift (@lines);
- chomp $this_line;
- if ($this_line =~ /^(\s+)/) {
- $user_indent = $1;
- }
- else {
- $user_indent = '';
- }
-
- if ($suppress_line_start_match)
- {
- $suppress_line_start_match = 0;
- }
- elsif (($this_line =~ /^(\s*)\*\s+[a-zA-Z0-9]/)
- || ($this_line =~ /^(\s*)\* [a-zA-Z0-9_\.\/\+-]+/)
- || ($this_line =~ /^(\s*)\([a-zA-Z0-9_\.\/\+-]+(\)|,\s*)/)
- || ($this_line =~ /^(\s+)(\S+)/)
- || ($this_line =~ /^(\s*)- +/)
- || ($this_line =~ /^()\s*$/)
- || ($this_line =~ /^(\s*)\*\) +/)
- || ($this_line =~ /^(\s*)[a-zA-Z0-9](\)|\.|\:) +/))
- {
- $length_remaining = $max_line_length - (length ($user_indent));
- }
-
-
- $this_line =~ s/^\s*//;
-
- my $this_len = length ($this_line);
- if ($this_len == 0)
- {
-
- $user_indent = '';
- $length_remaining = $max_line_length;
- }
- elsif ($this_len >= $length_remaining)
- {
-
-
- my $idx = $length_remaining - 1;
- if ($idx < 0) { $idx = 0 };
- while ($idx > 0)
- {
- if (substr ($this_line, $idx, 1) =~ /\s/)
- {
- my $line_now = substr ($this_line, 0, $idx);
- my $next_line = substr ($this_line, $idx);
- $this_line = $line_now;
-
- chomp $this_line;
-
- $this_line .= "\n${left_pad_str}";
-
- $length_remaining = $max_line_length - (length ($user_indent));
-
- $next_line =~ s/^\s*//;
-
-
-
-
- my $next_next_line = shift (@lines);
- if ((defined ($next_next_line)) && ($next_next_line =~ /^(\s+)/)) {
- $next_line = $1 . $next_line if (defined ($1));
-
- $next_next_line =~ s/^\s*//;
- }
- else {
- $next_line = $user_indent . $next_line;
- }
- if (defined ($next_next_line)) {
- unshift (@lines, $next_next_line);
- }
- unshift (@lines, $next_line);
-
-
-
- $suppress_line_start_match = 1;
- last;
- }
- else
- {
- $idx--;
- }
- }
- if ($idx == 0)
- {
-
-
-
-
- if ($length_remaining == ($max_line_length - (length ($user_indent))))
- {
-
-
-
- $this_line = "\n${left_pad_str}${this_line}";
- }
- else
- {
-
- unshift (@lines, $this_line);
- $length_remaining = $max_line_length - (length ($user_indent));
- $this_line = "\n${left_pad_str}";
- }
- }
- }
- else
- {
-
- $length_remaining = $length_remaining - $this_len;
- if ($this_line =~ /\.$/)
- {
- $this_line .= " ";
- $length_remaining -= 2;
- }
- else
- {
- $this_line .= " ";
- $length_remaining -= 1;
- }
- }
-
- $first_time = 0;
- $wrapped_text .= "${user_indent}${this_line}";
- }
-
- $wrapped_text .= "\n";
- return $wrapped_text;
- }
- my @date;
- my $author;
- my @files;
- my $comment;
- my $state;
- my $done = 0;
- $state = 0;
- if (key_ready())
- {
-
-
- $fh->fdopen(*STDIN, 'r');
- }
- else
- {
- $fh->open("LC_ALL=C git log --pretty --numstat --summary . |")
- or die("Cannot execute git log...$!\n");
- }
- while (my $_l = <$fh>) {
-
- if ($state == 0) {
- if ($_l =~ m,^Author: (.*),) {
- $author = $1;
- }
- if ($_l =~ m,^Date: (.*),) {
- @date = strptime($1);
- }
- $state = 1 if ($_l =~ m,^$, and $author and (@date+0>0));
- } elsif ($state == 1) {
-
-
-
-
-
- my $_s = $_l;
- $_s =~ s/^ //g;
- if ($_s =~ m/^Merge branch/)
- {
- $state=0;
- next;
- }
- $comment = $comment . $_s;
- $state = 2 if ($_l =~ m,^$,);
- } elsif ($state == 2) {
- if ($_l =~ m,^([0-9]+)\t([0-9]+)\t(.*)$,) {
- push @files, $3;
- }
- $done = 1 if ($_l =~ m,^$,);
- }
- if ($done) {
- print (strftime "%Y-%m-%d $author\n\n", @date);
- my $files = join (", ", @files);
- $files = mywrap ("\t", "\t", "* $files"), ": ";
- if (index($comment, EMPTY_LOG_MESSAGE) > -1 ) {
- $comment = "[no log message]\n";
- }
- my $files_last_line_len = 0;
- $files_last_line_len = last_line_len($files) + 1;
- my $msg = wrap_log_entry($comment, "\t", 69-$files_last_line_len, 69);
- $msg =~ s/[ \t]+\n/\n/g;
- print "$files: $msg\n";
- @date = ();
- $author = "";
- @files = ();
- $comment = "";
- $state = 0;
- $done = 0;
- }
- }
- if (@date + 0)
- {
- print (strftime "%Y-%m-%d $author\n\n", @date);
- my $msg = wrap_log_entry($comment, "\t", 69, 69);
- $msg =~ s/[ \t]+\n/\n/g;
- print "\t* $msg\n";
- }
|