Mirror of strace – the linux syscall tracer
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

gitlog-to-changelog 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. eval '(exit $?0)' && eval 'exec perl -wS "$0" ${1+"$@"}'
  2. & eval 'exec perl -wS "$0" $argv:q'
  3. if 0;
  4. # Convert git log output to ChangeLog format.
  5. my $VERSION = '2014-11-20 17:25'; # UTC
  6. # The definition above must lie within the first 8 lines in order
  7. # for the Emacs time-stamp write hook (at end) to update it.
  8. # If you change this file with Emacs, please let the write hook
  9. # do its job. Otherwise, update this string manually.
  10. # Copyright (C) 2008-2014 Free Software Foundation, Inc.
  11. # This program is free software: you can redistribute it and/or modify
  12. # it under the terms of the GNU General Public License as published by
  13. # the Free Software Foundation, either version 3 of the License, or
  14. # (at your option) any later version.
  15. # This program is distributed in the hope that it will be useful,
  16. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. # GNU General Public License for more details.
  19. # You should have received a copy of the GNU General Public License
  20. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. # Written by Jim Meyering
  22. use strict;
  23. use warnings;
  24. use Getopt::Long;
  25. use POSIX qw(strftime);
  26. (my $ME = $0) =~ s|.*/||;
  27. # use File::Coda; # http://meyering.net/code/Coda/
  28. END {
  29. defined fileno STDOUT or return;
  30. close STDOUT and return;
  31. warn "$ME: failed to close standard output: $!\n";
  32. $? ||= 1;
  33. }
  34. sub usage ($)
  35. {
  36. my ($exit_code) = @_;
  37. my $STREAM = ($exit_code == 0 ? *STDOUT : *STDERR);
  38. if ($exit_code != 0)
  39. {
  40. print $STREAM "Try '$ME --help' for more information.\n";
  41. }
  42. else
  43. {
  44. print $STREAM <<EOF;
  45. Usage: $ME [OPTIONS] [ARGS]
  46. Convert git log output to ChangeLog format. If present, any ARGS
  47. are passed to "git log". To avoid ARGS being parsed as options to
  48. $ME, they may be preceded by '--'.
  49. OPTIONS:
  50. --amend=FILE FILE maps from an SHA1 to perl code (i.e., s/old/new/) that
  51. makes a change to SHA1's commit log text or metadata.
  52. --append-dot append a dot to the first line of each commit message if
  53. there is no other punctuation or blank at the end.
  54. --no-cluster never cluster commit messages under the same date/author
  55. header; the default is to cluster adjacent commit messages
  56. if their headers are the same and neither commit message
  57. contains multiple paragraphs.
  58. --srcdir=DIR the root of the source tree, from which the .git/
  59. directory can be derived.
  60. --since=DATE convert only the logs since DATE;
  61. the default is to convert all log entries.
  62. --until=DATE convert only the logs older than DATE.
  63. --format=FMT set format string for commit subject and body;
  64. see 'man git-log' for the list of format metacharacters;
  65. the default is '%s%n%b%n'
  66. --strip-tab remove one additional leading TAB from commit message lines.
  67. --strip-cherry-pick remove data inserted by "git cherry-pick";
  68. this includes the "cherry picked from commit ..." line,
  69. and the possible final "Conflicts:" paragraph.
  70. --help display this help and exit
  71. --version output version information and exit
  72. EXAMPLE:
  73. $ME --since=2008-01-01 > ChangeLog
  74. $ME -- -n 5 foo > last-5-commits-to-branch-foo
  75. SPECIAL SYNTAX:
  76. The following types of strings are interpreted specially when they appear
  77. at the beginning of a log message line. They are not copied to the output.
  78. Copyright-paperwork-exempt: Yes
  79. Append the "(tiny change)" notation to the usual "date name email"
  80. ChangeLog header to mark a change that does not require a copyright
  81. assignment.
  82. Co-authored-by: Joe User <user\@example.com>
  83. List the specified name and email address on a second
  84. ChangeLog header, denoting a co-author.
  85. Signed-off-by: Joe User <user\@example.com>
  86. These lines are simply elided.
  87. In a FILE specified via --amend, comment lines (starting with "#") are ignored.
  88. FILE must consist of <SHA,CODE+> pairs where SHA is a 40-byte SHA1 (alone on
  89. a line) referring to a commit in the current project, and CODE refers to one
  90. or more consecutive lines of Perl code. Pairs must be separated by one or
  91. more blank line.
  92. Here is sample input for use with --amend=FILE, from coreutils:
  93. 3a169f4c5d9159283548178668d2fae6fced3030
  94. # fix typo in title:
  95. s/all tile types/all file types/
  96. 1379ed974f1fa39b12e2ffab18b3f7a607082202
  97. # Due to a bug in vc-dwim, I mis-attributed a patch by Paul to myself.
  98. # Change the author to be Paul. Note the escaped "@":
  99. s,Jim .*>,Paul Eggert <eggert\\\@cs.ucla.edu>,
  100. EOF
  101. }
  102. exit $exit_code;
  103. }
  104. # If the string $S is a well-behaved file name, simply return it.
  105. # If it contains white space, quotes, etc., quote it, and return the new string.
  106. sub shell_quote($)
  107. {
  108. my ($s) = @_;
  109. if ($s =~ m![^\w+/.,-]!)
  110. {
  111. # Convert each single quote to '\''
  112. $s =~ s/\'/\'\\\'\'/g;
  113. # Then single quote the string.
  114. $s = "'$s'";
  115. }
  116. return $s;
  117. }
  118. sub quoted_cmd(@)
  119. {
  120. return join (' ', map {shell_quote $_} @_);
  121. }
  122. # Parse file F.
  123. # Comment lines (starting with "#") are ignored.
  124. # F must consist of <SHA,CODE+> pairs where SHA is a 40-byte SHA1
  125. # (alone on a line) referring to a commit in the current project, and
  126. # CODE refers to one or more consecutive lines of Perl code.
  127. # Pairs must be separated by one or more blank line.
  128. sub parse_amend_file($)
  129. {
  130. my ($f) = @_;
  131. open F, '<', $f
  132. or die "$ME: $f: failed to open for reading: $!\n";
  133. my $fail;
  134. my $h = {};
  135. my $in_code = 0;
  136. my $sha;
  137. while (defined (my $line = <F>))
  138. {
  139. $line =~ /^\#/
  140. and next;
  141. chomp $line;
  142. $line eq ''
  143. and $in_code = 0, next;
  144. if (!$in_code)
  145. {
  146. $line =~ /^([0-9a-fA-F]{40})$/
  147. or (warn "$ME: $f:$.: invalid line; expected an SHA1\n"),
  148. $fail = 1, next;
  149. $sha = lc $1;
  150. $in_code = 1;
  151. exists $h->{$sha}
  152. and (warn "$ME: $f:$.: duplicate SHA1\n"),
  153. $fail = 1, next;
  154. }
  155. else
  156. {
  157. $h->{$sha} ||= '';
  158. $h->{$sha} .= "$line\n";
  159. }
  160. }
  161. close F;
  162. $fail
  163. and exit 1;
  164. return $h;
  165. }
  166. # git_dir_option $SRCDIR
  167. #
  168. # From $SRCDIR, the --git-dir option to pass to git (none if $SRCDIR
  169. # is undef). Return as a list (0 or 1 element).
  170. sub git_dir_option($)
  171. {
  172. my ($srcdir) = @_;
  173. my @res = ();
  174. if (defined $srcdir)
  175. {
  176. my $qdir = shell_quote $srcdir;
  177. my $cmd = "cd $qdir && git rev-parse --show-toplevel";
  178. my $qcmd = shell_quote $cmd;
  179. my $git_dir = qx($cmd);
  180. defined $git_dir
  181. or die "$ME: cannot run $qcmd: $!\n";
  182. $? == 0
  183. or die "$ME: $qcmd had unexpected exit code or signal ($?)\n";
  184. chomp $git_dir;
  185. push @res, "--git-dir=$git_dir/.git";
  186. }
  187. @res;
  188. }
  189. {
  190. my $since_date;
  191. my $until_date;
  192. my $format_string = '%s%n%b%n';
  193. my $amend_file;
  194. my $append_dot = 0;
  195. my $cluster = 1;
  196. my $strip_tab = 0;
  197. my $strip_cherry_pick = 0;
  198. my $srcdir;
  199. GetOptions
  200. (
  201. help => sub { usage 0 },
  202. version => sub { print "$ME version $VERSION\n"; exit },
  203. 'since=s' => \$since_date,
  204. 'until=s' => \$until_date,
  205. 'format=s' => \$format_string,
  206. 'amend=s' => \$amend_file,
  207. 'append-dot' => \$append_dot,
  208. 'cluster!' => \$cluster,
  209. 'strip-tab' => \$strip_tab,
  210. 'strip-cherry-pick' => \$strip_cherry_pick,
  211. 'srcdir=s' => \$srcdir,
  212. ) or usage 1;
  213. defined $since_date
  214. and unshift @ARGV, "--since=$since_date";
  215. defined $until_date
  216. and unshift @ARGV, "--until=$until_date";
  217. # This is a hash that maps an SHA1 to perl code (i.e., s/old/new/)
  218. # that makes a correction in the log or attribution of that commit.
  219. my $amend_code = defined $amend_file ? parse_amend_file $amend_file : {};
  220. my @cmd = ('git',
  221. git_dir_option $srcdir,
  222. qw(log --log-size),
  223. '--pretty=format:%H:%ct %an <%ae>%n%n'.$format_string, @ARGV);
  224. open PIPE, '-|', @cmd
  225. or die ("$ME: failed to run '". quoted_cmd (@cmd) ."': $!\n"
  226. . "(Is your Git too old? Version 1.5.1 or later is required.)\n");
  227. my $prev_multi_paragraph;
  228. my $prev_date_line = '';
  229. my @prev_coauthors = ();
  230. while (1)
  231. {
  232. defined (my $in = <PIPE>)
  233. or last;
  234. $in =~ /^log size (\d+)$/
  235. or die "$ME:$.: Invalid line (expected log size):\n$in";
  236. my $log_nbytes = $1;
  237. my $log;
  238. my $n_read = read PIPE, $log, $log_nbytes;
  239. $n_read == $log_nbytes
  240. or die "$ME:$.: unexpected EOF\n";
  241. # Extract leading hash.
  242. my ($sha, $rest) = split ':', $log, 2;
  243. defined $sha
  244. or die "$ME:$.: malformed log entry\n";
  245. $sha =~ /^[0-9a-fA-F]{40}$/
  246. or die "$ME:$.: invalid SHA1: $sha\n";
  247. # If this commit's log requires any transformation, do it now.
  248. my $code = $amend_code->{$sha};
  249. if (defined $code)
  250. {
  251. eval 'use Safe';
  252. my $s = new Safe;
  253. # Put the unpreprocessed entry into "$_".
  254. $_ = $rest;
  255. # Let $code operate on it, safely.
  256. my $r = $s->reval("$code")
  257. or die "$ME:$.:$sha: failed to eval \"$code\":\n$@\n";
  258. # Note that we've used this entry.
  259. delete $amend_code->{$sha};
  260. # Update $rest upon success.
  261. $rest = $_;
  262. }
  263. # Remove lines inserted by "git cherry-pick".
  264. if ($strip_cherry_pick)
  265. {
  266. $rest =~ s/^\s*Conflicts:\n.*//sm;
  267. $rest =~ s/^\s*\(cherry picked from commit [\da-f]+\)\n//m;
  268. }
  269. my @line = split "\n", $rest;
  270. my $author_line = shift @line;
  271. defined $author_line
  272. or die "$ME:$.: unexpected EOF\n";
  273. $author_line =~ /^(\d+) (.*>)$/
  274. or die "$ME:$.: Invalid line "
  275. . "(expected date/author/email):\n$author_line\n";
  276. # Format 'Copyright-paperwork-exempt: Yes' as a standard ChangeLog
  277. # `(tiny change)' annotation.
  278. my $tiny = (grep (/^Copyright-paperwork-exempt:\s+[Yy]es$/, @line)
  279. ? ' (tiny change)' : '');
  280. my $date_line = sprintf "%s %s$tiny\n",
  281. strftime ("%F", localtime ($1)), $2;
  282. my @coauthors = grep /^Co-authored-by:.*$/, @line;
  283. # Omit meta-data lines we've already interpreted.
  284. @line = grep !/^(?:Signed-off-by:[ ].*>$
  285. |Co-authored-by:[ ]
  286. |Copyright-paperwork-exempt:[ ]
  287. )/x, @line;
  288. # Remove leading and trailing blank lines.
  289. if (@line)
  290. {
  291. while ($line[0] =~ /^\s*$/) { shift @line; }
  292. while ($line[$#line] =~ /^\s*$/) { pop @line; }
  293. }
  294. # Record whether there are two or more paragraphs.
  295. my $multi_paragraph = grep /^\s*$/, @line;
  296. # Format 'Co-authored-by: A U Thor <email@example.com>' lines in
  297. # standard multi-author ChangeLog format.
  298. for (@coauthors)
  299. {
  300. s/^Co-authored-by:\s*/\t /;
  301. s/\s*</ </;
  302. /<.*?@.*\..*>/
  303. or warn "$ME: warning: missing email address for "
  304. . substr ($_, 5) . "\n";
  305. }
  306. # If clustering of commit messages has been disabled, if this header
  307. # would be different from the previous date/name/email/coauthors header,
  308. # or if this or the previous entry consists of two or more paragraphs,
  309. # then print the header.
  310. if ( ! $cluster
  311. || $date_line ne $prev_date_line
  312. || "@coauthors" ne "@prev_coauthors"
  313. || $multi_paragraph
  314. || $prev_multi_paragraph)
  315. {
  316. $prev_date_line eq ''
  317. or print "\n";
  318. print $date_line;
  319. @coauthors
  320. and print join ("\n", @coauthors), "\n";
  321. }
  322. $prev_date_line = $date_line;
  323. @prev_coauthors = @coauthors;
  324. $prev_multi_paragraph = $multi_paragraph;
  325. # If there were any lines
  326. if (@line == 0)
  327. {
  328. warn "$ME: warning: empty commit message:\n $date_line\n";
  329. }
  330. else
  331. {
  332. if ($append_dot)
  333. {
  334. # If the first line of the message has enough room, then
  335. if (length $line[0] < 72)
  336. {
  337. # append a dot if there is no other punctuation or blank
  338. # at the end.
  339. $line[0] =~ /[[:punct:]\s]$/
  340. or $line[0] .= '.';
  341. }
  342. }
  343. # Remove one additional leading TAB from each line.
  344. $strip_tab
  345. and map { s/^\t// } @line;
  346. # Prefix each non-empty line with a TAB.
  347. @line = map { length $_ ? "\t$_" : '' } @line;
  348. print "\n", join ("\n", @line), "\n";
  349. }
  350. defined ($in = <PIPE>)
  351. or last;
  352. $in ne "\n"
  353. and die "$ME:$.: unexpected line:\n$in";
  354. }
  355. close PIPE
  356. or die "$ME: error closing pipe from " . quoted_cmd (@cmd) . "\n";
  357. # FIXME-someday: include $PROCESS_STATUS in the diagnostic
  358. # Complain about any unused entry in the --amend=F specified file.
  359. my $fail = 0;
  360. foreach my $sha (keys %$amend_code)
  361. {
  362. warn "$ME:$amend_file: unused entry: $sha\n";
  363. $fail = 1;
  364. }
  365. exit $fail;
  366. }
  367. # Local Variables:
  368. # mode: perl
  369. # indent-tabs-mode: nil
  370. # eval: (add-hook 'write-file-hooks 'time-stamp)
  371. # time-stamp-start: "my $VERSION = '"
  372. # time-stamp-format: "%:y-%02m-%02d %02H:%02M"
  373. # time-stamp-time-zone: "UTC"
  374. # time-stamp-end: "'; # UTC"
  375. # End: