-
Notifications
You must be signed in to change notification settings - Fork 0
/
svnid.smudge.pl
executable file
·179 lines (164 loc) · 5.33 KB
/
svnid.smudge.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#! /usr/bin/perl
# gitfilters/svnid.smudge.pl 2017-9-18 Alan U. Kennington.
# (No keyword expansion here because of chicken-and-egg situation.)
# This script expands the embedded keyword $Id$ for Git.
# This is the version for Subversion-style times.
# Usage: svnid.smudge.pl %f
# Example output:
# $Id: gitfilters/README 79b816d 2017-09-16 10:03:55Z Alan U. Kennington $
my $trace_loc2utc = 0;
#------------------------------------------------------------------------------
# Convert a local date/time string to UTC with a Z.
#------------------------------------------------------------------------------
#-------------------#
# localtime2utc #
#-------------------#
sub localtime2utc {
my ($localtime) = @_;
if ($trace_loc2utc >= 10) {
print "localtime2utc: input = \"$localtime\"\n";
}
# Should check here that the local time string is valid.
# ....
# Convert the time from Git-style to Subversion style.
# Do it the hard way because Perl data-time parsing functions are scarce.
my ($date, $time, $zone) = split / /, $localtime;
if ($trace_loc2utc >= 10) {
print "(date, time, zone) = (\"$date\", \"$time\", \"$zone\")\n";
}
my ($yr, $mo, $da) = split /-/, $date;
if ($trace_loc2utc >= 10) {
print "(year, month, day) = (\"$yr\", \"$mo\", \"$da\")\n";
}
my ($hr, $mi, $se) = split /:/, $time;
if ($trace_loc2utc >= 10) {
print "(hour, minute, second) = (\"$hr\", \"$mi\", \"$se\")\n";
}
my $zo = $zone + 0;
if ($trace_loc2utc >= 10) {
print "zone integer = $zo\n";
}
my $zonesign = 1;
if ($zo < 0) {
$zonesign = -1;
$zo = -$zo;
}
if ($trace_loc2utc >= 10) {
print "zone sign/value = $zonesign/$zo\n";
}
my $zhour = int($zo/100);
my $zmin = $zo - $zhour * 100;
if ($trace_loc2utc >= 10) {
print "zone sign/hour/min = $zonesign/$zhour/$zmin\n";
}
# Assume the Gregorian calendar for all time!
my @mdays = (
31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
);
# Make the correction for leap years. Assume Gregorian calendar.
my $leap = 0;
if (($yr % 4) == 0) {
$leap = 1;
if (($yr % 100) == 0) {
$leap = 0;
if (($yr % 400) == 0) {
$leap = 1;
}
}
}
$mdays[1] += $leap;
if ($trace_loc2utc >= 10) {
print "leap = $leap\n";
}
if ($zonesign < 0 && ($zhour > 0 || $zmin > 0)) {
# Advance the zone hours and minutes to UTC.
$hr += $zhour;
$mi += $zmin;
# Normalise the time and date.
if ($mi >= 60) {
$mi -= 60;
$hr += 1;
}
if ($hr >= 24) {
$hr -= 24;
$da += 1;
if ($da > $mdays[$mo - 1]) {
$da -= $mdays[$mo - 1];
$mo += 1;
if ($mo > 12) {
$mo -= 12;
$yr += 1;
}
}
}
}
if ($zonesign > 0 && ($zhour > 0 || $zmin > 0)) {
# Reverse the zone hours and minutes to UTC.
$hr -= $zhour;
$mi -= $zmin;
# Normalise the time and date.
if ($mi < 0) {
$hr -= 1;
$mi += 60;
}
if ($hr < 0) {
$da -= 1;
$hr += 24;
if ($da <= 0) {
$mo -= 1;
if ($mo <= 0) {
$mo += 12;
$yr -= 1;
}
$da += $mdays[$mo - 1];
}
}
}
if ($trace_loc2utc >= 10) {
print "(year, month, day) = (\"$yr\", \"$mo\", \"$da\")\n";
print "(hour, minute, second) = (\"$hr\", \"$mi\", \"$se\")\n";
}
return sprintf("%04u-%02u-%02u %02u:%02u:%02uZ",
$yr, $mo, $da, $hr, $mi, $se);
} # End of function localtime2utc.
my $n_args = $#ARGV + 1;
if ($n_args != 1) {
print STDERR "Usage: $0 %f\n";
exit(1);
}
# The file to be filtered.
my $f = $ARGV[0];
# print "f = $f\n";
# This script is apparently called from the top working directory.
# So for git log, use the full relative path $f of the file %f.
my $cmd_h = "git log --pretty=format:\"%h\" -1 -- $f";
my $cmd_ai = "git log --pretty=format:\"%ai\" -1 -- $f";
my $cmd_an = "git log --pretty=format:\"%an\" -1 -- $f";
my $x_h = `$cmd_h`;
my $x_ai = `$cmd_ai`;
my $x_an = `$cmd_an`;
# Use the tail of the path because "/" is forbidden in "s///".
# my @names = split /\//, $f;
# my $n_names = $#names;
# my $t = $names[$n_names];
# # print "names[$n_names] = \"$t\"\n";
# Substitute \/ for / so that the sed s-pattern will be correct.
my $z = $f;
$z =~ s/\//\\\//g;
$z =~ s/\$/\\\$/g;
# print "z = \"$z\"\n";
#------------------------------------------------------------------------------
# Convert the timestamp from Git to Subversion style.
# my $testdate = "2000-01-01 03:05:34 +1030";
# my $testutc = localtime2utc($testdate);
# print "testdate = \"$testdate\"\n";
# print "testutc = \"$testutc\"\n";
my $utctime = localtime2utc($x_ai);
# print "utctime = \"$utctime\"\n";
my $x = "$x_h $utctime $x_an";
# print "x = \"$x\"\n";
# Apply the hash/datetime/author string to the standard input.
$cmd = "sed 's/\\\$Id\\\$/\$Id: $z $x \$/g'";
# print "cmd = \"$cmd\"\n";
system($cmd);
__END__