sj: Add -W/--wait option
[sunny256-utils.git] / convkeyw
blob3f6b7132d5da493ea2788d383bd562c22096b2d3
1 #!/usr/bin/env perl
3 #=======================================================================
4 # $Id$
5 # Converts Subversion/RCS/CVS keywords in text files in various ways.
6 # Optimised for use with Subversion at the moment, but also works with
7 # CVS and RCS.
9 # Character set: UTF-8
10 # License: GNU General Public License
11 # Author: Øyvind A. Holm <sunny@sunbase.org>
12 # This file is part of the svnutils package — http://svnutils.tigris.org
13 #=======================================================================
15 use strict;
16 use warnings;
17 use Getopt::Std;
19 our ($opt_c, $opt_D, $opt_h, $opt_S, $opt_s, $opt_V) =
20 ( 0, 0, 0, 0, 0, 0);
21 getopts('cDhSsV') || die("Option error, use -h for help");
23 my $rcs_id = '$Id$';
24 my $stripped_rcs_id = $rcs_id;
25 $stripped_rcs_id =~ s/^\$(.*)\s+\S+\s+\$$/$1/;
27 $| = 1;
29 my @Keywords = (
30 "Id",
31 "Author", "LastChangedBy",
32 "Date", "LastChangedDate",
33 "LastChangedRevision", "Revision", "Rev",
34 "URL", "HeadURL",
35 "Header",
36 "Name",
37 "Locker",
38 "Log",
39 "RCSfile",
40 "Source",
41 "State"
43 my $Keyw = join('|', @Keywords); # Used in regexps
45 $opt_h && usage(0);
47 $opt_s && $opt_S && die("convkeyw: Cannot mix the -s and -S option.\n");
49 if ($opt_V) {
50 print("$stripped_rcs_id\n");
51 exit(0);
54 my $rand_ext = "$$." . substr(rand, 2, 8);
55 while (defined($ARGV[0])) {
56 # Scan through all file names on the command line {{{
57 my $Curr = shift;
58 my $Dest = "$Curr.$rand_ext.tmp";
59 if (open(FromFP, $Curr)) {
60 (-e $Dest) && die("$Dest: What??? File already exists!");
61 if (open(ToFP, ">$Dest")) {
62 while (<FromFP>) {
63 /\$/ && ($_ = process_line($_));
64 print(ToFP $_);
66 close(FromFP);
67 close(ToFP);
68 rename($Dest, $Curr) || warn("rename($Dest, $Curr): $!");
69 } else {
70 warn("$Dest: Unable to create file: $!");
72 } else {
73 warn("$Curr: Unable to open file for read: $!");
75 # }}}
78 sub process_line {
79 # {{{
80 my $Retval = shift;
82 if ($opt_c) {
83 # Compress all keywords
84 $Retval =~ s/(\$)($Keyw): .*? (\$)/$1$2$3/;
87 if ($opt_D) {
88 # Remove the () stuff from the Subversion $Date.
89 $Retval =~ s/(\$Date: .*?)\(..., \d+ ... \d\d\d\d\) (\$)/$1$2/g;
92 if ($opt_s) {
93 # Strip keywords — remove dollars, keyword and colon. This
94 # action is destructive and has to be last.
95 $Retval =~ s/\$($Keyw): (.*?) \$/$2/g;
96 $Retval =~ s/\$($Keyw)\$/$1/g;
97 } elsif ($opt_S) {
98 # Strip keywords — remove dollars only. This action is
99 # destructive and has to be last.
100 $Retval =~ s/\$($Keyw): (.*?) \$/$1: $2/g;
101 $Retval =~ s/\$($Keyw)\$/$1/g;
104 return($Retval);
105 # }}}
108 sub usage {
109 # Send help text to stdout {{{
110 my $Retval = shift;
111 print <<END;
113 Syntax: convkeyw [options] file [file [...]]
115 Convert RCS/CVS/Subversion keywords in text files in various ways.
117 Options:
119 -c Compress keywords in the file, for example:
120 \$Id: dbk 963 2004-09-29 08:14:15Z sunny \$
121 is changed to
122 \$Id\$
123 This can be useful when comparing files against the text-base.
124 -D Remove the long date format from Subversion \$Id\$ strings.
125 \$Date: 2004-05-18 10:44:54 +0200 (Tue, 18 May 2004) \$
126 is changed to
127 \$Date: 2004-05-18 10:44:54 +0200 \$
128 -h Print this help.
129 -s Strip keywords, i.e. remove the " \$", "\$Id: :" and "\$Date: "
130 from keywords to protect them from changes when importing or
131 adding the file to a new repository.
132 \$Id: file.txt 123 2004-01-21 17:12:16Z fjodor \$
133 is changed to
134 file.txt 123 2004-01-21 17:12:16Z fjodor
135 WARNING: After using this option, further processing of keywords
136 in the file is impossible. Meant for use in tarballs and
137 releases.
138 -S Strip dollars from keywords like -s, but leave the keyword
139 itself and the colon intact. I.e.:
140 \$Id: file.txt 123 2004-01-21 17:12:16Z fjodor \$
141 is changed to
142 Id: file.txt 123 2004-01-21 17:12:16Z fjodor
143 -V Print version of the script:
144 $stripped_rcs_id
147 # }}}
148 exit($Retval);
151 __END__
153 # vim: set et ts=4 sw=4 sts=4 fo+=w fo+=c fo-=t tw=72 :
154 # End of file $Id$