| 1 |
#! /usr/bin/perl |
| 2 |
|
| 3 |
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 4 |
# |
| 5 |
# This script is maintained at https://github.com/openSUSE/linuxrc-devtools |
| 6 |
# |
| 7 |
# If you're in another project, this is just a copy. |
| 8 |
# You may update it to the latest version from time to time... |
| 9 |
# |
| 10 |
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 11 |
|
| 12 |
use strict; |
| 13 |
|
| 14 |
use Getopt::Long; |
| 15 |
|
| 16 |
use Data::Dumper; |
| 17 |
$Data::Dumper::Sortkeys = 1; |
| 18 |
$Data::Dumper::Terse = 1; |
| 19 |
$Data::Dumper::Indent = 1; |
| 20 |
|
| 21 |
sub usage; |
| 22 |
sub changelog_outdated; |
| 23 |
sub get_github_project; |
| 24 |
sub get_version; |
| 25 |
sub get_tags; |
| 26 |
sub get_log; |
| 27 |
sub is_formatted_tag; |
| 28 |
sub get_branch; |
| 29 |
sub choose_tags; |
| 30 |
sub add_head_tag; |
| 31 |
sub tags_to_str; |
| 32 |
sub format_log; |
| 33 |
sub format_all_logs; |
| 34 |
sub fix_dates; |
| 35 |
sub add_line_breaks; |
| 36 |
sub format_date_obs; |
| 37 |
sub format_date_iso; |
| 38 |
sub raw_date_to_s; |
| 39 |
|
| 40 |
usage 0 if !@ARGV; |
| 41 |
|
| 42 |
my @changelog_deps = qw ( .git/HEAD .git/refs/heads .git/refs/tags ); |
| 43 |
|
| 44 |
my $branch; |
| 45 |
my $current_version; |
| 46 |
my @tags; |
| 47 |
my @all_tags; |
| 48 |
my $config; |
| 49 |
|
| 50 |
my $opt_log; |
| 51 |
my $opt_version; |
| 52 |
my $opt_branch; |
| 53 |
my $opt_update; |
| 54 |
my $opt_file; |
| 55 |
my $opt_start; |
| 56 |
my $opt_max; |
| 57 |
my $opt_width = 66; |
| 58 |
my $opt_width_fuzz = 8; |
| 59 |
my $opt_sep_width = 68; |
| 60 |
my $opt_format = 'internal'; # obs, internal |
| 61 |
my $opt_merge_msg_before = 1; # log auto generated pr merge message before the commit messages (vs. after) |
| 62 |
my $opt_join_author = 1; # join consecutive commit messages as long as they are by the same author |
| 63 |
my $opt_keep_date = 1; # don't join consecutive commit messages if they have different time stamps |
| 64 |
my $opt_default_email = '[email protected]'; # default email to use in changelog |
| 65 |
|
| 66 |
GetOptions( |
| 67 |
'help' => sub { usage 0 }, |
| 68 |
'version' => \$opt_version, |
| 69 |
'branch' => \$opt_branch, |
| 70 |
'update' => \$opt_update, |
| 71 |
'start=s' => \$opt_start, |
| 72 |
'format=s' => \$opt_format, |
| 73 |
'max=i' => \$opt_max, |
| 74 |
'width=i' => \$opt_width, |
| 75 |
'fuzz=i' => \$opt_width_fuzz, |
| 76 |
'merge-msg=s' => sub { $opt_merge_msg_before = ($_[1] eq 'after' ? 0 : 1) }, |
| 77 |
'join-author!' => \$opt_join_author, |
| 78 |
'keep-date!' => \$opt_keep_date, |
| 79 |
'log|changelog' => \$opt_log, |
| 80 |
'default-email=s' => \$opt_default_email, |
| 81 |
) || usage 1; |
| 82 |
|
| 83 |
# ensure we are used correctly |
| 84 |
usage 1 if @ARGV > 1 || !($opt_log || $opt_version || $opt_branch); |
| 85 |
$opt_file = @ARGV ? shift : '-'; |
| 86 |
|
| 87 |
die "no git repo\n" unless -d ".git"; |
| 88 |
|
| 89 |
# if update option has been give write changelog only if git refs are newer |
| 90 |
exit 0 if $opt_update && $opt_file ne '-' && -f($opt_file) && !changelog_outdated($opt_file); |
| 91 |
|
| 92 |
$opt_max = 2 if $opt_version || $opt_branch; |
| 93 |
|
| 94 |
# gather some data |
| 95 |
get_github_project; |
| 96 |
get_branch; |
| 97 |
get_log; |
| 98 |
fix_dates; |
| 99 |
get_tags; |
| 100 |
choose_tags; |
| 101 |
add_head_tag; |
| 102 |
get_version; |
| 103 |
|
| 104 |
# just print current branch |
| 105 |
if($opt_branch) { |
| 106 |
open my $f, ">$opt_file"; |
| 107 |
print $f $config->{branch} ? $config->{branch} : "master", "\n"; |
| 108 |
close $f; |
| 109 |
|
| 110 |
exit 0; |
| 111 |
} |
| 112 |
|
| 113 |
# just print current version |
| 114 |
if($opt_version) { |
| 115 |
my $old_version; |
| 116 |
|
| 117 |
if($opt_file ne '-' && open(my $f, $opt_file)) { |
| 118 |
chomp($old_version = <$f>); |
| 119 |
close $f; |
| 120 |
} |
| 121 |
|
| 122 |
if($config->{version} ne $old_version) { |
| 123 |
open my $f, ">$opt_file"; |
| 124 |
print $f "$config->{version}\n"; |
| 125 |
close $f; |
| 126 |
} |
| 127 |
|
| 128 |
exit 0; |
| 129 |
} |
| 130 |
|
| 131 |
# set start tag |
| 132 |
if($opt_start) { |
| 133 |
my $x = is_formatted_tag $opt_start; |
| 134 |
die "$opt_start: not a valid start tag\n" if !$x; |
| 135 |
$x->{branch} = $config->{branch} if !$x->{branch}; |
| 136 |
$config->{start} = $x; |
| 137 |
} |
| 138 |
|
| 139 |
format_all_logs; |
| 140 |
|
| 141 |
open my $f, ">$opt_file"; |
| 142 |
|
| 143 |
print $f $_->{formatted} for @{$config->{log}}; |
| 144 |
|
| 145 |
close $f; |
| 146 |
|
| 147 |
exit 0; |
| 148 |
|
| 149 |
|
| 150 |
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 151 |
# usage(exit_code) |
| 152 |
# |
| 153 |
# Print help message and exit. |
| 154 |
# - exit_code: exit code |
| 155 |
# |
| 156 |
# Function does not return. |
| 157 |
# |
| 158 |
sub usage |
| 159 |
{ |
| 160 |
my $err = shift; |
| 161 |
|
| 162 |
print <<" usage"; |
| 163 |
Usage: git2log [OPTIONS] [FILE] |
| 164 |
Create changelog and project version from git repo. |
| 165 |
--changelog Write changelog to FILE. |
| 166 |
--version Write version number to FILE. |
| 167 |
--branch Write current branch to FILE. |
| 168 |
--start START_TAG Start with tag START_TAG. |
| 169 |
--max N Write at most MAX long entries. |
| 170 |
--update Write changelog or version only if FILE is outdated. |
| 171 |
--format FORMAT Write log using FORMAT. Supported FORMATs are 'internal' (default) and 'obs'. |
| 172 |
--width WIDTH Reformat log entries to be max WIDTH chars wide. |
| 173 |
--fuzz FUZZ Allow log lines to be up to FUZZ chars longer as WIDTH to avoid |
| 174 |
line breaks leaving tiny bits on the last line. |
| 175 |
--merge-msg WHERE Log message about merges before or after the actual merge commit messages. |
| 176 |
Valid values for WHERE are 'after' and 'before' (default). |
| 177 |
--join-author Join consecutive commits as long as they are by the same author. (default) |
| 178 |
--no-join-author Keep consecutive commits by the same author separate. |
| 179 |
--keep-date Join consecutive commits only if they have the same date. (default) |
| 180 |
--no-keep-date Join consecutive commits even if dates differ. |
| 181 |
--default-email Use this email in changelog entries if no other suitable email could be |
| 182 |
determined (default: opensuse-packaging\@opensuse.org). |
| 183 |
--help Print this help text. |
| 184 |
usage |
| 185 |
|
| 186 |
exit $err; |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 191 |
# res = changelog_outdated(file) |
| 192 |
# |
| 193 |
# Return status of changelog file. |
| 194 |
# - file: changelog file name |
| 195 |
# - res: status |
| 196 |
# 1: file is newer than the last git repo change and should be updated |
| 197 |
# 0: file is still recent enough |
| 198 |
# |
| 199 |
# Relies on global var @changelog_deps. |
| 200 |
# |
| 201 |
sub changelog_outdated |
| 202 |
{ |
| 203 |
my $file = $_[0]; |
| 204 |
|
| 205 |
my $changelog_time = (stat $file)[9]; |
| 206 |
|
| 207 |
return 1 if !defined $changelog_time; |
| 208 |
|
| 209 |
for (@changelog_deps) { |
| 210 |
return 1 if (stat)[9] > $changelog_time; |
| 211 |
} |
| 212 |
|
| 213 |
return 0; |
| 214 |
} |
| 215 |
|
| 216 |
|
| 217 |
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 218 |
# get_github_project() |
| 219 |
# |
| 220 |
# Set $config->{github_project} to the github project name. |
| 221 |
# |
| 222 |
sub get_github_project |
| 223 |
{ |
| 224 |
if(`git config remote.origin.url` =~ m#github.com[:/]+(\S+/\S+)#) { |
| 225 |
$config->{github_project} = $1; |
| 226 |
$config->{github_project} =~ s/\.git$//; |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
|
| 231 |
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 232 |
# get_version() |
| 233 |
# |
| 234 |
# Set $config->{branch} and $config->{version} to the current branch and |
| 235 |
# version info. |
| 236 |
# |
| 237 |
# This might be taken directly from HEAD if HEAD is tagged or otherwise be |
| 238 |
# exprapolated from the most recent tag (cf. add_head_tag()). |
| 239 |
# |
| 240 |
sub get_version |
| 241 |
{ |
| 242 |
$config->{version} = "0.0"; |
| 243 |
|
| 244 |
my $tag = $config->{log}[0]{tags}[0]; |
| 245 |
|
| 246 |
if($tag->{version}) { |
| 247 |
$config->{version} = $tag->{version}; |
| 248 |
$config->{branch} = $tag->{branch}; |
| 249 |
} |
| 250 |
} |
| 251 |
|
| 252 |
|
| 253 |
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 254 |
# get_tags() |
| 255 |
# |
| 256 |
# Parse $config->{raw_log}, extract tag names, and split into per-tag |
| 257 |
# sections. |
| 258 |
# |
| 259 |
# Only tags recognized by is_formatted_tag() are considered. |
| 260 |
# |
| 261 |
# Tags inside merge commits are ignored. |
| 262 |
# |
| 263 |
# The parsed logs is stored in $config->{log}, an array of log sections. |
| 264 |
# Each section is a hash with these keys: |
| 265 |
# - 'tags': array of tags for this section |
| 266 |
# - 'commit': git commit id associated with these tags |
| 267 |
# - 'lines': git log lines |
| 268 |
# |
| 269 |
sub get_tags |
| 270 |
{ |
| 271 |
my $log_entry; |
| 272 |
|
| 273 |
# the end of the merge commit if in a merge |
| 274 |
my $merge; |
| 275 |
|
| 276 |
for (@{$config->{raw_log}}) { |
| 277 |
if(/^commit (\S+)( \((.*)\))?/) { |
| 278 |
my $commit = $1; |
| 279 |
my $tag_list = $3; |
| 280 |
my $xtag; |
| 281 |
|
| 282 |
# we have reached the end of the merge commit |
| 283 |
undef $merge if $merge && $commit =~ /^$merge/; |
| 284 |
|
| 285 |
# ignore tag info inside a merge commit |
| 286 |
$tag_list = "" if $merge; |
| 287 |
|
| 288 |
for my $t (split /, /, $tag_list) { |
| 289 |
if($t =~ /tag: (\S+)/) { |
| 290 |
my $tag = $1; |
| 291 |
my $x = is_formatted_tag $tag; |
| 292 |
push @$xtag, $x if $x; |
| 293 |
} |
| 294 |
} |
| 295 |
|
| 296 |
if($xtag) { |
| 297 |
if($log_entry) { |
| 298 |
push @{$config->{log}}, $log_entry; |
| 299 |
last if $opt_max && @{$config->{log}} >= $opt_max; |
| 300 |
} |
| 301 |
$log_entry = { commit => $commit, tags => $xtag }; |
| 302 |
} |
| 303 |
else { |
| 304 |
$log_entry = { commit => $commit } if !$log_entry; |
| 305 |
} |
| 306 |
} |
| 307 |
elsif(!$merge && /^Merge: (\S+)/) { |
| 308 |
# remember end of merge |
| 309 |
$merge = $1; |
| 310 |
} |
| 311 |
|
| 312 |
push @{$log_entry->{lines}}, $_ if $log_entry; |
| 313 |
} |
| 314 |
} |
| 315 |
|
| 316 |
|
| 317 |
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 318 |
# get_log() |
| 319 |
# |
| 320 |
# Read git log and store lines as array in $config->{raw_log} (trailing |
| 321 |
# newlines removed). |
| 322 |
# |
| 323 |
sub get_log |
| 324 |
{ |
| 325 |
chomp(@{$config->{raw_log}} = `git log --pretty=medium --date=raw --topo-order --decorate`); |
| 326 |
} |
| 327 |
|
| 328 |
|
| 329 |
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 330 |
# hash_ref = is_formatted_tag(tag_name) |
| 331 |
# |
| 332 |
# Parse tag and return hash ref with branch and version number parts or |
| 333 |
# undef if it doesn't match. |
| 334 |
# - tag_name: tag as string |
| 335 |
# - hash_ref: hash ref with internal tag representation (with keys 'branch' and 'version'). |
| 336 |
# |
| 337 |
# This expects tags of the form "VERSION" or "BRANCH-VERSION" where VERSION |
| 338 |
# consists of decimal numbers separated by dots '.' and BRANCH can be any |
| 339 |
# string. |
| 340 |
# (Note: it doesn't really have to be the name of an existing branch.) |
| 341 |
# |
| 342 |
# Tags not conforming to this convention are ignored. |
| 343 |
# |
| 344 |
sub is_formatted_tag |
| 345 |
{ |
| 346 |
if($_[0] =~ /^((.+)-)?((\d+\.)*\d+)$/) { |
| 347 |
return { branch => $2, version => $3 } |
| 348 |
} |
| 349 |
|
| 350 |
return undef; |
| 351 |
} |
| 352 |
|
| 353 |
|
| 354 |
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 355 |
# get_branch() |
| 356 |
# |
| 357 |
# Get currently active git branch and store in $config->{branch}. |
| 358 |
# |
| 359 |
# 'master' branch is represented by empty 'branch' key. |
| 360 |
# |
| 361 |
sub get_branch |
| 362 |
{ |
| 363 |
chomp(my $branch = `git rev-parse --abbrev-ref HEAD`); |
| 364 |
|
| 365 |
$branch = "" if $branch eq 'master'; |
| 366 |
|
| 367 |
$config->{branch} = $branch; |
| 368 |
} |
| 369 |
|
| 370 |
|
| 371 |
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 372 |
# res = tag_sort(a, b) |
| 373 |
# |
| 374 |
# Compare 2 tags. |
| 375 |
# - a, b: refs to tag hash |
| 376 |
# - res: -1, 0, 1 |
| 377 |
# |
| 378 |
# This is used when we have to decide between alternative tags. |
| 379 |
# (Prefer 'lesser' variant.) |
| 380 |
# |
| 381 |
sub tag_sort |
| 382 |
{ |
| 383 |
my ($x, $y); |
| 384 |
|
| 385 |
$x = length $a->{version}; |
| 386 |
$y = length $b->{version}; |
| 387 |
|
| 388 |
# longer version number first |
| 389 |
return $y <=> $x if $y <=> $x; |
| 390 |
|
| 391 |
return $a->{branch} cmp $b->{branch}; |
| 392 |
} |
| 393 |
|
| 394 |
|
| 395 |
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 396 |
# str = tag_to_str(tag_ref) |
| 397 |
# |
| 398 |
# Convert tag into string. |
| 399 |
# - tag_ref: ref to hash with 'branch' and 'version' keys |
| 400 |
# - str: string (e.g. "foo-1.44") |
| 401 |
# |
| 402 |
# 'master' branch is represented by missing/empty 'branch' key. |
| 403 |
# |
| 404 |
sub tag_to_str |
| 405 |
{ |
| 406 |
my $tag = $_[0]; |
| 407 |
my $str; |
| 408 |
|
| 409 |
$str = "$tag->{branch}-" if $tag->{branch} ne ""; |
| 410 |
$str .= $tag->{version}; |
| 411 |
|
| 412 |
return $str; |
| 413 |
} |
| 414 |
|
| 415 |
|
| 416 |
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 417 |
# str = tags_to_str(tag_array_ref) |
| 418 |
# |
| 419 |
# Convert array of tags into string. |
| 420 |
# - tag_array_ref: ref to array of tags |
| 421 |
# - str: string (e.g. "(tag1, tag2)" |
| 422 |
# |
| 423 |
# This function is used only internally for debugging. |
| 424 |
# |
| 425 |
sub tags_to_str |
| 426 |
{ |
| 427 |
my $tags = $_[0]; |
| 428 |
my $str; |
| 429 |
|
| 430 |
for my $t (@$tags) { |
| 431 |
$str .= ", " if $str; |
| 432 |
$str .= tag_to_str $t; |
| 433 |
} |
| 434 |
|
| 435 |
return "($str)"; |
| 436 |
} |
| 437 |
|
| 438 |
|
| 439 |
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 440 |
# choose_tags() |
| 441 |
# |
| 442 |
# Scan commit messages and extract tag & branch information. |
| 443 |
# |
| 444 |
# This stores the tag/branch info in $config->{log}[]{tags}. |
| 445 |
# |
| 446 |
sub choose_tags |
| 447 |
{ |
| 448 |
my $branch = $config->{branch}; |
| 449 |
|
| 450 |
for my $x (@{$config->{log}}) { |
| 451 |
# printf "# %s\n", tags_to_str($x->{tags}); |
| 452 |
|
| 453 |
# no tag info? -> ignore |
| 454 |
next if !$x->{tags}; |
| 455 |
|
| 456 |
# single tag? -> remember branch info |
| 457 |
if(@{$x->{tags}} == 1) { |
| 458 |
$branch = $x->{tags}[0]{branch}; |
| 459 |
next; |
| 460 |
} |
| 461 |
|
| 462 |
# several tags? -> choose one |
| 463 |
|
| 464 |
# any with current branch name? |
| 465 |
my @t = grep { $_->{branch} eq $branch } @{$x->{tags}}; |
| 466 |
|
| 467 |
# no? -> choose among all |
| 468 |
@t = @{$x->{tags}} if @t == 0; |
| 469 |
|
| 470 |
# prefer longest version number, then alphanumerically smallest branch name |
| 471 |
@t = sort tag_sort @t; |
| 472 |
|
| 473 |
$branch = $t[0]{branch}; |
| 474 |
$x->{tags} = [ $t[0] ]; |
| 475 |
|
| 476 |
# printf "X %s\n", tags_to_str($x->{tags}); |
| 477 |
} |
| 478 |
} |
| 479 |
|
| 480 |
|
| 481 |
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 482 |
# add_head_tag() |
| 483 |
# |
| 484 |
# Suggest tag for HEAD if there isn't one. |
| 485 |
# |
| 486 |
# Basically, use branch + version from most recent tag and increment version. |
| 487 |
# |
| 488 |
sub add_head_tag |
| 489 |
{ |
| 490 |
return if @{$config->{log}} < 2; |
| 491 |
|
| 492 |
# HEAD tagged already? |
| 493 |
return if $config->{log}[0]{tags}; |
| 494 |
|
| 495 |
# the first tagged commit if HEAD isn't tagged |
| 496 |
my $tag = { %{$config->{log}[1]{tags}[0]} }; |
| 497 |
|
| 498 |
# increment version |
| 499 |
$tag->{version} =~ s/(\d+)$/$1 + 1/e; |
| 500 |
|
| 501 |
$config->{log}[0]{tags}[0] = $tag; |
| 502 |
|
| 503 |
# remember that the tag was generated |
| 504 |
$config->{log}[0]{was_untagged} = 1; |
| 505 |
} |
| 506 |
|
| 507 |
|
| 508 |
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 509 |
# fix_dates() |
| 510 |
# |
| 511 |
# Adjust time stamps in entire git log. |
| 512 |
# |
| 513 |
# The time stamps of the git commits are not necessarily ordered by date. |
| 514 |
# But the generated changelog is required to have a strictly monotonic time. |
| 515 |
# |
| 516 |
# We do this by going through the log in reverse and rewriting any dates we |
| 517 |
# find whenever the date decreases. |
| 518 |
# |
| 519 |
# A minimum time difference of 1 second beween entries is maintained. |
| 520 |
# |
| 521 |
# Not very subtle but it works. |
| 522 |
# |
| 523 |
sub fix_dates |
| 524 |
{ |
| 525 |
my $last_date; |
| 526 |
|
| 527 |
for (reverse @{$config->{raw_log}}) { |
| 528 |
# e.g. "Date: 1443184889 +0200" |
| 529 |
if(/^(Date:\s+)(\S+)(\s+\S+)/) { |
| 530 |
if(defined $last_date && $2 < $last_date) { |
| 531 |
$_ = "$1$last_date$3\n"; |
| 532 |
} |
| 533 |
else { |
| 534 |
$last_date = $2; |
| 535 |
} |
| 536 |
|
| 537 |
# ensure a minimal time gap of 1 second |
| 538 |
$last_date += 1; |
| 539 |
} |
| 540 |
} |
| 541 |
} |
| 542 |
|
| 543 |
|
| 544 |
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 545 |
# format_all_logs() |
| 546 |
# |
| 547 |
# Format the entire git log. |
| 548 |
# |
| 549 |
# This is done for every code version individually (the log has already been |
| 550 |
# split accordingly). |
| 551 |
# |
| 552 |
# If $config->{start} is set, use this as starting point. Else format the |
| 553 |
# entire git log. |
| 554 |
# |
| 555 |
sub format_all_logs |
| 556 |
{ |
| 557 |
# check if start tag actually exists - if not, print nothing |
| 558 |
if($config->{start}) { |
| 559 |
my $tag_found; |
| 560 |
for (@{$config->{log}}) { |
| 561 |
$tag_found = 1, last if grep { tag_to_str($config->{start}) eq tag_to_str($_) } @{$_->{tags}}; |
| 562 |
} |
| 563 |
return if !$tag_found; |
| 564 |
} |
| 565 |
|
| 566 |
for (@{$config->{log}}) { |
| 567 |
if($config->{start}) { |
| 568 |
# stop if we meet the start tag |
| 569 |
last if grep { tag_to_str($config->{start}) eq tag_to_str($_) } @{$_->{tags}}; |
| 570 |
} |
| 571 |
format_log $_; |
| 572 |
} |
| 573 |
} |
| 574 |
|
| 575 |
|
| 576 |
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 577 |
# format_log(log) |
| 578 |
# |
| 579 |
# Format log messages. |
| 580 |
# - log: is an array ref with individual commits |
| 581 |
# |
| 582 |
# All commits belong to a specific code version (stored in $log->{tag}). |
| 583 |
# $log->{formatted} holds the result. |
| 584 |
# |
| 585 |
# The process is done in several individual steps, documented below in the code. |
| 586 |
# |
| 587 |
sub format_log |
| 588 |
{ |
| 589 |
my $log = $_[0]; |
| 590 |
|
| 591 |
my $merge; |
| 592 |
my $commit; |
| 593 |
my $commits; |
| 594 |
|
| 595 |
for (@{$log->{lines}}) { |
| 596 |
if(/^commit (\S+)/) { |
| 597 |
$commit = { ref => $1 }; |
| 598 |
push @{$commits}, $commit; |
| 599 |
|
| 600 |
if( |
| 601 |
$merge && |
| 602 |
$merge->{merge_end} eq substr($commit->{ref}, 0, length($merge->{merge_end})) |
| 603 |
) { |
| 604 |
undef $merge; |
| 605 |
} |
| 606 |
|
| 607 |
if($merge) { |
| 608 |
$commit->{merge_ref} = $merge->{ref}; |
| 609 |
$commit->{date} = $merge->{date}; |
| 610 |
$commit->{author} = $merge->{author}; |
| 611 |
# add to all commits so it's not lost when we re-arrange |
| 612 |
$commit->{merge_msg} = $merge->{msg}; |
| 613 |
} |
| 614 |
|
| 615 |
next; |
| 616 |
} |
| 617 |
|
| 618 |
if(/^Merge: (\S+)/ && !$merge) { |
| 619 |
if($commit) { |
| 620 |
$merge = { merge_end => $1, ref => $commit->{ref} } unless $merge; |
| 621 |
} |
| 622 |
next; |
| 623 |
} |
| 624 |
|
| 625 |
if(/^Date:\s+(\S.*)/) { |
| 626 |
$commit->{date} ||= $1 if $commit; |
| 627 |
$merge->{date} ||= $1 if $merge; |
| 628 |
next; |
| 629 |
} |
| 630 |
|
| 631 |
if(/^Author:\s+(\S.*)/) { |
| 632 |
$commit->{author} ||= $1 if $commit; |
| 633 |
$merge->{author} ||= $1 if $merge; |
| 634 |
next; |
| 635 |
} |
| 636 |
|
| 637 |
if($merge) { |
| 638 |
if(/^ Merge pull request (#\d+) from (\S+)/) { |
| 639 |
if($config->{github_project}) { |
| 640 |
push @{$merge->{msg}}, "merge gh#$config->{github_project}$1"; |
| 641 |
} |
| 642 |
else { |
| 643 |
push @{$merge->{msg}}, "merge pr $2"; |
| 644 |
} |
| 645 |
} |
| 646 |
elsif(/^ Merge branch '([^']+)'( into)?/) { |
| 647 |
push @{$merge->{msg}}, "merge branch $1" if $2 eq ""; |
| 648 |
} |
| 649 |
elsif(/^ Merge remote-tracking branch /) { |
| 650 |
# ignore |
| 651 |
} |
| 652 |
elsif(s/^ //) { |
| 653 |
push @{$commit->{lines}}, $_ unless /^# /; |
| 654 |
} |
| 655 |
} |
| 656 |
elsif($commit) { |
| 657 |
if(s/^ //) { |
| 658 |
push @{$commit->{lines}}, $_ unless /^# /; |
| 659 |
} |
| 660 |
} |
| 661 |
} |
| 662 |
|
| 663 |
# Note: the individual steps below work on the array @$commits and modify |
| 664 |
# its content. |
| 665 |
|
| 666 |
# step 1 |
| 667 |
# - if there are paragraphs starting with '@log@' or '@+log@' |
| 668 |
# - delete first paragraph (short summary) |
| 669 |
# - else |
| 670 |
# - keep only first paragraph |
| 671 |
# - if there is a paragraph starting with '@-log', delete entire log |
| 672 |
# - tag commits that have a '@log@' tag so we can delete untagged commits |
| 673 |
# belonging to the same merge commit later |
| 674 |
|
| 675 |
my $tagged_merges = {}; |
| 676 |
|
| 677 |
for my $commit (@$commits) { |
| 678 |
# skip leading empty lines |
| 679 |
for (@{$commit->{lines}}) { |
| 680 |
last if !/^\s*$/; |
| 681 |
shift @{$commit->{lines}}; |
| 682 |
} |
| 683 |
my $para_cnt = 0; |
| 684 |
my $delete_all = 0; |
| 685 |
my $delete_first = 0; |
| 686 |
for (@{$commit->{lines}}) { |
| 687 |
$para_cnt++ if $_ eq ""; |
| 688 |
$para_cnt = 0, $delete_first = 1 if /^\@\+log\@/; |
| 689 |
$delete_all = 1 if /^\@\-log\@/; |
| 690 |
if(/^\@log\@/) { |
| 691 |
$para_cnt = 0; |
| 692 |
$commit->{clear} = 1; |
| 693 |
$tagged_merges->{$commit->{merge_ref}} = 1 if $commit->{merge_ref} || $log->{was_untagged}; |
| 694 |
} |
| 695 |
$_ = undef if $para_cnt; |
| 696 |
} |
| 697 |
shift @{$commit->{lines}} if $delete_first; |
| 698 |
$commit->{lines} = [] if $delete_all; |
| 699 |
} |
| 700 |
|
| 701 |
# step 2 |
| 702 |
# - clean up tagged commits or commits belonging to tagged merges |
| 703 |
|
| 704 |
for my $commit (@$commits) { |
| 705 |
next unless $commit->{clear} || $tagged_merges->{$commit->{merge_ref}}; |
| 706 |
for (@{$commit->{lines}}) { |
| 707 |
last if /^\@\+?log\@/; |
| 708 |
$_ = undef; |
| 709 |
} |
| 710 |
} |
| 711 |
|
| 712 |
# step 3 |
| 713 |
# - join lines |
| 714 |
|
| 715 |
for my $commit (@$commits) { |
| 716 |
my $lines; |
| 717 |
my $line; |
| 718 |
|
| 719 |
for (@{$commit->{lines}}) { |
| 720 |
next if $_ eq ""; |
| 721 |
if( |
| 722 |
s/^\s*[+\-][\-\s]*// || |
| 723 |
s/^\@\+?log\@// || |
| 724 |
$line eq "" |
| 725 |
) { |
| 726 |
s/^\s*//; |
| 727 |
push @$lines, $line if $line ne ""; |
| 728 |
$line = $_; |
| 729 |
} |
| 730 |
else { |
| 731 |
s/^\s*//; |
| 732 |
$line .= " " if $line ne ""; |
| 733 |
$line .= $_; |
| 734 |
} |
| 735 |
} |
| 736 |
push @$lines, $line if $line ne ""; |
| 737 |
|
| 738 |
$commit->{formatted} = $lines if $lines; |
| 739 |
} |
| 740 |
|
| 741 |
# step 4 |
| 742 |
# - fix small glitches |
| 743 |
|
| 744 |
for my $commit (@$commits) { |
| 745 |
next unless $commit->{formatted}; |
| 746 |
for (@{$commit->{formatted}}) { |
| 747 |
s/(fate|bnc|bsc|boo)\s*(#\d+)/\L$1\E$2/ig; |
| 748 |
} |
| 749 |
} |
| 750 |
|
| 751 |
# step 5 |
| 752 |
# - add merge info at the top or bottom (depending on $opt_merge_msg_before) |
| 753 |
|
| 754 |
my $merge_logged; |
| 755 |
|
| 756 |
for my $commit ($opt_merge_msg_before ? reverse(@$commits) : @$commits) { |
| 757 |
next unless $commit->{formatted}; |
| 758 |
|
| 759 |
if($commit->{merge_ref} && !$merge_logged->{$commit->{merge_ref}}) { |
| 760 |
$merge_logged->{$commit->{merge_ref}} = 1; |
| 761 |
if($commit->{merge_msg}) { |
| 762 |
if($opt_merge_msg_before) { |
| 763 |
unshift @{$commit->{formatted}}, @{$commit->{merge_msg}}; |
| 764 |
} |
| 765 |
else { |
| 766 |
push @{$commit->{formatted}}, @{$commit->{merge_msg}}; |
| 767 |
} |
| 768 |
} |
| 769 |
} |
| 770 |
} |
| 771 |
|
| 772 |
# step 6 |
| 773 |
# - join commit messages with same author (optionally even with different dates) |
| 774 |
|
| 775 |
my $commit0; |
| 776 |
|
| 777 |
for my $commit (@$commits) { |
| 778 |
next if !$commit->{formatted}; |
| 779 |
$commit0 = $commit, next if !$commit0; |
| 780 |
|
| 781 |
if( |
| 782 |
# $commit->{merge_ref} eq $commit0->{merge_ref} && |
| 783 |
( |
| 784 |
$opt_join_author && ($commit->{author} eq $commit0->{author}) |
| 785 |
&& (!$opt_keep_date || $commit->{date} eq $commit0->{date}) |
| 786 |
) |
| 787 |
|| $opt_format eq 'internal' |
| 788 |
) { |
| 789 |
unshift @{$commit0->{formatted}}, @{$commit->{formatted}}; |
| 790 |
delete $commit->{formatted}; |
| 791 |
} |
| 792 |
else { |
| 793 |
$commit0 = $commit; |
| 794 |
} |
| 795 |
} |
| 796 |
|
| 797 |
# step 7 |
| 798 |
# - add version tag at the end of the first log entry |
| 799 |
|
| 800 |
for my $commit (@$commits) { |
| 801 |
next unless $commit->{formatted}; |
| 802 |
|
| 803 |
if($opt_format eq 'obs') { |
| 804 |
push @{$commit->{formatted}}, $log->{tags}[0]{version} if defined $log->{tags}[0]{version}; |
| 805 |
} |
| 806 |
else { |
| 807 |
# push @{$commit->{formatted}}, tag_to_str($log->{tags}[0]); |
| 808 |
} |
| 809 |
|
| 810 |
last; |
| 811 |
} |
| 812 |
|
| 813 |
# step 8 |
| 814 |
# - remove identical lines |
| 815 |
|
| 816 |
for my $commit (@$commits) { |
| 817 |
next unless $commit->{formatted}; |
| 818 |
my %k; |
| 819 |
$commit->{formatted} = [ grep { !$k{$_}++ } @{$commit->{formatted}} ] |
| 820 |
} |
| 821 |
|
| 822 |
# step 9 |
| 823 |
# - remove shortened lines (that match the beginning of other lines) |
| 824 |
|
| 825 |
for my $commit (@$commits) { |
| 826 |
next unless $commit->{formatted}; |
| 827 |
|
| 828 |
# return 1 if some other commit line starts with function arg |
| 829 |
my $is_substr = sub { |
| 830 |
my $str = $_[0]; |
| 831 |
$str =~ s/\s*…$//; # github likes to shorten lines with ' …' |
| 832 |
my $str_len = length $str; |
| 833 |
for (@{$commit->{formatted}}) { |
| 834 |
return 1 if $str_len < length($_) && $str eq substr($_, 0, $str_len); |
| 835 |
} |
| 836 |
|
| 837 |
return 0; |
| 838 |
}; |
| 839 |
|
| 840 |
$commit->{formatted} = [ grep { ! $is_substr->($_) } @{$commit->{formatted}} ] |
| 841 |
} |
| 842 |
|
| 843 |
# step 10 |
| 844 |
# - add line breaks |
| 845 |
|
| 846 |
for my $commit (@$commits) { |
| 847 |
next unless $commit->{formatted}; |
| 848 |
for (@{$commit->{formatted}}) { |
| 849 |
$_ = add_line_breaks $_; |
| 850 |
} |
| 851 |
} |
| 852 |
|
| 853 |
# step 11 |
| 854 |
# - generate final log message |
| 855 |
# |
| 856 |
# note: non-(open)suse email addresses are replaced by $opt_default_email |
| 857 |
|
| 858 |
my $formated_log; |
| 859 |
|
| 860 |
for my $commit (@$commits) { |
| 861 |
next unless $commit->{formatted} && @{$commit->{formatted}}; |
| 862 |
|
| 863 |
if($opt_format eq 'obs') { |
| 864 |
$formated_log .= "-" x $opt_sep_width . "\n"; |
| 865 |
$formated_log .= format_date_obs($commit->{date}); |
| 866 |
} |
| 867 |
else { |
| 868 |
$formated_log .= format_date_iso($commit->{date}); |
| 869 |
} |
| 870 |
if($opt_format eq 'obs') { |
| 871 |
my $auth = $commit->{author}; |
| 872 |
$auth =~ s/^.*<//; |
| 873 |
$auth =~ s/>.*$//; |
| 874 |
# replace non-suse e-mail addresses with a generic one |
| 875 |
if($auth !~ /\@(suse\.(com|cz|de)|opensuse\.org)$/) { |
| 876 |
$auth = $opt_default_email; |
| 877 |
} |
| 878 |
$formated_log .= " - $auth\n\n"; |
| 879 |
} |
| 880 |
else { |
| 881 |
$formated_log .= ":\t" . tag_to_str($log->{tags}[0]) . "\n"; |
| 882 |
} |
| 883 |
|
| 884 |
for (@{$commit->{formatted}}) { |
| 885 |
s/^/\t/mg if $opt_format eq 'internal'; |
| 886 |
$formated_log .= "$_\n"; |
| 887 |
} |
| 888 |
|
| 889 |
$formated_log .= "\n"; |
| 890 |
} |
| 891 |
|
| 892 |
$log->{formatted} = $formated_log; |
| 893 |
} |
| 894 |
|
| 895 |
|
| 896 |
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 897 |
# new_text = add_line_breaks(text) |
| 898 |
# |
| 899 |
# Add line breaks to text. |
| 900 |
# - text: some text |
| 901 |
# - new_text: same text, reformatted |
| 902 |
# |
| 903 |
# Lines are formatted to have a maximal length of $opt_width. If this causes |
| 904 |
# the last line to be shorter than $opt_width_fuzz, it is appended to the |
| 905 |
# previous line. |
| 906 |
# |
| 907 |
sub add_line_breaks |
| 908 |
{ |
| 909 |
my @words = split /\s+/, @_[0]; |
| 910 |
my $remaining_len = length(join '', @words); |
| 911 |
|
| 912 |
my $str = shift(@words); |
| 913 |
my $len = length $str; |
| 914 |
|
| 915 |
my $next_len; |
| 916 |
my $word_len; |
| 917 |
|
| 918 |
for (@words) { |
| 919 |
$word_len = length; |
| 920 |
$remaining_len -= $word_len; |
| 921 |
$next_len = $len + $word_len + 1; |
| 922 |
if( |
| 923 |
$next_len >= $opt_width && |
| 924 |
$next_len + $remaining_len + 1 >= $opt_width + $opt_width_fuzz |
| 925 |
) { |
| 926 |
$str .= "\n $_"; |
| 927 |
$len = $word_len; |
| 928 |
} |
| 929 |
else { |
| 930 |
$str .= " $_"; |
| 931 |
$len += $word_len + 1; |
| 932 |
} |
| 933 |
} |
| 934 |
|
| 935 |
return "- " . $str; |
| 936 |
} |
| 937 |
|
| 938 |
|
| 939 |
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 940 |
# seconds = raw_date_to_s(git_date) |
| 941 |
# |
| 942 |
# Convert git raw date to seconds. |
| 943 |
# - git_date: raw git format (e.g. "1443184889 +0200") |
| 944 |
# - seconds: the seconds part (e.g. "1443184889") |
| 945 |
# |
| 946 |
sub raw_date_to_s |
| 947 |
{ |
| 948 |
return (split / /, $_[0])[0]; |
| 949 |
} |
| 950 |
|
| 951 |
|
| 952 |
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 953 |
# date = format_date_obs(git_date) |
| 954 |
# |
| 955 |
# Convert git raw date to obs format. |
| 956 |
# - git_date: raw git format (e.g. "1443184889 +0200") |
| 957 |
# - date: obs format ("Fri Sep 25 12:41:29 UTC 2015") |
| 958 |
# |
| 959 |
sub format_date_obs |
| 960 |
{ |
| 961 |
my @d = gmtime(raw_date_to_s($_[0])); |
| 962 |
|
| 963 |
return |
| 964 |
qw(Sun Mon Tue Wed Thu Fri Sat)[$d[6]] . " " . |
| 965 |
qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec)[$d[4]] . " " . |
| 966 |
$d[3] . " " . |
| 967 |
sprintf("%02d:%02d:%02d", $d[2], $d[1], $d[0]) . " UTC " . |
| 968 |
(1900 + $d[5]); |
| 969 |
} |
| 970 |
|
| 971 |
|
| 972 |
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 973 |
# date = format_date_iso(git_date) |
| 974 |
# |
| 975 |
# Convert git raw date to iso format. |
| 976 |
# - git_date: raw git format (e.g. "1443184889 +0200") |
| 977 |
# - date: obs format ("2015-09-25") |
| 978 |
# |
| 979 |
sub format_date_iso |
| 980 |
{ |
| 981 |
my @d = gmtime(raw_date_to_s($_[0])); |
| 982 |
|
| 983 |
return sprintf("%04d-%02d-%02d", 1900 + $d[5], $d[4] + 1, $d[3]); |
| 984 |
} |