-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathav-cherrypick
executable file
·328 lines (288 loc) · 9.19 KB
/
av-cherrypick
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
#!/usr/bin/env perl
use warnings;
use strict;
use feature qw/say state/;
use Git::Repository;
use Git::Repository::Command;
use List::Util 1.33 qw/any first/;
use Hash::Ordered;
use File::Spec::Functions qw/catdir catfile updir/;
use Term::ANSIColor qw/:constants :pushpop/;
BEGIN {
eval {
require Smart::Comments;
Smart::Comments->import();
}
}
use lib::abs 'lib';
use Git::WorkFlow::AstraVer::Branch qw/list_branches_sorted check_branch_empty/;
use Git::WorkFlow::AstraVer::Notes;
use Git::WorkFlow::AstraVer::Commit qw/commit_info/;
use Git::WorkFlow::AstraVer::Repo qw/check_changes_exist get_git_dir/;
use Interactive::Background;
$| = 1; #autoflush
my $print = 0;
my $save = 1;
if ((!@ARGV) || (($ARGV[0] ne '--spec') && ($ARGV[0] ne '--devel'))) {
die "$0 <--devel|--spec>\n"
}
my $branch = substr $ARGV[0], 2;
say "PID: $$";
sub check_drop_command
{
my $drop = 0;
state $filename = catfile(get_git_dir($_[0]), 'drop_commit');
if (-e $filename) {
$drop = 1;
unlink $filename
}
$drop
}
sub drop_latest_commit
{
say "Dropping latest commit.";
$_[0]->run('format-patch' => 'HEAD~1');
$_[0]->run(reset => '--hard' => 'HEAD~1');
}
sub git_cherry_pick
{
my ($r, $cmd, $err_handler) = @_;
my $c = Git::Repository::Command->new($r, @$cmd, {fatal => [-128, -129]});
$c->final_output();
$err_handler->()
if $c->exit();
}
my $r = Git::Repository->new(work_tree => $ENV{CURRENT_PROJECT});
if (check_changes_exist($r)) {
die "There are changes in repository. You should commit/remove them first.\n";
}
my ($lbranch, $nbranch);
my @branches = list_branches_sorted($r, $branch . '-*');
die "Can't find versioned branches $branch\n"
unless @branches;
if (check_branch_empty($r, $branch eq 'devel' ? 'master' : $branches[-1] =~ s/\Aspec/devel/r, $branches[-1])) { # if latest empty, then -2
if ($branches[-2]) {
$lbranch = $branches[-2]
}
$nbranch = $branches[-1];
} else { # use -1 and create new branch
$lbranch = $branches[-1];
#$nbranch = $new
}
die "Can't determine previous versioned branch of development.\n"
unless $lbranch;
die "You should create new versioned branch $branch first!\n"
unless $nbranch;
### LATEST BRANCH: $lbranch
my $first_merge;
foreach my $i ($r->run('rev-list' => '--merges' => $branch)) {
my $parents = $r->run(show => '--format=%P' => $i);
chomp $parents;
my @branches = split ' ', $r->run(branch => '--contains' => (split ' ', $parents)[-1]);
if (any {$_ eq $lbranch} @branches) {
$first_merge = $i
} elsif ($first_merge) {
last
}
}
unless ($first_merge) {
my @log = $r->run(log => '--format=%H|%s' => $branch);
foreach(@log) {
my ($sha, $msg) = split /\|/;
if ($msg =~ /merge/i) {
if ($msg =~ /\Q$lbranch\E/i) {
$first_merge = $sha
} else {
last
}
}
}
}
die "Can't find first merge point for branch $lbranch\n"
unless $first_merge;
### COMMITS FOR CHERRYPICKING
my $ids;
{
my $fp = $branch eq 'devel' ? 'master' : ($lbranch =~ s/^spec/devel/r);
$ids = Hash::Ordered->new(
map {($_, undef)}
(
$r->run('rev-list' => '--reverse' => $fp . '..' . $lbranch),
$r->run('rev-list' => '--reverse' => $first_merge . '..' . $branch)
)
);
}
### NOTES PROCESSING
my $notes = Git::WorkFlow::AstraVer::Notes->new($r);
if ($notes->empty) {
print "### NOTES ARE EMPTY\n";
}
### ORDER DETERMINATION
{
my @attached;
my $iter = $ids->iterator();
while (my ($sha, $value) = $iter->()) {
my $iter = $notes->iterator;
while (my $nsha = $iter->()) {
my $note = $notes->get($nsha);
if ($note->{attach} && $sha =~ /\A$note->{attach}/) {
delete $note->{attach};
my $nvalue = { %$note, attached => $nsha };
if ($value) {
push @$value, $nvalue
} else {
$value = [ $nvalue ]
}
$ids->set($sha => { updates => $value });
$notes->delete($nsha);
push @attached, $nsha;
}
}
}
# updates sorting
$iter = $ids->iterator();
while (my ($sha, $value) = $iter->()) {
if ($value && exists $value->{updates} && @{$value->{updates}} > 1) {
# sorting
my @keys = $ids->keys;
my %idx = map {
my $v = $_;
my $c = first {$v->{attached} eq $keys[$_]} 0..$#keys;
die "Dangling note: $v->{obj}\n" unless defined $c;
($v, $c)
} @{$value->{updates}};
$ids->set($sha => { updates => [sort {$idx{$a} <=> $idx{$b}} @{$value->{updates}}] })
}
}
# delete only after updates sorting
foreach (@attached) {
$ids->delete($_) # Existance is guaranted
}
}
{
# attach regular notes
my $n_iter = $notes->iterator;
while (my $sha = $n_iter->()) {
my $val = $ids->get($sha);
if ($val) {
$val->{note} = $notes->get($sha)
} elsif ($ids->exists($sha)) {
$ids->set($sha => {note => $notes->get($sha)})
} # else - old notes should not be added
$notes->delete($sha);
}
}
if ($print) {
my $iter = $ids->iterator;
while (my ($k, $v) = $iter->()) {
unless ($v) {
say commit_info($r, $k)
} else {
if (exists $v->{updates}) {
say commit_info($r, $k) . (( exists $v->{note} )? " NOTE: " . $v->{note}{content} : '');
foreach (@{$v->{updates}}) {
say " " . commit_info($r, $_->{attached}) . " NOTE: " . $_->{content}
}
} else {
say commit_info($r, $k) . " NOTE: " . $v->{note}{content}
}
}
}
exit 0;
}
$r->run(checkout => $nbranch);
my $iter = $ids->iterator;
while (my ($k, $v) = $iter->()) {
if ($v) {
if (exists $v->{updates}) { ## NOTES, UPDATES
say '{';
my $info = commit_info($r, $k);
my $t = $v->{note}{tags} || [[],[]];
say "\tCP: $info";
git_cherry_pick($r, ['cherry-pick' => $k], sub {
stop( BOLD PUSHCOLOR RED
'conflict resolution' .
POPCOLOR .
" (first commit)\n" . RESET)
});
foreach(@{$v->{updates}}) {
my $info = commit_info($r, $_->{attached});
say "\tCP+: $info";
git_cherry_pick($r, ['cherry-pick' => '--no-commit' => $_->{attached}], sub {
stop( BOLD PUSHCOLOR RED
'conflict resolution' .
POPCOLOR .
" (update commit)\n" . RESET)
});
$t = Git::WorkFlow::AstraVer::Notes::_summarize_tags($t, $_->{tags});
$r->run('notes' => 'remove' => $_->{attached});
$r->run('commit' => '-a' => '--amend' => '-F' => '.git/COMMIT_EDITMSG');
}
stop( BOLD PUSHCOLOR GREEN
'message editing' .
POPCOLOR .
" (final editing)|commit dropping\n" . RESET);
unless (check_drop_command($r)) {
if (any {$_ eq 'moved_to_devel' || $_ eq 'moved_to_spec'} @{$t->[0]}) {
stop("PATCH: $info is marked as moved_to_[devel/spec] and will be removed\n");
$r->run(reset => '--hard' => 'HEAD~1');
next
}
my $note = Git::WorkFlow::AstraVer::Notes::_tags_to_string($t);
if ($note) {
say "\tNOTE: $note";
$r->run('notes' => 'add' => '-m' => $note);
}
} else {
drop_latest_commit($r)
}
$r->run('notes' => 'remove' => $k);
say '}';
} else { ## NOTES
say '{';
my $drop = 0;
my $info = commit_info($r, $k);
if (any {$_ eq 'moved_to_devel' || $_ eq 'moved_to_spec'} @{$v->{note}{tags}[0]}) {
stop("PATCH: $info moved_to_[devel/spec]\n");
next
}
say "\tCP: $info";
git_cherry_pick($r, ['cherry-pick' => $k], sub {
stop( BOLD PUSHCOLOR RED
'conflict resolution' .
POPCOLOR .
" (commit with note)|commit dropping\n" . RESET);
$drop = check_drop_command($r);
});
unless ($drop) {
say "\tNOTE: $v->{note}{content}";
$r->run('notes' => 'add' => '-C' => $v->{note}{obj});
} else {
drop_latest_commit($r)
}
$r->run('notes' => 'remove' => $k);
say '}';
}
} else { ## NO NOTES
my $info = commit_info($r, $k);
say "CP: $info";
git_cherry_pick($r, ['cherry-pick' => $k], sub {
stop( BOLD PUSHCOLOR RED
'conflict resolution' .
POPCOLOR .
" (commit only)|commit dropping `git commit && touch .git/drop_commit`\n" . RESET);
if (check_drop_command($r)) {
drop_latest_commit($r)
}
});
}
}
{
my $moved = 'moved_to_' . ($branch eq 'devel' ? 'spec' : 'devel');
my @notes = $notes->search_tag($moved);
if (@notes) {
print "NOTE $moved:\n";
print join("\n", @notes);
print "\n";
}
}