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 sub get_last_svn_rev()
27 my $v = util::FileLoad($LAST_SVN_REV_PATH);
30 $v = $def if $v eq "";
34 print "get_last_svn_rev: $v\n";
38 sub set_last_svn_rev($)
42 util::FileSave($LAST_SVN_REV_PATH, $v);
43 print "set_last_svn_rev: $v\n";
49 my $infocmd = "LANG=C svn info --non-interactive $SVN_PATH";
50 my $info = `$infocmd` || confess "$infocmd: failed";
53 if ($v =~ /\nRevision: (\d+)/) {
56 $v = $def if $v eq $info;
60 print "get_cur_svn_rev: $v\n";
66 my $f = util::FileLoad($AUTHORS_PATH);
67 my @lines = split("\n", $f);
70 foreach my $l (@lines) {
71 if ($l =~ /^([\w\-]+) = (.*)$/) {
76 confess "line: $l: invalid";
87 $authors = load_authors() unless defined($authors);
89 my $out = $authors->{$in};
91 confess "author: $in:not found" unless defined($out);
99 my $delim = "------------------------------------------------------------------------";
100 my @tmp1 = split($delim, $in);
101 my @tmp2 = split("\n", $tmp1[0]);
105 last unless $l =~ /^[ \t]*$/;
109 my $out = join("\n", @tmp2);
124 return 1 if $in eq ".";
125 return 1 if $in =~ /\/$/;
129 sub construct_chunk($)
133 my $out = join("\n", @{$chunk->{content}->{lines}});
135 $out =~ s/^Index: ([^\n]+)\n[=]+/diff -u a\/$1 b\/$1/g;
136 $out =~ s/--- ([^\(]+)\(revision: 0\)/--- \/dev\/null/g;
137 $out =~ s/--- ([^\(]+)\([^\n]+/--- a\/$1/g;
138 $out =~ s/\+\+\+ ([^\(]+)\(revision: 0\)/+++ \/dev\/null/g;
139 $out =~ s/\+\+\+ ([^\(]+)\([^\n]+/+++ b\/$1/g;
144 sub strip_svn_properties($)
147 my @in = split("\n", $in);
151 foreach my $l (@in) {
152 if ($l =~ /^Index: ([\w\.\_\-\/]+)/) {
153 #print "content: $1\n";
156 $chunk->{type} = "content";
158 confess "$1 content exists" if defined($chunks->{$1}) and defined($chunks->{$1}->{content});
159 $chunks->{$1}->{content} = $chunk;
160 } elsif ($l =~ /^Property changes on: ([\w\.\_\-\/]+)/) {
161 #print "properties(".is_dir($1)."): $1\n";
164 $chunk->{type} = "property";
166 confess "$1 properties exists" if defined($chunks->{$1}) and defined($chunks->{$1}->{properties});
167 $chunks->{$1}->{properties} = $chunk unless is_dir($1);
170 push(@{$chunk->{lines}}, $l);
173 #print Data::Dumper::Dumper($chunks);
176 foreach $chunk (values %{$chunks}) {
177 #print Data::Dumper::Dumper($chunk);
178 my $v = construct_chunk($chunk);
182 my $out = join("\n", @out);
183 return undef if $out eq "";
193 confess("binary diff") if ($in =~ /Cannot display: file marked as a binary type/);
194 return undef unless ($in =~ /^Index: [\w\.\_\-\/]+/);
196 $out = strip_svn_properties($in);
201 sub get_new_svn_revs($$)
203 my ($lastrev, $currev) = @_;
204 my $nextrev = $lastrev + 1;
205 return undef if $nextrev > $currev;
206 my $logcmd = "LANG=C svn log --non-interactive -r $nextrev:$currev $SVN_PATH";
207 my $log = `$logcmd` || confess "$logcmd: failed";
210 while($log =~ /\nr(\d+) \| (\w+) \| ([^\|]+) \|.*?line(s?)\n(.*)$/s) {
212 $revs->{$1}->{svnrev} = $1;
213 $revs->{$1}->{author} = fix_author($2);
214 $revs->{$1}->{log} = fix_log($5);
215 $revs->{$1}->{date} = fix_date($3);
225 my $diffcmd = "LANG=C svn diff --non-interactive -r $orev:$rev $SVN_PATH";
226 my $diff = `$diffcmd` || confess "$diffcmd: failed";
228 return fix_diff($diff);
231 sub generate_patch($$)
234 my @log = split("\n", $r->{log});
235 my $subject = shift @log;
236 my $body = join("\n", @log);
239 $p .= "From 123456780abcdef\n";
240 $p .= "From: $r->{author}\n";
241 $p .= "Date: $r->{date}\n";
242 $p .= "Subject: [PATCH] r$r->{svnrev}: $subject\n";
246 $p .= "\n---\nsvn-sync script\n\n";
254 my $applycmd = "cd $GIT_PATH && git am --binary $r->{patch_path}";
256 my $apply = `$applycmd` || confess "$applycmd: failed";
263 foreach my $rev (sort keys %{$revs}) {
264 my $r = $revs->{$rev};
265 $r->{patch_path} = "$PATCH_PATH/$r->{svnrev}.patch";
266 my $diff = get_svn_diff($r->{svnrev});
267 next unless defined($diff);
268 my $patch = generate_patch($r, $diff);
270 util::FileSave($r->{patch_path}, $patch);
272 set_last_svn_rev($r);
283 my $lastrev = get_last_svn_rev();
284 my $currev = get_cur_svn_rev();
286 my $revs = get_new_svn_revs($lastrev, $currev);
287 store_patches($revs);
288 #print Data::Dumper::Dumper($revs);