-
Notifications
You must be signed in to change notification settings - Fork 21
/
fixmergeheaders.pl
79 lines (76 loc) · 1.99 KB
/
fixmergeheaders.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
#!/usr/bin/perl
#
# Author : mth
# Created: 2003-06-02
#
# $RCSfile$
# $Author: egonw $
# $Date: 2003-07-25 05:39:24 -0500 (Fri, 25 Jul 2003) $
# $Revision: 1197 $
#
# cd to the Jmol directory and run me in order to resolve
# cvs conflicts in the headers of .java files after a merge
#
# IMPORTANT: it resolved any conflict, whether in the header
# or not. Use 'grep -R -n "<<<<<"' to see in which lines the
# conflicts are found. The '-n' option outputs the line number,
# which is 2 for conflicts in the header. Resolve conflicts
# with other line numbers PRIOR to running this script!
#
# The "conflict" files generated by CVS are saved as .java.bak
#
# This will probably only work on a Linux/Unix system
#
use strict;
my $endheaderlinenumber = 15; # set arbitrarily
open(CONFLICT, 'find src -name \*.java | xargs grep -l "<<<<<" |');
while (<CONFLICT>) {
chop;
fixConflict($_);
}
sub fixConflict {
my $filename = $_;
print "Fixing header conflict in $filename\n";
my $filenameBak = $filename . ".bak";
rename $filename, $filenameBak;
open(IN, "<$filenameBak");
open(OUT, ">$filename");
my $state = 'lookingForConflict';
my $linenumber = 0;
while (<IN>) {
my $line = $_;
$linenumber++;
if ($state eq 'lookingForConflict') {
if ($line =~ /^<<<<</) {
if ($linenumber < $endheaderlinenumber) {
$state = 'discardingBeforeDoubleBar';
next;
} else {
print "Skipping conflict starting at line $linenumber!\n";
}
}
if ($line =~ /\s*(final|public|private|abstract|class)/) {
$state = 'copyingFile';
}
print OUT $line;
next;
}
if ($state eq 'discardingBeforeDoubleBar') {
if ($line =~ /^=====/) {
$state = 'copyingAfterDoubleBar';
}
next;
}
if ($state eq 'copyingAfterDoubleBar') {
if ($line =~ /^>>>>>/) {
$state = 'lookingForConflict';
} else {
print OUT $line;
}
next;
}
print OUT $line;
}
close IN;
close OUT;
}