/
git-svn-split.pl
executable file
·165 lines (146 loc) · 5.29 KB
/
git-svn-split.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
#!/usr/bin/perl -w
# --
# send latest Subversion commit to github, split per module
use strict;
use warnings;
use Git::Repository;
my $verbose = 0; # 1 for debug
my $repository = shift || '/home/svn/nextwiki';
my $statefile = '/home/git/last-git-split';
my $lastrev = shift;
chomp( $lastrev = `/usr/local/bin/svnlook youngest $repository` )
unless $lastrev;
# Read up to where has already been processed
sub getLastProcessedRev {
my $firstrev = 11813;
if ( -f $statefile ) {
open( my $state, '<', $statefile ) or die "Can't open $statefile: $!";
chomp( $firstrev = <$state> );
close $state;
}
return $firstrev;
}
# Update the state file with the latest processed revision
sub setLastProcessedRev {
my $lastrev = shift or return;
open( my $state, '>', $statefile ) or die "Can't open $statefile: $!";
print $state $lastrev . "\n";
close $state;
}
# Build modified plugins per branch
sub buildModifiedList {
my $revision = shift;
my $modifiedPlugin = shift; # hash ref built recursively
return unless $revision;
open( my $svnlook, '-|',
"/usr/local/bin/svnlook dirs-changed -r $revision $repository" )
or die "Can't open svn look pipe: $!";
while (<$svnlook>) {
if ( m#^(trunk)/([^/]*)/# || m#^(?:branches|tags)/([^/]*)/([^/]*)/# ) {
my $plugin = $2;
my $branch = $1 || 'master';
for ($branch) {
s/trunk/master/;
}
$modifiedPlugin->{$plugin}->{$branch}++;
}
}
close $svnlook;
}
sub getGitHubToken {
my $secfile = '/home/git/.token';
open( my $tokenfh, '<', $secfile )
or die "Cannot get GitHub token from $secfile: $!";
chomp( my $token = <$tokenfh> );
die "Invalid token: $token" unless $token =~ /[0-9a-f]/i;
return $token;
}
# Create the submodule on GitHub, and add it in the master repo
sub createSubModule {
my $masterRepo = shift;
my $module = shift;
warn "Creating github module $module..." if $verbose;
require Net::GitHub;
my $token = getGitHubToken;
my $github = Net::GitHub->new(
owner => 'foswiki',
repo => $module,
login => 'foswiki',
token => $token,
);
$github->repos->create(
$module,
"Foswiki module $module",
"http://foswiki.org/Extensions/$module", 1
);
warn
"\tAdding: git submodule add git\@github.com:foswiki/$module.git $module"
if $verbose;
# Cannot use Git::Repository here because submodule add doesn't like overriding GIT_WORK_TREE
# BooK provided a fix, which I will test tomorrow:
# https://github.com/book/System-Command/commit/1aa35fef43a2178e36d2f46cc3385d5b345ab237
system( 'cd '
. $masterRepo->work_tree()
. " && git submodule add git\@github.com:foswiki/$module.git $module"
);
}
# Create the subversion links from one submodule to foswiki.org
sub getSubModule {
my $masterRepo = shift;
my $module = shift;
my $moduleDir = $masterRepo->work_tree() . '/' . $module;
my $submodule = eval { Git::Repository->new( work_tree => $moduleDir ) };
createSubModule( $masterRepo, $module ) unless $submodule;
$submodule = Git::Repository->new( work_tree => $moduleDir );
# Ensute subversion configuration is consistent
unless ( eval { $submodule->run( svn => "log" ) } ) {
my $gitconfig = $submodule->git_dir() . '/config';
open( my $config, '<', $gitconfig )
or die "Can't open $module .git/config ($gitconfig): $!";
my $gitsvn = 0;
while (<$config>) {
$gitsvn++ if /svn-remote/;
}
close $config;
unless ($gitsvn) {
open( my $config, '>>', $gitconfig )
or die "Can't open $module .git/config ($gitconfig): $!";
print $config <<"END_CONFIG";
[svn-remote "svn"]
url = http://svn.foswiki.org
fetch = trunk/$module:refs/remotes/trunk
branches = branches/*/$module:refs/remotes/*
tags = tags/*/$module:refs/remotes/tags/*
END_CONFIG
$submodule->run( svn => 'fetch' );
}
}
return $submodule;
}
my %modifiedPlugin;
my $startrev = getLastProcessedRev() + 1;
warn "F:$startrev L:$lastrev" if $verbose;
for my $revision ( $startrev .. $lastrev ) {
buildModifiedList( $revision, \%modifiedPlugin );
}
exit unless keys %modifiedPlugin;
#my $masterRepo = Git::Repository->new( work_tree => "/usr/home/git/_allDeveloper" );
my $masterRepo =
Git::Repository->new( git_dir => "/usr/home/git/_allDeveloper/.git" );
for my $module ( sort keys %modifiedPlugin ) {
my $submodule = getSubModule( $masterRepo, $module );
for my $branch ( sort keys %{ $modifiedPlugin{$module} } ) {
warn "Updating module $module, branch $branch" if $verbose;
$submodule->run( checkout => $branch );
# Create local branch if it doesn't exist
unless ( eval { $submodule->run( 'symbolic-ref' => 'HEAD' ) } ) {
$submodule->run( checkout => '-b' => $branch );
$submodule->run( push => '-f', origin => $branch );
}
$submodule->run( svn => 'rebase' );
}
$submodule->run( push => '--all' );
}
$masterRepo->run( commit => '-am', "Pushed latest revision $lastrev" );
$masterRepo->run( push => '--all' );
setLastProcessedRev($lastrev);