use en_US.UTF-8 for everything
[metze/old/samba4-sync/samba4-sync.scripts/.git] / samba4-sync.pl
1 #!/usr/bin/perl
2 #
3
4 use strict;
5
6 use FindBin qw($RealBin);
7
8 use lib "$RealBin";
9 use util;
10
11 use POSIX;
12 use Data::Dumper;
13 use File::stat;
14 use Carp;
15
16 my $SVN_REPO="file:///home/svn/samba";
17 my $SVN_BRANCH="branches/SAMBA_4_0/";
18 my $SVN_PATH="$SVN_REPO/$SVN_BRANCH";
19 my $PATCH_PATH="$ENV{HOME}/svnmirror/samba4-sync.patches";
20 my $AUTHORS_PATH="$ENV{HOME}/svnmirror/samba4-sync.scripts/svn-authors";
21 my $GIT_PATH="$ENV{HOME}/svnmirror/samba4-sync.git";
22
23 my $LAST_SVN_REV_PATH = "$PATCH_PATH/latest.rev";
24
25 $ENV{LANG} = "en_US.UTF-8";
26
27 sub get_last_svn_rev()
28 {
29         my $v = util::FileLoad($LAST_SVN_REV_PATH);
30         my $def = 25600;
31
32         $v = $def if $v eq "";
33         $v *= 1;
34         $v = $def if $v == 0;
35
36         print "get_last_svn_rev: $v\n";
37         return $v;
38 }
39
40 sub set_last_svn_rev($)
41 {
42         my ($r) = @_;
43         my $v = $r->{svnrev};
44         util::FileSave($LAST_SVN_REV_PATH, $v);
45         print "set_last_svn_rev: $v\n";
46 }
47
48 sub get_cur_svn_rev()
49 {
50         my $def = 25650;
51         my $infocmd = "svn info --non-interactive $SVN_PATH";
52         my $info = `$infocmd` || confess "$infocmd: failed";
53
54         my $v = $info;
55         if ($v =~ /\nRevision: (\d+)/) {
56                 $v = $1;
57         }
58         $v = $def if $v eq $info;
59         $v *= 1;
60         $v = $def if $v == 0;
61
62         print "get_cur_svn_rev: $v\n";
63         return $v;
64 }
65
66 sub load_authors()
67 {
68         my $f = util::FileLoad($AUTHORS_PATH);
69         my @lines = split("\n", $f);
70         my $authors = undef;
71
72         foreach my $l (@lines) {
73                 if ($l =~ /^([\w\-]+) = (.*)$/) {
74                         $authors->{$1} = $2;
75                         next;
76                 }
77
78                 confess "line: $l: invalid";
79         }
80
81         return $authors;
82 }
83
84 sub fix_author($)
85 {
86         my ($in) = @_;
87         my $authors = undef;
88
89         $authors = load_authors() unless defined($authors);
90
91         my $out = $authors->{$in};
92
93         confess "author: $in:not found" unless defined($out);
94
95         return $out;
96 }
97
98 sub fix_log($)
99 {
100         my ($in) = @_;
101         my $delim = "------------------------------------------------------------------------";
102         my @tmp1 = split($delim, $in);
103         my @tmp2 = split("\n", $tmp1[0]);
104
105         while (1){
106                 my $l = $tmp2[0];
107                 last unless $l =~ /^[ \t]*$/;
108                 shift(@tmp2);
109         }
110
111         my $out = join("\n", @tmp2);
112
113         return $out;
114 }
115
116 sub fix_date($)
117 {
118         my ($in) = @_;
119
120         return $in;
121 }
122
123 sub is_dir($)
124 {
125         my ($in) = @_;
126         return 1 if $in eq ".";
127         return 1 if $in =~ /\/$/;
128         return 0;
129 }
130
131 sub construct_chunk($)
132 {
133         my ($chunk) = @_;
134
135         my $out = join("\n", @{$chunk->{content}->{lines}});
136
137         $out =~ s/^Index: ([^\n]+)\n[=]+/diff -u a\/$1 b\/$1/g;
138         $out =~ s/--- ([^\(]+)\(revision: 0\)/--- \/dev\/null/g;
139         $out =~ s/--- ([^\(]+)\([^\n]+/--- a\/$1/g;
140         $out =~ s/\+\+\+ ([^\(]+)\(revision: 0\)/+++ \/dev\/null/g;
141         $out =~ s/\+\+\+ ([^\(]+)\([^\n]+/+++ b\/$1/g;
142
143         return $out;
144 }
145
146 sub strip_svn_properties($)
147 {
148         my ($in) = @_;
149         my @in = split("\n", $in);
150
151         my $chunk = undef;
152         my $chunks = undef;
153         foreach my $l (@in) {
154                 if ($l =~ /^Index: ([\w\.\_\-\/]+)/) {
155                         #print "content: $1\n";
156
157                         $chunk = undef;
158                         $chunk->{type} = "content";
159                         $chunk->{file} = $1;
160                         confess "$1 content exists" if defined($chunks->{$1}) and defined($chunks->{$1}->{content});
161                         $chunks->{$1}->{content} = $chunk;
162                 } elsif ($l =~ /^Property changes on: ([\w\.\_\-\/]+)/) {
163                         #print "properties(".is_dir($1)."): $1\n";
164
165                         $chunk = undef;
166                         $chunk->{type} = "property";
167                         $chunk->{path} = $1;
168                         confess "$1 properties exists" if defined($chunks->{$1}) and defined($chunks->{$1}->{properties});
169                         $chunks->{$1}->{properties} = $chunk unless is_dir($1);
170                 }
171
172                 push(@{$chunk->{lines}}, $l);
173         }
174
175         #print Data::Dumper::Dumper($chunks);
176
177         my @out = ();
178         foreach $chunk (values %{$chunks}) {
179                 #print Data::Dumper::Dumper($chunk);
180                 my $v = construct_chunk($chunk);
181                 push(@out, $v);
182         }
183
184         my $out = join("\n", @out);
185         return undef if $out eq "";
186
187         return $out;
188 }
189
190 sub fix_diff($)
191 {
192         my ($in) = @_;
193         my $out = $in;
194
195         confess("binary diff") if ($in =~ /Cannot display: file marked as a binary type/);
196         return undef unless ($in =~ /^Index: [\w\.\_\-\/]+/);
197
198         $out = strip_svn_properties($in);
199
200         return $out;
201 }
202
203 sub get_new_svn_revs($$)
204 {
205         my ($lastrev, $currev) = @_;
206         my $nextrev = $lastrev + 1;
207         return undef if $nextrev > $currev;
208         my $logcmd = "svn log --non-interactive -r $nextrev:$currev $SVN_PATH";
209         my $log = `$logcmd` || confess "$logcmd: failed";
210         my $revs = undef;
211
212         while($log =~ /\nr(\d+) \| (\w+) \| ([^\|]+) \|.*?line(s?)\n(.*)$/s) {
213                 $log = $5;
214                 $revs->{$1}->{svnrev} = $1;
215                 $revs->{$1}->{author} = fix_author($2);
216                 $revs->{$1}->{log}    = fix_log($5);
217                 $revs->{$1}->{date}   = fix_date($3);
218         }
219
220         return $revs;
221 }
222
223 sub get_svn_diff($)
224 {
225         my ($rev) = @_;
226         my $orev = $rev - 1;
227         my $diffcmd = "svn diff --non-interactive -r $orev:$rev $SVN_PATH";
228         my $diff = `$diffcmd` || confess "$diffcmd: failed";
229
230         return fix_diff($diff); 
231 }
232
233 sub generate_patch($$)
234 {
235         my ($r, $diff) = @_;
236         my @log = split("\n", $r->{log});
237         my $subject = shift @log;
238         my $body = join("\n", @log);
239         my $p = "";
240
241         $p .= "From 123456780abcdef\n";
242         $p .= "From: $r->{author}\n";
243         $p .= "Date: $r->{date}\n";
244         $p .= "Subject: [PATCH] r$r->{svnrev}: $subject\n";
245         $p .= "$body\n";
246         $p .= "\n";
247         $p .= $diff;
248         $p .= "\n---\nsvn-sync script\n\n";
249
250         return $p;
251 }
252
253 sub apply_patch($)
254 {
255         my ($r) = @_;
256         my $applycmd = "cd $GIT_PATH && git am --binary $r->{patch_path}";
257
258         my $apply = `$applycmd` || confess "$applycmd: failed";
259 }
260
261 sub store_patches($)
262 {
263         my ($revs) = @_;
264
265         foreach my $rev (sort keys %{$revs}) {
266                 my $r = $revs->{$rev};
267                 $r->{patch_path} = "$PATCH_PATH/$r->{svnrev}.patch";
268                 my $diff = get_svn_diff($r->{svnrev});
269                 next unless defined($diff);
270                 my $patch = generate_patch($r, $diff);
271                 #print $patch;
272                 util::FileSave($r->{patch_path}, $patch);
273                 apply_patch($r);
274                 set_last_svn_rev($r);
275         }
276 }
277
278 sub apply_patches($)
279 {
280         my ($revs) = @_;
281
282         
283 }
284
285 my $lastrev = get_last_svn_rev();
286 my $currev = get_cur_svn_rev();
287
288 my $revs = get_new_svn_revs($lastrev, $currev);
289 store_patches($revs);
290 #print Data::Dumper::Dumper($revs);
291