Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sudo-project
GitHub Repository: sudo-project/sudo
Path: blob/main/scripts/log2cl.pl
1532 views
1
#!/usr/bin/env perl
2
#
3
# SPDX-License-Identifier: ISC
4
#
5
# Copyright (c) 2017, 2020 Todd C. Miller <[email protected]>
6
#
7
# Permission to use, copy, modify, and distribute this software for any
8
# purpose with or without fee is hereby granted, provided that the above
9
# copyright notice and this permission notice appear in all copies.
10
#
11
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18
#
19
# Simple script to massage "git log" output into a GNU style ChangeLog.
20
# The goal is to emulate "hg log --template=changelog" via perl format.
21
22
use Getopt::Std;
23
use Text::Wrap;
24
use strict;
25
use warnings;
26
27
# Git log format: author date, author name, author email
28
# abbreviated commit hash
29
# raw commit body
30
my $format="%ad %aN <%aE>%n%h%n%B%n";
31
32
# Parse options and build up "git log" command
33
my @cmd = ( "git" );
34
my %opts;
35
getopts('mR:', \%opts);
36
push(@cmd, "--git-dir", $opts{"R"}) if exists $opts{"R"};
37
push(@cmd, "log", "--log-size", "--name-only", "--date=short", "--format=$format", @ARGV);
38
39
open(LOG, '-|', @cmd) || die "$0: unable to run git log: $!";
40
41
my $hash;
42
my $body;
43
my @files;
44
my $key_date = "";
45
my $log_size = 0;
46
my @lines;
47
my $hash_link = "https://git.sudo.ws/sudo/commit/?id=";
48
49
# Wrap like "hg log --template=changelog"
50
$Text::Wrap::columns = 77;
51
# Don't preserve tabs
52
$Text::Wrap::unexpand = 0;
53
54
while (<LOG>) {
55
chomp;
56
if (/^log size (\d+)$/) {
57
$log_size = $1;
58
59
# Print previous entry if there is one
60
print_entry($hash, $body, @files) if defined($hash);
61
62
# Init new entry
63
undef $hash;
64
undef $body;
65
undef @files;
66
undef @lines;
67
68
# Read entry and split on newlines
69
read(LOG, my $buf, $log_size) ||
70
die "$0: unable to read $log_size bytes: $!\n";
71
@lines = split(/\r?\n/, $buf);
72
73
# Check for continued entry (duplicate Date + Author)
74
$_ = shift(@lines);
75
# Strip author email address for markdown
76
s/\s*<[^>]+>$// if exists $opts{'m'};
77
78
if ($_ ne $key_date) {
79
# New entry
80
print "$_\n\n";
81
$key_date = $_;
82
}
83
84
# Hash comes first
85
$hash = shift(@lines);
86
87
# Commit message body (multi-line)
88
my $sep = "";
89
foreach (@lines) {
90
last if $_ eq "--HG--";
91
if ($_ eq "") {
92
$sep = "\n\n";
93
next;
94
}
95
s/^\s+//;
96
s/\s+$//;
97
$body .= ${sep} . $_;
98
$sep = " ";
99
}
100
} else {
101
# Not a log entry, must be the file list
102
push(@files, $_) unless $_ eq "";
103
}
104
}
105
106
# Print the last entry
107
print_entry($hash, $body, @files) if defined($hash);
108
109
exit(0);
110
111
sub print_entry
112
{
113
if (exists $opts{'m'}) {
114
print_entry_markdown(@_);
115
} else {
116
print_entry_plain(@_);
117
}
118
}
119
120
sub print_entry_plain
121
{
122
my $hash = shift;
123
my $body = shift;
124
my $files = "* " . join(", ", @_) . ":";
125
126
print wrap("\t", "\t", $files) . "\n";
127
print fill("\t", "\t", $body) . "\n";
128
print "\t[$hash]\n\n";
129
}
130
131
sub print_entry_markdown
132
{
133
my $hash = shift;
134
my $body = shift;
135
my $files = ": * " . join(", ", @_) . ": ";
136
137
# Obfuscate email addresses in body
138
$body =~ s/([^@ ]+@)[\w\.-]+\.(com|org|edu|ws|io)/$1.../g;
139
140
# Escape email chars in body
141
$body =~ s/([@<>])/\\$1/g;
142
143
# Expand GitHub issue and bugzilla links
144
$body =~ s@(GitHub issue #)(\d+)@[$1$2](https://github.com/sudo-project/sudo/issues/$2)@;
145
$body =~ s@(Bug #)(\d+)@[$1$2](https://bugzilla.sudo.ws/show_bug.cgi?id=$2)@;
146
147
print wrap("", " ", $files) . "\n";
148
print fill(" ", " ", $body) . "\n";
149
print " [[${hash}]](${hash_link}${hash})\n\n";
150
}
151
152