gitprep / lib / Gitprep / Manager.pm /
Newer Older
855 lines | 21.271kb
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

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

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

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

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

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

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

            
140
  return $members;
141
}
142

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

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

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

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

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

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

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

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

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

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

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

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

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

            
333
  # Create ssh_public_key table
334
  eval {
335
    my $sql = <<"EOS";
336
create table ssh_public_key (
337
  row_id integer primary key autoincrement,
fix ssh key authentication b...
gitprep authored on 2014-05-20
338
  key not null unique default ''
add ssh key add feature
gitprep authored on 2014-05-17
339
);
340
EOS
341
    $dbi->execute($sql);
342
  };
343

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

            
385
  # Check project table
cleanup delete original_proj...
Yuki Kimoto authored on 2013-05-26
386
  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
387
  if ($@) {
388
    my $error = "Can't create project table properly: $@";
389
    $self->app->log->error($error);
390
    croak $error;
391
  }
392

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

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

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

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

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

            
460
  my $authorized_keys_file = $self->authorized_keys_file;
461
  if (defined $authorized_keys_file) {
462
    
463
    # Lock file
fix update_authorized_keys_f...
gitprep authored on 2014-05-19
464
    my $lock_file = $self->app->home->rel_file('lock/authorized_keys');
add key delete feature and u...
gitprep authored on 2014-05-19
465
    open my $lock_fh, $lock_file
466
      or croak "Can't open lock file $lock_file";
467
    flock $lock_fh, LOCK_EX
468
      or croak "Can't lock $lock_file";
469
    
470
    # Create authorized_keys_file
471
    unless (-f $authorized_keys_file) {
472
      open my $fh, '>', $authorized_keys_file
fix gitprep-shell
gitprep authored on 2014-05-19
473
        or croak "Can't create authorized_keys file: $authorized_keys_file";
474
      chmod 0600, $authorized_keys_file
475
        or croak "Can't chmod authorized_keys file: $authorized_keys_file";
add key delete feature and u...
gitprep authored on 2014-05-19
476
    }
477
    
478
    # Parse file
forbidden public key which i...
Yuki Kimoto authored on 2014-12-15
479
    my $result = $self->parse_authorized_keys_file($authorized_keys_file);
fix update_authorized_keys_f...
gitprep authored on 2014-05-19
480
    my $before_part = $result->{before_part};
481
    my $gitprep_part = $result->{gitprep_part};
482
    my $after_part = $result->{after_part};
483
    my $start_symbol = $result->{start_symbol};
484
    my $end_symbol = $result->{end_symbol};
add key delete feature and u...
gitprep authored on 2014-05-19
485
    
486
    # Backup at first time
487
    if ($gitprep_part eq '') {
488
      # Backup original file
489
      my $to = "$authorized_keys_file.gitprep.original";
490
      unless (-f $to) {
491
        copy $authorized_keys_file, $to
492
          or croak "Can't copy $authorized_keys_file to $to";
493
      }
494
    }
fix update_authorized_keys_f...
gitprep authored on 2014-05-19
495

            
add key delete feature and u...
gitprep authored on 2014-05-19
496
    # Create public keys
fix update_authorized_keys_f...
gitprep authored on 2014-05-19
497
    my $ssh_public_keys = $self->app->dbi->model('ssh_public_key')->select->all;
add key delete feature and u...
gitprep authored on 2014-05-19
498
    my $ssh_public_keys_str = '';
499
    for my $key (@$ssh_public_keys) {
fix permission bug
gitprep authored on 2014-05-19
500
      my $ssh_public_key_str = 'command="' . $self->app->home->rel_file('script/gitprep-shell')
501
        . " $key->{user_id}\",no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty $key->{key}";
fix update_authorized_keys_f...
gitprep authored on 2014-05-19
502
      $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
503
    }
504
    
505
    # Output tmp file
fix gitprep-shell
gitprep authored on 2014-05-19
506
    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
507
    my $output_file = "$authorized_keys_file.gitprep.tmp";
508
    open my $out_fh, '>', $output_file
509
      or croak "Can't create authorized_keys tmp file $output_file";
510
    print $out_fh $output;
511
    close $out_fh
512
      or croak "Can't close authorized_keys tmp file $output_file";
fix update_authorized_keys_f...
gitprep authored on 2014-05-19
513

            
add key delete feature and u...
gitprep authored on 2014-05-19
514
    # Replace
fix permission bug
gitprep authored on 2014-05-19
515
    chmod 0600, $output_file
516
      or croak "Can't chmod authorized_keys tmp file: $output_file";
add key delete feature and u...
gitprep authored on 2014-05-19
517
    move $output_file, $authorized_keys_file
518
      or croak "Can't replace $authorized_keys_file by $output_file";
519
    
520
    # Unlock file
521
    flock $lock_fh, LOCK_EX
522
      or croak "Can't unlock $lock_file"
523
  }
