forked from mdamt/gfxboot-blankon
-
Notifications
You must be signed in to change notification settings - Fork 1
/
git2log
executable file
·163 lines (122 loc) · 2.94 KB
/
git2log
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
156
157
158
159
160
161
#! /usr/bin/perl
use Getopt::Long;
sub usage;
usage 0 if !@ARGV;
@deps = qw ( .git/HEAD .git/refs/heads .git/refs/tags );
GetOptions(
'help' => sub { usage 0 },
'version' => \$opt_version,
'update' => \$opt_update,
'log|changelog' => \$opt_log,
) || usage 1;
usage 1 if @ARGV > 1 || !($opt_log || $opt_version);
$opt_file = @ARGV ? shift : '-';
die "no git repo\n" unless -d ".git";
if($opt_update && $opt_file ne '-' && -f($opt_file)) {
$ok = 1;
$t = (stat $opt_file)[9];
for (@deps) {
$ok = 0 if (stat)[9] > $t;
}
exit 0 if $ok;
}
for (`git branch`) {
if(/^\*\s+(\S+)/) {
$branch = $1;
last;
}
}
$branch = "master" if $branch eq '(no';
die "no branch?\n" unless $branch;
# print STDERR "writing log for branch $branch\n";
@tags = `git tag`;
chomp @tags;
for (@tags) {
if(/^\d/) {
s/(\d+)/sprintf "%04d", $1/eg;
push @ntags, $_;
}
elsif(s/^$branch\-//) {
s/(\d+)/sprintf "%04d", $1/eg;
push @ntags, "$branch-$_";
}
}
@tags = sort @ntags;
die "no tags?\n" unless @tags;
if($opt_version) {
$t = $tags[-1];
$t =~ s/(\d+)/$1 + 0/eg;
if(`git log --pretty=medium --date=iso '$t..HEAD'`) {
$t =~ s/(\d+)$/$1 + 1/e;
}
$t =~ s/^$branch\-//;
open F, ">$opt_file";
print F "$t\n";
close F;
exit 0;
}
if($branch ne 'master') {
for ($i = 0; $i < @tags; $i++) {
if($tags[$i] =~ /^$branch\-(\S+)/) {
$i2 = $i;
$bi = $1;
last;
}
}
# print STDERR ">> $branch-$bi\n";
die "no tags in this branch?\n" unless $bi;
for ($i = 0; $i < $i2; $i++) {
if($tags[$i] eq $bi) {
$i1 = $i;
last;
}
}
splice @tags, $i1, $i2 - $i1;
}
map { s/(\d+)/$1 + 0/eg } @tags;
# for (@tags) { print "$_\n"; }
push @tags, "HEAD";
open F, ">$opt_file";
for ($i = @tags - 1; $i > 0; $i--) {
$date = undef;
@t = `git log --pretty=medium --date=iso '$tags[$i-1]..$tags[$i]'`;
chomp @t;
for (@t) {
if(/^Date:\s*(\S+)/) {
$date = $1;
last;
}
}
@t = grep { !/^(commit|Author:|Date:|Merge:|\s*$)|created.*tag/ } @t;
if(@t) {
# rewrite a bit to have it look more consistent
map { s/(fate|bnc)#/$1 #/g } @t;
map { s/\(#/(bnc #/g } @t;
map { s/bug\s*#/bnc #/g } @t;
map { s/feat(\.|ure)?\s*#?(\d+)/fate #$2/g } @t;
map { s/^ {4}// } @t;
map { s/^ {8}// } @t;
map { s/^([^-\s])/- $1/ } @t;
map { s/^- - /- / } @t;
map { s/^- \+\s+(- )?/- / } @t;
map { s/^/\t/ } @t;
$t = $tags[$i];
$t = "${branch}-$t" if $branch ne 'master' && $t eq "HEAD";
print F "$date:\t$t\n";
print F join("\n", @t), "\n\n";
}
}
close F;
sub usage
{
my $err = shift;
print <<" usage";
Usage: git2log [OPTIONS] [FILE]
Create changelog and project version from git repo.
--changelog Write changelog to FILE.
--version Write version number to FILE.
--update Write changelog or version only if FILE is outdated.
--help Print this help text.
usage
exit $err;
}