gitprep / lib / Gitprep.pm /
Newer Older
400 lines | 11.424kb
copy gitweblite soruce code
root authored on 2012-11-23
1
use 5.008007;
renamed gitpub to gitprep
Yuki Kimoto authored on 2012-11-26
2
package Gitprep;
copy gitweblite soruce code
root authored on 2012-11-23
3
use Mojo::Base 'Mojolicious';
cleanup
Yuki Kimoto authored on 2013-05-14
4

            
5
use Carp 'croak';
added start page
Yuki Kimoto authored on 2013-02-09
6
use DBIx::Custom;
little more secure login
Yuki Kimoto authored on 2013-03-16
7
use Gitprep::API;
cleanup
Yuki Kimoto authored on 2013-05-14
8
use Gitprep::Git;
fixed tags page paging bug a...
Yuki Kimoto authored on 2013-05-01
9
use Gitprep::Manager;
added RepManager rename feat...
Yuki Kimoto authored on 2013-03-27
10
use Scalar::Util 'weaken';
cleanup
Yuki Kimoto authored on 2013-05-14
11
use Validator::Custom;
fix bug that forst . is not ...
Yuki Kimoto authored on 2014-04-21
12
use Mojolicious::Plugin::AutoRoute::Util 'template';
copy gitweblite soruce code
root authored on 2012-11-23
13

            
Digest::SHA loading to Mojo:...
Yuki Kimoto authored on 2013-08-13
14
# Digest::SHA loading to Mojo::Util if not loaded
15
{
16
  package Mojo::Util;
17
  eval {require Digest::SHA; import Digest::SHA qw(sha1 sha1_hex)};
18
}
19

            
version up
Yuki Kimoto authored on 2014-04-25
20
our $VERSION = 'v1.7';
cleanup
Yuki Kimoto authored on 2013-05-14
21

            
added start page
Yuki Kimoto authored on 2013-02-09
22
has 'dbi';
revert encoding support
Yuki Kimoto authored on 2013-11-22
23
has 'git';
added RepManager rename feat...
Yuki Kimoto authored on 2013-03-27
24
has 'manager';
rename validator to vc to up...
Yuki Kimoto authored on 2013-12-02
25
has 'vc';
revert encoding support
Yuki Kimoto authored on 2013-11-22
26

            
27
use constant BUFFER_SIZE => 8192;
add /info/refs request
Yuki Kimoto authored on 2013-10-01
28

            
copy gitweblite soruce code
root authored on 2012-11-23
29
sub startup {
30
  my $self = shift;
31
  
cleanup
Yuki Kimoto authored on 2013-05-14
32
  # Config file
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
33
  $self->plugin('INIConfig', {ext => 'conf'});
34
  
cleanup
Yuki Kimoto authored on 2013-05-14
35
  # Config file for developper
add need_login_always_option
Yuki Kimoto authored on 2013-08-21
36
  unless ($ENV{GITPREP_NO_MYCONFIG}) {
37
    my $my_conf_file = $self->home->rel_file('gitprep.my.conf');
38
    $self->plugin('INIConfig', {file => $my_conf_file}) if -f $my_conf_file;
39
  }
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
40
  
cleanup
Yuki Kimoto authored on 2013-05-14
41
  # Listen
copy gitweblite soruce code
root authored on 2012-11-23
42
  my $conf = $self->config;
cleanup
Yuki Kimoto authored on 2013-05-14
43
  my $listen = $conf->{hypnotoad}{listen} ||= ['http://*:10020'];
44
  $listen = [split /,/, $listen] unless ref $listen eq 'ARRAY';
fixed listen bug
Yuki Kimoto authored on 2013-03-29
45
  $conf->{hypnotoad}{listen} = $listen;
copy gitweblite soruce code
root authored on 2012-11-23
46
  
revert encoding support
Yuki Kimoto authored on 2013-11-22
47
  # Git
renamed gitpub to gitprep
Yuki Kimoto authored on 2012-11-26
48
  my $git = Gitprep::Git->new;
cleanup
Yuki Kimoto authored on 2013-05-14
49
  my $git_bin
50
    = $conf->{basic}{git_bin} ? $conf->{basic}{git_bin} : $git->search_bin;
fix mini bug
Yuki Kimoto authored on 2013-03-31
51
  if (!$git_bin || ! -e $git_bin) {
52
    $git_bin ||= '';
cleanup
Yuki Kimoto authored on 2013-05-14
53
    my $error = "Can't detect or found git command ($git_bin)."
54
      . " set git_bin in gitprep.conf";
fixed git_bin not found bug
Yuki Kimoto authored on 2013-03-31
55
    $self->log->error($error);
56
    croak $error;
57
  }
improve code structures
Yuki Kimoto authored on 2013-11-19
58
  $git->bin($git_bin);
revert encoding support
Yuki Kimoto authored on 2013-11-22
59
  
Supported specific character...
Tetsuya Hayashi authored on 2014-03-15
60
  # Encoding suspects list for Git
61
  my $encoding_suspects
62
    = $conf->{basic}{encoding_suspects} ||= 'utf8';
63
  $encoding_suspects = [split /,/, $encoding_suspects] unless ref $encoding_suspects eq 'ARRAY';
64
  $git->encoding_suspects($encoding_suspects);
65

            
revert encoding support
Yuki Kimoto authored on 2013-11-22
66
  # Repository Manager
67
  my $manager = Gitprep::Manager->new(app => $self);
68
  weaken $manager->{app};
69
  $self->manager($manager);
70
  
add key delete feature and u...
gitprep authored on 2014-05-19
71
  # authorized_keys file
72
  my $authorized_keys_file = $self->config('authorized_keys_file');
73
  unless (defined $authorized_keys_file) {
74
    if (defined $ENV{HOME}) {
75
      $authorized_keys_file = "$ENV{HOME}/.ssh/authorized_keys_file";
76
    }
77
  }
78
  if (defined $authorized_keys_file) {
79
    $self->manager->authorized_keys_file($authorized_keys_file);
80
  }
81
  else {
82
    $self->app->log->warn(qq/Config "authorized_keys_file" can't be detected/);
83
  }
84
  
fixed git_bin not found bug
Yuki Kimoto authored on 2013-03-31
85
  # Repository home
cleanup
Yuki Kimoto authored on 2013-05-14
86
  my $rep_home = $ENV{GITPREP_REP_HOME} || $self->home->rel_file('data/rep');
revert encoding support
Yuki Kimoto authored on 2013-11-22
87
  $git->rep_home($rep_home);
added create repository feat...
Yuki Kimoto authored on 2013-03-18
88
  unless (-d $rep_home) {
89
    mkdir $rep_home
90
      or croak "Can't create directory $rep_home: $!";
91
  }
cleanup
Yuki Kimoto authored on 2013-05-14
92
  
add time zone system
Yuki Kimoto authored on 2014-03-08
93
  # Time Zone
94
  if (my $time_zone = $conf->{basic}{time_zone}) {
95
    
96
    if ($time_zone =~ /^([\+-])?([0-9]?[0-9]):([0-9][0-9])$/) {
97
      my $sign = $1 || '';
98
      my $hour = $2;
99
      my $min = $3;
100
      
101
      my $time_zone_second = $sign . ($hour * 60 * 60) + ($min * 60);
102
      $git->time_zone_second($time_zone_second);
103
    }
104
    else {
105
      $self->log->warn("Bad time zone $time_zone. Time zone become GMT");
106
    }
107
  }
108
  $self->git($git);
cleanup
Yuki Kimoto authored on 2013-05-14
109
  
improved create repository f...
Yuki Kimoto authored on 2013-03-21
110
  # DBI
added test
Yuki Kimoto authored on 2013-04-30
111
  my $db_file = $ENV{GITPREP_DB_FILE}
112
    || $self->home->rel_file('data/gitprep.db');
improved create repository f...
Yuki Kimoto authored on 2013-03-21
113
  my $dbi = DBIx::Custom->connect(
114
    dsn => "dbi:SQLite:database=$db_file",
115
    connector => 1,
added original_pid column
Yuki Kimoto authored on 2013-04-18
116
    option => {sqlite_unicode => 1, sqlite_use_immediate_transaction => 1}
improved create repository f...
Yuki Kimoto authored on 2013-03-21
117
  );
118
  $self->dbi($dbi);
119
  
cleanup
Yuki Kimoto authored on 2013-05-14
120
  # Database file permision
fixed database file permissi...
Yuki Kimoto authored on 2013-04-19
121
  if (my $user = $self->config->{hypnotoad}{user}) {
122
    my $uid = (getpwnam $user)[2];
123
    chown $uid, -1, $db_file;
124
  }
125
  if (my $group = $self->config->{hypnotoad}{group}) {
126
    my $gid = (getgrnam $group)[2];
127
    chown -1, $gid, $db_file;
128
  }
129
  
added tab_index columns
Yuki Kimoto authored on 2013-04-11
130
  # Setup database
131
  $self->manager->setup_database;
improved create repository f...
Yuki Kimoto authored on 2013-03-21
132
  
133
  # Model
134
  my $models = [
135
    {table => 'user', primary_key => 'id'},
add ssh key add feature
gitprep authored on 2014-05-17
136
    {table => 'ssh_public_key', primary_key => ['user_id', 'key']},
added original_pid column
Yuki Kimoto authored on 2013-04-18
137
    {table => 'project', primary_key => ['user_id', 'name']},
add collaborator register lo...
Yuki Kimoto authored on 2013-11-17
138
    {table => 'number', primary_key => 'key'},
139
    {table => 'collaboration', primary_key => ['user_id', 'project_name', 'collaborator_id']}
improved create repository f...
Yuki Kimoto authored on 2013-03-21
140
  ];
141
  $dbi->create_model($_) for @$models;
142

            
143
  # Validator
rename validator to vc to up...
Yuki Kimoto authored on 2013-12-02
144
  my $vc = Validator::Custom->new;
145
  $self->vc($vc);
146
  $vc->register_constraint(
added project delete feature
Yuki Kimoto authored on 2013-03-24
147
    user_name => sub {
148
      my $value = shift;
149
      
allow . as project name
Yuki Kimoto authored on 2014-04-21
150
      return ($value || '') =~ /^[a-zA-Z0-9_\-]+$/;
added project delete feature
Yuki Kimoto authored on 2013-03-24
151
    },
152
    project_name => sub {
153
      my $value = shift;
allow . as project name
Yuki Kimoto authored on 2014-04-21
154
      return 0 unless defined $value;
155
      return 0 if $value eq '.' || $value eq '..';
156

            
157
      return ($value || '') =~ /[a-zA-Z0-9_\-\.]+$/;
added project delete feature
Yuki Kimoto authored on 2013-03-24
158
    }
159
  );
improved create repository f...
Yuki Kimoto authored on 2013-03-21
160
  
fixed receive pack bug and a...
Yuki Kimoto authored on 2013-10-03
161
  # Basic auth plugin
162
  $self->plugin('BasicAuth');
added admin user tests
Yuki Kimoto authored on 2013-05-17
163

            
added login page
Yuki Kimoto authored on 2013-02-09
164
  {
cleanup
Yuki Kimoto authored on 2013-05-14
165
    my $r = $self->routes;
166

            
167
    # DBViewer(only development)
168
    if ($self->mode eq 'development') {
169
      eval {
170
        $self->plugin(
171
          'DBViewer',
172
          dsn => "dbi:SQLite:database=$db_file"
173
        );
174
      };
added reset password feature
Yuki Kimoto authored on 2013-04-10
175
    }
added AutoRoute plugin
Yuki Kimoto authored on 2013-04-09
176
    
cleanup
Yuki Kimoto authored on 2013-05-14
177
    # Auto route
cleanup many pages
Yuki Kimoto authored on 2013-03-31
178
    {
cleanup
Yuki Kimoto authored on 2013-05-14
179
      my $r = $r->under(sub {
180
        my $self = shift;
181
        
182
        my $api = $self->gitprep_api;
183
        
add need_login_always_option
Yuki Kimoto authored on 2013-08-21
184
        # Authentication
cleanup
Yuki Kimoto authored on 2013-05-14
185
        {
186
          my $path = $self->req->url->path->parts->[0] || '';
add need_login_always_option
Yuki Kimoto authored on 2013-08-21
187
          
188
          # Admin
cleanup
Yuki Kimoto authored on 2013-05-14
189
          if ($path eq '_admin' && !$api->logined_admin) {
190
            $self->redirect_to('/');
191
            return;
192
          }
193
        }
194
        
195
        return 1; 
196
      });
add need_login_always_option
Yuki Kimoto authored on 2013-08-21
197
      
198
      # Auto routes
cleanup
Yuki Kimoto authored on 2013-05-14
199
      $self->plugin('AutoRoute', route => $r);
fix bug that forst . is not ...
Yuki Kimoto authored on 2014-04-21
200
      
add need_login_always_option
Yuki Kimoto authored on 2013-08-21
201
      # Custom routes
cleanup
Yuki Kimoto authored on 2013-05-14
202
      {
add ssh key add feature
gitprep authored on 2014-05-17
203
        # Show ssh keys
204
        $r->get('/:user.keys' => template '/user-keys');
205
        
add need_login_always_option
Yuki Kimoto authored on 2013-08-21
206
        # User
allow . as project name
Yuki Kimoto authored on 2014-04-21
207
        my $r = $r->route('/:user');
add need_login_always_option
Yuki Kimoto authored on 2013-08-21
208
        {
209
          # Home
fix bug that forst . is not ...
Yuki Kimoto authored on 2014-04-21
210
          $r->get('/' => [format => 0] => template '/user');
add need_login_always_option
Yuki Kimoto authored on 2013-08-21
211
          
212
          # Settings
213
          $r->get('/_settings' => template '/user-settings');
add ssh keys page design
gitprep authored on 2014-05-17
214
          
215
          # SSH keys
add ssh key add feature
gitprep authored on 2014-05-17
216
          $r->any('/_settings/ssh' => template '/user-settings/ssh');
add need_login_always_option
Yuki Kimoto authored on 2013-08-21
217
        }
add /info/refs request
Yuki Kimoto authored on 2013-10-01
218

            
219
        # Smart HTTP
220
        {
allow . as project name
Yuki Kimoto authored on 2014-04-21
221
          my $r = $r->route('/(#project).git');
add /info/refs request
Yuki Kimoto authored on 2013-10-01
222
          
fixed receive pack bug and a...
Yuki Kimoto authored on 2013-10-03
223
          {
224
            my $r = $r->under(sub {
225
              my $self = shift;
226
              
cleanup
Yuki Kimoto authored on 2013-11-16
227
              my $api = $self->gitprep_api;
complete private repository ...
Yuki Kimoto authored on 2013-11-16
228
              my $user = $self->param('user');
229
              my $project = $self->param('project');
230
              my $private = $self->app->manager->is_private_project($user, $project);
cleanup
Yuki Kimoto authored on 2013-11-16
231
              
fixed receive pack bug and a...
Yuki Kimoto authored on 2013-10-03
232
              # Basic auth when push request
233
              my $service = $self->param('service') || '';
complete private repository ...
Yuki Kimoto authored on 2013-11-16
234
              if ($service eq 'git-receive-pack' || $private) {
fixed receive pack bug and a...
Yuki Kimoto authored on 2013-10-03
235
                
236
                $self->basic_auth("Git Area", sub {
complete private repository ...
Yuki Kimoto authored on 2013-11-16
237
                  my ($auth_user, $auth_password) = @_;
add warnings when authentica...
Yuki Kimoto authored on 2014-02-18
238
                  
239
                  if (!defined $auth_user || !length $auth_user) {
240
                    $self->app->log->warn("Authentication: User name is empty");
241
                  }
242
                  
fix warnings
Yuki Kimoto authored on 2013-11-18
243
                  $auth_user = '' unless defined $auth_user;
244
                  $auth_password = '' unless defined $auth_password;
fixed receive pack bug and a...
Yuki Kimoto authored on 2013-10-03
245
                  
complete collaborator featur...
Yuki Kimoto authored on 2013-11-17
246
                  my $is_valid =
247
                    ($user eq $auth_user || $api->is_collaborator($user, $project, $auth_user))
248
                    && $api->check_user_and_password($auth_user, $auth_password);
fixed receive pack bug and a...
Yuki Kimoto authored on 2013-10-03
249
                  
250
                  return $is_valid;
251
                });
252
              }
253
              else {
254
                return 1;
255
              }
256
            });
257
            
redirect .git access to /use...
Yuki Kimoto authored on 2014-02-26
258
            # /
259
            $r->get('/')->to(cb => sub {
260
              my $self = shift;
261
              
262
              my $user = $self->param('user');
263
              my $project = $self->param('project');
264
              
265
              $self->redirect_to("/$user/$project");
266
            });
267
            
fixed receive pack bug and a...
Yuki Kimoto authored on 2013-10-03
268
            # /info/refs
269
            $r->get('/info/refs' => template 'smart-http/info-refs');
270
            
271
            # /git-upload-pack or /git-receive-pack
272
            $r->any('/git-(:service)'
273
              => [service => qr/(?:upload-pack|receive-pack)/]
274
              => template 'smart-http/service'
275
            );
276
            
277
            # Static file
278
            $r->get('/(*Path)' => template 'smart-http/static');
279
          }
add /info/refs request
Yuki Kimoto authored on 2013-10-01
280
        }
281
                
add need_login_always_option
Yuki Kimoto authored on 2013-08-21
282
        # Project
283
        {
allow . as project name
Yuki Kimoto authored on 2014-04-21
284
          my $r = $r->route('/#project');
add need_login_always_option
Yuki Kimoto authored on 2013-08-21
285
          
add private reporitory featu...
Yuki Kimoto authored on 2013-11-16
286
          {
287
            my $r = $r->under(sub {
288
              my $self = shift;
289
              
cleanup
Yuki Kimoto authored on 2013-11-16
290
              # API
291
              my $api = $self->gitprep_api;
improve code structures
Yuki Kimoto authored on 2013-11-19
292
              
293
              # Private
revert encoding support
Yuki Kimoto authored on 2013-11-22
294
              my $user = $self->param('user');
295
              my $project = $self->param('project');
add private reporitory featu...
Yuki Kimoto authored on 2013-11-16
296
              my $private = $self->app->manager->is_private_project($user, $project);
297
              if ($private) {
complete collaborator featur...
Yuki Kimoto authored on 2013-11-17
298
                if ($api->can_access_private_project($user, $project)) {
add private reporitory featu...
Yuki Kimoto authored on 2013-11-16
299
                  return 1;
300
                }
301
                else {
302
                  $self->render('private');
303
                  return 0;
304
                }
305
              }
306
              else {
307
                return 1;
308
              }
309
            });
310
            
311
            # Home
312
            $r->get('/' => template '/project');
313
            
314
            # Commit
315
            $r->get('/commit/*diff' => template '/commit');
316
            
317
            # Commits
318
            $r->get('/commits/*rev_file' => template '/commits');
319
            
320
            # Branches
321
            $r->any('/branches/*base_branch' => {base_branch => undef} => template '/branches');
added feature that you see p...
Yuki Kimoto authored on 2013-04-11
322

            
add private reporitory featu...
Yuki Kimoto authored on 2013-11-16
323
            # Tags
324
            $r->get('/tags' => template '/tags');
improved network page
Yuki Kimoto authored on 2013-04-16
325

            
add private reporitory featu...
Yuki Kimoto authored on 2013-11-16
326
            # Tree
327
            $r->get('/tree/*rev_dir' => template '/tree');
328
            
329
            # Blob
330
            $r->get('/blob/*rev_file' => template '/blob');
331
            
332
            # Sub module
333
            $r->get('/submodule/*rev_file' => template '/submodule');
add blame method
Yuki Kimoto authored on 2013-08-10
334

            
add private reporitory featu...
Yuki Kimoto authored on 2013-11-16
335
            # Raw
336
            $r->get('/raw/*rev_file' => template '/raw');
add blame method
Yuki Kimoto authored on 2013-08-10
337

            
add private reporitory featu...
Yuki Kimoto authored on 2013-11-16
338
            # Blame
339
            $r->get('/blame/*rev_file' => template '/blame');
340
            
341
            # Archive
342
            $r->get('/archive/(*rev).tar.gz' => template '/archive')->to(archive_type => 'tar');
343
            $r->get('/archive/(*rev).zip' => template '/archive')->to(archive_type => 'zip' );
344
            
345
            # Compare
346
            $r->get('/compare/(*rev1)...(*rev2)' => template '/compare');
347
            
348
            # Settings
349
            $r->any('/settings' => template '/settings');
350
            
add collaboration page
Yuki Kimoto authored on 2013-11-17
351
            # Collaboration
352
            $r->any('/settings/collaboration' => template '/settings/collaboration');
353
            
add private reporitory featu...
Yuki Kimoto authored on 2013-11-16
354
            # Fork
355
            $r->any('/fork' => template '/fork');
cleanup
Yuki Kimoto authored on 2013-05-14
356

            
add private reporitory featu...
Yuki Kimoto authored on 2013-11-16
357
            # Network
358
            $r->get('/network' => template '/network');
cleanup
Yuki Kimoto authored on 2013-05-14
359

            
add private reporitory featu...
Yuki Kimoto authored on 2013-11-16
360
            # Network Graph
361
            $r->get('/network/graph/(*rev1)...(*rev2_abs)' => template '/network/graph');
add pull page design
Yuki Kimoto authored on 2013-08-13
362

            
add private reporitory featu...
Yuki Kimoto authored on 2013-11-16
363
            # Import branch
364
            $r->any('/import-branch/:remote_user/:remote_project' => template '/import-branch');
365
            
366
            # Get branches and tags
367
            $r->get('/api/revs' => template '/api/revs');
368
          }
add need_login_always_option
Yuki Kimoto authored on 2013-08-21
369
        }
cleanup
Yuki Kimoto authored on 2013-05-14
370
      }
added AutoRoute plugin
Yuki Kimoto authored on 2013-04-09
371
    }
372
  }
added admin user tests
Yuki Kimoto authored on 2013-05-17
373

            
374
  # Helper
375
  {
376
    # API
377
    $self->helper(gitprep_api => sub { Gitprep::API->new(shift) });
378
  }
added reverse proxy path_dep...
Yuki Kimoto authored on 2013-04-22
379
  
added AutoRoute plugin
Yuki Kimoto authored on 2013-04-09
380
  # Reverse proxy support
added reverse proxy path_dep...
Yuki Kimoto authored on 2013-04-22
381
  my $reverse_proxy_on = $self->config->{reverse_proxy}{on};
382
  my $path_depth = $self->config->{reverse_proxy}{path_depth};
383
  if ($reverse_proxy_on) {
384
    $ENV{MOJO_REVERSE_PROXY} = 1;
385
    if ($path_depth) {
386
      $self->hook('before_dispatch' => sub {
387
        my $self = shift;
388
        for (1 .. $path_depth) {
389
          my $prefix = shift @{$self->req->url->path->parts};
390
          push @{$self->req->url->base->path->parts}, $prefix;
391
        }
392
      });
added AutoRoute plugin
Yuki Kimoto authored on 2013-04-09
393
    }
added reverse proxy path_dep...
Yuki Kimoto authored on 2013-04-22
394
  }
cleanup
Yuki Kimoto authored on 2013-10-11
395
  
396
  # Smart HTTP Buffer size
fix bug that lerge repositor...
Yuki Kimoto authored on 2014-02-17
397
  $ENV{GITPREP_SMART_HTTP_BUFFER_SIZE} ||= 16384;
copy gitweblite soruce code
root authored on 2012-11-23
398
}
399

            
400
1;