add import_rep script to imp...
|
1 |
#!/usr/bin/env perl |
2 | ||
3 |
use strict; |
|
4 |
use warnings; |
|
5 |
use utf8; |
|
6 |
use FindBin; |
|
7 |
use lib "$FindBin::Bin/../mojo/lib"; |
|
8 |
use lib "$FindBin::Bin/../lib"; |
|
9 |
use lib "$FindBin::Bin/../extlib/lib/perl5"; |
|
10 |
use Getopt::Long; |
|
11 |
use File::Basename 'basename'; |
|
12 |
use Gitprep; |
|
13 |
use Encode 'decode'; |
|
14 | ||
15 |
no warnings; |
|
16 |
my $app = Gitprep->new; |
|
17 |
my $dbi = $app->dbi; |
|
18 |
my $manager = $app->manager; |
|
19 |
my $git = $app->git; |
|
20 | ||
21 |
my $user; |
|
22 |
my $only_push; |
|
improve improt_rep error mes...
|
23 |
my $help; |
add import_rep script to imp...
|
24 | |
25 |
GetOptions( |
|
26 |
'user=s' => \$user, |
|
improve improt_rep error mes...
|
27 |
'help=s' => \$help |
add import_rep script to imp...
|
28 |
); |
29 | ||
improve improt_rep error mes...
|
30 |
my $usage = <<'EOS'; |
31 |
Usage: |
|
32 | ||
33 |
./import_rep -u USER REPOSITORY_DIR |
|
34 | ||
35 |
Example: |
|
36 | ||
37 |
./import_rep -u kimoto reps |
|
38 |
EOS |
|
39 | ||
40 | ||
add import_rep script to imp...
|
41 |
my $rep_dir = shift; |
improve improt_rep error mes...
|
42 |
die "$usage\n" if !defined $rep_dir || $help; |
add import_rep script to imp...
|
43 | |
44 |
# Check user |
|
45 |
my $user_row = $dbi->model('user')->select(id => $user)->one; |
|
46 |
die "User $user is not exists\n" unless $user_row; |
|
47 | ||
48 |
for my $rep (glob "$rep_dir/*") { |
|
49 | ||
50 |
if (-d $rep && $rep =~ /\.git$/) { |
|
51 |
my $project = basename $rep; |
|
52 |
$project =~ s/\.git$//; |
|
53 |
|
|
54 |
my $project_row = $dbi->model('project')->select(id => [$user, $project])->one; |
|
55 |
|
|
56 |
# Create project |
|
57 |
if ($project_row) { |
|
58 |
warn "Repository $project Already exists : $@\n"; |
|
59 |
} |
|
60 |
else { |
|
61 |
eval { |
|
62 |
$manager->create_project($user, $project); |
|
63 |
}; |
|
64 |
if ($@) { |
|
65 |
warn "Can't creat repository $project: $@\n"; |
|
66 |
} |
|
67 |
else { |
|
68 |
warn "Create repository $project\n"; |
|
69 |
} |
|
70 |
} |
|
71 |
|
|
72 |
# Copy description |
|
73 |
my $description = do { |
|
74 |
my $success = open my $fh, '<', "$rep/description"; |
|
75 |
|
|
76 |
if ($success) { |
|
77 |
local $/; |
|
78 |
<$fh> |
|
79 |
} |
|
80 |
else { |
|
81 |
''; |
|
82 |
} |
|
83 |
}; |
|
84 |
$description = decode('UTF-8', $description); |
|
85 |
eval {$git->description($user, $project, $description) }; |
|
86 |
if ($@) { |
|
87 |
warn "Can't update description $project\n"; |
|
88 |
} |
|
89 |
|
|
90 |
# Push repository |
|
91 |
chdir $rep |
|
92 |
or warn "Can't change directory $rep: $!\n"; |
|
add import_rep test
|
93 |
my $remote_rep = $git->rep($user, $project); |
improve import_rep script to...
|
94 |
|
95 |
# push branches |
|
96 |
{ |
|
97 |
my @cmd = ('git', 'push', $remote_rep, '--all'); |
|
98 |
system(@cmd) == 0 |
|
99 |
or warn "Can't push branches: @cmd"; |
|
100 |
} |
|
101 |
|
|
102 |
# push tags |
|
103 |
{ |
|
104 |
my @cmd = ('git', 'push', $remote_rep, '--tags'); |
|
105 |
system(@cmd) == 0 |
|
106 |
or warn "Can't push tags: @cmd"; |
|
107 |
} |
|
add import_rep script to imp...
|
108 |
} |
109 |
} |
|
110 | ||
111 |
=head1 NAME |
|
112 | ||
improve improt_rep error mes...
|
113 |
import_rep - Import existing repositories into GitPrep. |
add import_rep script to imp...
|
114 | |
115 |
=head1 USAGE |
|
116 | ||
117 |
./import_rep -u kimoto rep_dir |
|
118 | ||
119 |
rep_dir must contains git respoitories |
|
120 | ||
121 |
rep_dir/project1.git |
|
122 |
/project2.git |
|
123 |
/project3.git |
|
124 |
/project3.git |
|
125 | ||
126 |
If C<description> file exists in git repository, it is copied. |
|
127 |