gitprep / lib / Gitprep / Manager.pm /
Newer Older
905 lines | 22.916kb
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
  };
add name and mail column to ...
Yuki Kimoto authored on 2016-04-06
314
  
315
  # Check mail column
316
  my $not_exists_user_mail;
317
  eval { $dbi->select('mail', table => 'user') };
318
  if ($@) {
319
    $not_exists_user_mail = 1;
320
  }
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
321

            
add ssh key add feature
gitprep authored on 2014-05-17
322
  # Create user columns
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
323
  my $user_columns = [
324
    "admin not null default '0'",
325
    "password not null default ''",
add user.name and user.mail
Yuki Kimoto authored on 2016-04-06
326
    "salt not null default ''",
327
    "mail not null default ''",
328
    "name not null default ''"
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
329
  ];
330
  for my $column (@$user_columns) {
331
    eval { $dbi->execute("alter table user add column $column") };
332
  }
add name and mail column to ...
Yuki Kimoto authored on 2016-04-06
333

            
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
334
  # Check user table
add user.name and user.mail
Yuki Kimoto authored on 2016-04-06
335
  eval { $dbi->select([qw/row_id id admin password salt mail name/], table => 'user') };
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
336
  if ($@) {
337
    my $error = "Can't create user table properly: $@";
338
    $self->app->log->error($error);
339
    croak $error;
340
  }
add name and mail column to ...
Yuki Kimoto authored on 2016-04-06
341
  
342
  # If mail is empty, id is copied to mail for uniqueness
343
  my $user_ids = $dbi->select('id', table => 'user', where => {mail => ''})->values;
344
  for my $user_id (@$user_ids) {
345
    $dbi->update({mail => "$user_id\@gitprep.example"}, table => 'user', where => {id => $user_id});
346
  }
347
  
348
  # add unique to mail
349
  eval { $dbi->execute("create unique index user__mail on user(mail)") };
350
  my $created_user_mail_index = $dbi->execute("select * from sqlite_master where type = 'index' and name = 'user__mail'")->one;
351
  unless ($created_user_mail_index) {
352
    croak "Can't create user__mail index";
353
  }
354
  
add ssh key add feature
gitprep authored on 2014-05-17
355
  # Create ssh_public_key table
356
  eval {
357
    my $sql = <<"EOS";
358
create table ssh_public_key (
359
  row_id integer primary key autoincrement,
fix ssh key authentication b...
gitprep authored on 2014-05-20
360
  key not null unique default ''
add ssh key add feature
gitprep authored on 2014-05-17
361
);
362
EOS
363
    $dbi->execute($sql);
364
  };
365

            
366
  # Create ssh_public_key columns
367
  my $ssh_public_key_columns = [
fix ssh key authentication b...
gitprep authored on 2014-05-20
368
    "user_id not null default ''",
369
    "title not null default ''"
add ssh key add feature
gitprep authored on 2014-05-17
370
  ];
371
  for my $column (@$ssh_public_key_columns) {
372
    eval { $dbi->execute("alter table ssh_public_key add column $column") };
373
  }
374
  
375
  # Check ssh_public_key table
376
  eval { $dbi->select([qw/row_id user_id key title/], table => 'ssh_public_key') };
377
  if ($@) {
378
    my $error = "Can't create ssh_public_key table properly: $@";
379
    $self->app->log->error($error);
380
    croak $error;
381
  }
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
382
  
383
  # Create project table
384
  eval {
385
    my $sql = <<"EOS";
386
create table project (
387
  row_id integer primary key autoincrement,
388
  user_id not null,
389
  name not null,
390
  unique(user_id, name)
391
);
392
EOS
393
    $dbi->execute($sql);
394
  };
395
  
396
  # Create Project columns
