-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpost-merge-filter.pl
executable file
·51 lines (40 loc) · 1.53 KB
/
post-merge-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
#!/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 $squash_merge = $ARGV[0];
exit 0 if $squash_merge;
my @files = $git->command('diff-tree', '--name-only', '-r', 'ORIG_HEAD', 'HEAD');
# full file list from ORIG_HEAD to HEAD
# could contain renamed or deleted files
@files = grep { -e } @files;
# extract extant files following a merge
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;