4 # src/tools/git_changelog
6 # Display all commits on active branches, merging together commits from
7 # different branches that occur close together in time and with identical
10 # Most of the time, matchable commits occur in the same order on all branches,
11 # and we print them out in that order. However, if commit A occurs before
12 # commit B on branch X and commit B occurs before commit A on branch Y, then
13 # there's no ordering which is consistent with both branches.
15 # When we encounter a situation where there's no single "best" commit to
16 # print next, we print the one that involves the least distortion of the
17 # commit order, summed across all branches. In the event of a tie on the
18 # distortion measure (which is actually the common case: normally, the
19 # distortion is zero), we choose the commit with latest timestamp. If
20 # that's a tie too, the commit from the newer branch prints first.
29 # Adjust this list when the set of active branches changes.
30 my @BRANCHES = qw(master REL9_0_STABLE REL8_4_STABLE REL8_3_STABLE
31 REL8_2_STABLE REL8_1_STABLE REL8_0_STABLE REL7_4_STABLE);
33 # Might want to make this parameter user-settable.
34 my $timestamp_slop = 600;
37 Getopt::Long::GetOptions('since=s' => \$since) || usage();
40 my @git = qw(git log --date=iso);
41 push @git, '--since=' . $since if defined $since;
44 my %all_commits_by_branch;
46 for my $branch (@BRANCHES) {
48 IPC::Open2::open2(my $git_out, my $git_in, @git, "origin/$branch")
49 || die "can't run @git origin/$branch: $!";
52 while (my $line = <$git_out>) {
53 if ($line =~ /^commit\s+(.*)/) {
54 push_commit(\%commit) if %commit;
59 'commitnum' => $commitnum++,
62 elsif ($line =~ /^Author:\s+(.*)/) {
63 $commit{'author'} = $1;
65 elsif ($line =~ /^Date:\s+(.*)/) {
68 elsif ($line =~ /^\s\s/) {
69 $commit{'message'} .= $line;
72 push_commit(\%commit) if %commit;
74 my $child_exit_status = $? >> 8;
75 die "@git origin/$branch failed" if $child_exit_status != 0;
79 for my $branch (@BRANCHES) {
80 $position{$branch} = 0;
87 for my $branch (@BRANCHES) {
88 my $leader = $all_commits_by_branch{$branch}->[$position{$branch}];
89 next if !defined $leader;
91 for my $branch2 (@BRANCHES) {
92 if (defined $leader->{'branch_position'}{$branch2}) {
93 $inversions += $leader->{'branch_position'}{$branch2}
94 - $position{$branch2};
97 if (!defined $best_inversions ||
98 $inversions < $best_inversions ||
99 ($inversions == $best_inversions &&
100 $leader->{'timestamp'} > $best_timestamp)) {
101 $best_branch = $branch;
102 $best_inversions = $inversions;
103 $best_timestamp = $leader->{'timestamp'};
106 last if !defined $best_branch;
108 $all_commits_by_branch{$best_branch}->[$position{$best_branch}];
109 print $winner->{'header'};
110 print "Commit-Order-Inversions: $best_inversions\n"
111 if $best_inversions != 0;
113 print $winner->{'message'};
115 $winner->{'done'} = 1;
116 for my $branch (@BRANCHES) {
117 my $leader = $all_commits_by_branch{$branch}->[$position{$branch}];
118 if (defined $leader && $leader->{'done'}) {
119 ++$position{$branch};
127 my $ht = hash_commit($c);
128 my $ts = parse_datetime($c->{'date'});
130 for my $candidate (@{$all_commits{$ht}}) {
131 if (abs($ts - $candidate->{'timestamp'}) < $timestamp_slop
132 && !exists $candidate->{'branch_position'}{$c->{'branch'}})
140 'header' => sprintf("Author: %s\n", $c->{'author'}),
141 'message' => $c->{'message'},
144 push @{$all_commits{$ht}}, $cc;
146 $cc->{'header'} .= sprintf "Branch: %s [%s] %s\n",
147 $c->{'branch'}, substr($c->{'commit'}, 0, 9), $c->{'date'};
148 push @{$all_commits_by_branch{$c->{'branch'}}}, $cc;
149 $cc->{'branch_position'}{$c->{'branch'}} =
150 -1+@{$all_commits_by_branch{$c->{'branch'}}};
155 return $c->{'author'} . "\0" . $c->{'message'};
160 $dt =~ /^(\d\d\d\d)-(\d\d)-(\d\d)\s+(\d\d):(\d\d):(\d\d)\s+([-+])(\d\d)(\d\d)$/;
161 my $gm = Time::Local::timegm($6, $5, $4, $3, $2-1, $1);
162 my $tzoffset = ($8 * 60 + $9) * 60;
163 $tzoffset = - $tzoffset if $7 eq '-';
164 return $gm - $tzoffset;
169 Usage: git_changelog [--since=SINCE]