-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathav-import-to-master
executable file
·128 lines (104 loc) · 3.12 KB
/
av-import-to-master
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
#!/usr/bin/env perl
use warnings;
use strict;
use feature qw/say/;
use GnuPG;
use Archive::Extract;
use Git::Repository;
use File::Slurp qw/read_file/;
use FindBin;
use Term::ReadKey;
use File::Find;
use File::Temp qw/tempdir/;
use File::Copy::Recursive qw/dirmove/;
use Getopt::Long qw(:config pass_through);
use lib::abs 'lib';
use Git::WorkFlow::AstraVer::Repo qw/check_changes_exist/;
use Interactive::Background;
BEGIN {
eval {
require Smart::Comments;
Smart::Comments->import();
}
}
die "$0 <ciphered archive>\n"
unless @ARGV;
my $file;
my $dir = $ENV{CURRENT_PROJECT};
my $config_file = "$FindBin::Bin/.config";
my $keep_archive = 0;
GetOptions("c|config=s" => \$config_file,
"f|file=s" => \$file,
"k|keep!" => \$keep_archive,
"d|dir=s" => \$dir)
or die("Error in command line arguments\n");
if (!defined $file && defined $ARGV[0]) {
$file = shift @ARGV
} elsif (defined $file && defined $ARGV[0]) {
die "Error in command line arguments: '$file' and '$ARGV[0]'\n"
}
die "Unknown arguments: @ARGV\n"
if @ARGV;
die("Can't access file $file\n")
unless -f $file && -r _;
die("Can't determine git repository\n")
unless -d $dir;
my %config;
if (-r $config_file) {
my @l = read_file($config_file);
if (@l > 1) {
die "Wrong format of .config file.\n"
} else {
$config{passphrase} = $l[0];
}
} else {
ReadMode('noecho');
print 'Enter your password: ';
$config{passphrase} = ReadLine(0);
print "\n";
ReadMode(0);
}
chomp $config{passphrase};
### GIT CHECKOUT
my $r = Git::Repository->new(work_tree => $dir);
if (check_changes_exist($r)) {
die "There are changes in repository. You should commit/remove them first.\n";
}
$r->run('checkout' => 'master');
{
my $output = ($file =~ s/\.(pgp|gpg)$//r);
if ($file ne $output) {
### GNUPG DECRYPTING
GnuPG->new()->decrypt(ciphertext => $file, output => $output, passphrase => $config{passphrase});
}
### ARCHIVE EXTRACTING
my $extract_dir = tempdir();
unless(Archive::Extract->new(archive => $output)->extract(to => $extract_dir)) {
unlink $output unless $keep_archive;
die "Can't extract\n";
}
unlink $output unless $keep_archive;
### MOVING FILES
my @broken_links;
find(sub { if ( -l $_ && ! -e readlink($_) ) { push @broken_links, $File::Find::name } }, $extract_dir);
foreach (@broken_links) {
my $rel_name = substr($_, length($extract_dir));
warn "Link $rel_name is broken. Skipping ...\n";
unlink $_;
}
my @dirs;
find(sub { if ( -d $_ && $_ eq 'parsec' ) { push @dirs, $File::Find::name } }, $extract_dir);
@dirs = sort {length($a) <=> length($b)} @dirs;
dirmove(@dirs ? $dirs[-1] : $extract_dir, $dir);
while(!rmdir($extract_dir)) {
print "Can't properly copy extracted files to repo directory. Please, look at $extract_dir\n";
stop();
}
}
my @time = (localtime)[3,4,5];
my $date = sprintf('%02d.%02d.%d', $time[0], $time[1] + 1, $time[2] - 100);
my $msg = "update: $date";
### ADDING parsec/*
$r->run('add' => 'parsec/*');
### COMMIT MSG: $msg
$r->run('commit' => '-a' => '-m' => $msg);