524
  else {
525
    croak qq/authorized_keys file "$authorized_keys_file" is not found./;
526
  }
527
}
528

            
forbidden public key which i...
Yuki Kimoto authored on 2014-12-15
529
sub parse_authorized_keys_file {
fix update_authorized_keys_f...
gitprep authored on 2014-05-19
530
  my ($self, $file) = @_;
add key delete feature and u...
gitprep authored on 2014-05-19
531
  
532
  my $start_symbol = "# gitprep start";
533
  my $end_symbol = "# gitprep end";
534
  
535
  # Parse
536
  open my $fh, '<', $file
fix update_authorized_keys_f...
gitprep authored on 2014-05-19
537
    or croak "Can't open authorized_key file $file";
add key delete feature and u...
gitprep authored on 2014-05-19
538
  my $start_symbol_count = 0;
539
  my $end_symbol_count = 0;
540
  my $before_part = '';
541
  my $gitprep_part = '';
542
  my $after_part = '';
543
  my $error_prefix = "authorized_keys file $file format error:";
544
  while (my $line = <$fh>) {
545
    if ($line =~ /^$start_symbol/) {
546
      if ($start_symbol_count > 0) {
547
        croak qq/$error_prefix "$start_symbol" is found more than one/;
548
      }
549
      else {
550
        if ($end_symbol_count > 0) {
551
          croak qq/$error_prefix "$end_symbol" is found before "$start_symbol"/;
552
        }
553
        else {
554
          $start_symbol_count++;
555
        }
556
      }
557
    }
558
    elsif ($line =~ /^$end_symbol/) {
fix ssh key authentication b...
gitprep authored on 2014-05-20
559
      if ($end_symbol_count > 0) {
add key delete feature and u...
gitprep authored on 2014-05-19
560
        croak qq/$error_prefix "$end_symbol" is found more than one/;
561
      }
562
      else {
fix gitprep-shell
gitprep authored on 2014-05-19
563
        $end_symbol_count++;
add key delete feature and u...
gitprep authored on 2014-05-19
564
      }
565
    }
566
    elsif ($start_symbol_count == 0 && $end_symbol_count == 0) {
567
      $before_part .= $line;
568
    }
569
    elsif ($start_symbol_count == 1 && $end_symbol_count == 0) {
570
      $gitprep_part .= $line;
571
    }
572
    elsif ($start_symbol_count == 1 && $end_symbol_count == 1) {
573
      $after_part .= $line;
574
    }
575
  }
fix update_authorized_keys_f...
gitprep authored on 2014-05-19
576
  
577
  my $result = {
578
    start_symbol => $start_symbol,
579
    end_symbol => $end_symbol,
580
    before_part => $before_part,
581
    gitprep_part => $gitprep_part,
582
    after_part => $after_part
583
  };
584
  
585
  return $result;
add key delete feature and u...
gitprep authored on 2014-05-19
586
}
587

            
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
588
sub _create_project {
589
  my ($self, $user, $project, $params) = @_;
590
  $params ||= {};
591
  
592
  # Create project
cleanup
Yuki Kimoto authored on 2013-05-15
593
  my $dbi = $self->app->dbi;
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
594
  $dbi->connector->txn(sub {
595
    unless (defined $params->{original_pid}) {
596
      my $number = $dbi->model('number')->select('value', where => {key => 'original_pid'})->value;
597
      $number++;
598
      $dbi->model('number')->update({value => $number}, where => {key => 'original_pid'});
599
      $params->{original_pid} = $number;
600
    }
601
    $dbi->model('project')->insert($params, id => [$user, $project]);
602
  });
603
}
604

            
605
sub _create_rep {
606
  my ($self, $user, $project, $opts) = @_;
607
  
608
  # Create repository directory
revert encoding support
Yuki Kimoto authored on 2013-11-22
609
  my $git = $self->app->git;
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
610
  my $rep = $git->rep($user, $project);
611
  mkdir $rep
612
    or croak "Can't create directory $rep: $!";
fix bug that in CGI reposito...
Yuki Kimoto authored on 2015-11-05
613
  
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
614
  eval {
615
    # Git init
616
    {
617
      my @git_init_cmd = $git->cmd_rep($rep, 'init', '--bare');
fix bug that in CGI reposito...
Yuki Kimoto authored on 2015-11-05
618
      Gitprep::Util::run_command(@git_init_cmd)
fix git 2.0 create repositor...
Yuki Kimoto authored on 2015-11-04
619
        or croak  "Can't execute git init --bare:@git_init_cmd";
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
620
    }
621
    
622
    # Add git-daemon-export-ok
623
    {
624
      my $file = "$rep/git-daemon-export-ok";
625
      open my $fh, '>', $file
626
        or croak "Can't create git-daemon-export-ok: $!"
627
    }
628
    
629
    # HTTP support
630
    my @git_update_server_info_cmd = $git->cmd_rep(
631
      $rep,
632
      '--bare',
633
      'update-server-info'
634
    );
fix bug that in CGI reposito...
Yuki Kimoto authored on 2015-11-05
635
    Gitprep::Util::run_command(@git_update_server_info_cmd)
fix git 2.0 create repositor...
Yuki Kimoto authored on 2015-11-04
636
      or croak "Can't execute git --bare update-server-info";
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
637
    move("$rep/hooks/post-update.sample", "$rep/hooks/post-update")
638
      or croak "Can't move post-update";
639
    
640
    # Description
version up and add descripti...
Yuki Kimoto authored on 2013-11-22
641
    my $description = $opts->{description};
642
    $description = '' unless defined $description;
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
643
    {
644
      my $file = "$rep/description";
645
      open my $fh, '>', $file
646
        or croak "Can't open $file: $!";
647
      print $fh encode('UTF-8', $description)
648
        or croak "Can't write $file: $!";
649
      close $fh;
650
    }
651
    
652
    # Add README and commit
653
    if ($opts->{readme}) {
654
      # Create working directory
use own tmp directory to cre...
Yuki Kimoto authored on 2016-03-24
655
      my $home_tmp_dir = $self->app->home->rel_file('tmp');
656
      
657
      my $temp_dir =  File::Temp->newdir(DIR => $home_tmp_dir);
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
658
      my $temp_work = "$temp_dir/work";
659
      mkdir $temp_work
660
        or croak "Can't create directory $temp_work: $!";
use own tmp directory to cre...
Yuki Kimoto authored on 2016-03-24
661
      
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
662
      # Git init
663
      my @git_init_cmd = $git->cmd_rep($temp_work, 'init', '-q');
fix bug that in CGI reposito...
Yuki Kimoto authored on 2015-11-05
664
      Gitprep::Util::run_command(@git_init_cmd)
fix git 2.0 create repositor...
Yuki Kimoto authored on 2015-11-04
665
        or croak "Can't execute git init: @git_init_cmd";
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
666
      
667
      # Add README
default readme file is chang...
Yuki Kimoto authored on 2013-11-16
668
      my $file = "$temp_work/README.md";
669
      open my $readme_fh, '>', $file
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
670
        or croak "Can't create $file: $!";
version up and add descripti...
Yuki Kimoto authored on 2013-11-22
671
      print $readme_fh "# $project\n";
672
      print $readme_fh "\n" . encode('UTF-8', $description) . "\n";
default readme file is chang...
Yuki Kimoto authored on 2013-11-16
673
      close $readme_fh;
674
      
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
675
      my @git_add_cmd = $git->cmd_rep(
676
        $temp_work,
677
        "--work-tree=$temp_work",
678
        'add',
default readme file is chang...
Yuki Kimoto authored on 2013-11-16
679
        'README.md'
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
680
      );
fix bug that in CGI reposito...
Yuki Kimoto authored on 2015-11-05
681
      Gitprep::Util::run_command(@git_add_cmd)
fix git 2.0 create repositor...
Yuki Kimoto authored on 2015-11-04
682
        or croak "Can't execute git add: @git_add_cmd";
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
683
      
684
      # Commit
safe first commit with READM...
Yuki Kimoto authored on 2013-06-03
685
      my $author = "$user <$user\@localhost>";
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
686
      my @git_commit_cmd = $git->cmd_rep(
687
        $temp_work,
688
        "--work-tree=$temp_work",
689
        'commit',
690
        '-q',
first commit README bug
Yuki Kimoto authored on 2013-06-03
691
        "--author=$author",
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
692
        '-m',
693
        'first commit'
694
      );
fix bug that in CGI reposito...
Yuki Kimoto authored on 2015-11-05
695
      Gitprep::Util::run_command(@git_commit_cmd)
fix git 2.0 create repositor...
Yuki Kimoto authored on 2015-11-04
696
        or croak "Can't execute git commit: @git_commit_cmd";
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
697
      
698
      # Push
699
      {
700
        my @git_push_cmd = $git->cmd_rep(
701
          $temp_work,
702
          "--work-tree=$temp_work",
703
          'push',
704
          '-q',
705
          $rep,
706
          'master'
707
        );
708
        # (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
709
        Gitprep::Util::run_command(@git_push_cmd)
fix git 2.0 create repositor...
Yuki Kimoto authored on 2015-11-04
710
          or croak "Can't execute git push: @git_push_cmd";
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
711
      }
712
    }
713
  };
improve faq of pushing lerge...
Yuki Kimoto authored on 2014-02-17
714
  if (my $e = $@) {
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
715
    rmtree $rep;
improve faq of pushing lerge...
Yuki Kimoto authored on 2014-02-17
716
    croak $e;
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
717
  }
718
}
719

            
show deleted repository in u...
Yuki Kimoto authored on 2013-05-15
720
sub _create_db_user {
721
  my ($self, $user, $data) = @_;
722
  
723
  # Create database user
724
  $self->app->dbi->model('user')->insert($data, id => $user);
725
}
726

            
727
sub _create_user_dir {
728
  my ($self, $user) = @_;
729
  
730
  # Create user directory
revert encoding support
Yuki Kimoto authored on 2013-11-22
731
  my $rep_home = $self->app->git->rep_home;
show deleted repository in u...
Yuki Kimoto authored on 2013-05-15
732
  my $user_dir = "$rep_home/$user";
733
  mkpath $user_dir;
734
}
735

            
736
sub _delete_db_user {
737
  my ($self, $user) = @_;
738
  
739
  # Delete database user
added delete user test
Yuki Kimoto authored on 2013-05-18
740
  my $count = $self->app->dbi->model('user')->delete(id => $user);
741
  
742
  return $count;
show deleted repository in u...
Yuki Kimoto authored on 2013-05-15
743
}
744

            
745
sub _delete_user_dir {
746
  my ($self, $user) = @_;
747
  
748
  # Delete user directory
revert encoding support
Yuki Kimoto authored on 2013-11-22
749
  my $rep_home = $self->app->git->rep_home;
show deleted repository in u...
Yuki Kimoto authored on 2013-05-15
750
  my $user_dir = "$rep_home/$user";
751
  rmtree $user_dir;
752
}
753

            
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
754
sub _delete_project {
755
  my ($self, $user, $project) = @_;
756
  
cleanup
Yuki Kimoto authored on 2013-05-15
757
  # Delete project
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
758
  my $dbi = $self->app->dbi;
759
  $dbi->model('project')->delete(id => [$user, $project]);
760
}
761

            
762
sub _delete_rep {
763
  my ($self, $user, $project) = @_;
764

            
cleanup
Yuki Kimoto authored on 2013-05-15
765
  # Delete repository
revert encoding support
Yuki Kimoto authored on 2013-11-22
766
  my $rep_home = $self->app->git->rep_home;
add missing 'o'
reneeb authored on 2013-10-14
767
  croak "Can't remove repository. repository home is empty"
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
768
    if !defined $rep_home || $rep_home eq '';
769
  my $rep = "$rep_home/$user/$project.git";
770
  rmtree $rep;
771
  croak "Can't remove repository. repository is rest"
772
    if -e $rep;
773
}
774

            
fixed bug that setting page ...
Yuki Kimoto authored on 2013-05-15
775
sub exists_project {
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
776
  my ($self, $user, $project) = @_;
cleanup
Yuki Kimoto authored on 2013-05-15
777
  
778
  # Exists project
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
779
  my $dbi = $self->app->dbi;
780
  my $row = $dbi->model('project')->select(id => [$user, $project])->one;
781
  
782
  return $row ? 1 : 0;
783
}
784

            
add rename project and suppr...
Yuki Kimoto authored on 2013-05-24
785
sub exists_user {
786
  my ($self, $user) = @_;
787
  
788
  # Exists project
789
  my $row = $self->app->dbi->model('user')->select(id => $user)->one;
790
  
791
  return $row ? 1 : 0;
792
}
793

            
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
794
sub _exists_rep {
795
  my ($self, $user, $project) = @_;
796
  
cleanup
Yuki Kimoto authored on 2013-05-15
797
  # Exists repository
revert encoding support
Yuki Kimoto authored on 2013-11-22
798
  my $rep = $self->app->git->rep($user, $project);
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
799
  
800
  return -e $rep;
801
}
802

            
show deleted repository in u...
Yuki Kimoto authored on 2013-05-15
803
sub _fork_rep {
804
  my ($self, $user, $project, $to_user, $to_project) = @_;
805
  
cleanup
Yuki Kimoto authored on 2013-05-15
806
  # Fork repository
revert encoding support
Yuki Kimoto authored on 2013-11-22
807
  my $git = $self->app->git;
show deleted repository in u...
Yuki Kimoto authored on 2013-05-15
808
  my $rep = $git->rep($user, $project);
809
  my $to_rep = $git->rep($to_user, $to_project);
810
  my @cmd = (
811
    $git->bin,
812
    'clone',
813
    '-q',
814
    '--bare',
815
    $rep,
816
    $to_rep
817
  );
fix bug that in CGI reposito...
Yuki Kimoto authored on 2015-11-05
818
  Gitprep::Util::run_command(@cmd)
fix description file not cop...
Yuki Kimoto authored on 2013-05-25
819
    or croak "Can't fork repository(_fork_rep): @cmd";
820
  
821
  # Copy description
822
  copy "$rep/description", "$to_rep/description"
823
    or croak "Can't copy description file(_fork_rep)";
show deleted repository in u...
Yuki Kimoto authored on 2013-05-15
824
}
825

            
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
826
sub _rename_project {
827
  my ($self, $user, $project, $renamed_project) = @_;
828
  
cleanup
Yuki Kimoto authored on 2013-05-15
829
  # Check arguments
add rename project and suppr...
Yuki Kimoto authored on 2013-05-24
830
  croak "Invalid parameters(_rename_project)"
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
831
    unless defined $user && defined $project && defined $renamed_project;
832
  
833
  # Rename project
cleanup
Yuki Kimoto authored on 2013-05-15
834
  my $dbi = $self->app->dbi;
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
835
  $dbi->model('project')->update(
836
    {name => $renamed_project},
837
    id => [$user, $project]
838
  );
839
}
840

            
841
sub _rename_rep {
842
  my ($self, $user, $project, $renamed_project) = @_;
843
  
cleanup
Yuki Kimoto authored on 2013-05-15
844
  # Check arguments
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
845
  croak "Invalid user name or project"
846
    unless defined $user && defined $project && defined $renamed_project;
cleanup
Yuki Kimoto authored on 2013-05-15
847

            
848
  # Rename repository
revert encoding support
Yuki Kimoto authored on 2013-11-22
849
  my $rep = $self->app->git->rep($user, $project);
850
  my $renamed_rep = $self->app->git->rep($user, $renamed_project);
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
851
  move($rep, $renamed_rep)
852
    or croak "Can't move $rep to $renamed_rep: $!";
853
}
854

            
855
1;