Showing 6 changed files with 137 additions and 90 deletions
+2
lib/Gitprep.pm
... ...
@@ -8,6 +8,8 @@ use Gitprep::Git;
8 8
 
9 9
 has 'git';
10 10
 
11
+has 'root' => '/gitpub';
12
+
11 13
 sub startup {
12 14
   my $self = shift;
13 15
   
+62
lib/Gitprep/API.pm
... ...
@@ -0,0 +1,62 @@
1
+package Gitprep::API;
2
+use Mojo::Base -base;
3
+
4
+use Carp ();
5
+use File::Basename ();
6
+
7
+sub croak { Carp::croak(@_) }
8
+sub dirname { File::Basename::dirname(@_) }
9
+
10
+has 'cntl';
11
+
12
+sub new {
13
+  my ($class, $cntl) = @_;
14
+
15
+  my $self = $class->SUPER::new(cntl => $cntl);
16
+  
17
+  return $self;
18
+}
19
+
20
+sub root_ns {
21
+  my ($self, $root) = @_;
22
+
23
+  $root =~ s/^\///;
24
+  
25
+  return $root;
26
+}
27
+
28
+sub parse_id_path {
29
+  my ($self, $project, $id_path) = @_;
30
+  
31
+  my $c = $self->cntl;
32
+  
33
+  # Git
34
+  my $git = $c->app->git;
35
+  
36
+  # Parse id and path
37
+  my $refs = $git->references($project);
38
+  my $id;
39
+  my $path;
40
+  for my $rs (values %$refs) {
41
+    for my $ref (@$rs) {
42
+      $ref =~ s#^heads/##;
43
+      $ref =~ s#^tags/##;
44
+      if ($id_path =~ s#^\Q$ref(/|$)##) {
45
+        $id = $ref;
46
+        $path = $id_path;
47
+        last;
48
+      }      
49
+    }
50
+  }
51
+  unless (defined $id) {
52
+    if ($id_path =~ s#(^[^/]+)(/|$)##) {
53
+      $id = $1;
54
+      $path = $id_path;
55
+    }
56
+  }
57
+  
58
+  return ($id, $path);
59
+}
60
+
61
+1;
62
+
+1 -90
lib/Gitprep/Main.pm
... ...
@@ -2,6 +2,7 @@ package Gitprep::Main;
2 2
 use Mojo::Base 'Mojolicious::Controller';
3 3
 use File::Basename 'dirname';
4 4
 use Carp 'croak';
5
+use Gitprep::API;
5 6
 
6 7
 sub blob {
7 8
   my $self = shift;
... ...
@@ -320,15 +321,6 @@ sub commitdiff {
320 321
   }
321 322
 }
322 323
 
323
-sub home {
324
-  my $self = shift;
325
-
326
-  my $rep_home = '/gitpub';
327
-  my @users = qw/kimoto ken/;
328
-
329
-  $self->render(users => \@users);
330
-}
331
-
332 324
 sub heads {
333 325
   my $self = shift;
334 326
   
... ...
@@ -412,87 +404,6 @@ sub _root_ns {
412 404
   return $root;
413 405
 }
414 406
 
415
-sub repository {
416
-  my $self = shift;
417
-  
418
-  # Parameters
419
-  my $user = $self->param('user');
420
-  my $repository = $self->param('repository');
421
-  my $root_ns = $self->_root_ns;
422
-  
423
-  my $project_ns = "$root_ns/$user/$repository.git";
424
-  my $project = "/$project_ns";
425
-  my $home_ns = dirname $project_ns;
426
-  my $home = "/$home_ns";
427
-  my $id_dir = $self->param('id_dir') || 'master/';
428
-
429
-  # Id and directory
430
-  my ($id, $dir) = $self->_parse_id_path($project, $id_dir);
431
-
432
-  # Git
433
-  my $git = $self->app->git;
434
-  
435
-  # Tree id
436
-  my $tid;
437
-  my $commit = $git->parse_commit($project, $id);
438
-  unless (defined $tid) {
439
-    if (defined $dir && $dir ne '') {
440
-      $tid = $git->id_by_path($project, $id, $dir, 'tree');
441
-    }
442
-    else { $tid = $commit->{tree} }
443
-  }
444
-  $self->render_not_found unless defined $tid;
445
-
446
-  # Get tree (command "git ls-tree")
447
-  my @entries = ();
448
-  my $show_sizes = 0;
449
-  open my $fh, '-|', $git->cmd($project), 'ls-tree', '-z',
450
-      ($show_sizes ? '-l' : ()), $tid
451
-    or croak 'Open git-ls-tree failed';
452
-  local $/ = "\0";
453
-  @entries = map { chomp; $git->dec($_) } <$fh>;
454
-  close $fh
455
-    or croak 404, "Reading tree failed";
456
-  
457
-  # Parse tree
458
-  my @trees;
459
-  for my $line (@entries) {
460
-    my $tree = $git->parse_ls_tree_line($line, -z => 1, -l => $show_sizes);
461
-    $tree->{mode_str} = $git->_mode_str($tree->{mode});
462
-    push @trees, $tree;
463
-  }
464
-  
465
-  # References
466
-  my $refs = $git->references($project);
467
-  
468
-  # Render
469
-  $self->render(
470
-    home => $home,
471
-    home_ns => $home_ns,
472
-    project => $project,
473
-    project_ns => $project_ns,
474
-    dir => $dir,
475
-    id => $id,
476
-    tid => $tid,
477
-    commit => $commit,
478
-    trees => \@trees,
479
-    refs => $refs
480
-  );
481
-}
482
-
483
-sub repositories {
484
-  my $self = shift;
485
-
486
-  my $root = $self->root;
487
-  my $user = $self->param('user');
488
-  
489
-  # Repositories
490
-  my $reps = $self->app->git->repositories("/$root/$user");
491
-  
492
-  # Render
493
-  $self->render(reps => $reps);
494
-}
495
-
496 407
 sub snapshot {
497 408
   my $self = shift;
498 409
 
+5
templates/main/home.html.ep
... ...
@@ -1,3 +1,8 @@
1
+<%
2
+  my $rep_home = app->root;;
3
+  my $users = [qw/kimoto ken/];
4
+%>
5
+
1 6
 % layout 'common';
2 7
   %= include '/include/header', title => 'Gitprep';
3 8
   <table class="project_list">
+8
templates/main/repositories.html.ep
... ...
@@ -1,3 +1,11 @@
1
+<%
2
+  my $root = app->root;
3
+  my $user = param('user');
4
+  
5
+  # Repositories
6
+  my $reps = app->git->repositories("/$root/$user");
7
+%>
8
+
1 9
 % layout 'common';
2 10
   %= include '/include/header', title => 'Repositories';
3 11
   <table class="project_list">
+59
templates/main/repository.html.ep
... ...
@@ -1,3 +1,62 @@
1
+<%
2
+  use Gitprep::API;
3
+  
4
+  # Parameters
5
+  my $user = param('user');
6
+  my $repository = param('repository');
7
+  my $id_dir ||= param('id_dir') || 'master/';
8
+
9
+  # API
10
+  my $api = Gitprep::API->new($self);
11
+  
12
+  # Parameters
13
+  my $root_ns = $api->root_ns(app->root);
14
+  
15
+  my $project_ns = "$root_ns/$user/$repository.git";
16
+  my $project = "/$project_ns";
17
+  my $home_ns = $api->dirname($project_ns);
18
+  my $home = "/$home_ns";
19
+
20
+  # Id and directory
21
+  my ($id, $dir) = $api->parse_id_path($project, $id_dir);
22
+
23
+  # Git
24
+  my $git = app->git;
25
+  
26
+  # Tree id
27
+  my $tid;
28
+  my $commit = $git->parse_commit($project, $id);
29
+  unless (defined $tid) {
30
+    if (defined $dir && $dir ne '') {
31
+      $tid = $git->id_by_path($project, $id, $dir, 'tree');
32
+    }
33
+    else { $tid = $commit->{tree} }
34
+  }
35
+  $self->render_not_found unless defined $tid;
36
+
37
+  # Get tree (command "git ls-tree")
38
+  my @entries = ();
39
+  my $show_sizes = 0;
40
+  open my $fh, '-|', $git->cmd($project), 'ls-tree', '-z',
41
+      ($show_sizes ? '-l' : ()), $tid
42
+    or $api->croak('Open git-ls-tree failed');
43
+  local $/ = "\0";
44
+  @entries = map { chomp; $git->dec($_) } <$fh>;
45
+  close $fh
46
+    or $api->croak(404, "Reading tree failed");
47
+  
48
+  # Parse tree
49
+  my $trees;
50
+  for my $line (@entries) {
51
+    my $tree = $git->parse_ls_tree_line($line, -z => 1, -l => $show_sizes);
52
+    $tree->{mode_str} = $git->_mode_str($tree->{mode});
53
+    push @$trees, $tree;
54
+  }
55
+  
56
+  # References
57
+  my $refs = $git->references($project);
58
+%>
59
+
1 60
 % layout 'new_common';
2 61
   %= include '/css/common';
3 62