-
Notifications
You must be signed in to change notification settings - Fork 0
/
editor
executable file
·114 lines (95 loc) · 2.95 KB
/
editor
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
#!/opt/local/bin/perl -w
use warnings;
use strict;
use Data::Dumper qw(Dumper);
# my $re_is_backup = qr/^do backup at /;
my $re_is_backup = qr/./;
my $file = $ARGV[0];
open FILE, $file or die("can't open $file: $!" );
my $t = select FILE;
$/ = undef; # slurp!
select $t;
my $input = <FILE>;
close $file;
sub abort {
system( "echo -n > $file" );
exit
}
my @input = split "\n", $input;
if( $input =~ /Rebase .* onto / ) {
# initial pass of rebase: lists all the commits that are eligible for
# rewriting. We'll read them in, then re-save the file with marks for
# squashing any of the them which are a) older than our cutoff age and
# b) backups, per the standard log message used for backups.
my $keep = $ENV{KEEP};
my $day = 24 * 60 * 60;
my $multiplier = {
fast => 5, # seconds
minutely => 60, # seconds
slow => 5 * 60,
hourly => 1 * 60 * 60,
daily => 1 * $day,
weekly => 7 * $day,
monthly => 30 * $day,
}->{ $ENV{FREQUENCY} };
my $cutoff = time() - ( $keep * $multiplier );
my @commits = split "\n", `git-log --pretty=format:'%h %at %s' master..$ENV{BRANCH}`;
my $ages = {};
foreach my $c ( @commits ) {
my( $h, $time, $desc ) = split "\t", $c;
$ages->{$h} = $time;
}
open OUT, "> $file" or die( "can't open $file for output: $!" );
my @lines = split "\n", $input;
my $non_squashed = 0;
my $count_squashed;
for my $l( @lines ) {
next if $l =~ /^#/; # skip comments;
next unless $l;
my( $action, $hash, $desc ) = split " ", $l, 3;
my $squashed = 0;
if( $non_squashed ) {
if( $desc =~ $re_is_backup ) {
if( $ages->{$hash} ) {
if( $ages->{$hash} < $cutoff ) {
$squashed = 1;
$count_squashed ++;
# print "squash $hash $desc\n";
print OUT "squash $hash $desc\n";
} else {
# warn "age";
}
} else {
# warn "no entry in ages hash";
}
} else {
# warn "doesn't look like a backup"
}
}
unless( $squashed ) {
# print "$action $hash $desc\n";
print OUT "$action $hash $desc\n";
$non_squashed++;
}
}
abort unless $count_squashed;
close OUT;
} else {
# second pass of rebase, where it asks for a commit message
# for the Heir of the Squash, which by default contains the original
# message, plus the message of the Dearly Departed.
# Reverse the order of checkin messages, so that the message reflects the most recent backup that's the effective result of the squashing.
my @t = split /\n+# this is the .* commit message:\n+/i, $input;
@t = map {
join( "\n",
grep {
! /^#/
}
split( "\n", $_ )
)
} @t;
my $result = join( "\n\n---\n\n", reverse @t );
open OUT, "> $file";
print OUT $result;
close OUT;
}