gitprep / script / import_rep /
Newer Older
131 lines | 2.761kb
add import_rep script to imp...
Yuki Kimoto authored on 2013-06-29
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
Yuki Kimoto authored on 2016-04-14
13
use Mojo::Server;
add import_rep script to imp...
Yuki Kimoto authored on 2013-06-29
14

            
15
no warnings;
fix tests
Yuki Kimoto authored on 2016-04-14
16
my $app = Mojo::Server->new->load_app("$FindBin::Bin/gitprep");
add import_rep script to imp...
Yuki Kimoto authored on 2013-06-29
17
my $dbi = $app->dbi;
18
my $manager = $app->manager;
19
my $git = $app->git;
20

            
fix import_rep
Yuki Kimoto authored on 2016-04-22
21
my $user_id;
add import_rep script to imp...
Yuki Kimoto authored on 2013-06-29
22
my $only_push;
improve improt_rep error mes...
Yuki Kimoto authored on 2013-06-29
23
my $help;
add import_rep script to imp...
Yuki Kimoto authored on 2013-06-29
24

            
25
GetOptions(
fix import_rep
Yuki Kimoto authored on 2016-04-22
26
  'user=s' => \$user_id,
improve improt_rep error mes...
Yuki Kimoto authored on 2013-06-29
27
  'help=s' => \$help
add import_rep script to imp...
Yuki Kimoto authored on 2013-06-29
28
);
29

            
improve improt_rep error mes...
Yuki Kimoto authored on 2013-06-29
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...
Yuki Kimoto authored on 2013-06-29
41
my $rep_dir = shift;
improve improt_rep error mes...
Yuki Kimoto authored on 2013-06-29
42
die "$usage\n" if !defined $rep_dir || $help;
add import_rep script to imp...
Yuki Kimoto authored on 2013-06-29
43

            
44
# Check user
fix import_rep
Yuki Kimoto authored on 2016-04-22
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...
Yuki Kimoto authored on 2013-06-29
47

            
48
for my $rep (glob "$rep_dir/*") {
49

            
50
  if (-d $rep && $rep =~ /\.git$/) {
fix import_rep
Yuki Kimoto authored on 2016-04-22
51
    my $project_id = basename $rep;
52
    $project_id =~ s/\.git$//;
add import_rep script to imp...
Yuki Kimoto authored on 2013-06-29
53
    
fix import_rep
Yuki Kimoto authored on 2016-04-22
54
    my $project = $dbi->model('project')->select(
remove table __ prefix
Yuki Kimoto authored on 2016-06-11
55
      where => {'user.id' => $user_id, 'project.id' => $project_id}
fix import_rep
Yuki Kimoto authored on 2016-04-22
56
    )->one;
add import_rep script to imp...
Yuki Kimoto authored on 2013-06-29
57
    
58
    # Create project
fix import_rep
Yuki Kimoto authored on 2016-04-22
59
    if ($project) {
60
      warn "Repository $project_id Already exists : $@\n";
add import_rep script to imp...
Yuki Kimoto authored on 2013-06-29
61
    }
62
    else {
63
      eval {
fix import_rep
Yuki Kimoto authored on 2016-04-22
64
        $manager->create_project($user_id, $project_id);
add import_rep script to imp...
Yuki Kimoto authored on 2013-06-29
65
      };       
66
      if ($@) {
fix import_rep
Yuki Kimoto authored on 2016-04-22
67
        warn "Can't creat repository $project_id: $@\n";
add import_rep script to imp...
Yuki Kimoto authored on 2013-06-29
68
      }
69
      else {
fix import_rep
Yuki Kimoto authored on 2016-04-22
70
        warn "Create repository $project_id\n";
add import_rep script to imp...
Yuki Kimoto authored on 2013-06-29
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
Yuki Kimoto authored on 2016-04-22
87
    eval {$git->description(app->rep_info($user_id, $project_id), $description) };
add import_rep script to imp...
Yuki Kimoto authored on 2013-06-29
88
    if ($@) {
fix import_rep
Yuki Kimoto authored on 2016-04-22
89
      warn "Can't update description $project_id\n";
add import_rep script to imp...
Yuki Kimoto authored on 2013-06-29
90
    }
91
    
92
    # Push repository
93
    chdir $rep
94
      or warn "Can't change directory $rep: $!\n";
remove rep_path
Yuki Kimoto authored on 2016-04-16
95
    
fix import_rep
Yuki Kimoto authored on 2016-04-22
96
    my $remote_rep_info = $app->rep_info($user_id, $project_id);
remove rep_path
Yuki Kimoto authored on 2016-04-16
97
    my $remote_rep_git_dir = $remote_rep_info->{git_dir};
improve import_rep script to...
Yuki Kimoto authored on 2013-07-20
98
    
99
    # push branches
100
    {
remove rep_path
Yuki Kimoto authored on 2016-04-16
101
      my @cmd = ('git', 'push', $remote_rep_git_dir, '--all');
improve import_rep script to...
Yuki Kimoto authored on 2013-07-20
102
      system(@cmd) == 0
103
        or warn "Can't push branches: @cmd";
104
    }
105
    
106
    # push tags
107
    {
remove rep_path
Yuki Kimoto authored on 2016-04-16
108
      my @cmd = ('git', 'push', $remote_rep_git_dir, '--tags');
improve import_rep script to...
Yuki Kimoto authored on 2013-07-20
109
      system(@cmd) == 0
110
        or warn "Can't push tags: @cmd";
111
    }
add import_rep script to imp...
Yuki Kimoto authored on 2013-06-29
112
  }
113
}
114

            
115
=head1 NAME
116

            
improve improt_rep error mes...
Yuki Kimoto authored on 2013-06-29
117
import_rep - Import existing repositories into GitPrep.
add import_rep script to imp...
Yuki Kimoto authored on 2013-06-29
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