Debian Codegraph @

src/tools/add_commit_links.pl

raw
1 #! /usr/bin/perl
2
3 #################################################################
4 # add_commit_links.pl -- add commit links to the release notes
5 #
6 # Copyright (c) 2024-2025, PostgreSQL Global Development Group
7 #
8 # src/tools/add_commit_links.pl
9 #################################################################
10
11 #
12 # This script adds commit links to the release notes.
13 #
14 # Usage: cd to top of source tree and issue
15 # src/tools/add_commit_links.pl release_notes_file
16 #
17 # The script can add links for release note items that lack them, and update
18 # those that have them. The script is sensitive to the release note file being
19 # in a specific format:
20 #
21 # * File name contains the major version number preceded by a dash
22 # and followed by a period
23 # * Commit text is generated by src/tools/git_changelog
24 # * SGML comments around commit text start in the first column
25 # * The commit item title ends with an attribution that ends with
26 # a closing parentheses
27 # * previously added URL link text is unmodified
28 # * a "<para>" follows the commit item title
29 #
30 # The major version number is used to select the commit hash for minor
31 # releases. An error will be generated if valid commits are found but
32 # no proper location for the commit links is found.
33
34 use strict;
35 use warnings FATAL => 'all';
36
37 sub process_file
38 {
39 my $file = shift;
40
41 my $in_comment = 0;
42 my $prev_line_ended_with_paren = 0;
43 my $prev_leading_space = '';
44 my $lineno = 0;
45
46 my @hashes = ();
47
48 my $tmpfile = $file . '.tmp';
49
50 # Get major version number from the file name.
51 $file =~ m/-(\d+)\./;
52 my $major_version = $1;
53
54 open(my $fh, '<', $file) || die "could not open file $file: $!\n";
55 open(my $tfh, '>', $tmpfile) || die "could not open file $tmpfile: $!\n";
56
57 while (<$fh>)
58 {
59 $lineno++;
60
61 $in_comment = 1 if (m/^<!--/);
62
63 # skip over commit links because we will add them below
64 next
65 if (!$in_comment
66 && m{^\s*<ulink url="&commit_baseurl;[[:xdigit:]]+">&sect;</ulink>\s*$}
67 );
68
69 if ($in_comment && m/\[([[:xdigit:]]+)\]/)
70 {
71 my $hash = $1;
72
73 # major release item
74 (!m/^Branch:/) && push(@hashes, $hash);
75
76 # minor release item
77 m/^Branch:/
78 && defined($major_version)
79 && m/_${major_version}_/
80 && push(@hashes, $hash);
81 }
82
83 if (!$in_comment && m{</para>})
84 {
85 if (@hashes)
86 {
87 if ($prev_line_ended_with_paren)
88 {
89 for my $hash (@hashes)
90 {
91 print $tfh
92 "$prev_leading_space<ulink url=\"&commit_baseurl;$hash\">&sect;</ulink>\n";
93 }
94 @hashes = ();
95 }
96 else
97 {
98 print
99 "hashes found but no matching text found for placement on line $lineno\n";
100 exit(1);
101 }
102 }
103 }
104
105 print $tfh $_;
106
107 $prev_line_ended_with_paren = m/\)\s*$/;
108
109 m/^(\s*)/;
110 $prev_leading_space = $1;
111
112 $in_comment = 0 if (m/^-->/);
113 }
114
115 close($fh);
116 close($tfh);
117
118 rename($tmpfile, $file) || die "could not rename %s to %s: $!\n",
119 $tmpfile,
120 $file;
121
122 return;
123 }
124
125 if (@ARGV == 0)
126 {
127 printf(STDERR "Usage: %s release_notes_file [...]\n", $0);
128 exit(1);
129 }
130
131 for my $file (@ARGV)
132 {
133 process_file($file);
134 }