| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package App::codefork; |
|
2
|
|
|
|
|
|
|
# ABSTRACT: Worlds dumbest code forker |
|
3
|
|
|
|
|
|
|
our $VERSION = '1.000'; |
|
4
|
2
|
|
|
2
|
|
545284
|
use Moo; |
|
|
2
|
|
|
|
|
21244
|
|
|
|
2
|
|
|
|
|
14
|
|
|
5
|
2
|
|
|
2
|
|
4970
|
use Path::Tiny; |
|
|
2
|
|
|
|
|
18960
|
|
|
|
2
|
|
|
|
|
181
|
|
|
6
|
2
|
|
|
2
|
|
1450
|
use Text::Diff; |
|
|
2
|
|
|
|
|
26361
|
|
|
|
2
|
|
|
|
|
4486
|
|
|
7
|
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
has config => ( |
|
9
|
|
|
|
|
|
|
is => 'ro', |
|
10
|
|
|
|
|
|
|
required => 1, |
|
11
|
|
|
|
|
|
|
coerce => sub { path($_[0]) }, |
|
12
|
|
|
|
|
|
|
); |
|
13
|
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
has dir => ( |
|
15
|
|
|
|
|
|
|
is => 'ro', |
|
16
|
|
|
|
|
|
|
default => sub { path('.')->absolute }, |
|
17
|
|
|
|
|
|
|
coerce => sub { path($_[0])->absolute }, |
|
18
|
|
|
|
|
|
|
); |
|
19
|
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
has debug => ( |
|
21
|
|
|
|
|
|
|
is => 'ro', |
|
22
|
|
|
|
|
|
|
default => sub { $ENV{CODEFORK_DEBUG} ? 1 : 0 }, |
|
23
|
|
|
|
|
|
|
); |
|
24
|
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
has mods => ( |
|
26
|
|
|
|
|
|
|
is => 'lazy', |
|
27
|
|
|
|
|
|
|
); |
|
28
|
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
sub _build_mods { |
|
31
|
1
|
|
|
1
|
|
13
|
my ($self) = @_; |
|
32
|
1
|
|
|
|
|
12
|
my @lines = $self->config->lines({ chomp => 1 }); |
|
33
|
|
|
|
|
|
|
return [ map { |
|
34
|
1
|
100
|
|
|
|
281
|
if (/\|/) { |
|
|
3
|
50
|
|
|
|
16
|
|
|
35
|
2
|
|
|
|
|
10
|
[ replace => split(/\|/, $_, 2) ] |
|
36
|
|
|
|
|
|
|
} elsif (/%/) { |
|
37
|
1
|
|
|
|
|
16
|
[ word => split(/%/, $_, 2) ] |
|
38
|
|
|
|
|
|
|
} else { |
|
39
|
|
|
|
|
|
|
() |
|
40
|
0
|
|
|
|
|
0
|
} |
|
41
|
|
|
|
|
|
|
} @lines ]; |
|
42
|
|
|
|
|
|
|
} |
|
43
|
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
sub collect_changes { |
|
45
|
1
|
|
|
1
|
1
|
181
|
my ($self) = @_; |
|
46
|
1
|
|
|
|
|
6
|
$self->log('Parsing config...'); |
|
47
|
1
|
|
|
|
|
7
|
$self->log($_->[0].': '.$_->[1].' => '.$_->[2]) for @{$self->mods}; |
|
|
1
|
|
|
|
|
27
|
|
|
48
|
1
|
|
|
|
|
2
|
my @changes; |
|
49
|
1
|
|
|
|
|
26
|
$self->_collect_dir($self->dir, '', \@changes); |
|
50
|
1
|
|
|
|
|
7
|
return \@changes; |
|
51
|
|
|
|
|
|
|
} |
|
52
|
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
sub _collect_dir { |
|
55
|
2
|
|
|
2
|
|
7
|
my ($self, $dir, $new_dir_prefix, $changes) = @_; |
|
56
|
2
|
|
|
|
|
4
|
my @mods = @{$self->mods}; |
|
|
2
|
|
|
|
|
70
|
|
|
57
|
|
|
|
|
|
|
|
|
58
|
2
|
|
|
|
|
26
|
for my $entry (sort $dir->children) { |
|
59
|
3
|
|
|
|
|
473
|
my $basename = $entry->basename; |
|
60
|
3
|
50
|
33
|
|
|
149
|
next if $basename eq '.git' || $basename eq '.svn'; |
|
61
|
|
|
|
|
|
|
|
|
62
|
3
|
|
|
|
|
7
|
my $new_basename = $basename; |
|
63
|
3
|
|
|
|
|
7
|
for my $mod (@mods) { |
|
64
|
9
|
100
|
|
|
|
29
|
if ($mod->[0] eq 'replace') { |
|
65
|
6
|
|
|
|
|
125
|
$new_basename =~ s/$mod->[1]/$mod->[2]/g; |
|
66
|
|
|
|
|
|
|
} |
|
67
|
|
|
|
|
|
|
} |
|
68
|
|
|
|
|
|
|
|
|
69
|
3
|
|
|
|
|
22
|
my $old_rel = $entry->relative($self->dir)->stringify; |
|
70
|
3
|
100
|
|
|
|
793
|
my $new_rel = length($new_dir_prefix) |
|
71
|
|
|
|
|
|
|
? "$new_dir_prefix/$new_basename" |
|
72
|
|
|
|
|
|
|
: $new_basename; |
|
73
|
|
|
|
|
|
|
|
|
74
|
3
|
100
|
|
|
|
15
|
if ($entry->is_dir) { |
|
75
|
1
|
|
|
|
|
36
|
$self->_collect_dir($entry, $new_rel, $changes); |
|
76
|
|
|
|
|
|
|
} else { |
|
77
|
2
|
|
|
|
|
62
|
my $content = $entry->slurp_raw; |
|
78
|
2
|
|
|
|
|
419
|
my $new_content = $content; |
|
79
|
2
|
|
|
|
|
6
|
for my $mod (@mods) { |
|
80
|
6
|
|
|
|
|
21
|
my ($cmd, $from, $to) = @$mod; |
|
81
|
6
|
100
|
|
|
|
18
|
if ($cmd eq 'replace') { |
|
|
|
50
|
|
|
|
|
|
|
82
|
4
|
|
|
|
|
104
|
$new_content =~ s/$from/$to/g; |
|
83
|
|
|
|
|
|
|
} elsif ($cmd eq 'word') { |
|
84
|
2
|
|
|
|
|
55
|
$new_content =~ s/([^A-Za-z0-9]+)$from([^A-Za-z0-9]+)/$1$to$2/g; |
|
85
|
|
|
|
|
|
|
} |
|
86
|
|
|
|
|
|
|
} |
|
87
|
|
|
|
|
|
|
|
|
88
|
2
|
50
|
33
|
|
|
11
|
if ($new_rel ne $old_rel || $new_content ne $content) { |
|
89
|
2
|
|
|
|
|
22
|
push @$changes, { |
|
90
|
|
|
|
|
|
|
old_path => $old_rel, |
|
91
|
|
|
|
|
|
|
new_path => $new_rel, |
|
92
|
|
|
|
|
|
|
old_content => $content, |
|
93
|
|
|
|
|
|
|
new_content => $new_content, |
|
94
|
|
|
|
|
|
|
}; |
|
95
|
|
|
|
|
|
|
} |
|
96
|
|
|
|
|
|
|
} |
|
97
|
|
|
|
|
|
|
} |
|
98
|
|
|
|
|
|
|
} |
|
99
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
sub generate_diff { |
|
101
|
1
|
|
|
1
|
1
|
4999
|
my ($self, $changes) = @_; |
|
102
|
1
|
|
33
|
|
|
5
|
$changes //= $self->collect_changes; |
|
103
|
1
|
|
|
|
|
3
|
my $output = ''; |
|
104
|
1
|
|
|
|
|
3
|
for my $change (@$changes) { |
|
105
|
2
|
|
|
|
|
8
|
my $renamed = $change->{old_path} ne $change->{new_path}; |
|
106
|
2
|
|
|
|
|
4
|
my $modified = $change->{old_content} ne $change->{new_content}; |
|
107
|
|
|
|
|
|
|
|
|
108
|
2
|
50
|
|
|
|
8
|
if ($modified) { |
|
|
|
0
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
my $diff_text = diff( |
|
110
|
|
|
|
|
|
|
\$change->{old_content}, |
|
111
|
|
|
|
|
|
|
\$change->{new_content}, |
|
112
|
|
|
|
|
|
|
{ |
|
113
|
2
|
|
|
|
|
21
|
STYLE => 'Unified', |
|
114
|
|
|
|
|
|
|
FILENAME_A => "a/$change->{old_path}", |
|
115
|
|
|
|
|
|
|
FILENAME_B => "b/$change->{new_path}", |
|
116
|
|
|
|
|
|
|
}, |
|
117
|
|
|
|
|
|
|
); |
|
118
|
2
|
50
|
|
|
|
964
|
if ($renamed) { |
|
119
|
2
|
|
|
|
|
8
|
my $header = "rename from $change->{old_path}\nrename to $change->{new_path}\n"; |
|
120
|
2
|
|
|
|
|
21
|
$diff_text =~ s/^(---\s)/$header$1/m; |
|
121
|
|
|
|
|
|
|
} |
|
122
|
2
|
|
|
|
|
10
|
$output .= $diff_text; |
|
123
|
|
|
|
|
|
|
} elsif ($renamed) { |
|
124
|
0
|
|
|
|
|
0
|
$output .= "diff $change->{old_path} $change->{new_path}\n"; |
|
125
|
0
|
|
|
|
|
0
|
$output .= "rename from $change->{old_path}\n"; |
|
126
|
0
|
|
|
|
|
0
|
$output .= "rename to $change->{new_path}\n"; |
|
127
|
|
|
|
|
|
|
} |
|
128
|
|
|
|
|
|
|
} |
|
129
|
1
|
|
|
|
|
4
|
return $output; |
|
130
|
|
|
|
|
|
|
} |
|
131
|
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
sub apply_changes { |
|
134
|
1
|
|
|
1
|
1
|
3603
|
my ($self, $changes) = @_; |
|
135
|
1
|
|
33
|
|
|
5
|
$changes //= $self->collect_changes; |
|
136
|
1
|
|
|
|
|
4
|
for my $change (@$changes) { |
|
137
|
2
|
|
|
|
|
150
|
my $old = $self->dir->child($change->{old_path}); |
|
138
|
2
|
|
|
|
|
117
|
my $new = $self->dir->child($change->{new_path}); |
|
139
|
2
|
|
|
|
|
83
|
my $renamed = $change->{old_path} ne $change->{new_path}; |
|
140
|
2
|
|
|
|
|
6
|
my $modified = $change->{old_content} ne $change->{new_content}; |
|
141
|
|
|
|
|
|
|
|
|
142
|
2
|
50
|
|
|
|
14
|
$new->parent->mkpath if $renamed; |
|
143
|
|
|
|
|
|
|
|
|
144
|
2
|
50
|
|
|
|
392
|
if ($modified) { |
|
|
|
0
|
|
|
|
|
|
|
145
|
2
|
|
|
|
|
15
|
$self->log("modify: $change->{new_path}"); |
|
146
|
2
|
|
|
|
|
11
|
$new->spew_raw($change->{new_content}); |
|
147
|
2
|
50
|
|
|
|
2072
|
if ($renamed) { |
|
148
|
2
|
|
|
|
|
15
|
$self->log("rename: $change->{old_path} -> $change->{new_path}"); |
|
149
|
2
|
|
|
|
|
8
|
$old->remove; |
|
150
|
|
|
|
|
|
|
} |
|
151
|
|
|
|
|
|
|
} elsif ($renamed) { |
|
152
|
0
|
|
|
|
|
0
|
$self->log("rename: $change->{old_path} -> $change->{new_path}"); |
|
153
|
0
|
|
|
|
|
0
|
$old->move($new); |
|
154
|
|
|
|
|
|
|
} |
|
155
|
|
|
|
|
|
|
} |
|
156
|
1
|
|
|
|
|
129
|
return scalar @$changes; |
|
157
|
|
|
|
|
|
|
} |
|
158
|
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
sub log { |
|
161
|
8
|
|
|
8
|
1
|
19
|
my ($self, $text) = @_; |
|
162
|
8
|
50
|
|
|
|
43
|
print STDERR $text."\n" if $self->debug; |
|
163
|
|
|
|
|
|
|
} |
|
164
|
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
sub run { |
|
167
|
0
|
|
|
0
|
1
|
|
my ($class, @args) = @_; |
|
168
|
|
|
|
|
|
|
|
|
169
|
0
|
|
|
|
|
|
my $command = 'diff'; |
|
170
|
0
|
|
|
|
|
|
my $config = 'fork.conf'; |
|
171
|
0
|
|
|
|
|
|
my $output_file; |
|
172
|
|
|
|
|
|
|
|
|
173
|
0
|
0
|
0
|
|
|
|
if (@args && $args[0] eq 'apply') { |
|
|
|
0
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
174
|
0
|
|
|
|
|
|
$command = 'apply'; |
|
175
|
0
|
|
|
|
|
|
shift @args; |
|
176
|
0
|
0
|
|
|
|
|
$config = shift @args if @args; |
|
177
|
|
|
|
|
|
|
} elsif (@args && $args[0] eq 'output') { |
|
178
|
0
|
|
|
|
|
|
$command = 'output'; |
|
179
|
0
|
|
|
|
|
|
shift @args; |
|
180
|
0
|
0
|
|
|
|
|
if (@args == 2) { |
|
181
|
0
|
|
|
|
|
|
$config = shift @args; |
|
182
|
|
|
|
|
|
|
} |
|
183
|
0
|
|
|
|
|
|
$output_file = shift @args; |
|
184
|
0
|
0
|
|
|
|
|
die "output command requires a filename\n" unless defined $output_file; |
|
185
|
|
|
|
|
|
|
} elsif (@args) { |
|
186
|
0
|
|
|
|
|
|
$config = shift @args; |
|
187
|
|
|
|
|
|
|
} |
|
188
|
|
|
|
|
|
|
|
|
189
|
0
|
0
|
|
|
|
|
die "Config file '$config' not found\n" unless -f $config; |
|
190
|
|
|
|
|
|
|
|
|
191
|
0
|
|
|
|
|
|
my $app = $class->new(config => $config); |
|
192
|
0
|
|
|
|
|
|
my $changes = $app->collect_changes; |
|
193
|
|
|
|
|
|
|
|
|
194
|
0
|
0
|
|
|
|
|
if ($command eq 'diff') { |
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
195
|
0
|
|
|
|
|
|
my $diff = $app->generate_diff($changes); |
|
196
|
0
|
0
|
|
|
|
|
if (length $diff) { |
|
197
|
0
|
|
|
|
|
|
print $diff; |
|
198
|
|
|
|
|
|
|
} else { |
|
199
|
0
|
|
|
|
|
|
print "No changes.\n"; |
|
200
|
|
|
|
|
|
|
} |
|
201
|
|
|
|
|
|
|
} elsif ($command eq 'apply') { |
|
202
|
0
|
|
|
|
|
|
my $count = $app->apply_changes($changes); |
|
203
|
0
|
|
|
|
|
|
print "Applied $count change(s).\n"; |
|
204
|
|
|
|
|
|
|
} elsif ($command eq 'output') { |
|
205
|
0
|
|
|
|
|
|
my $diff = $app->generate_diff($changes); |
|
206
|
0
|
|
|
|
|
|
path($output_file)->spew_utf8($diff); |
|
207
|
0
|
|
|
|
|
|
print "Wrote diff to $output_file\n"; |
|
208
|
|
|
|
|
|
|
} |
|
209
|
|
|
|
|
|
|
} |
|
210
|
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
|
|
212
|
|
|
|
|
|
|
|
|
213
|
|
|
|
|
|
|
1; |
|
214
|
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
__END__ |