397
  my $project_columns = [
398
    "default_branch not null default 'master'",
399
    "original_user not null default ''",
add private checkbox
Yuki Kimoto authored on 2013-11-16
400
    "original_pid integer not null default 0",
remove [basic]show_ignore_sp...
Yuki Kimoto authored on 2016-04-02
401
    "private not null default 0",
add guess_encoding column an...
Yuki Kimoto authored on 2016-04-05
402
    "ignore_space_change not null default 0",
403
    "guess_encoding not null default ''"
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
404
  ];
405
  for my $column (@$project_columns) {
406
    eval { $dbi->execute("alter table project add column $column") };
407
  }
408

            
409
  # Check project table
add guess_encoding column an...
Yuki Kimoto authored on 2016-04-05
410
  eval {
411
    $dbi->select(
412
      [qw/default_branch original_user original_pid private ignore_space_change guess_encoding/],
413
      table => 'project'
414
    );
415
  };
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
416
  if ($@) {
417
    my $error = "Can't create project table properly: $@";
418
    $self->app->log->error($error);
419
    croak $error;
420
  }
421

            
add collaborator register lo...
Yuki Kimoto authored on 2013-11-17
422
  # Create collaboration table
423
  eval {
424
    my $sql = <<"EOS";
425
create table collaboration (
426
  row_id integer primary key autoincrement,
fix bug that can't create co...
Yuki Kimoto authored on 2014-03-08
427
  user_id not null default '',
428
  project_name not null default '',
429
  collaborator_id not null default '',
add collaborator register lo...
Yuki Kimoto authored on 2013-11-17
430
  unique(user_id, project_name, collaborator_id)
431
);
432
EOS
433
    $dbi->execute($sql);
434
  };
435
  
436
  # Check collaboration table
437
  eval { $dbi->select([qw/row_id user_id project_name collaborator_id/], table => 'collaboration') };
438
  if ($@) {
439
    my $error = "Can't create collaboration table properly: $@";
440
    $self->app->log->error($error);
441
    croak $error;
442
  }
443

            
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
444
  # Create number table
445
  eval {
446
    my $sql = <<"EOS";
447
create table number (
448
  row_id integer primary key autoincrement,
449
  key not null unique
450
);
451
EOS
452
    $dbi->execute($sql);
453
  };
454
  
455
  # Create number columns
456
  my $number_columns = [
457
    "value integer not null default '0'"
458
  ];
459
  for my $column (@$number_columns) {
460
    eval { $dbi->execute("alter table number add column $column") };
461
  }
462

            
463
  # Check number table
464
  eval { $dbi->select([qw/row_id key value/], table => 'number') };
465
  if ($@) {
466
    my $error = "Can't create number table properly: $@";
467
    $self->app->log->error($error);
468
    croak $error;
469
  }
470
  
471
  # Original project id numbert
472
  eval { $dbi->insert({key => 'original_pid'}, table => 'number') };
473
  my $original_pid = $dbi->select(
474
    'key',
475
    table => 'number',
476
    where => {key => 'original_pid'}
477
  )->value;
478
  unless (defined $original_pid) {
479
    my $error = "Can't create original_pid row in number table";
480
    $self->app->log->error($error);
481
    croak $error;
482
  }
