-
Notifications
You must be signed in to change notification settings - Fork 59
/
branch-manager
executable file
·344 lines (303 loc) · 6.2 KB
/
branch-manager
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
#!/usr/bin/perl
use strict;
use warnings;
sub OutputOf($@)
{
my ($cmd, @args) = @_;
$cmd =~ s/#(\d)/"\$ARG$1"/g;
$ENV{"ARG$_"} = $args[$_-1]
for 1..@args;
open my $fh, '-|', $cmd
or die "popen $cmd: $!";
$ENV{"ARG$_"} = ''
for 1..@args;
undef local $/;
my $ret = <$fh>;
close $fh;
return $ret;
}
my %conf =
(
master => '',
revisions_applied => ''
);
my @revisions_applied = (); # array of [first, last] ranges
sub LoadSettings()
{
open my $fh, '<', ".patchsets"
or return;
while(<$fh>)
{
chomp;
/^([^=]*?)\s*=\s*(.*?)$/s
or next;
die "Invalid config item: $1 (allowed: @{[sort keys %conf]})"
if not exists $conf{$1};
$conf{$1} = $2;
}
@revisions_applied = map { /^(\d+)-(\d+)$/s or die "Invalid revision spec $_"; [$1, $2]; } split /,/, $conf{revisions_applied};
}
sub WriteSettings()
{
$conf{revisions_applied} = join ',', map { "$_->[0]-$_->[1]" } @revisions_applied;
open my $fh, '>', ".patchsets"
or die "writing settings: $!";
for(sort keys %conf)
{
print $fh "$_ = $conf{$_}\n";
}
}
sub AddPatch($$)
{
my ($first, $last) = @_;
die "Invalid range" if $first > $last;
my @rev = sort { $a->[0] <=> $b->[0] } (@revisions_applied, [$first, $last]);
my $i = 0;
while($i < @rev - 1)
{
my $a = $rev[$i][0];
my $b = $rev[$i][1];
my $c = $rev[$i+1][0];
my $d = $rev[$i+1][1];
if($b >= $c)
{
die "overlapping patch: $a-$b overlaps $c-$d";
}
if($b == $c - 1)
{
splice @rev, $i, 2, [$a, $d];
next;
}
++$i;
}
@revisions_applied = @rev;
}
sub RemovePatch($$)
{
my ($first, $last) = @_;
die "Invalid range" if $first > $last;
my @rev = sort { $a->[0] <=> $b->[0] } (@revisions_applied);
my $i = 0;
while($i < @rev)
{
my $a = $rev[$i][0];
my $b = $rev[$i][1];
if($first >= $a && $last <= $b)
{
# this is the range
my @replacement;
if($first == $a && $last == $b)
{
@replacement = ();
}
elsif($first == $a)
{
@replacement = ([$last + 1, $b]);
}
elsif($last == $b)
{
@replacement = ([$a, $first - 1]);
}
else
{
@replacement = ([$a, $first - 1], [$last + 1, $b]);
}
splice @rev, $i, 1, @replacement;
@revisions_applied = @rev;
return;
}
}
die "could not remove range: not in set";
}
sub GetUnappliedRanges($)
{
my ($lastrev) = @_;
my @unapplied = ();
my $cur = 0;
for(@revisions_applied)
{
my ($a, $b) = @$_;
if($a - 1 >= $cur + 1)
{
push @unapplied, [$cur + 1, $a - 1];
}
$cur = $b;
}
if($lastrev >= $cur + 1)
{
push @unapplied, [$cur + 1, $lastrev];
}
return @unapplied;
}
sub GetMasterRev()
{
my $svninfo = OutputOf 'svn info #1', $conf{master};
$svninfo =~ /^Last Changed Rev: (\d+)$/m
or die "could not get svn info";
return $1;
}
sub GetLog($$)
{
my ($first, $last) = @_;
my $log = OutputOf 'svn log -r#1:#2 #3', $first, $last, $conf{master};
$log =~ s/^-*$//gm;
$log =~ s/\n+/\n/gs;
$log =~ s/^\n//s;
$log =~ s/\n$//s;
return $log;
}
sub GetDiff($$)
{
my ($first, $last) = @_;
return OutputOf 'svn diff -r#1:#2 #3', $first-1, $last, $conf{master};
}
my ($cmd, @args) = @ARGV;
$cmd = 'help' if not defined $cmd;
if($cmd eq 'info')
{
LoadSettings();
for(@revisions_applied)
{
my ($a, $b) = @$_;
print "Applied: $a to $b\n";
}
for(GetUnappliedRanges(GetMasterRev()))
{
my ($a, $b) = @$_;
print "Unapplied: $a to $b\n";
}
}
elsif($cmd eq 'unmerged-diff')
{
LoadSettings();
my @autoadd = ();
for(GetUnappliedRanges(GetMasterRev()))
{
my ($a, $b) = @$_;
my $log = GetLog $a, $b;
if($log eq '')
{
push @autoadd, [$a, $b];
next;
}
$log =~ s/^/ /gm;
print "Unapplied: $a to $b\n";
print "$log\n";
print GetDiff $a, $b;
print "\n";
}
for(@autoadd)
{
my ($a, $b) = @$_;
print "Autofilled revision hole $a to $b\n";
AddPatch $a, $b;
}
WriteSettings() if @autoadd;
}
elsif($cmd eq 'unmerged')
{
LoadSettings();
my @autoadd = ();
for(GetUnappliedRanges(GetMasterRev()))
{
my ($a, $b) = @$_;
my $log = GetLog $a, $b;
if($log eq '')
{
push @autoadd, [$a, $b];
next;
}
$log =~ s/^/ /gm;
print "Unapplied: $a to $b\n";
print "$log\n\n";
}
for(@autoadd)
{
my ($a, $b) = @$_;
print "Autofilled revision hole $a to $b\n";
AddPatch $a, $b;
}
WriteSettings() if @autoadd;
}
elsif($cmd eq 'merge')
{
my ($first, $last, $force) = @args;
die "Usage: $0 merge first last [--force]"
if not defined $first;
$last = $first if not defined $last;
die "Usage: $0 merge first last"
if "$first$last" =~ /[^0-9]/;
die "There is an uncommitted merge"
if -f '.commitmsg' and $force ne '--force';
LoadSettings();
AddPatch $first, $last;
print OutputOf 'svn merge -r#1:#2 #3', ($first - 1), $last, $conf{master};
print "You may also want to run $0 unmerged to fill possible revision holes\n";
print "Make sure there are no conflicts, then run $0 commit\n";
print "To abort, use $0 revert\n";
open my $fh, '>>', '.commitmsg'
or die "open .commitmsg";
print $fh GetLog $first, $last;
print $fh "\n";
close $fh;
WriteSettings();
}
elsif($cmd eq 'undo')
{
my ($first, $last, $force) = @args;
die "Usage: $0 undo first last"
if not defined $first;
$last = $first if not defined $last;
die "Usage: $0 merge first last"
if "$first$last" =~ /[^0-9]/;
die "There is an uncommitted merge"
if -f '.commitmsg' and $force ne '--force';
LoadSettings();
RemovePatch $first, $last;
print OutputOf 'svn merge -r#2:#1 #3', ($first - 1), $last, $conf{master};
print "Make sure there are no conflicts, then run $0 commit\n";
print "To abort, use $0 revert\n";
open my $fh, '>>', '.commitmsg'
or die "open .commitmsg";
print $fh "undo the following merge:\n", GetLog $first, $last;
close $fh;
WriteSettings();
}
elsif($cmd eq 'commit')
{
system 'vim .commitmsg';
print "Hit Enter if OK to commit, Ctrl-C otherwise...\n";
my $ok = <STDIN>;
if(!system 'svn commit -F .commitmsg')
{
unlink '.commitmsg';
}
}
elsif($cmd eq 'revert')
{
if(!system 'svn revert -R .')
{
unlink '.commitmsg';
}
}
elsif($cmd eq 'init')
{
my ($master, $rev) = @args;
$conf{master} = $master;
@revisions_applied = [1, $rev];
WriteSettings();
}
else
{
print <<EOF;
Usage:
$0 init masterrepo rev
$0 info
$0 unmerged
$0 unmerged-diff
$0 merge rev1 [rev2] [--force]
$0 undo rev1 [rev2] [--force]
$0 commit
$0 revert
EOF
}