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'; |
|
fix tests
|
13 |
use Mojo::Server; |
add import_rep script to imp...
|
14 | |
15 |
no warnings; |
|
fix tests
|
16 |
my $app = Mojo::Server->new->load_app("$FindBin::Bin/gitprep"); |
add import_rep script to imp...
|
17 |
my $dbi = $app->dbi; |
18 |
my $manager = $app->manager; |
|
19 |
my $git = $app->git; |
|
20 | ||
fix import_rep
|
21 |
my $user_id; |
add import_rep script to imp...
|
22 |
my $only_push; |
improve improt_rep error mes...
|
23 |
my $help; |
add import_rep script to imp...
|
24 | |
25 |
GetOptions( |
|
fix import_rep
|
26 |
'user=s' => \$user_id, |
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 |
|
fix import_rep
|
45 |
my $user = $dbi->model('user')->select(where => {id => $user_id})->one; |
46 |
die "User $user_id is not exists\n" unless $user; |
|
add import_rep script to imp...
|
47 | |
48 |
for my $rep (glob "$rep_dir/*") { |
|
49 | ||
50 |
if (-d $rep && $rep =~ /\.git$/) { |
|
fix import_rep
|
51 |
my $project_id = basename $rep; |
52 |
$project_id =~ s/\.git$//; |
|
add import_rep script to imp...
|
53 |
|
fix import_rep
|
54 |
my $project = $dbi->model('project')->select( |
remove table __ prefix
|
55 |
where => {'user.id' => $user_id, 'project.id' => $project_id} |
fix import_rep
|
56 |
)->one; |
add import_rep script to imp...
|
57 |
|
58 |
# Create project |
|
fix import_rep
|
59 |
if ($project) { |
60 |
warn "Repository $project_id Already exists : $@\n"; |
|
add import_rep script to imp...
|
61 |
} |
62 |
else { |
|
63 |
eval { |
|
fix import_rep
|
64 |
$manager->create_project($user_id, $project_id); |
add import_rep script to imp...
|
65 |
}; |
66 |
if ($@) { |
|
fix import_rep
|
67 |
warn "Can't creat repository $project_id: $@\n"; |
add import_rep script to imp...
|
68 |
} |
69 |
else { |
|
fix import_rep
|
70 |
warn "Create repository $project_id\n"; |
add import_rep script to imp...
|
71 |
} |
72 |
} |
|
73 |
|
|
74 |
# Copy description |
|
75 |
my $description = do { |
|
76 |
my $success = open my $fh, '<', "$rep/description"; |
|
77 |
|
|
78 |
if ($success) { |
|
79 |
local $/; |
|
80 |
<$fh> |
|
81 |
} |
|
82 |
else { |
|
83 |
''; |
|
84 |
} |
|
85 |
}; |
|
86 |
$description = decode('UTF-8', $description); |
|
fix import_rep
|
87 |
eval {$git->description(app->rep_info($user_id, $project_id), $description) }; |
add import_rep script to imp...
|
88 |
if ($@) { |
fix import_rep
|
89 |
warn "Can't update description $project_id\n"; |
add import_rep script to imp...
|
90 |
} |
91 |
|
|
92 |
# Push repository |
|
93 |
chdir $rep |
|
94 |
or warn "Can't change directory $rep: $!\n"; |
|
remove rep_path
|
95 |
|
fix import_rep
|
96 |
my $remote_rep_info = $app->rep_info($user_id, $project_id); |
remove rep_path
|
97 |
my $remote_rep_git_dir = $remote_rep_info->{git_dir}; |
improve import_rep script to...
|
98 |
|
99 |
# push branches |
|
100 |
{ |
|
remove rep_path
|
101 |
my @cmd = ('git', 'push', $remote_rep_git_dir, '--all'); |
improve import_rep script to...
|
102 |
system(@cmd) == 0 |
103 |
or warn "Can't push branches: @cmd"; |
|
104 |
} |
|
105 |
|
|
106 |
# push tags |
|
107 |
{ |
|
remove rep_path
|
108 |
my @cmd = ('git', 'push', $remote_rep_git_dir, '--tags'); |
improve import_rep script to...
|
109 |
system(@cmd) == 0 |
110 |
or warn "Can't push tags: @cmd"; |
|
111 |
} |
|
add import_rep script to imp...
|
112 |
} |
113 |
} |
|
114 | ||
115 |
=head1 NAME |
|
116 | ||
improve improt_rep error mes...
|
117 |
import_rep - Import existing repositories into GitPrep. |
add import_rep script to imp...
|
118 | |
119 |
=head1 USAGE |
|
120 | ||
121 |
./import_rep -u kimoto rep_dir |
|
122 | ||
123 |
rep_dir must contains git respoitories |
|
124 | ||
125 |
rep_dir/project1.git |
|
126 |
/project2.git |
|
127 |
/project3.git |
|
128 |
/project3.git |
|
129 | ||
130 |
If C<description> file exists in git repository, it is copied. |
|
131 |