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