-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpost-rewrite-filter.pl
executable file
·60 lines (50 loc) · 1.65 KB
/
post-rewrite-filter.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
#!/usr/bin/env perl
# $Author: $Format:%an <%ae>$ $
# $Date: $Format:%ai$ $
# $Revision: $Format:%h$ $
use strict;
use warnings;
use Git;
use Archive::Zip qw(:ERROR_CODES);
my $git = Git->repository();
if (!defined $git) {
print STDERR "Must be executed from within a Git repository.\n";
exit 1;
}
my @commits = ();
my %uniq = ();
{
my $commit;
while (<STDIN>) {
$commit = (split)[1]
and ++$uniq{$commit} == 1
and push @commits, $commit;
}
}
undef %uniq;
my @files = grep {
++$uniq{$_} == 1 && -e
} map {
$git->command('diff-tree', '--no-commit-id', '--name-only', '-r', $_)
} @commits;
# extract extant files following an amend or rebase
for my $file (@files) {
my $commit = $git->command_oneline('log', '-1', '--format=%H', 'HEAD', '--', $file);
my ($fh, $ctx) = $git->command_output_pipe('archive', '--worktree-attributes', '--format=zip', '-0', $commit, $file);
my $zip_file = do { local $/; <$fh> };
$git->command_close_pipe($fh, $ctx);
use IO::String;
my $zh = IO::String->new($zip_file);
my $zip = Archive::Zip->new();
$zip->readFromFileHandle($zh) == AZ_OK or die 'Couldn\'t open original ' . $file . '.';
$zip->extractMember($file);
close($zh) or die 'Failed to close in-memory zip:' . $!;
$git->command('update-index', $file);
}
my $repo_path = $git->repo_path();
my $keywords_path = $repo_path.'/keywords';
my $files_path = $keywords_path.'/files';
my $use_orig_head_path = $keywords_path.'/use_orig_head';
unlink $files_path if -e $files_path;
unlink $use_orig_head_path if -e $use_orig_head_path;
rmdir $keywords_path if -d $keywords_path;