6 use FindBin qw($RealBin);
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";
23 my $LAST_SVN_REV_PATH = "$PATCH_PATH/latest.rev";
25 $ENV{LANG} = "en_US.UTF-8";
27 sub get_last_svn_rev()
29 my $v = util::FileLoad($LAST_SVN_REV_PATH);
32 $v = $def if $v eq "";
36 print "get_last_svn_rev: $v\n";
40 sub set_last_svn_rev($)
44 util::FileSave($LAST_SVN_REV_PATH, $v);
45 print "set_last_svn_rev: $v\n";
51 my $infocmd = "svn info --non-interactive $SVN_PATH";
52 my $info = `$infocmd` || confess "$infocmd: failed";
55 if ($v =~ /\nRevision: (\d+)/) {
58 $v = $def if $v eq $info;
62 print "get_cur_svn_rev: $v\n";
68 my $f = util::FileLoad($AUTHORS_PATH);
69 my @lines = split("\n", $f);
72 foreach my $l (@lines) {
73 if ($l =~ /^([\w\-]+) = (.*)$/) {
78 confess "line: $l: invalid";
89 $authors = load_authors() unless defined($authors);
91 my $out = $authors->{$in};
93 confess "author: $in:not found" unless defined($out);
101 my $delim = "------------------------------------------------------------------------";
102 my @tmp1 = split($delim, $in);
103 my @tmp2 = split("\n", $tmp1[0]);
107 last unless $l =~ /^[ \t]*$/;
111 my $out = join("\n", @tmp2);
126 return 1 if $in eq ".";
127 return 1 if $in =~ /\/$/;
131 sub construct_chunk($)
135 my $out = join("\n", @{$chunk->{content}->{lines}});
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;
146 sub strip_svn_properties($)
149 my @in = split("\n", $in);
153 foreach my $l (@in) {
154 if ($l =~ /^Index: ([\w\.\_\-\/]+)/) {
155 #print "content: $1\n";
158 $chunk->{type} = "content";
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";
166 $chunk->{type} = "property";
168 confess "$1 properties exists" if defined($chunks->{$1}) and defined($chunks->{$1}->{properties});
169 $chunks->{$1}->{properties} = $chunk unless is_dir($1);
172 push(@{$chunk->{lines}}, $l);
175 #print Data::Dumper::Dumper($chunks);
178 foreach $chunk (values %{$chunks}) {
179 #print Data::Dumper::Dumper($chunk);
180 my $v = construct_chunk($chunk);
184 my $out = join("\n", @out);
185 return undef if $out eq "";
195 confess("binary diff") if ($in =~ /Cannot display: file marked as a binary type/);
196 return undef unless ($in =~ /^Index: [\w\.\_\-\/]+/);
198 $out = strip_svn_properties($in);
203 sub get_new_svn_revs($$)
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";
212 while($log =~ /\nr(\d+) \| (\w+) \| ([^\|]+) \|.*?line(s?)\n(.*)$/s) {
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);
227 my $diffcmd = "svn diff --non-interactive -r $orev:$rev $SVN_PATH";
228 my $diff = `$diffcmd` || confess "$diffcmd: failed";
230 return fix_diff($diff);
233 sub generate_patch($$)
236 my @log = split("\n", $r->{log});
237 my $subject = shift @log;
238 my $body = join("\n", @log);
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";
248 $p .= "\n---\nsvn-sync script\n\n";
256 my $applycmd = "cd $GIT_PATH && git am --binary $r->{patch_path}";
258 my $apply = `$applycmd` || confess "$applycmd: failed";
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);
272 util::FileSave($r->{patch_path}, $patch);
274 set_last_svn_rev($r);
285 my $lastrev = get_last_svn_rev();
286 my $currev = get_cur_svn_rev();
288 my $revs = get_new_svn_revs($lastrev, $currev);
289 store_patches($revs);
290 #print Data::Dumper::Dumper($revs);