gitprep / lib / Gitprep / Manager.pm /
Newer Older
842 lines | 20.969kb
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/;
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
12

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

            
added admin tests
Yuki Kimoto authored on 2013-05-16
16
sub admin_user {
cleanup
Yuki Kimoto authored on 2013-05-13
17
  my $self = shift;
18
  
19
  # Admin user
20
  my $admin_user = $self->app->dbi->model('user')
added admin tests
Yuki Kimoto authored on 2013-05-16
21
    ->select(where => {admin => 1})->one;
cleanup
Yuki Kimoto authored on 2013-05-13
22
  
23
  return $admin_user;
24
}
25

            
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
26
sub default_branch {
added default branch change ...
Yuki Kimoto authored on 2013-05-22
27
  my ($self, $user, $project, $default_branch) = @_;
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
28
  
added default branch change ...
Yuki Kimoto authored on 2013-05-22
29
  # Set default branch
30
  my $dbi = $self->app->dbi;
31
  if (defined $default_branch) {
32
    $dbi->model('project')->update(
33
      {default_branch => $default_branch},
34
      id => [$user, $project]
35
    );
36
  }
37
  else {
38
    # Get default branch
39
    my $default_branch = $dbi->model('project')
40
      ->select('default_branch', id => [$user, $project])
41
      ->value;
42
    
43
    return $default_branch;
44
  }
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
45
}
46

            
show deleted repository in u...
Yuki Kimoto authored on 2013-05-15
47
sub fork_project {
48
  my ($self, $user, $original_user, $project) = @_;
49
  
50
  # Fork project
51
  my $dbi = $self->app->dbi;
52
  my $error;
53
  eval {
54
    $dbi->connector->txn(sub {
55
      
56
      # Original project id
version up and add descripti...
Yuki Kimoto authored on 2013-11-22
57
      my $project_info = $dbi->model('project')->select(
58
        ['original_pid', 'private'],
59
        id => [$original_user, $project]
60
      )->one;
61
      
62
      my $original_pid = $project_info->{original_pid};
show deleted repository in u...
Yuki Kimoto authored on 2013-05-15
63
      
64
      croak "Can't get original project id"
65
        unless defined $original_pid && $original_pid > 0;
66
      
67
      # Create project
68
      eval {
69
        $self->_create_project(
70
          $user,
71
          $project,
72
          {
73
            original_user => $original_user,
version up and add descripti...
Yuki Kimoto authored on 2013-11-22
74
            original_pid => $original_pid,
75
            private => $project_info->{private}
show deleted repository in u...
Yuki Kimoto authored on 2013-05-15
76
          }
77
        );
78
      };
79
      croak $error = $@ if $@;
80
      
81
      # Create repository
82
      eval {
83
        $self->_fork_rep($original_user, $project, $user, $project);
84
      };
85
      croak $error = $@ if $@;
86
    });
87
  };
88
  croak $error if $@;
89
}
90

            
cleanup
Yuki Kimoto authored on 2013-05-13
91
sub is_admin {
92
  my ($self, $user) = @_;
93
  
94
  # Check admin
95
  my $is_admin = $self->app->dbi->model('user')
96
    ->select('admin', id => $user)->value;
97
  
98
  return $is_admin;
99
}
100

            
add private checkbox
Yuki Kimoto authored on 2013-11-16
101
sub is_private_project {
102
  my ($self, $user, $project) = @_;
103
  
104
  # Is private
105
  my $private = $self->app->dbi->model('project')
106
    ->select('private', id => [$user, $project])->value;
107
  
108
  return $private;
109
}
110

            
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
111
sub members {
112
  my ($self, $user, $project) = @_;
113
  
114
  # DBI
115
  my $dbi = $self->app->dbi;
116
  
117
  # Original project id
118
  my $original_pid = $dbi->model('project')
119
    ->select('original_pid', id => [$user, $project])->value;
120
  
121
  # Members
122
  my $members = $dbi->model('project')->select(
123
    ['user_id as id', 'name as project'],
124
    where => [
125
      ['and',
126
        ':original_pid{=}',
127
        ['or', ':user_id{<>}', ':name{<>}']
128
      ],
129
      {
130
        original_pid => $original_pid,
131
        user_id => $user,
132
        name => $project
133
      }
134
    ],
135
    append => 'order by user_id, name'
136
  )->all;
137

            
138
  return $members;
139
}
140

            
141
sub create_project {
142
  my ($self, $user, $project, $opts) = @_;
143
  
add private checkbox to new ...
Yuki Kimoto authored on 2013-11-26
144
  my $params = {};
145
  if ($opts->{private}) {
146
    $params->{private} = 1;
147
  }
148
  
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
149
  # Create project
fixed bug that setting page ...
Yuki Kimoto authored on 2013-05-15
150
  my $dbi = $self->app->dbi;
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
151
  my $error;
152
  eval {
153
    $dbi->connector->txn(sub {
add private checkbox to new ...
Yuki Kimoto authored on 2013-11-26
154
      eval { $self->_create_project($user, $project, $params) };
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
155
      croak $error = $@ if $@;
156
      eval {$self->_create_rep($user, $project, $opts) };
157
      croak $error = $@ if $@;
158
    });
159
  };
160
  croak $error if $@;
161
}
162

            
163
sub create_user {
164
  my ($self, $user, $data) = @_;
165

            
166
  # Create user
fixed bug that setting page ...
Yuki Kimoto authored on 2013-05-15
167
  my $dbi = $self->app->dbi;
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
168
  my $error;
169
  eval {
170
    $dbi->connector->txn(sub {
171
      eval { $self->_create_db_user($user, $data) };
172
      croak $error = $@ if $@;
173
      eval {$self->_create_user_dir($user) };
174
      croak $error = $@ if $@;
175
    });
176
  };
177
  croak $error if $@;
178
}
179

            
180
sub delete_project {
181
  my ($self, $user, $project) = @_;
182
  
183
  # Delete project
fixed bug that setting page ...
Yuki Kimoto authored on 2013-05-15
184
  my $dbi = $self->app->dbi;
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
185
  my $error;
186
  eval {
187
    $dbi->connector->txn(sub {
188
      eval { $self->_delete_project($user, $project) };
189
      croak $error = $@ if $@;
190
      eval {$self->_delete_rep($user, $project) };
191
      croak $error = $@ if $@;
192
    });
193
  };
194
  croak $error if $@;
195
}
196

            
197
sub delete_user {
198
  my ($self, $user) = @_;
199
  
200
  # Delete user
fixed bug that setting page ...
Yuki Kimoto authored on 2013-05-15
201
  my $dbi = $self->app->dbi;
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
202
  my $error;
added delete user test
Yuki Kimoto authored on 2013-05-18
203
  my $count;
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
204
  eval {
205
    $dbi->connector->txn(sub {
added delete user test
Yuki Kimoto authored on 2013-05-18
206
      eval { $count = $self->_delete_db_user($user) };
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
207
      croak $error = $@ if $@;
208
      eval {$self->_delete_user_dir($user) };
209
      croak $error = $@ if $@;
210
    });
211
  };
212
  croak $error if $@;
added delete user test
Yuki Kimoto authored on 2013-05-18
213
  
214
  return $count;
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
215
}
216

            
217
sub original_project {
218
  my ($self, $user, $project) = @_;
219
  
cleanup delete original_proj...
Yuki Kimoto authored on 2013-05-26
220
  # Original project id
221
  my $dbi = $self->app->dbi;
222
  my $row = $dbi->model('project')->select(
223
    ['original_user', 'original_pid'],
224
    id => [$user, $project]
225
  )->one;
226
  
add import branch tests
Yuki Kimoto authored on 2013-08-19
227
  croak "Original project don't eixsts." unless $row;
cleanup delete original_proj...
Yuki Kimoto authored on 2013-05-26
228
  
fixed bug that setting page ...
Yuki Kimoto authored on 2013-05-15
229
  # Original project
cleanup delete original_proj...
Yuki Kimoto authored on 2013-05-26
230
  my $original_project = $dbi->model('project')->select(
231
    'name',
232
    where => {
233
      user_id => $row->{original_user},
234
      original_pid => $row->{original_pid}
235
    }
236
  )->value;
237
  
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
238
  return unless defined $original_project && length $original_project;
239
  
240
  return $original_project;
241
}
242

            
243
sub original_user {
244
  my ($self, $user, $project) = @_;
245
  
fixed bug that setting page ...
Yuki Kimoto authored on 2013-05-15
246
  # Orginal user
247
  my $original_user = $self->app->dbi->model('project')
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
248
    ->select('original_user', id => [$user, $project])
249
    ->value;
250
  return unless defined $original_user && length $original_user;
251
  
252
  return $original_user;
253
}
254

            
show deleted repository in u...
Yuki Kimoto authored on 2013-05-15
255
sub projects {
256
  my ($self, $user) = @_;
257

            
258
  # Projects
259
  my $projects = $self->app->dbi->model('project')->select(
260
    where => {user_id => $user},
261
    append => 'order by name'
262
  )->all;
263
  
264
  return $projects;
265
}
266

            
cleanup
Yuki Kimoto authored on 2013-05-13
267
sub users {
268
  my $self = shift;
fixed bug that setting page ...
Yuki Kimoto authored on 2013-05-15
269
  
270
  # Users
cleanup
Yuki Kimoto authored on 2013-05-13
271
  my $users = $self->app->dbi->model('user')->select(
272
    where => [':admin{<>}',{admin => 1}],
273
    append => 'order by id'
274
  )->all;
275
  
276
  return $users;
277
}
278

            
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
279
sub rename_project {
show deleted repository in u...
Yuki Kimoto authored on 2013-05-15
280
  my ($self, $user, $project, $to_project) = @_;
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
281
  
show deleted repository in u...
Yuki Kimoto authored on 2013-05-15
282
  # Rename project
revert encoding support
Yuki Kimoto authored on 2013-11-22
283
  my $git = $self->app->git;
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
284
  my $dbi = $self->app->dbi;
simplify rename project
Yuki Kimoto authored on 2013-05-22
285
  my $error;
286
  eval {
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
287
    $dbi->connector->txn(sub {
simplify rename project
Yuki Kimoto authored on 2013-05-22
288
      eval { $self->_rename_project($user, $project, $to_project) };
289
      croak $error = $@ if $@;
290
      eval { $self->_rename_rep($user, $project, $to_project) };
291
      croak $error = $@ if $@;
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
292
    });
simplify rename project
Yuki Kimoto authored on 2013-05-22
293
  };
294
  croak $error if $error;
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
295
}
296

            
297
sub setup_database {
298
  my $self = shift;
299
  
300
  my $dbi = $self->app->dbi;
301
  
302
  # Create user table
303
  eval {
304
    my $sql = <<"EOS";
305
create table user (
306
  row_id integer primary key autoincrement,
307
  id not null unique default ''
308
);
309
EOS
310
    $dbi->execute($sql);
311
  };
312

            
add ssh key add feature
gitprep authored on 2014-05-17
313
  # Create user columns
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
314
  my $user_columns = [
315
    "admin not null default '0'",
316
    "password not null default ''",
317
    "salt not null default ''"
318
  ];
319
  for my $column (@$user_columns) {
320
    eval { $dbi->execute("alter table user add column $column") };
321
  }
322
  
323
  # Check user table
324
  eval { $dbi->select([qw/row_id id admin password salt/], table => 'user') };
325
  if ($@) {
326
    my $error = "Can't create user table properly: $@";
327
    $self->app->log->error($error);
328
    croak $error;
329
  }
add ssh key add feature
gitprep authored on 2014-05-17
330

            
331
  # Create ssh_public_key table
332
  eval {
333
    my $sql = <<"EOS";
334
create table ssh_public_key (
335
  row_id integer primary key autoincrement,
336
  user_id not null default '',
337
  key not null default '',
338
  unique(user_id, key)
339
);
340
EOS
341
    $dbi->execute($sql);
342
  };
343

            
344
  # Create ssh_public_key columns
345
  my $ssh_public_key_columns = [
346
    "title not null default ''",
347
  ];
348
  for my $column (@$ssh_public_key_columns) {
349
    eval { $dbi->execute("alter table ssh_public_key add column $column") };
350
  }
351
  
352
  # Check ssh_public_key table
353
  eval { $dbi->select([qw/row_id user_id key title/], table => 'ssh_public_key') };
354
  if ($@) {
355
    my $error = "Can't create ssh_public_key table properly: $@";
356
    $self->app->log->error($error);
357
    croak $error;
358
  }
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
359
  
360
  # Create project table
361
  eval {
362
    my $sql = <<"EOS";
363
create table project (
364
  row_id integer primary key autoincrement,
365
  user_id not null,
366
  name not null,
367
  unique(user_id, name)
368
);
369
EOS
370
    $dbi->execute($sql);
371
  };
372
  
373
  # Create Project columns
374
  my $project_columns = [
375
    "default_branch not null default 'master'",
376
    "original_user not null default ''",
add private checkbox
Yuki Kimoto authored on 2013-11-16
377
    "original_pid integer not null default 0",
revert encoding support
Yuki Kimoto authored on 2013-11-22
378
    "private not null default 0"
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
379
  ];
380
  for my $column (@$project_columns) {
381
    eval { $dbi->execute("alter table project add column $column") };
382
  }
383

            
384
  # Check project table
cleanup delete original_proj...
Yuki Kimoto authored on 2013-05-26
385
  eval { $dbi->select([qw/default_branch original_user original_pid/], table => 'project') };
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
386
  if ($@) {
387
    my $error = "Can't create project table properly: $@";
388
    $self->app->log->error($error);
389
    croak $error;
390
  }
391

            
add collaborator register lo...
Yuki Kimoto authored on 2013-11-17
392
  # Create collaboration table
393
  eval {
394
    my $sql = <<"EOS";
395
create table collaboration (
396
  row_id integer primary key autoincrement,
fix bug that can't create co...
Yuki Kimoto authored on 2014-03-08
397
  user_id not null default '',
398
  project_name not null default '',
399
  collaborator_id not null default '',
add collaborator register lo...
Yuki Kimoto authored on 2013-11-17
400
  unique(user_id, project_name, collaborator_id)
401
);
402
EOS
403
    $dbi->execute($sql);
404
  };
405
  
406
  # Check collaboration table
407
  eval { $dbi->select([qw/row_id user_id project_name collaborator_id/], table => 'collaboration') };
408
  if ($@) {
409
    my $error = "Can't create collaboration table properly: $@";
410
    $self->app->log->error($error);
411
    croak $error;
412
  }
413

            
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
414
  # Create number table
415
  eval {
416
    my $sql = <<"EOS";
417
create table number (
418
  row_id integer primary key autoincrement,
419
  key not null unique
420
);
421
EOS
422
    $dbi->execute($sql);
423
  };
424
  
425
  # Create number columns
426
  my $number_columns = [
427
    "value integer not null default '0'"
428
  ];
429
  for my $column (@$number_columns) {
430
    eval { $dbi->execute("alter table number add column $column") };
431
  }
432

            
433
  # Check number table
434
  eval { $dbi->select([qw/row_id key value/], table => 'number') };
435
  if ($@) {
436
    my $error = "Can't create number table properly: $@";
437
    $self->app->log->error($error);
438
    croak $error;
439
  }
440
  
441
  # Original project id numbert
442
  eval { $dbi->insert({key => 'original_pid'}, table => 'number') };
443
  my $original_pid = $dbi->select(
444
    'key',
445
    table => 'number',
446
    where => {key => 'original_pid'}
447
  )->value;
448
  unless (defined $original_pid) {
449
    my $error = "Can't create original_pid row in number table";
450
    $self->app->log->error($error);
451
    croak $error;
452
  }
453
}
454

            
add key delete feature and u...
gitprep authored on 2014-05-19
455

            
456
sub update_authorized_keys_file {
457
  my $self = shift;
458

            
459
  my $authorized_keys_file = $self->authorized_keys_file;
460
  if (defined $authorized_keys_file) {
461
    
462
    # Lock file
463
    my $lock_file = $self->app->rel_file('lock/authorized_keys');
464
    open my $lock_fh, $lock_file
465
      or croak "Can't open lock file $lock_file";
466
    flock $lock_fh, LOCK_EX
467
      or croak "Can't lock $lock_file";
468
    
469
    # Create authorized_keys_file
470
    unless (-f $authorized_keys_file) {
471
      open my $fh, '>', $authorized_keys_file
472
        or croak "Can't create $authorized_keys_file";
473
    }
474
    
475
    # Parse file
476
    my ($before_part, $gitprep_part, $after_part)
477
      = $self->_parse_authorized_keys_file($authorized_keys_file);
478
    
479
    # Backup at first time
480
    if ($gitprep_part eq '') {
481
      # Backup original file
482
      my $to = "$authorized_keys_file.gitprep.original";
483
      unless (-f $to) {
484
        copy $authorized_keys_file, $to
485
          or croak "Can't copy $authorized_keys_file to $to";
486
      }
487
    }
488
    
489
    # Create public keys
490
    my $ssh_public_keys = $self->app->dbi->mode('ssh_public_key')->select->all;
491
    my $ssh_public_keys_str = '';
492
    for my $key (@$ssh_public_keys) {
493
      my $ssh_public_key_str = $self->app->home->rel_file('script/gitprep-shell')
494
        . " $key->{user_id},no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty $key->{key}";
495
      $ssh_public_keys_str .= "$ssh_public_key_str\n\n";
496
    }
497
    
498
    # Output tmp file
499
    my $output = "$before_part\n\n$ssh_public_keys_str\n\n$after_part";
500
    my $output_file = "$authorized_keys_file.gitprep.tmp";
501
    open my $out_fh, '>', $output_file
502
      or croak "Can't create authorized_keys tmp file $output_file";
503
    print $out_fh $output;
504
    close $out_fh
505
      or croak "Can't close authorized_keys tmp file $output_file";
506
    
507
    # Replace
508
    move $output_file, $authorized_keys_file
509
      or croak "Can't replace $authorized_keys_file by $output_file";
510
    
511
    # Unlock file
512
    flock $lock_fh, LOCK_EX
513
      or croak "Can't unlock $lock_file"
514
  }
515
  else {
516
    croak qq/authorized_keys file "$authorized_keys_file" is not found./;
517
  }
518
}
519

            
520
sub _parse_authorized_keys_file {
521
  my ($self, $file) = shift;
522
  
523
  my $start_symbol = "# gitprep start";
524
  my $end_symbol = "# gitprep end";
525
  
526
  # Parse
527
  open my $fh, '<', $file
528
    or croak "Can't open $file";
529
  my $start_symbol_count = 0;
530
  my $end_symbol_count = 0;
531
  my $before_part = '';
532
  my $gitprep_part = '';
533
  my $after_part = '';
534
  my $error_prefix = "authorized_keys file $file format error:";
535
  while (my $line = <$fh>) {
536
    if ($line =~ /^$start_symbol/) {
537
      if ($start_symbol_count > 0) {
538
        croak qq/$error_prefix "$start_symbol" is found more than one/;
539
      }
540
      else {
541
        if ($end_symbol_count > 0) {
542
          croak qq/$error_prefix "$end_symbol" is found before "$start_symbol"/;
543
        }
544
        else {
545
          $start_symbol_count++;
546
        }
547
      }
548
    }
549
    elsif ($line =~ /^$end_symbol/) {
550
      if ($end_symbol > 0) {
551
        croak qq/$error_prefix "$end_symbol" is found more than one/;
552
      }
553
      else {
554
        $end_symbol++;
555
      }
556
    }
557
    elsif ($start_symbol_count == 0 && $end_symbol_count == 0) {
558
      $before_part .= $line;
559
    }
560
    elsif ($start_symbol_count == 1 && $end_symbol_count == 0) {
561
      $gitprep_part .= $line;
562
    }
563
    elsif ($start_symbol_count == 1 && $end_symbol_count == 1) {
564
      $after_part .= $line;
565
    }
566
  }
567
  return ($before_part, $gitprep_part, $after_part);
568
}
569

            
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
570
sub _create_project {
571
  my ($self, $user, $project, $params) = @_;
572
  $params ||= {};
573
  
574
  # Create project
cleanup
Yuki Kimoto authored on 2013-05-15
575
  my $dbi = $self->app->dbi;
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
576
  $dbi->connector->txn(sub {
577
    unless (defined $params->{original_pid}) {
578
      my $number = $dbi->model('number')->select('value', where => {key => 'original_pid'})->value;
579
      $number++;
580
      $dbi->model('number')->update({value => $number}, where => {key => 'original_pid'});
581
      $params->{original_pid} = $number;
582
    }
583
    $dbi->model('project')->insert($params, id => [$user, $project]);
584
  });
585
}
586

            
587
sub _create_rep {
588
  my ($self, $user, $project, $opts) = @_;
589
  
590
  # Create repository directory
revert encoding support
Yuki Kimoto authored on 2013-11-22
591
  my $git = $self->app->git;
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
592
  my $rep = $git->rep($user, $project);
593
  mkdir $rep
594
    or croak "Can't create directory $rep: $!";
595

            
596
  eval {
597
    # Git init
598
    {
599
      my @git_init_cmd = $git->cmd_rep($rep, 'init', '--bare');
fix bug that create_reposito...
Yuki Kimoto authored on 2013-08-28
600
      open my $fh, "-|", @git_init_cmd
601
        or croak  "Can't open git init --bare:@git_init_cmd";
602
      close $fh or croak  "Can't execute git init --bare:@git_init_cmd";
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
603
    }
604
    
605
    # Add git-daemon-export-ok
606
    {
607
      my $file = "$rep/git-daemon-export-ok";
608
      open my $fh, '>', $file
609
        or croak "Can't create git-daemon-export-ok: $!"
610
    }
611
    
612
    # HTTP support
613
    my @git_update_server_info_cmd = $git->cmd_rep(
614
      $rep,
615
      '--bare',
616
      'update-server-info'
617
    );
fix bug that create_reposito...
Yuki Kimoto authored on 2013-08-28
618
    open my $update_server_info_fh, "-|", @git_update_server_info_cmd
619
      or croak "Can't open git --bare update-server-info";
620
    close $update_server_info_fh or croak "Can't execute git --bare update-server-info";
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
621
    move("$rep/hooks/post-update.sample", "$rep/hooks/post-update")
622
      or croak "Can't move post-update";
623
    
624
    # Description
version up and add descripti...
Yuki Kimoto authored on 2013-11-22
625
    my $description = $opts->{description};
626
    $description = '' unless defined $description;
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
627
    {
628
      my $file = "$rep/description";
629
      open my $fh, '>', $file
630
        or croak "Can't open $file: $!";
631
      print $fh encode('UTF-8', $description)
632
        or croak "Can't write $file: $!";
633
      close $fh;
634
    }
635
    
636
    # Add README and commit
637
    if ($opts->{readme}) {
638
      # Create working directory
639
      my $temp_dir =  File::Temp->newdir;
640
      my $temp_work = "$temp_dir/work";
641
      mkdir $temp_work
642
        or croak "Can't create directory $temp_work: $!";
643

            
644
      # Git init
645
      my @git_init_cmd = $git->cmd_rep($temp_work, 'init', '-q');
fix bug that create_reposito...
Yuki Kimoto authored on 2013-08-28
646
      open my $init_fh, "-|", @git_init_cmd
improve error message
Yuki Kimoto authored on 2014-02-13
647
        or croak "Can't open git init: @git_init_cmd";
648
      close $init_fh or croak "Can't execute git init: @git_init_cmd";
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
649
      
650
      # Add README
default readme file is chang...
Yuki Kimoto authored on 2013-11-16
651
      my $file = "$temp_work/README.md";
652
      open my $readme_fh, '>', $file
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
653
        or croak "Can't create $file: $!";
version up and add descripti...
Yuki Kimoto authored on 2013-11-22
654
      print $readme_fh "# $project\n";
655
      print $readme_fh "\n" . encode('UTF-8', $description) . "\n";
default readme file is chang...
Yuki Kimoto authored on 2013-11-16
656
      close $readme_fh;
657
      
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
658
      my @git_add_cmd = $git->cmd_rep(
659
        $temp_work,
660
        "--work-tree=$temp_work",
661
        'add',
default readme file is chang...
Yuki Kimoto authored on 2013-11-16
662
        'README.md'
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
663
      );
fix bug that create_reposito...
Yuki Kimoto authored on 2013-08-28
664
      open my $add_fh, "-|", @git_add_cmd
improve error message
Yuki Kimoto authored on 2014-02-13
665
        or croak "Can't open git add: @git_add_cmd";
666
      close $add_fh or croak "Can't execute git add: @git_add_cmd";
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
667
      
668
      # Commit
safe first commit with READM...
Yuki Kimoto authored on 2013-06-03
669
      my $author = "$user <$user\@localhost>";
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
670
      my @git_commit_cmd = $git->cmd_rep(
671
        $temp_work,
672
        "--work-tree=$temp_work",
673
        'commit',
674
        '-q',
first commit README bug
Yuki Kimoto authored on 2013-06-03
675
        "--author=$author",
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
676
        '-m',
677
        'first commit'
678
      );
fix bug that create_reposito...
Yuki Kimoto authored on 2013-08-28
679
      open my $commit_fh, "-|", @git_commit_cmd
improve error message
Yuki Kimoto authored on 2014-02-13
680
        or croak "Can't open git commit: @git_commit_cmd";
681
      close $commit_fh or croak "Can't execute git commit: @git_commit_cmd";
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
682
      
683
      # Push
684
      {
685
        my @git_push_cmd = $git->cmd_rep(
686
          $temp_work,
687
          "--work-tree=$temp_work",
688
          'push',
689
          '-q',
690
          $rep,
691
          'master'
692
        );
693
        # (This is bad, but --quiet option can't supress in old git)
694
        my $git_push_cmd = join(' ', @git_push_cmd);
fix bug that create_reposito...
Yuki Kimoto authored on 2013-08-28
695
        open my $commit_fh, "-|", "$git_push_cmd 2> /dev/null"
improve error message
Yuki Kimoto authored on 2014-02-13
696
          or croak "Can't open git push: @git_push_cmd";
improve error message
Yuki Kimoto authored on 2014-02-13
697
        close $commit_fh or croak "Can't execute git push: @git_push_cmd";
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
698
      }
699
    }
700
  };
improve faq of pushing lerge...
Yuki Kimoto authored on 2014-02-17
701
  if (my $e = $@) {
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
702
    rmtree $rep;
improve faq of pushing lerge...
Yuki Kimoto authored on 2014-02-17
703
    croak $e;
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
704
  }
705
}
706

            
show deleted repository in u...
Yuki Kimoto authored on 2013-05-15
707
sub _create_db_user {
708
  my ($self, $user, $data) = @_;
709
  
710
  # Create database user
711
  $self->app->dbi->model('user')->insert($data, id => $user);
712
}
713

            
714
sub _create_user_dir {
715
  my ($self, $user) = @_;
716
  
717
  # Create user directory
revert encoding support
Yuki Kimoto authored on 2013-11-22
718
  my $rep_home = $self->app->git->rep_home;
show deleted repository in u...
Yuki Kimoto authored on 2013-05-15
719
  my $user_dir = "$rep_home/$user";
720
  mkpath $user_dir;
721
}
722

            
723
sub _delete_db_user {
724
  my ($self, $user) = @_;
725
  
726
  # Delete database user
added delete user test
Yuki Kimoto authored on 2013-05-18
727
  my $count = $self->app->dbi->model('user')->delete(id => $user);
728
  
729
  return $count;
show deleted repository in u...
Yuki Kimoto authored on 2013-05-15
730
}
731

            
732
sub _delete_user_dir {
733
  my ($self, $user) = @_;
734
  
735
  # Delete user directory
revert encoding support
Yuki Kimoto authored on 2013-11-22
736
  my $rep_home = $self->app->git->rep_home;
show deleted repository in u...
Yuki Kimoto authored on 2013-05-15
737
  my $user_dir = "$rep_home/$user";
738
  rmtree $user_dir;
739
}
740

            
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
741
sub _delete_project {
742
  my ($self, $user, $project) = @_;
743
  
cleanup
Yuki Kimoto authored on 2013-05-15
744
  # Delete project
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
745
  my $dbi = $self->app->dbi;
746
  $dbi->model('project')->delete(id => [$user, $project]);
747
}
748

            
749
sub _delete_rep {
750
  my ($self, $user, $project) = @_;
751

            
cleanup
Yuki Kimoto authored on 2013-05-15
752
  # Delete repository
revert encoding support
Yuki Kimoto authored on 2013-11-22
753
  my $rep_home = $self->app->git->rep_home;
add missing 'o'
reneeb authored on 2013-10-14
754
  croak "Can't remove repository. repository home is empty"
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
755
    if !defined $rep_home || $rep_home eq '';
756
  my $rep = "$rep_home/$user/$project.git";
757
  rmtree $rep;
758
  croak "Can't remove repository. repository is rest"
759
    if -e $rep;
760
}
761

            
fixed bug that setting page ...
Yuki Kimoto authored on 2013-05-15
762
sub exists_project {
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
763
  my ($self, $user, $project) = @_;
cleanup
Yuki Kimoto authored on 2013-05-15
764
  
765
  # Exists project
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
766
  my $dbi = $self->app->dbi;
767
  my $row = $dbi->model('project')->select(id => [$user, $project])->one;
768
  
769
  return $row ? 1 : 0;
770
}
771

            
add rename project and suppr...
Yuki Kimoto authored on 2013-05-24
772
sub exists_user {
773
  my ($self, $user) = @_;
774
  
775
  # Exists project
776
  my $row = $self->app->dbi->model('user')->select(id => $user)->one;
777
  
778
  return $row ? 1 : 0;
779
}
780

            
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
781
sub _exists_rep {
782
  my ($self, $user, $project) = @_;
783
  
cleanup
Yuki Kimoto authored on 2013-05-15
784
  # Exists repository
revert encoding support
Yuki Kimoto authored on 2013-11-22
785
  my $rep = $self->app->git->rep($user, $project);
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
786
  
787
  return -e $rep;
788
}
789

            
show deleted repository in u...
Yuki Kimoto authored on 2013-05-15
790
sub _fork_rep {
791
  my ($self, $user, $project, $to_user, $to_project) = @_;
792
  
cleanup
Yuki Kimoto authored on 2013-05-15
793
  # Fork repository
revert encoding support
Yuki Kimoto authored on 2013-11-22
794
  my $git = $self->app->git;
show deleted repository in u...
Yuki Kimoto authored on 2013-05-15
795
  my $rep = $git->rep($user, $project);
796
  my $to_rep = $git->rep($to_user, $to_project);
797
  my @cmd = (
798
    $git->bin,
799
    'clone',
800
    '-q',
801
    '--bare',
802
    $rep,
803
    $to_rep
804
  );
805
  system(@cmd) == 0
fix description file not cop...
Yuki Kimoto authored on 2013-05-25
806
    or croak "Can't fork repository(_fork_rep): @cmd";
807
  
808
  # Copy description
809
  copy "$rep/description", "$to_rep/description"
810
    or croak "Can't copy description file(_fork_rep)";
show deleted repository in u...
Yuki Kimoto authored on 2013-05-15
811
}
812

            
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
813
sub _rename_project {
814
  my ($self, $user, $project, $renamed_project) = @_;
815
  
cleanup
Yuki Kimoto authored on 2013-05-15
816
  # Check arguments
add rename project and suppr...
Yuki Kimoto authored on 2013-05-24
817
  croak "Invalid parameters(_rename_project)"
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
818
    unless defined $user && defined $project && defined $renamed_project;
819
  
820
  # Rename project
cleanup
Yuki Kimoto authored on 2013-05-15
821
  my $dbi = $self->app->dbi;
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
822
  $dbi->model('project')->update(
823
    {name => $renamed_project},
824
    id => [$user, $project]
825
  );
826
}
827

            
828
sub _rename_rep {
829
  my ($self, $user, $project, $renamed_project) = @_;
830
  
cleanup
Yuki Kimoto authored on 2013-05-15
831
  # Check arguments
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
832
  croak "Invalid user name or project"
833
    unless defined $user && defined $project && defined $renamed_project;
cleanup
Yuki Kimoto authored on 2013-05-15
834

            
835
  # Rename repository
revert encoding support
Yuki Kimoto authored on 2013-11-22
836
  my $rep = $self->app->git->rep($user, $project);
837
  my $renamed_rep = $self->app->git->rep($user, $renamed_project);
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
838
  move($rep, $renamed_rep)
839
    or croak "Can't move $rep to $renamed_rep: $!";
840
}
841

            
842
1;