483
}
484

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

            
486
sub update_authorized_keys_file {
487
  my $self = shift;
488

            
489
  my $authorized_keys_file = $self->authorized_keys_file;
490
  if (defined $authorized_keys_file) {
491
    
492
    # Lock file
fix update_authorized_keys_f...
gitprep authored on 2014-05-19
493
    my $lock_file = $self->app->home->rel_file('lock/authorized_keys');
add key delete feature and u...
gitprep authored on 2014-05-19
494
    open my $lock_fh, $lock_file
495
      or croak "Can't open lock file $lock_file";
496
    flock $lock_fh, LOCK_EX
497
      or croak "Can't lock $lock_file";
498
    
499
    # Create authorized_keys_file
500
    unless (-f $authorized_keys_file) {
501
      open my $fh, '>', $authorized_keys_file
fix gitprep-shell
gitprep authored on 2014-05-19
502
        or croak "Can't create authorized_keys file: $authorized_keys_file";
503
      chmod 0600, $authorized_keys_file
504
        or croak "Can't chmod authorized_keys file: $authorized_keys_file";
add key delete feature and u...
gitprep authored on 2014-05-19
505
    }
506
    
507
    # Parse file
forbidden public key which i...
Yuki Kimoto authored on 2014-12-15
508
    my $result = $self->parse_authorized_keys_file($authorized_keys_file);
fix update_authorized_keys_f...
gitprep authored on 2014-05-19
509
    my $before_part = $result->{before_part};
510
    my $gitprep_part = $result->{gitprep_part};
511
    my $after_part = $result->{after_part};
512
    my $start_symbol = $result->{start_symbol};
513
    my $end_symbol = $result->{end_symbol};
add key delete feature and u...
gitprep authored on 2014-05-19
514
    
515
    # Backup at first time
516
    if ($gitprep_part eq '') {
517
      # Backup original file
518
      my $to = "$authorized_keys_file.gitprep.original";
519
      unless (-f $to) {
520
        copy $authorized_keys_file, $to
521
          or croak "Can't copy $authorized_keys_file to $to";
522
      }
523
    }
fix update_authorized_keys_f...
gitprep authored on 2014-05-19
524

            
add key delete feature and u...
gitprep authored on 2014-05-19
525
    # Create public keys
fix update_authorized_keys_f...
gitprep authored on 2014-05-19
526
    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
527
    my $ssh_public_keys_str = '';
528
    for my $key (@$ssh_public_keys) {
fix permission bug
gitprep authored on 2014-05-19
529
      my $ssh_public_key_str = 'command="' . $self->app->home->rel_file('script/gitprep-shell')
530
        . " $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
531
      $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
532
    }
533
    
534
    # Output tmp file
fix gitprep-shell
gitprep authored on 2014-05-19
535
    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
536
    my $output_file = "$authorized_keys_file.gitprep.tmp";
537
    open my $out_fh, '>', $output_file
538
      or croak "Can't create authorized_keys tmp file $output_file";
539
    print $out_fh $output;
540
    close $out_fh
541
      or croak "Can't close authorized_keys tmp file $output_file";
fix update_authorized_keys_f...
gitprep authored on 2014-05-19
542

            
add key delete feature and u...
gitprep authored on 2014-05-19
543
    # Replace
fix permission bug
gitprep authored on 2014-05-19
544
    chmod 0600, $output_file
545
      or croak "Can't chmod authorized_keys tmp file: $output_file";
add key delete feature and u...
gitprep authored on 2014-05-19
546
    move $output_file, $authorized_keys_file
547
      or croak "Can't replace $authorized_keys_file by $output_file";
548
    
549
    # Unlock file
550
    flock $lock_fh, LOCK_EX
551
      or croak "Can't unlock $lock_file"
552
  }
553
  else {
554
    croak qq/authorized_keys file "$authorized_keys_file" is not found./;
555
  }
556
}
557

            
forbidden public key which i...
Yuki Kimoto authored on 2014-12-15
558
sub parse_authorized_keys_file {
fix update_authorized_keys_f...
gitprep authored on 2014-05-19
559
  my ($self, $file) = @_;
add key delete feature and u...
gitprep authored on 2014-05-19
560
  
561
  my $start_symbol = "# gitprep start";
562
  my $end_symbol = "# gitprep end";
563
  
564
  # Parse
565
  open my $fh, '<', $file
fix update_authorized_keys_f...
gitprep authored on 2014-05-19
566
    or croak "Can't open authorized_key file $file";
add key delete feature and u...
gitprep authored on 2014-05-19
567
  my $start_symbol_count = 0;
568
  my $end_symbol_count = 0;
569
  my $before_part = '';
570
  my $gitprep_part = '';
571
  my $after_part = '';
572
  my $error_prefix = "authorized_keys file $file format error:";
573
  while (my $line = <$fh>) {
574
    if ($line =~ /^$start_symbol/) {
575
      if ($start_symbol_count > 0) {
576
        croak qq/$error_prefix "$start_symbol" is found more than one/;
577
      }
578
      else {
579
        if ($end_symbol_count > 0) {
580
          croak qq/$error_prefix "$end_symbol" is found before "$start_symbol"/;
581
        }
582
        else {
583
          $start_symbol_count++;
584
        }
585
      }
586
    }
587
    elsif ($line =~ /^$end_symbol/) {
fix ssh key authentication b...
gitprep authored on 2014-05-20
588
      if ($end_symbol_count > 0) {
add key delete feature and u...
gitprep authored on 2014-05-19
589
        croak qq/$error_prefix "$end_symbol" is found more than one/;
590
      }
591
      else {
fix gitprep-shell
gitprep authored on 2014-05-19
592
        $end_symbol_count++;
add key delete feature and u...
gitprep authored on 2014-05-19
593
      }
594
    }
595
    elsif ($start_symbol_count == 0 && $end_symbol_count == 0) {
596
      $before_part .= $line;
597
    }
598
    elsif ($start_symbol_count == 1 && $end_symbol_count == 0) {
599
      $gitprep_part .= $line;
600
    }
601
    elsif ($start_symbol_count == 1 && $end_symbol_count == 1) {
602
      $after_part .= $line;
603
    }
604
  }
fix update_authorized_keys_f...
gitprep authored on 2014-05-19
605
  
606
  my $result = {
607
    start_symbol => $start_symbol,
608
    end_symbol => $end_symbol,
609
    before_part => $before_part,
610
    gitprep_part => $gitprep_part,
611
    after_part => $after_part
612
  };
613
  
614
  return $result;
add key delete feature and u...
gitprep authored on 2014-05-19
615
}
616

            
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
617
sub _create_project {
618
  my ($self, $user, $project, $params) = @_;
619
  $params ||= {};
620
  
621
  # Create project
cleanup
Yuki Kimoto authored on 2013-05-15
622
  my $dbi = $self->app->dbi;
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
623
  $dbi->connector->txn(sub {
624
    unless (defined $params->{original_pid}) {
625
      my $number = $dbi->model('number')->select('value', where => {key => 'original_pid'})->value;
626
      $number++;
627
      $dbi->model('number')->update({value => $number}, where => {key => 'original_pid'});
628
      $params->{original_pid} = $number;
629
    }
630
    $dbi->model('project')->insert($params, id => [$user, $project]);
631
  });
632
}
633

            
634
sub _create_rep {
635
  my ($self, $user, $project, $opts) = @_;
636
  
637
  # Create repository directory
revert encoding support
Yuki Kimoto authored on 2013-11-22
638
  my $git = $self->app->git;
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
639
  my $rep = $git->rep($user, $project);
640
  mkdir $rep
641
    or croak "Can't create directory $rep: $!";
fix bug that in CGI reposito...
Yuki Kimoto authored on 2015-11-05
642
  
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
643
  eval {
644
    # Git init
645
    {
646
      my @git_init_cmd = $git->cmd_rep($rep, 'init', '--bare');
fix bug that in CGI reposito...
Yuki Kimoto authored on 2015-11-05
647
      Gitprep::Util::run_command(@git_init_cmd)
fix git 2.0 create repositor...
Yuki Kimoto authored on 2015-11-04
648
        or croak  "Can't execute git init --bare:@git_init_cmd";
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
649
    }
650
    
651
    # Add git-daemon-export-ok
652
    {
653
      my $file = "$rep/git-daemon-export-ok";
654
      open my $fh, '>', $file
655
        or croak "Can't create git-daemon-export-ok: $!"
656
    }
657
    
658
    # HTTP support
659
    my @git_update_server_info_cmd = $git->cmd_rep(
660
      $rep,
661
      '--bare',
662
      'update-server-info'
663
    );
fix bug that in CGI reposito...
Yuki Kimoto authored on 2015-11-05
664
    Gitprep::Util::run_command(@git_update_server_info_cmd)
fix git 2.0 create repositor...
Yuki Kimoto authored on 2015-11-04
665
      or croak "Can't execute git --bare update-server-info";
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
666
    move("$rep/hooks/post-update.sample", "$rep/hooks/post-update")
667
      or croak "Can't move post-update";
668
    
669
    # Description
version up and add descripti...
Yuki Kimoto authored on 2013-11-22
670
    my $description = $opts->{description};
671
    $description = '' unless defined $description;
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
672
    {
673
      my $file = "$rep/description";
674
      open my $fh, '>', $file
675
        or croak "Can't open $file: $!";
676
      print $fh encode('UTF-8', $description)
677
        or croak "Can't write $file: $!";
678
      close $fh;
679
    }
680
    
681
    # Add README and commit
682
    if ($opts->{readme}) {
683
      # Create working directory
use own tmp directory to cre...
Yuki Kimoto authored on 2016-03-24
684
      my $home_tmp_dir = $self->app->home->rel_file('tmp');
685
      
686
      my $temp_dir =  File::Temp->newdir(DIR => $home_tmp_dir);
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
687
      my $temp_work = "$temp_dir/work";
688
      mkdir $temp_work
689
        or croak "Can't create directory $temp_work: $!";
use own tmp directory to cre...
Yuki Kimoto authored on 2016-03-24
690
      
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
691
      # Git init
692
      my @git_init_cmd = $git->cmd_rep($temp_work, 'init', '-q');
fix bug that in CGI reposito...
Yuki Kimoto authored on 2015-11-05
693
      Gitprep::Util::run_command(@git_init_cmd)
fix git 2.0 create repositor...
Yuki Kimoto authored on 2015-11-04
694
        or croak "Can't execute git init: @git_init_cmd";
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
695
      
696
      # Add README
default readme file is chang...
Yuki Kimoto authored on 2013-11-16
697
      my $file = "$temp_work/README.md";
698
      open my $readme_fh, '>', $file
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
699
        or croak "Can't create $file: $!";
version up and add descripti...
Yuki Kimoto authored on 2013-11-22
700
      print $readme_fh "# $project\n";
701
      print $readme_fh "\n" . encode('UTF-8', $description) . "\n";
default readme file is chang...
Yuki Kimoto authored on 2013-11-16
702
      close $readme_fh;
703
      
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
704
      my @git_add_cmd = $git->cmd_rep(
705
        $temp_work,
706
        "--work-tree=$temp_work",
707
        'add',
default readme file is chang...
Yuki Kimoto authored on 2013-11-16
708
        'README.md'
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
709
      );
fix bug that in CGI reposito...
Yuki Kimoto authored on 2015-11-05
710
      Gitprep::Util::run_command(@git_add_cmd)
fix git 2.0 create repositor...
Yuki Kimoto authored on 2015-11-04
711
        or croak "Can't execute git add: @git_add_cmd";
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
712
      
fix bug that repository with...
Yuki Kimoto authored on 2016-04-12
713
      # Set user name
714
      my @git_config_user_name = $git->cmd_rep(
715
        $temp_work,
716
        "--work-tree=$temp_work",
717
        'config',
718
        'user.name',
719
        $user
720
      );
721
      Gitprep::Util::run_command(@git_config_user_name)
722
        or croak "Can't execute git config: @git_config_user_name";
723
      
724
      # Set user mail
725
      my $user_mail = $self->app->dbi->model('user')->select('mail', where => {id => $user})->value;
726
      my @git_config_user_mail = $git->cmd_rep(
727
        $temp_work,
728
        "--work-tree=$temp_work",
729
        'config',
730
        'user.email',
731
        "$user_mail"
732
      );
733
      Gitprep::Util::run_command(@git_config_user_mail)
734
        or croak "Can't execute git config: @git_config_user_mail";
735
      
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
736
      # Commit
737
      my @git_commit_cmd = $git->cmd_rep(
738
        $temp_work,
739
        "--work-tree=$temp_work",
740
        'commit',
741
        '-q',
742
        '-m',
743
        'first commit'
744
      );
fix bug that in CGI reposito...
Yuki Kimoto authored on 2015-11-05
745
      Gitprep::Util::run_command(@git_commit_cmd)
fix git 2.0 create repositor...
Yuki Kimoto authored on 2015-11-04
746
        or croak "Can't execute git commit: @git_commit_cmd";
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
747
      
748
      # Push
749
      {
750
        my @git_push_cmd = $git->cmd_rep(
751
          $temp_work,
752
          "--work-tree=$temp_work",
753
          'push',
754
          '-q',
755
          $rep,
756
          'master'
757
        );
758
        # (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
759
        Gitprep::Util::run_command(@git_push_cmd)
fix git 2.0 create repositor...
Yuki Kimoto authored on 2015-11-04
760
          or croak "Can't execute git push: @git_push_cmd";
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
761
      }
762
    }
763
  };
improve faq of pushing lerge...
Yuki Kimoto authored on 2014-02-17
764
  if (my $e = $@) {
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
765
    rmtree $rep;
improve faq of pushing lerge...
Yuki Kimoto authored on 2014-02-17
766
    croak $e;
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
767
  }
768
}
769

            
show deleted repository in u...
Yuki Kimoto authored on 2013-05-15
770
sub _create_db_user {
771
  my ($self, $user, $data) = @_;
772
  
773
  # Create database user
774
  $self->app->dbi->model('user')->insert($data, id => $user);
775
}
776

            
777
sub _create_user_dir {
778
  my ($self, $user) = @_;
779
  
780
  # Create user directory
revert encoding support
Yuki Kimoto authored on 2013-11-22
781
  my $rep_home = $self->app->git->rep_home;
show deleted repository in u...
Yuki Kimoto authored on 2013-05-15
782
  my $user_dir = "$rep_home/$user";
783
  mkpath $user_dir;
784
}
785

            
786
sub _delete_db_user {
787
  my ($self, $user) = @_;
788
  
789
  # Delete database user
added delete user test
Yuki Kimoto authored on 2013-05-18
790
  my $count = $self->app->dbi->model('user')->delete(id => $user);
791
  
792
  return $count;
show deleted repository in u...
Yuki Kimoto authored on 2013-05-15
793
}
794

            
795
sub _delete_user_dir {
796
  my ($self, $user) = @_;
797
  
798
  # Delete user directory
revert encoding support
Yuki Kimoto authored on 2013-11-22
799
  my $rep_home = $self->app->git->rep_home;
show deleted repository in u...
Yuki Kimoto authored on 2013-05-15
800
  my $user_dir = "$rep_home/$user";
801
  rmtree $user_dir;
802
}
803

            
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
804
sub _delete_project {
805
  my ($self, $user, $project) = @_;
806
  
cleanup
Yuki Kimoto authored on 2013-05-15
807
  # Delete project
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
808
  my $dbi = $self->app->dbi;
809
  $dbi->model('project')->delete(id => [$user, $project]);
810
}
811

            
812
sub _delete_rep {
813
  my ($self, $user, $project) = @_;
814

            
cleanup
Yuki Kimoto authored on 2013-05-15
815
  # Delete repository
revert encoding support
Yuki Kimoto authored on 2013-11-22
816
  my $rep_home = $self->app->git->rep_home;
add missing 'o'
reneeb authored on 2013-10-14
817
  croak "Can't remove repository. repository home is empty"
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
818
    if !defined $rep_home || $rep_home eq '';
819
  my $rep = "$rep_home/$user/$project.git";
820
  rmtree $rep;
821
  croak "Can't remove repository. repository is rest"
822
    if -e $rep;
823
}
824

            
fixed bug that setting page ...
Yuki Kimoto authored on 2013-05-15
825
sub exists_project {
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
826
  my ($self, $user, $project) = @_;
cleanup
Yuki Kimoto authored on 2013-05-15
827
  
828
  # Exists project
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
829
  my $dbi = $self->app->dbi;
830
  my $row = $dbi->model('project')->select(id => [$user, $project])->one;
831
  
832
  return $row ? 1 : 0;
833
}
834

            
add rename project and suppr...
Yuki Kimoto authored on 2013-05-24
835
sub exists_user {
836
  my ($self, $user) = @_;
837
  
838
  # Exists project
839
  my $row = $self->app->dbi->model('user')->select(id => $user)->one;
840
  
841
  return $row ? 1 : 0;
842
}
843

            
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
844
sub _exists_rep {
845
  my ($self, $user, $project) = @_;
846
  
cleanup
Yuki Kimoto authored on 2013-05-15
847
  # Exists repository
revert encoding support
Yuki Kimoto authored on 2013-11-22
848
  my $rep = $self->app->git->rep($user, $project);
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
849
  
850
  return -e $rep;
851
}
852

            
show deleted repository in u...
Yuki Kimoto authored on 2013-05-15
853
sub _fork_rep {
854
  my ($self, $user, $project, $to_user, $to_project) = @_;
855
  
cleanup
Yuki Kimoto authored on 2013-05-15
856
  # Fork repository
revert encoding support
Yuki Kimoto authored on 2013-11-22
857
  my $git = $self->app->git;
show deleted repository in u...
Yuki Kimoto authored on 2013-05-15
858
  my $rep = $git->rep($user, $project);
859
  my $to_rep = $git->rep($to_user, $to_project);
860
  my @cmd = (
861
    $git->bin,
862
    'clone',
863
    '-q',
864
    '--bare',
865
    $rep,
866
    $to_rep
867
  );
fix bug that in CGI reposito...
Yuki Kimoto authored on 2015-11-05
868
  Gitprep::Util::run_command(@cmd)
fix description file not cop...
Yuki Kimoto authored on 2013-05-25
869
    or croak "Can't fork repository(_fork_rep): @cmd";
870
  
871
  # Copy description
872
  copy "$rep/description", "$to_rep/description"
873
    or croak "Can't copy description file(_fork_rep)";
show deleted repository in u...
Yuki Kimoto authored on 2013-05-15
874
}
875

            
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
876
sub _rename_project {
877
  my ($self, $user, $project, $renamed_project) = @_;
878
  
cleanup
Yuki Kimoto authored on 2013-05-15
879
  # Check arguments
add rename project and suppr...
Yuki Kimoto authored on 2013-05-24
880
  croak "Invalid parameters(_rename_project)"
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
881
    unless defined $user && defined $project && defined $renamed_project;
882
  
883
  # Rename project
cleanup
Yuki Kimoto authored on 2013-05-15
884
  my $dbi = $self->app->dbi;
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
885
  $dbi->model('project')->update(
886
    {name => $renamed_project},
887
    id => [$user, $project]
888
  );
889
}
890

            
891
sub _rename_rep {
892
  my ($self, $user, $project, $renamed_project) = @_;
893
  
cleanup
Yuki Kimoto authored on 2013-05-15
894
  # Check arguments
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
895
  croak "Invalid user name or project"
896
    unless defined $user && defined $project && defined $renamed_project;
cleanup
Yuki Kimoto authored on 2013-05-15
897

            
898
  # Rename repository
revert encoding support
Yuki Kimoto authored on 2013-11-22
899
  my $rep = $self->app->git->rep($user, $project);
900
  my $renamed_rep = $self->app->git->rep($user, $renamed_project);
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
901
  move($rep, $renamed_rep)
902
    or croak "Can't move $rep to $renamed_rep: $!";
903
}
904

            
905
1;