gitprep / lib / Gitprep / Manager.pm /
Newer Older
966 lines | 25.288kb
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
1
package Gitprep::Manager;
2
use Mojo::Base -base;
3

            
4
use Carp 'croak';
fixed bug that setting page ...
Yuki Kimoto authored on 2013-05-15
5
use Encode 'encode';
fix description file not cop...
Yuki Kimoto authored on 2013-05-25
6
use File::Copy qw/move copy/;
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
7
use File::Path qw/mkpath rmtree/;
8
use File::Temp ();
add key delete feature and u...
gitprep authored on 2014-05-19
9
use Fcntl ':flock';
10
use Carp 'croak';
11
use File::Copy qw/copy move/;
fix bug that in CGI reposito...
Yuki Kimoto authored on 2015-11-05
12
use File::Spec;
13
use Gitprep::Util;
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
14

            
15
has 'app';
add key delete feature and u...
gitprep authored on 2014-05-19
16
has 'authorized_keys_file';
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
17

            
cleanup prepare merge
Yuki Kimoto authored on 2016-04-23
18
has '_tmp_branch' => '__gitprep_tmp_branch__';
19

            
20
sub prepare_merge {
fix tests
Yuki Kimoto authored on 2016-04-27
21
  my ($self, $work_rep_info, $base_rep_info, $base_branch, $target_rep_info, $target_branch) = @_;
add lock system
Yuki Kimoto authored on 2016-04-18
22
  
cleanup prepare merge
Yuki Kimoto authored on 2016-04-23
23
  # Fetch base repository
fix tests
Yuki Kimoto authored on 2016-04-27
24
  my $base_user_id = $base_rep_info->{user};
fix pull_request table bug
Yuki Kimoto authored on 2016-04-27
25
  my @git_fetch_base_cmd = $self->app->git->cmd($work_rep_info, 'fetch', 'origin');
cleanup prepare merge
Yuki Kimoto authored on 2016-04-23
26
  Gitprep::Util::run_command(@git_fetch_base_cmd)
27
    or Carp::croak "Can't execute git fetch: @git_fetch_base_cmd";
add lock system
Yuki Kimoto authored on 2016-04-18
28
  
cleanup prepare merge
Yuki Kimoto authored on 2016-04-23
29
  # Fetch target repository
fix tests
Yuki Kimoto authored on 2016-04-27
30
  my @git_fetch_target_cmd = $self->app->git->cmd($work_rep_info, 'fetch', $target_rep_info->{git_dir});
cleanup prepare merge
Yuki Kimoto authored on 2016-04-23
31
  Gitprep::Util::run_command(@git_fetch_target_cmd)
32
    or Carp::croak "Can't execute git fetch: @git_fetch_target_cmd";
cleanup merge and push logic
Yuki Kimoto authored on 2016-04-27
33

            
34
  # Ensure no diff
35
  my @git_reset_hard_cmd = $self->app->git->cmd(
36
    $work_rep_info,
37
    'reset',
38
    '--hard'
39
  );
40
  Gitprep::Util::run_command(@git_reset_hard_cmd)
41
    or Carp::croak "Can't execute git reset --hard: @git_reset_hard_cmd";
42

            
43
  # Checkout first branch
44
  my $tmp_branch = $self->_tmp_branch;
45
  my $branch_names = $self->app->git->branch_names($work_rep_info);
46
  my $first_branch;
47
  for my $branch_name (@$branch_names) {
48
    if ($branch_name ne $tmp_branch) {
49
      $first_branch = $branch_name;
50
      last;
51
    }
52
  }
53
  my @git_checkout_first_branch = $self->app->git->cmd(
54
    $work_rep_info,
55
    'checkout',
56
    $first_branch
57
  );
58
  Gitprep::Util::run_command(@git_checkout_first_branch)
59
    or Carp::croak "Can't execute git checkout: @git_checkout_first_branch";
60
  
61
  # Delete temparary branch if it eixsts
62
  if (grep { $_ eq $tmp_branch } @$branch_names) {
63
    my @git_branch_remove_cmd = $self->app->git->cmd(
64
      $work_rep_info,
65
      'branch',
66
      '-D',
67
      $tmp_branch
68
    );
69
    Gitprep::Util::run_command(@git_branch_remove_cmd)
70
      or Carp::croak "Can't execute git branch: @git_branch_remove_cmd";
71
  }
72

            
73
  # Create temparary branch
74
  my @git_branch_cmd = $self->app->git->cmd(
75
    $work_rep_info,
76
    'branch',
77
    $tmp_branch
78
  );
79
  Gitprep::Util::run_command(@git_branch_cmd)
80
    or Carp::croak "Can't execute git branch: @git_branch_cmd";
cleanup prepare merge
Yuki Kimoto authored on 2016-04-23
81
  
82
  # Checkout tmp branch and git reset --hard from my remote branch
83
  my @git_checkout_tmp_branch = $self->app->git->cmd(
84
    $work_rep_info,
85
    'checkout',
cleanup merge and push logic
Yuki Kimoto authored on 2016-04-27
86
    $tmp_branch
cleanup prepare merge
Yuki Kimoto authored on 2016-04-23
87
  );
88
  Gitprep::Util::run_command(@git_checkout_tmp_branch)
89
    or Carp::croak "Can't execute git checkout: @git_checkout_tmp_branch";
90
  
fix merge pull request bug
Yuki Kimoto authored on 2016-04-27
91
  # git reset --hard 
fix tests
Yuki Kimoto authored on 2016-04-27
92
  my $base_object_id = $self->app->git->ref_to_object_id($base_rep_info, $base_branch);
fix merge pull request bug
Yuki Kimoto authored on 2016-04-27
93
  my @git_reset_hard_base_cmd = $self->app->git->cmd(
cleanup prepare merge
Yuki Kimoto authored on 2016-04-23
94
    $work_rep_info,
95
    'reset',
96
    '--hard',
fix tests
Yuki Kimoto authored on 2016-04-27
97
    $base_object_id
cleanup prepare merge
Yuki Kimoto authored on 2016-04-23
98
  );
fix merge pull request bug
Yuki Kimoto authored on 2016-04-27
99
  Gitprep::Util::run_command(@git_reset_hard_base_cmd)
100
    or Carp::croak "Can't execute git reset --hard: @git_reset_hard_base_cmd";
add lock system
Yuki Kimoto authored on 2016-04-18
101
}
102

            
cleanup merge and push logic
Yuki Kimoto authored on 2016-04-27
103
sub merge {
add --no-ff option to merge ...
Yuki Kimoto authored on 2016-04-30
104
  my ($self, $work_rep_info, $target_rep_info, $target_branch, $pull_request_number) = @_;
cleanup merge and push logic
Yuki Kimoto authored on 2016-04-27
105
  
fix tests
Yuki Kimoto authored on 2016-04-27
106
  my $object_id = $self->app->git->ref_to_object_id($target_rep_info, $target_branch);
fix pull_request table bug
Yuki Kimoto authored on 2016-04-27
107
  
add --no-ff option to merge ...
Yuki Kimoto authored on 2016-04-30
108
  my $message;
fix tests
Yuki Kimoto authored on 2016-04-27
109
  my $target_user_id = $target_rep_info->{user};
add --no-ff option to merge ...
Yuki Kimoto authored on 2016-04-30
110
  if (defined $pull_request_number) {
111
    $message = "Merge pull request #$pull_request_number from $target_user_id/$target_branch";
112
  }
113
  else {
114
    $message = "Merge from $target_user_id/$target_branch";
115
  }
116
  
117
  # Merge
cleanup merge and push logic
Yuki Kimoto authored on 2016-04-27
118
  my @git_merge_cmd = $self->app->git->cmd(
119
    $work_rep_info,
120
    'merge',
add --no-ff option to merge ...
Yuki Kimoto authored on 2016-04-30
121
    '--no-ff',
122
    "--message=$message",
fix pull_request table bug
Yuki Kimoto authored on 2016-04-27
123
    $object_id
cleanup merge and push logic
Yuki Kimoto authored on 2016-04-27
124
  );
add --no-ff option to merge ...
Yuki Kimoto authored on 2016-04-30
125
  # 
cleanup merge and push logic
Yuki Kimoto authored on 2016-04-27
126
  
127
  my $success = Gitprep::Util::run_command(@git_merge_cmd);
128
  
129
  return $success;
130
}
131

            
add patch feature
Yuki Kimoto authored on 2016-06-04
132
sub get_patch {
133
  my ($self, $work_rep_info, $target_rep_info, $target_branch) = @_;
134
  
135
  my $object_id = $self->app->git->ref_to_object_id($target_rep_info, $target_branch);
136
  
137
  # Merge
138
  my @git_format_patch_cmd = $self->app->git->cmd(
139
    $work_rep_info,
140
    'format-patch',
141
    '--stdout',
142
    "HEAD..$object_id"
143
  );
144
  
145
  open my $fh, '-|', @git_format_patch_cmd
146
    or croak "Execute git format-patch cmd:@git_format_patch_cmd";
147
  
148
  my $patch = do { local $/; <$fh> };
149
  
150
  return $patch;
151
}
152

            
cleanup merge and push logic
Yuki Kimoto authored on 2016-04-27
153
sub push {
fix merge pull request bug
Yuki Kimoto authored on 2016-04-27
154
  my ($self, $work_rep_info, $base_branch) = @_;
cleanup merge and push logic
Yuki Kimoto authored on 2016-04-27
155
  
156
  # Push
fix merge pull request bug
Yuki Kimoto authored on 2016-04-27
157
  my $tmp_branch = $self->_tmp_branch;
158
  my @git_push_cmd = $self->app->git->cmd(
159
    $work_rep_info,
160
    'push',
161
    'origin',
162
    "$tmp_branch:$base_branch"
163
  );
cleanup merge and push logic
Yuki Kimoto authored on 2016-04-27
164
  Gitprep::Util::run_command(@git_push_cmd)
165
    or Carp::croak "Can't execute git push: @git_push_cmd";
166
}
167

            
cleanup prepare merge
Yuki Kimoto authored on 2016-04-23
168
sub lock_rep {
169
  my ($self, $rep_info) = @_;
170
  
171
  my $git_dir = $rep_info->{git_dir};
172
  my $lock_file = "$git_dir/config";
173
  
174
  open my $lock_fh, '<', $lock_file
175
    or croak "Can't open lock file $lock_file: $!";
176
    
177
  flock $lock_fh, LOCK_EX
178
    or croak "Can't lock $lock_file";
179
  
180
  return $lock_fh;
181
}
182

            
cleanup
Yuki Kimoto authored on 2016-04-16
183
sub create_work_rep {
improve create working repos...
Yuki Kimoto authored on 2016-04-15
184
  my ($self, $user, $project) = @_;
185
  
cleanup create working direc...
Yuki Kimoto authored on 2016-04-16
186
  # Remote repository
187
  my $rep_info = $self->app->rep_info($user, $project);
188
  my $rep_git_dir = $rep_info->{git_dir};
189
  
190
  # Working repository
191
  my $work_rep_info = $self->app->work_rep_info($user, $project);
192
  my $work_tree = $work_rep_info->{work_tree};
193
  
improve create working repos...
Yuki Kimoto authored on 2016-04-15
194
  # Create working repository if it don't exist
cleanup create working direc...
Yuki Kimoto authored on 2016-04-16
195
  unless (-e $work_tree) {
196

            
improve create working repos...
Yuki Kimoto authored on 2016-04-15
197
    # git clone
cleanup create working direc...
Yuki Kimoto authored on 2016-04-16
198
    my @git_clone_cmd = ($self->app->git->bin, 'clone', $rep_git_dir, $work_tree);
improve create working repos...
Yuki Kimoto authored on 2016-04-15
199
    Gitprep::Util::run_command(@git_clone_cmd)
200
      or croak "Can't git clone: @git_clone_cmd";
cleanup compare logic
Yuki Kimoto authored on 2016-04-18
201
    
improve create working repos...
Yuki Kimoto authored on 2016-04-15
202
    # Set user name
cleanup create working direc...
Yuki Kimoto authored on 2016-04-16
203
    my @git_config_user_name = $self->app->git->cmd(
204
      $work_rep_info,
improve create working repos...
Yuki Kimoto authored on 2016-04-15
205
      'config',
206
      'user.name',
207
      $user
208
    );
209
    Gitprep::Util::run_command(@git_config_user_name)
210
      or croak "Can't execute git config: @git_config_user_name";
211
    
fix mail bug
Yuki Kimoto authored on 2016-04-21
212
    # Set user email
213
    my $user_email = $self->app->dbi->model('user')->select('email', where => {id => $user})->value;
214
    my @git_config_user_email = $self->app->git->cmd(
cleanup create working direc...
Yuki Kimoto authored on 2016-04-16
215
      $work_rep_info,
improve create working repos...
Yuki Kimoto authored on 2016-04-15
216
      'config',
217
      'user.email',
fix mail bug
Yuki Kimoto authored on 2016-04-21
218
      "$user_email"
improve create working repos...
Yuki Kimoto authored on 2016-04-15
219
    );
fix mail bug
Yuki Kimoto authored on 2016-04-21
220
    Gitprep::Util::run_command(@git_config_user_email)
221
      or croak "Can't execute git config: @git_config_user_email";
improve create working repos...
Yuki Kimoto authored on 2016-04-15
222
  }
223
}
224

            
added admin tests
Yuki Kimoto authored on 2013-05-16
225
sub admin_user {
cleanup
Yuki Kimoto authored on 2013-05-13
226
  my $self = shift;
227
  
228
  # Admin user
229
  my $admin_user = $self->app->dbi->model('user')
added admin tests
Yuki Kimoto authored on 2013-05-16
230
    ->select(where => {admin => 1})->one;
cleanup
Yuki Kimoto authored on 2013-05-13
231
  
232
  return $admin_user;
233
}
234

            
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
235
sub default_branch {
fix project page
Yuki Kimoto authored on 2016-04-21
236
  my ($self, $user_id, $project_id, $default_branch) = @_;
237
  
238
  my $user_row_id = $self->api->get_user_row_id($user_id);
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
239
  
added default branch change ...
Yuki Kimoto authored on 2013-05-22
240
  # Set default branch
241
  my $dbi = $self->app->dbi;
242
  if (defined $default_branch) {
243
    $dbi->model('project')->update(
244
      {default_branch => $default_branch},
fix project page
Yuki Kimoto authored on 2016-04-21
245
      where => {user => $user_row_id, id => $project_id}
added default branch change ...
Yuki Kimoto authored on 2013-05-22
246
    );
247
  }
248
  else {
249
    # Get default branch
250
    my $default_branch = $dbi->model('project')
fix project page
Yuki Kimoto authored on 2016-04-21
251
      ->select('default_branch', where => {user => $user_row_id, id => $project_id})
added default branch change ...
Yuki Kimoto authored on 2013-05-22
252
      ->value;
253
    
254
    return $default_branch;
255
  }
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
256
}
257

            
show deleted repository in u...
Yuki Kimoto authored on 2013-05-15
258
sub fork_project {
cleanup
Yuki Kimoto authored on 2016-04-22
259
  my ($self, $forked_user_id, $user_id, $project_id) = @_;
fix fork
Yuki Kimoto authored on 2016-04-21
260
  
cleanup
Yuki Kimoto authored on 2016-04-22
261
  my $user_row_id = $self->api->get_user_row_id($user_id);
show deleted repository in u...
Yuki Kimoto authored on 2013-05-15
262
  
263
  # Fork project
264
  my $dbi = $self->app->dbi;
265
  my $error;
266
  eval {
267
    $dbi->connector->txn(sub {
268
      
269
      # Original project id
fix fork
Yuki Kimoto authored on 2016-04-21
270
      my $project = $dbi->model('project')->select(
cleanup
Yuki Kimoto authored on 2016-04-22
271
        {__MY__ => ['row_id', 'private']},
remove table __ prefix
Yuki Kimoto authored on 2016-06-11
272
        where => {'user.id' => $user_id, 'project.id' => $project_id}
version up and add descripti...
Yuki Kimoto authored on 2013-11-22
273
      )->one;
274
      
show deleted repository in u...
Yuki Kimoto authored on 2013-05-15
275
      # Create project
276
      eval {
277
        $self->_create_project(
cleanup
Yuki Kimoto authored on 2016-04-22
278
          $forked_user_id,
fix fork
Yuki Kimoto authored on 2016-04-21
279
          $project_id,
show deleted repository in u...
Yuki Kimoto authored on 2013-05-15
280
          {
fix fork
Yuki Kimoto authored on 2016-04-21
281
            original_project => $project->{row_id},
282
            private => $project->{private}
show deleted repository in u...
Yuki Kimoto authored on 2013-05-15
283
          }
284
        );
285
      };
286
      croak $error = $@ if $@;
287
      
288
      # Create repository
289
      eval {
cleanup
Yuki Kimoto authored on 2016-04-22
290
        $self->_fork_rep($user_id, $project_id, $forked_user_id, $project_id);
show deleted repository in u...
Yuki Kimoto authored on 2013-05-15
291
      };
292
      croak $error = $@ if $@;
293
    });
294
  };
295
  croak $error if $@;
296
}
297

            
cleanup
Yuki Kimoto authored on 2013-05-13
298
sub is_admin {
fix project page
Yuki Kimoto authored on 2016-04-21
299
  my ($self, $user_id) = @_;
cleanup
Yuki Kimoto authored on 2013-05-13
300
  
301
  # Check admin
302
  my $is_admin = $self->app->dbi->model('user')
fix project page
Yuki Kimoto authored on 2016-04-21
303
    ->select('admin', where => {id => $user_id})->value;
cleanup
Yuki Kimoto authored on 2013-05-13
304
  
305
  return $is_admin;
306
}
307

            
add private checkbox
Yuki Kimoto authored on 2013-11-16
308
sub is_private_project {
fix project page
Yuki Kimoto authored on 2016-04-21
309
  my ($self, $user_id, $project_id) = @_;
310
  
311
  my $user_row_id = $self->api->get_user_row_id($user_id);
add private checkbox
Yuki Kimoto authored on 2013-11-16
312
  
313
  # Is private
fix setting page
Yuki Kimoto authored on 2016-04-21
314
  my $private = $self->app->dbi->model('project')->select(
315
    'private', where => {user => $user_row_id, id => $project_id}
316
  )->value;
add private checkbox
Yuki Kimoto authored on 2013-11-16
317
  
318
  return $private;
319
}
320

            
fix project page
Yuki Kimoto authored on 2016-04-21
321
sub api { shift->app->gitprep_api }
322

            
323

            
fix network page
Yuki Kimoto authored on 2016-04-21
324
sub member_projects {
fix project page
Yuki Kimoto authored on 2016-04-21
325
  my ($self, $user_id, $project_id) = @_;
326
  
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
327
  # DBI
328
  my $dbi = $self->app->dbi;
329
  
cleanup
Yuki Kimoto authored on 2016-04-22
330
  # project id
331
  my $project_row_id = $dbi->model('project')->select(
332
    'project.row_id',
remove table __ prefix
Yuki Kimoto authored on 2016-06-11
333
    where => {'user.id' => $user_id, 'project.id' => $project_id}
cleanup
Yuki Kimoto authored on 2016-04-22
334
  )->value;
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
335
  
336
  # Members
cleanup
Yuki Kimoto authored on 2016-04-22
337
  my $member_projects = $dbi->model('project')->select(
fix project page
Yuki Kimoto authored on 2016-04-21
338
    [
fix network page
Yuki Kimoto authored on 2016-04-21
339
      {__MY__ => ['id']},
remove table __ prefix
Yuki Kimoto authored on 2016-06-11
340
      {user => ['id']}
fix project page
Yuki Kimoto authored on 2016-04-21
341
    ],
fix network page
Yuki Kimoto authored on 2016-04-21
342
    where => {
343
      original_project => $project_row_id,
344
    },
remove table __ prefix
Yuki Kimoto authored on 2016-06-11
345
    append => 'order by user.id, project.id'
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
346
  )->all;
347

            
cleanup
Yuki Kimoto authored on 2016-04-22
348
  return $member_projects;
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
349
}
350

            
351
sub create_project {
fix fork
Yuki Kimoto authored on 2016-04-21
352
  my ($self, $user_id, $project_id, $opts) = @_;
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
353
  
add private checkbox to new ...
Yuki Kimoto authored on 2013-11-26
354
  my $params = {};
355
  if ($opts->{private}) {
356
    $params->{private} = 1;
357
  }
358
  
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
359
  # Create project
fixed bug that setting page ...
Yuki Kimoto authored on 2013-05-15
360
  my $dbi = $self->app->dbi;
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
361
  my $error;
362
  eval {
363
    $dbi->connector->txn(sub {
fix fork
Yuki Kimoto authored on 2016-04-21
364
      eval { $self->_create_project($user_id, $project_id, $params) };
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
365
      croak $error = $@ if $@;
fix fork
Yuki Kimoto authored on 2016-04-21
366
      eval {$self->_create_rep($user_id, $project_id, $opts) };
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
367
      croak $error = $@ if $@;
368
    });
369
  };
370
  croak $error if $@;
371
}
372

            
373
sub create_user {
374
  my ($self, $user, $data) = @_;
375

            
376
  # Create user
fixed bug that setting page ...
Yuki Kimoto authored on 2013-05-15
377
  my $dbi = $self->app->dbi;
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
378
  my $error;
379
  eval {
380
    $dbi->connector->txn(sub {
381
      eval { $self->_create_db_user($user, $data) };
382
      croak $error = $@ if $@;
383
      eval {$self->_create_user_dir($user) };
384
      croak $error = $@ if $@;
385
    });
386
  };
387
  croak $error if $@;
388
}
389

            
390
sub delete_project {
391
  my ($self, $user, $project) = @_;
392
  
393
  # Delete project
fixed bug that setting page ...
Yuki Kimoto authored on 2013-05-15
394
  my $dbi = $self->app->dbi;
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
395
  my $error;
396
  eval {
397
    $dbi->connector->txn(sub {
398
      eval { $self->_delete_project($user, $project) };
399
      croak $error = $@ if $@;
400
      eval {$self->_delete_rep($user, $project) };
401
      croak $error = $@ if $@;
402
    });
403
  };
404
  croak $error if $@;
405
}
406

            
407
sub delete_user {
408
  my ($self, $user) = @_;
409
  
410
  # Delete user
fixed bug that setting page ...
Yuki Kimoto authored on 2013-05-15
411
  my $dbi = $self->app->dbi;
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
412
  my $error;
added delete user test
Yuki Kimoto authored on 2013-05-18
413
  my $count;
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
414
  eval {
415
    $dbi->connector->txn(sub {
added delete user test
Yuki Kimoto authored on 2013-05-18
416
      eval { $count = $self->_delete_db_user($user) };
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
417
      croak $error = $@ if $@;
418
      eval {$self->_delete_user_dir($user) };
419
      croak $error = $@ if $@;
420
    });
421
  };
422
  croak $error if $@;
added delete user test
Yuki Kimoto authored on 2013-05-18
423
  
424
  return $count;
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
425
}
426

            
427
sub original_project {
fix project page
Yuki Kimoto authored on 2016-04-21
428
  my ($self, $user_id, $project_id) = @_;
429
  
430
  my $user_row_id = $self->api->get_user_row_id($user_id);
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
431
  
cleanup delete original_proj...
Yuki Kimoto authored on 2013-05-26
432
  # Original project id
433
  my $dbi = $self->app->dbi;
fix project page
Yuki Kimoto authored on 2016-04-21
434
  my $original_project_row_id = $dbi->model('project')->select(
435
    'original_project',
436
    where => {user => $user_row_id, id => $project_id}
437
  )->value;
cleanup delete original_proj...
Yuki Kimoto authored on 2013-05-26
438
  
fix project page
Yuki Kimoto authored on 2016-04-21
439
  croak "Original project don't eixsts." unless defined $original_project_row_id;
cleanup delete original_proj...
Yuki Kimoto authored on 2013-05-26
440
  
fixed bug that setting page ...
Yuki Kimoto authored on 2013-05-15
441
  # Original project
cleanup delete original_proj...
Yuki Kimoto authored on 2013-05-26
442
  my $original_project = $dbi->model('project')->select(
fix project page
Yuki Kimoto authored on 2016-04-21
443
    [
444
      {__MY__ => '*'},
remove table __ prefix
Yuki Kimoto authored on 2016-06-11
445
      {user => ['id']}
fix project page
Yuki Kimoto authored on 2016-04-21
446
    ],
cleanup delete original_proj...
Yuki Kimoto authored on 2013-05-26
447
    where => {
fix project page
Yuki Kimoto authored on 2016-04-21
448
      'project.row_id' => $original_project_row_id
cleanup delete original_proj...
Yuki Kimoto authored on 2013-05-26
449
    }
fix project page
Yuki Kimoto authored on 2016-04-21
450
  )->one;
cleanup delete original_proj...
Yuki Kimoto authored on 2013-05-26
451
  
fix project page
Yuki Kimoto authored on 2016-04-21
452
  return unless defined $original_project;
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
453
  
454
  return $original_project;
455
}
456

            
get target branch logic
Yuki Kimoto authored on 2016-04-27
457
sub child_project {
458
  my ($self, $user_id, $project_id, $child_user_id) = @_;
459
  
460
  my $project_row_id = $self->app->dbi->model('project')->select(
remove table __ prefix
Yuki Kimoto authored on 2016-06-11
461
    'project.row_id', where => {'user.id' => $user_id, 'project.id' => $project_id}
get target branch logic
Yuki Kimoto authored on 2016-04-27
462
  )->value;
463
  
464
  my $child_project = $self->app->dbi->model('project')->select(
465
    [
466
      {__MY__ => '*'},
remove table __ prefix
Yuki Kimoto authored on 2016-06-11
467
      {user => ['id']}
get target branch logic
Yuki Kimoto authored on 2016-04-27
468
    ],
469
    where => {
470
      'project.original_project' => $project_row_id,
remove table __ prefix
Yuki Kimoto authored on 2016-06-11
471
      'user.id' => $child_user_id
get target branch logic
Yuki Kimoto authored on 2016-04-27
472
    }
473
  )->one;
474
  
475
  return $child_project;
476
}
477

            
show deleted repository in u...
Yuki Kimoto authored on 2013-05-15
478
sub projects {
fix user page
Yuki Kimoto authored on 2016-04-21
479
  my ($self, $user_id) = @_;
480
  
481
  my $user_row_id = $self->app->dbi->model('user')->select('row_id', where => {id => $user_id})->value;
482
  
show deleted repository in u...
Yuki Kimoto authored on 2013-05-15
483
  # Projects
484
  my $projects = $self->app->dbi->model('project')->select(
fix user page
Yuki Kimoto authored on 2016-04-21
485
    where => {user => $user_row_id},
486
    append => 'order by id'
show deleted repository in u...
Yuki Kimoto authored on 2013-05-15
487
  )->all;
488
  
489
  return $projects;
490
}
491

            
cleanup
Yuki Kimoto authored on 2013-05-13
492
sub users {
493
  my $self = shift;
fixed bug that setting page ...
Yuki Kimoto authored on 2013-05-15
494
  
495
  # Users
cleanup
Yuki Kimoto authored on 2013-05-13
496
  my $users = $self->app->dbi->model('user')->select(
497
    where => [':admin{<>}',{admin => 1}],
498
    append => 'order by id'
499
  )->all;
500
  
501
  return $users;
502
}
503

            
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
504
sub rename_project {
show deleted repository in u...
Yuki Kimoto authored on 2013-05-15
505
  my ($self, $user, $project, $to_project) = @_;
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
506
  
show deleted repository in u...
Yuki Kimoto authored on 2013-05-15
507
  # Rename project
revert encoding support
Yuki Kimoto authored on 2013-11-22
508
  my $git = $self->app->git;
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
509
  my $dbi = $self->app->dbi;
simplify rename project
Yuki Kimoto authored on 2013-05-22
510
  my $error;
511
  eval {
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
512
    $dbi->connector->txn(sub {
simplify rename project
Yuki Kimoto authored on 2013-05-22
513
      eval { $self->_rename_project($user, $project, $to_project) };
514
      croak $error = $@ if $@;
515
      eval { $self->_rename_rep($user, $project, $to_project) };
516
      croak $error = $@ if $@;
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
517
    });
simplify rename project
Yuki Kimoto authored on 2013-05-22
518
  };
519
  croak $error if $error;
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
520
}
521

            
add key delete feature and u...
gitprep authored on 2014-05-19
522
sub update_authorized_keys_file {
523
  my $self = shift;
524

            
525
  my $authorized_keys_file = $self->authorized_keys_file;
526
  if (defined $authorized_keys_file) {
527
    
528
    # Lock file
fix update_authorized_keys_f...
gitprep authored on 2014-05-19
529
    my $lock_file = $self->app->home->rel_file('lock/authorized_keys');
add key delete feature and u...
gitprep authored on 2014-05-19
530
    open my $lock_fh, $lock_file
531
      or croak "Can't open lock file $lock_file";
532
    flock $lock_fh, LOCK_EX
533
      or croak "Can't lock $lock_file";
534
    
535
    # Create authorized_keys_file
536
    unless (-f $authorized_keys_file) {
537
      open my $fh, '>', $authorized_keys_file
fix gitprep-shell
gitprep authored on 2014-05-19
538
        or croak "Can't create authorized_keys file: $authorized_keys_file";
539
      chmod 0600, $authorized_keys_file
540
        or croak "Can't chmod authorized_keys file: $authorized_keys_file";
add key delete feature and u...
gitprep authored on 2014-05-19
541
    }
542
    
543
    # Parse file
forbidden public key which i...
Yuki Kimoto authored on 2014-12-15
544
    my $result = $self->parse_authorized_keys_file($authorized_keys_file);
fix update_authorized_keys_f...
gitprep authored on 2014-05-19
545
    my $before_part = $result->{before_part};
546
    my $gitprep_part = $result->{gitprep_part};
547
    my $after_part = $result->{after_part};
548
    my $start_symbol = $result->{start_symbol};
549
    my $end_symbol = $result->{end_symbol};
add key delete feature and u...
gitprep authored on 2014-05-19
550
    
551
    # Backup at first time
552
    if ($gitprep_part eq '') {
553
      # Backup original file
554
      my $to = "$authorized_keys_file.gitprep.original";
555
      unless (-f $to) {
556
        copy $authorized_keys_file, $to
557
          or croak "Can't copy $authorized_keys_file to $to";
558
      }
559
    }
fix update_authorized_keys_f...
gitprep authored on 2014-05-19
560

            
add key delete feature and u...
gitprep authored on 2014-05-19
561
    # Create public keys
fix ssh public key bug
Yuki Kimoto authored on 2016-05-04
562
    my $ssh_public_keys = $self->app->dbi->model('ssh_public_key')->select(
563
      [
564
        {__MY__ => '*'},
remove table __ prefix
Yuki Kimoto authored on 2016-06-11
565
        {user => ['id']}
fix ssh public key bug
Yuki Kimoto authored on 2016-05-04
566
      ]
567
    )->all;
add key delete feature and u...
gitprep authored on 2014-05-19
568
    my $ssh_public_keys_str = '';
569
    for my $key (@$ssh_public_keys) {
fix permission bug
gitprep authored on 2014-05-19
570
      my $ssh_public_key_str = 'command="' . $self->app->home->rel_file('script/gitprep-shell')
remove table __ prefix
Yuki Kimoto authored on 2016-06-11
571
        . " $key->{'user.id'}\",no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty $key->{key}";
572
      $ssh_public_keys_str .= "$ssh_public_key_str $key->{'user.id'}\n\n";
add key delete feature and u...
gitprep authored on 2014-05-19
573
    }
574
    
575
    # Output tmp file
fix gitprep-shell
gitprep authored on 2014-05-19
576
    my $output = "$before_part$start_symbol\n\n$ssh_public_keys_str$end_symbol$after_part";
add key delete feature and u...
gitprep authored on 2014-05-19
577
    my $output_file = "$authorized_keys_file.gitprep.tmp";
578
    open my $out_fh, '>', $output_file
579
      or croak "Can't create authorized_keys tmp file $output_file";
580
    print $out_fh $output;
581
    close $out_fh
582
      or croak "Can't close authorized_keys tmp file $output_file";
fix update_authorized_keys_f...
gitprep authored on 2014-05-19
583

            
add key delete feature and u...
gitprep authored on 2014-05-19
584
    # Replace
fix permission bug
gitprep authored on 2014-05-19
585
    chmod 0600, $output_file
586
      or croak "Can't chmod authorized_keys tmp file: $output_file";
add key delete feature and u...
gitprep authored on 2014-05-19
587
    move $output_file, $authorized_keys_file
588
      or croak "Can't replace $authorized_keys_file by $output_file";
589
  }
590
  else {
591
    croak qq/authorized_keys file "$authorized_keys_file" is not found./;
592
  }
593
}
594

            
forbidden public key which i...
Yuki Kimoto authored on 2014-12-15
595
sub parse_authorized_keys_file {
fix update_authorized_keys_f...
gitprep authored on 2014-05-19
596
  my ($self, $file) = @_;
add key delete feature and u...
gitprep authored on 2014-05-19
597
  
598
  my $start_symbol = "# gitprep start";
599
  my $end_symbol = "# gitprep end";
600
  
601
  # Parse
602
  open my $fh, '<', $file
fix update_authorized_keys_f...
gitprep authored on 2014-05-19
603
    or croak "Can't open authorized_key file $file";
add key delete feature and u...
gitprep authored on 2014-05-19
604
  my $start_symbol_count = 0;
605
  my $end_symbol_count = 0;
606
  my $before_part = '';
607
  my $gitprep_part = '';
608
  my $after_part = '';
609
  my $error_prefix = "authorized_keys file $file format error:";
610
  while (my $line = <$fh>) {
611
    if ($line =~ /^$start_symbol/) {
612
      if ($start_symbol_count > 0) {
613
        croak qq/$error_prefix "$start_symbol" is found more than one/;
614
      }
615
      else {
616
        if ($end_symbol_count > 0) {
617
          croak qq/$error_prefix "$end_symbol" is found before "$start_symbol"/;
618
        }
619
        else {
620
          $start_symbol_count++;
621
        }
622
      }
623
    }
624
    elsif ($line =~ /^$end_symbol/) {
fix ssh key authentication b...
gitprep authored on 2014-05-20
625
      if ($end_symbol_count > 0) {
add key delete feature and u...
gitprep authored on 2014-05-19
626
        croak qq/$error_prefix "$end_symbol" is found more than one/;
627
      }
628
      else {
fix gitprep-shell
gitprep authored on 2014-05-19
629
        $end_symbol_count++;
add key delete feature and u...
gitprep authored on 2014-05-19
630
      }
631
    }
632
    elsif ($start_symbol_count == 0 && $end_symbol_count == 0) {
633
      $before_part .= $line;
634
    }
635
    elsif ($start_symbol_count == 1 && $end_symbol_count == 0) {
636
      $gitprep_part .= $line;
637
    }
638
    elsif ($start_symbol_count == 1 && $end_symbol_count == 1) {
639
      $after_part .= $line;
640
    }
641
  }
fix update_authorized_keys_f...
gitprep authored on 2014-05-19
642
  
643
  my $result = {
644
    start_symbol => $start_symbol,
645
    end_symbol => $end_symbol,
646
    before_part => $before_part,
647
    gitprep_part => $gitprep_part,
648
    after_part => $after_part
649
  };
650
  
651
  return $result;
add key delete feature and u...
gitprep authored on 2014-05-19
652
}
653

            
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
654
sub _create_project {
fix fork
Yuki Kimoto authored on 2016-04-21
655
  my ($self, $user_id, $project_id, $params) = @_;
656
  
657
  my $user_row_id = $self->api->get_user_row_id($user_id);
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
658
  $params ||= {};
fix fork
Yuki Kimoto authored on 2016-04-21
659
  $params->{user} = $user_row_id;
660
  $params->{id} = $project_id;
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
661
  
662
  # Create project
cleanup
Yuki Kimoto authored on 2013-05-15
663
  my $dbi = $self->app->dbi;
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
664
  $dbi->connector->txn(sub {
fix fork
Yuki Kimoto authored on 2016-04-21
665
    $dbi->model('project')->insert($params);
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
666
  });
667
}
668

            
669
sub _create_rep {
670
  my ($self, $user, $project, $opts) = @_;
671
  
672
  # Create repository directory
revert encoding support
Yuki Kimoto authored on 2013-11-22
673
  my $git = $self->app->git;
remove rep_path
Yuki Kimoto authored on 2016-04-16
674
  
675
  my $rep_info = $self->app->rep_info($user, $project);
676
  my $rep_git_dir = $rep_info->{git_dir};
677
  
678
  mkdir $rep_git_dir
679
    or croak "Can't create directory $rep_git_dir: $!";
fix bug that in CGI reposito...
Yuki Kimoto authored on 2015-11-05
680
  
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
681
  eval {
682
    # Git init
683
    {
remove cmd_dir
Yuki Kimoto authored on 2016-04-16
684
      my @git_init_cmd = $git->cmd($rep_info, 'init', '--bare');
fix bug that in CGI reposito...
Yuki Kimoto authored on 2015-11-05
685
      Gitprep::Util::run_command(@git_init_cmd)
fix git 2.0 create repositor...
Yuki Kimoto authored on 2015-11-04
686
        or croak  "Can't execute git init --bare:@git_init_cmd";
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
687
    }
688
    
689
    # Add git-daemon-export-ok
690
    {
remove rep_path
Yuki Kimoto authored on 2016-04-16
691
      my $file = "$rep_git_dir/git-daemon-export-ok";
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
692
      open my $fh, '>', $file
693
        or croak "Can't create git-daemon-export-ok: $!"
694
    }
695
    
696
    # HTTP support
remove cmd_dir
Yuki Kimoto authored on 2016-04-16
697
    my @git_update_server_info_cmd = $git->cmd(
698
      $rep_info,
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
699
      '--bare',
700
      'update-server-info'
701
    );
fix bug that in CGI reposito...
Yuki Kimoto authored on 2015-11-05
702
    Gitprep::Util::run_command(@git_update_server_info_cmd)
fix git 2.0 create repositor...
Yuki Kimoto authored on 2015-11-04
703
      or croak "Can't execute git --bare update-server-info";
remove rep_path
Yuki Kimoto authored on 2016-04-16
704
    move("$rep_git_dir/hooks/post-update.sample", "$rep_git_dir/hooks/post-update")
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
705
      or croak "Can't move post-update";
706
    
707
    # Description
version up and add descripti...
Yuki Kimoto authored on 2013-11-22
708
    my $description = $opts->{description};
709
    $description = '' unless defined $description;
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
710
    {
remove rep_path
Yuki Kimoto authored on 2016-04-16
711
      my $file = "$rep_git_dir/description";
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
712
      open my $fh, '>', $file
713
        or croak "Can't open $file: $!";
714
      print $fh encode('UTF-8', $description)
715
        or croak "Can't write $file: $!";
716
      close $fh;
717
    }
718
    
719
    # Add README and commit
720
    if ($opts->{readme}) {
721
      # Create working directory
use own tmp directory to cre...
Yuki Kimoto authored on 2016-03-24
722
      my $home_tmp_dir = $self->app->home->rel_file('tmp');
723
      
remove cmd_work_dir
Yuki Kimoto authored on 2016-04-16
724
      # Temp directory
use own tmp directory to cre...
Yuki Kimoto authored on 2016-03-24
725
      my $temp_dir =  File::Temp->newdir(DIR => $home_tmp_dir);
remove cmd_work_dir
Yuki Kimoto authored on 2016-04-16
726
      
727
      # Working repository
728
      my $work_rep_work_tree = "$temp_dir/work";
729
      my $work_rep_git_dir = "$work_rep_work_tree/.git";
730
      my $work_rep_info = {
731
        work_tree => $work_rep_work_tree,
732
        git_dir => $work_rep_git_dir
733
      };
734
      
735
      mkdir $work_rep_work_tree
736
        or croak "Can't create directory $work_rep_work_tree: $!";
use own tmp directory to cre...
Yuki Kimoto authored on 2016-03-24
737
      
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
738
      # Git init
remove cmd_work_dir
Yuki Kimoto authored on 2016-04-16
739
      my @git_init_cmd = $git->cmd($work_rep_info, 'init', '-q');
fix bug that in CGI reposito...
Yuki Kimoto authored on 2015-11-05
740
      Gitprep::Util::run_command(@git_init_cmd)
fix git 2.0 create repositor...
Yuki Kimoto authored on 2015-11-04
741
        or croak "Can't execute git init: @git_init_cmd";
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
742
      
743
      # Add README
remove cmd_work_dir
Yuki Kimoto authored on 2016-04-16
744
      my $file = "$work_rep_work_tree/README.md";
default readme file is chang...
Yuki Kimoto authored on 2013-11-16
745
      open my $readme_fh, '>', $file
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
746
        or croak "Can't create $file: $!";
version up and add descripti...
Yuki Kimoto authored on 2013-11-22
747
      print $readme_fh "# $project\n";
748
      print $readme_fh "\n" . encode('UTF-8', $description) . "\n";
default readme file is chang...
Yuki Kimoto authored on 2013-11-16
749
      close $readme_fh;
750
      
remove cmd_work_dir
Yuki Kimoto authored on 2016-04-16
751
      my @git_add_cmd = $git->cmd(
752
        $work_rep_info,
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
753
        'add',
default readme file is chang...
Yuki Kimoto authored on 2013-11-16
754
        'README.md'
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
755
      );
improve create working repos...
Yuki Kimoto authored on 2016-04-15
756
      
fix bug that in CGI reposito...
Yuki Kimoto authored on 2015-11-05
757
      Gitprep::Util::run_command(@git_add_cmd)
fix git 2.0 create repositor...
Yuki Kimoto authored on 2015-11-04
758
        or croak "Can't execute git add: @git_add_cmd";
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
759
      
fix bug that repository with...
Yuki Kimoto authored on 2016-04-12
760
      # Set user name
remove cmd_work_dir
Yuki Kimoto authored on 2016-04-16
761
      my @git_config_user_name = $git->cmd(
762
        $work_rep_info,
fix bug that repository with...
Yuki Kimoto authored on 2016-04-12
763
        'config',
764
        'user.name',
765
        $user
766
      );
767
      Gitprep::Util::run_command(@git_config_user_name)
768
        or croak "Can't execute git config: @git_config_user_name";
769
      
fix mail bug
Yuki Kimoto authored on 2016-04-21
770
      # Set user email
771
      my $user_email = $self->app->dbi->model('user')->select('email', where => {id => $user})->value;
772
      my @git_config_user_email = $git->cmd(
remove cmd_work_dir
Yuki Kimoto authored on 2016-04-16
773
        $work_rep_info,
fix bug that repository with...
Yuki Kimoto authored on 2016-04-12
774
        'config',
775
        'user.email',
fix mail bug
Yuki Kimoto authored on 2016-04-21
776
        "$user_email"
fix bug that repository with...
Yuki Kimoto authored on 2016-04-12
777
      );
fix mail bug
Yuki Kimoto authored on 2016-04-21
778
      Gitprep::Util::run_command(@git_config_user_email)
779
        or croak "Can't execute git config: @git_config_user_email";
fix bug that repository with...
Yuki Kimoto authored on 2016-04-12
780
      
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
781
      # Commit
remove cmd_work_dir
Yuki Kimoto authored on 2016-04-16
782
      my @git_commit_cmd = $git->cmd(
783
        $work_rep_info,
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
784
        'commit',
785
        '-q',
786
        '-m',
787
        'first commit'
788
      );
fix bug that in CGI reposito...
Yuki Kimoto authored on 2015-11-05
789
      Gitprep::Util::run_command(@git_commit_cmd)
fix git 2.0 create repositor...
Yuki Kimoto authored on 2015-11-04
790
        or croak "Can't execute git commit: @git_commit_cmd";
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
791
      
792
      # Push
793
      {
remove cmd_work_dir
Yuki Kimoto authored on 2016-04-16
794
        my @git_push_cmd = $git->cmd(
795
          $work_rep_info,
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
796
          'push',
797
          '-q',
remove rep_path
Yuki Kimoto authored on 2016-04-16
798
          $rep_git_dir,
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
799
          'master'
800
        );
801
        # (This is bad, but --quiet option can't supress in old git)
fix bug that in CGI reposito...
Yuki Kimoto authored on 2015-11-05
802
        Gitprep::Util::run_command(@git_push_cmd)
fix git 2.0 create repositor...
Yuki Kimoto authored on 2015-11-04
803
          or croak "Can't execute git push: @git_push_cmd";
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
804
      }
805
    }
806
  };
improve faq of pushing lerge...
Yuki Kimoto authored on 2014-02-17
807
  if (my $e = $@) {
remove rep_path
Yuki Kimoto authored on 2016-04-16
808
    rmtree $rep_git_dir;
improve faq of pushing lerge...
Yuki Kimoto authored on 2014-02-17
809
    croak $e;
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
810
  }
811
}
812

            
show deleted repository in u...
Yuki Kimoto authored on 2013-05-15
813
sub _create_db_user {
fix project page
Yuki Kimoto authored on 2016-04-21
814
  my ($self, $user_id, $data) = @_;
815
  
816
  $data->{id} = $user_id;
show deleted repository in u...
Yuki Kimoto authored on 2013-05-15
817
  
818
  # Create database user
fix project page
Yuki Kimoto authored on 2016-04-21
819
  $self->app->dbi->model('user')->insert($data);
show deleted repository in u...
Yuki Kimoto authored on 2013-05-15
820
}
821

            
822
sub _create_user_dir {
823
  my ($self, $user) = @_;
824
  
825
  # Create user directory
cleanup ->rep_home
Yuki Kimoto authored on 2016-04-14
826
  my $rep_home = $self->app->rep_home;
show deleted repository in u...
Yuki Kimoto authored on 2013-05-15
827
  my $user_dir = "$rep_home/$user";
828
  mkpath $user_dir;
829
}
830

            
831
sub _delete_db_user {
fix project page
Yuki Kimoto authored on 2016-04-21
832
  my ($self, $user_id) = @_;
show deleted repository in u...
Yuki Kimoto authored on 2013-05-15
833
  
834
  # Delete database user
fix project page
Yuki Kimoto authored on 2016-04-21
835
  my $count = $self->app->dbi->model('user')->delete(where => {id => $user_id});
added delete user test
Yuki Kimoto authored on 2013-05-18
836
  
837
  return $count;
show deleted repository in u...
Yuki Kimoto authored on 2013-05-15
838
}
839

            
840
sub _delete_user_dir {
841
  my ($self, $user) = @_;
842
  
843
  # Delete user directory
cleanup ->rep_home
Yuki Kimoto authored on 2016-04-14
844
  my $rep_home = $self->app->rep_home;
show deleted repository in u...
Yuki Kimoto authored on 2013-05-15
845
  my $user_dir = "$rep_home/$user";
846
  rmtree $user_dir;
847
}
848

            
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
849
sub _delete_project {
fix project page
Yuki Kimoto authored on 2016-04-21
850
  my ($self, $user_id, $project_id) = @_;
851
  
852
  my $user_row_id = $self->api->get_user_row_id($user_id);
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
853
  
cleanup
Yuki Kimoto authored on 2013-05-15
854
  # Delete project
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
855
  my $dbi = $self->app->dbi;
fix project page
Yuki Kimoto authored on 2016-04-21
856
  $dbi->model('project')->delete(where => {user => $user_row_id, id => $project_id});
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
857
}
858

            
859
sub _delete_rep {
860
  my ($self, $user, $project) = @_;
861

            
cleanup
Yuki Kimoto authored on 2013-05-15
862
  # Delete repository
cleanup ->rep_home
Yuki Kimoto authored on 2016-04-14
863
  my $rep_home = $self->app->rep_home;
add missing 'o'
reneeb authored on 2013-10-14
864
  croak "Can't remove repository. repository home is empty"
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
865
    if !defined $rep_home || $rep_home eq '';
866
  my $rep = "$rep_home/$user/$project.git";
867
  rmtree $rep;
868
  croak "Can't remove repository. repository is rest"
869
    if -e $rep;
870
}
871

            
fixed bug that setting page ...
Yuki Kimoto authored on 2013-05-15
872
sub exists_project {
fix project page
Yuki Kimoto authored on 2016-04-21
873
  my ($self, $user_id, $project_id) = @_;
874
  
875
  my $user_row_id = $self->api->get_user_row_id($user_id);
cleanup
Yuki Kimoto authored on 2013-05-15
876
  
877
  # Exists project
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
878
  my $dbi = $self->app->dbi;
fix project page
Yuki Kimoto authored on 2016-04-21
879
  my $row = $dbi->model('project')->select(where => {user => $user_row_id, id => $project_id})->one;
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
880
  
881
  return $row ? 1 : 0;
882
}
883

            
add rename project and suppr...
Yuki Kimoto authored on 2013-05-24
884
sub exists_user {
fix project page
Yuki Kimoto authored on 2016-04-21
885
  my ($self, $user_id) = @_;
add rename project and suppr...
Yuki Kimoto authored on 2013-05-24
886
  
887
  # Exists project
fix project page
Yuki Kimoto authored on 2016-04-21
888
  my $row = $self->app->dbi->model('user')->select(where => {id => $user_id})->one;
add rename project and suppr...
Yuki Kimoto authored on 2013-05-24
889
  
890
  return $row ? 1 : 0;
891
}
892

            
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
893
sub _exists_rep {
894
  my ($self, $user, $project) = @_;
895
  
cleanup
Yuki Kimoto authored on 2013-05-15
896
  # Exists repository
remove rep_path
Yuki Kimoto authored on 2016-04-16
897
  my $rep_info = $self->app->rep_info($user, $project);
898
  my $rep_git_dir = $rep_info->{git_dir};
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
899
  
remove rep_path
Yuki Kimoto authored on 2016-04-16
900
  return -e $rep_git_dir;
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
901
}
902

            
show deleted repository in u...
Yuki Kimoto authored on 2013-05-15
903
sub _fork_rep {
fix fork
Yuki Kimoto authored on 2016-04-21
904
  my ($self, $user_id, $project_id, $to_user_id, $to_project_id) = @_;
show deleted repository in u...
Yuki Kimoto authored on 2013-05-15
905
  
cleanup
Yuki Kimoto authored on 2013-05-15
906
  # Fork repository
revert encoding support
Yuki Kimoto authored on 2013-11-22
907
  my $git = $self->app->git;
remove rep_path
Yuki Kimoto authored on 2016-04-16
908
  
fix fork
Yuki Kimoto authored on 2016-04-21
909
  my $rep_info = $self->app->rep_info($user_id, $project_id);
remove rep_path
Yuki Kimoto authored on 2016-04-16
910
  my $rep_git_dir = $rep_info->{git_dir};
911
  
fix fork
Yuki Kimoto authored on 2016-04-21
912
  my $to_rep_info = $self->app->rep_info($to_user_id, $to_project_id);
remove rep_path
Yuki Kimoto authored on 2016-04-16
913
  my $to_rep_git_dir = $to_rep_info->{git_dir};
914

            
show deleted repository in u...
Yuki Kimoto authored on 2013-05-15
915
  my @cmd = (
916
    $git->bin,
917
    'clone',
918
    '-q',
919
    '--bare',
remove rep_path
Yuki Kimoto authored on 2016-04-16
920
    $rep_git_dir,
921
    $to_rep_git_dir
show deleted repository in u...
Yuki Kimoto authored on 2013-05-15
922
  );
fix bug that in CGI reposito...
Yuki Kimoto authored on 2015-11-05
923
  Gitprep::Util::run_command(@cmd)
fix description file not cop...
Yuki Kimoto authored on 2013-05-25
924
    or croak "Can't fork repository(_fork_rep): @cmd";
925
  
926
  # Copy description
remove rep_path
Yuki Kimoto authored on 2016-04-16
927
  copy "$rep_git_dir/description", "$to_rep_git_dir/description"
fix description file not cop...
Yuki Kimoto authored on 2013-05-25
928
    or croak "Can't copy description file(_fork_rep)";
show deleted repository in u...
Yuki Kimoto authored on 2013-05-15
929
}
930

            
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
931
sub _rename_project {
fix project page
Yuki Kimoto authored on 2016-04-21
932
  my ($self, $user_id, $project_id, $renamed_project_id) = @_;
933
  
934
  my $user_row_id = $self->api->get_user_row_id($user_id);
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
935
  
cleanup
Yuki Kimoto authored on 2013-05-15
936
  # Check arguments
add rename project and suppr...
Yuki Kimoto authored on 2013-05-24
937
  croak "Invalid parameters(_rename_project)"
fix project page
Yuki Kimoto authored on 2016-04-21
938
    unless defined $user_id && defined $project_id && defined $renamed_project_id;
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
939
  
940
  # Rename project
cleanup
Yuki Kimoto authored on 2013-05-15
941
  my $dbi = $self->app->dbi;
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
942
  $dbi->model('project')->update(
fix project page
Yuki Kimoto authored on 2016-04-21
943
    {id => $renamed_project_id},
944
    where => {user => $user_row_id, id => $project_id}
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
945
  );
946
}
947

            
948
sub _rename_rep {
949
  my ($self, $user, $project, $renamed_project) = @_;
950
  
cleanup
Yuki Kimoto authored on 2013-05-15
951
  # Check arguments
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
952
  croak "Invalid user name or project"
953
    unless defined $user && defined $project && defined $renamed_project;
cleanup
Yuki Kimoto authored on 2013-05-15
954

            
955
  # Rename repository
remove rep_path
Yuki Kimoto authored on 2016-04-16
956
  my $rep_info = $self->app->rep_info($user, $project);
957
  my $rep_git_dir = $rep_info->{git_dir};
958
  
959
  my $renamed_rep_info = $self->app->rep_info($user, $renamed_project);
960
  my $renamed_rep_git_dir = $renamed_rep_info->{git_dir};
961

            
962
  move($rep_git_dir, $renamed_rep_git_dir)
963
    or croak "Can't move $rep_git_dir to $renamed_rep_git_dir: $!";
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
964
}
965

            
966
1;