Skip to content
This repository has been archived by the owner on Nov 1, 2020. It is now read-only.

Commit

Permalink
improve code structures
Browse files Browse the repository at this point in the history
  • Loading branch information
yuki-kimoto committed Nov 19, 2013
1 parent 185696d commit f5f13e4
Show file tree
Hide file tree
Showing 31 changed files with 93 additions and 48 deletions.
20 changes: 6 additions & 14 deletions lib/Gitprep.pm
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,7 @@ our $VERSION = '1.0301';
has 'dbi';
has 'manager';
has 'validator';

sub git {
my $self = shift;

my $git = Gitprep::Git->new;
$git->bin($self->config->{internal}{git_bin});
$git->rep_home($self->config->{internal}{git_rep_home});

return $git;
}
has 'git';

sub startup {
my $self = shift;
Expand Down Expand Up @@ -63,15 +54,16 @@ sub startup {
$self->log->error($error);
croak $error;
}
$conf->{internal}{git_bin} = $git_bin;
$git->bin($git_bin);

# Repository home
my $rep_home = $ENV{GITPREP_REP_HOME} || $self->home->rel_file('data/rep');
unless (-d $rep_home) {
mkdir $rep_home
or croak "Can't create directory $rep_home: $!";
}
$conf->{internal}{git_rep_home} = $rep_home;
$git->rep_home($rep_home);
$self->git($git);

# Repository Manager
my $manager = Gitprep::Manager->new(app => $self);
Expand Down Expand Up @@ -245,10 +237,10 @@ sub startup {

# API
my $api = $self->gitprep_api;

# Private
my $user = $self->param('user');
my $project = $self->param('project');

# Private
my $private = $self->app->manager->is_private_project($user, $project);
if ($private) {
if ($api->can_access_private_project($user, $project)) {
Expand Down
20 changes: 20 additions & 0 deletions lib/Gitprep/API.pm
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,26 @@ sub check_user_and_password {
return $is_valid;
}

sub git {
my $self = shift;

my $git = $self->app->git->clone;

my $user = $self->cntl->param('user');
my $project = $self->cntl->param('project');

if (defined $user && defined $project){
# Project encoding
my $encoding = $self->app->dbi->model('project')->select(
'encoding',
id => [$user, $project]
)->value;
$git->encoding($encoding) if length $encoding;
}

return $git;
}

sub is_collaborator {
my ($self, $user, $project, $session_user) = @_;

Expand Down
12 changes: 12 additions & 0 deletions lib/Gitprep/Git.pm
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,18 @@ sub branch_status {
return $status;
}

sub clone {
my $self = shift;

my $clone = __PACKAGE__->new;
$clone->bin($self->bin);
$clone->encoding($self->encoding);
$clone->rep_home($self->rep_home);
$clone->text_exts([@{$self->text_exts}]);

return $clone;
}

sub no_merged_branch_h {
my ($self, $user, $project) = @_;

Expand Down
2 changes: 1 addition & 1 deletion templates/api/revs.html.ep
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
my $api = gitprep_api;

# Git
my $git = app->git;
my $git = $api->git;

# Branches
my $branches = $git->branches($user, $project) || [];
Expand Down
4 changes: 2 additions & 2 deletions templates/archive.html.ep
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<%
# API
my $api = $self->gitprep_api;
my $api = gitprep_api;

# Parameter
my $user = param('user');
Expand All @@ -23,7 +23,7 @@
}

# Git
my $git = app->git;
my $git = $api->git;

# Object type
my $type = $git->object_type($user, $project, "$rev^{}");
Expand Down
2 changes: 1 addition & 1 deletion templates/auto/_new.html.ep
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
my $vresult = $validator->validate($params, $rule);

# Git
my $git = app->git;
my $git = $api->git;
if ($vresult->is_ok) {
# Not logined
unless ($api->logined) {
Expand Down
2 changes: 1 addition & 1 deletion templates/blame.html.ep
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
my $api = gitprep_api;

# Git
my $git = $self->app->git;
my $git = $api->git;

# Parameters
my $user = param('user');
Expand Down
2 changes: 1 addition & 1 deletion templates/blob.html.ep
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
my $api = gitprep_api;

# Git
my $git = $self->app->git;
my $git = $api->git;

# Parameters
my $user = param('user');
Expand Down
2 changes: 1 addition & 1 deletion templates/branches.html.ep
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
my $op = param('op') || '';

# Git
my $git = $self->app->git;
my $git = $api->git;

# Delete
my $errors;
Expand Down
2 changes: 1 addition & 1 deletion templates/commit.html.ep
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
$rev = $diff unless defined $rev;

# Git
my $git = app->git;
my $git = $api->git;

# Commit
my $commit = $git->get_commit($user, $project, $rev);
Expand Down
2 changes: 1 addition & 1 deletion templates/commits.html.ep
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
my $api = gitprep_api;

# Git
my $git = $self->app->git;
my $git = $api->git;

# Parameters
my $user = param('user');
Expand Down
2 changes: 1 addition & 1 deletion templates/compare.html.ep
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
my $page = param('page') || 0;

# Git
my $git = $self->app->git;
my $git = $api->git;

# Commits
my $commits = $git->forward_commits($user, $project, $from_rev, $rev);
Expand Down
2 changes: 1 addition & 1 deletion templates/import-branch.html.ep
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
}

# Branches
my $git = app->git;
my $git = $api->git;
my $remote_branches = $git->branches($remote_user, $remote_project);
my $remote_branch_names = [map { $_->{name} } @$remote_branches];

Expand Down
4 changes: 3 additions & 1 deletion templates/include/blob_diff_body.html.ep
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
<%
my $api = gitprep_api;

my $rev = stash('rev');
my $from_rev = stash('from_rev');

my $git = app->git;
my $git = $api->git;

my $diff_tree = stash('diff_tree');
my $blob_diff = stash('blob_diff');
Expand Down
6 changes: 4 additions & 2 deletions templates/include/code_menu.html.ep
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
<%
my $api = gitprep_api;

my $display = stash('display') || '';
my $rev = stash('rev');
$rev = '' unless defined $rev;
my $branches = stash('branches');
my $branches_count = app->git->branches_count($user, $project);
my $branches_count = $api->git->branches_count($user, $project);
my $default_branch_name = app->manager->default_branch($user, $project);
my $tags_count = app->git->tags_count($user, $project);
my $tags_count = $api->git->tags_count($user, $project);
%>

%= javascript begin
Expand Down
4 changes: 3 additions & 1 deletion templates/include/commit_body.html.ep
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
<%
my $api = gitprep_api;

# Parameters
my $rev = stash('rev');
my $from_rev = stash('from_rev');

# Git
my $git = app->git;
my $git = $api->git;

# Diff tree
my $diff_trees = $git->diff_tree(
Expand Down
6 changes: 4 additions & 2 deletions templates/include/readme.html.ep
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
use Mojo::ByteStream ();
use Text::Markdown::Discount ();

my $api = gitprep_api;

# README
my $type;
my $lines;
eval { $lines = app->git->blob($user, $project, $rev, 'README') };
eval { $lines = $api->git->blob($user, $project, $rev, 'README') };
my $readme_e;
if ($lines) {
$type = 'plain';
Expand All @@ -14,7 +16,7 @@
$readme_e =~ s#(^|\s|[^\x00-\x7F])(http(?:s)?://.+?)($|\s|[^\x00-\x7F])#$1<a href="$2">$2</a>$3#msg;
}
else {
eval { $lines = app->git->blob($user, $project, $rev, 'README.md') };
eval { $lines = $api->git->blob($user, $project, $rev, 'README.md') };
if ($lines) {
$type = 'markdown';
my $readme = join "\n", @$lines;
Expand Down
4 changes: 2 additions & 2 deletions templates/network.html.ep
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
my $api = gitprep_api;

# Branches
my $branches = [map { $_->{name} } @{app->git->branches($user, $project)}];
my $branches = [map { $_->{name} } @{$api->git->branches($user, $project)}];

# Members
my $members = app->manager->members($user, $project);

# Members branches
for my $member (@$members) {
my $branches = [
map { $_->{name} } @{app->git->branches($member->{id}, $member->{project})}
map { $_->{name} } @{$api->git->branches($member->{id}, $member->{project})}
];
$member->{branches} = $branches;
}
Expand Down
6 changes: 4 additions & 2 deletions templates/network/graph.html.ep
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
<%
my $api = gitprep_api;

my $user = param('user');
my $project = param('project');
my $branch = param('rev1');
my $rev2_abs = param('rev2_abs');
my ($remote_user, $remote_project, $remote_branch) = split /\//, $rev2_abs, 3;

my $commits = app->git->get_commits($user, $project, $branch, 100);
my $remote_commits = app->git->get_commits(
my $commits = $api->git->get_commits($user, $project, $branch, 100);
my $remote_commits = $api->git->get_commits(
$remote_user,
$remote_project,
$remote_branch,
Expand Down
4 changes: 2 additions & 2 deletions templates/project.html.ep
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
my $api = gitprep_api;

# Git
my $git = app->git;
my $git = $api->git;

# Parameters
my $user = param('user');
Expand Down Expand Up @@ -49,7 +49,7 @@
my $url = url_for->to_abs;
$url->base(undef);
my $ssh_port = config->{basic}{ssh_port};
my $rep_home = app->git->rep_home;
my $rep_home = $api->git->rep_home;
my $execute_user = getpwuid($>);
my $ssh_rep_url = "ssh://$execute_user\@" . $url->host
. ($ssh_port ? ":$ssh_port" : '') . "$rep_home/$user/$project.git";
Expand Down
5 changes: 4 additions & 1 deletion templates/raw.html.ep
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<%
# API
my $api = gitprep_api;

# Git
my $git = app->git;
my $git = $api->git;

# Parameters
my $user = param('user');
Expand Down
2 changes: 1 addition & 1 deletion templates/settings.html.ep
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
}

# Rename project
my $git = app->git;
my $git = $api->git;
my $errors;
my $post = lc $self->req->method eq 'post';
if ($op eq 'rename-project' && $post) {
Expand Down
2 changes: 1 addition & 1 deletion templates/settings/collaboration.html.ep
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
}

# Rename project
my $git = app->git;
my $git = $api->git;
my $errors;
if (lc $self->req->method eq 'post') {
if ($op eq 'add') {
Expand Down
6 changes: 4 additions & 2 deletions templates/smart-http/info-refs.html.ep
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
use Symbol ();
use IO::Select ();

my $api = gitprep_api;

my $service = param('service') || '';
my $user = param('user');
my $project = param('project');
my $git = app->git;
my $git = $api->git;

# Smart HTTP
if ($service eq 'git-upload-pack' || $service eq 'git-receive-pack') {
Expand Down Expand Up @@ -65,7 +67,7 @@
or die "Can't close pipe for @cmd:$!";

my $content_type = 'text/plain; charset=UTF-8';
my $rep_home = app->git->rep_home;
my $rep_home = $api->git->rep_home;
my $file = "$rep_home/$user/$project.git/info/refs";
if (-f $file) {
my $asset = Mojo::Asset::File->new(path => $file);
Expand Down
4 changes: 3 additions & 1 deletion templates/smart-http/service.html.ep
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
use Symbol ();
use IO::Select ();

my $api = gitprep_api;

my $service = param('service');
my $user = param('user');
my $project = param('project');

my $git = app->git;
my $git = $api->git;

my $rep_dir = $git->rep($user, $project);
my @cmd = $git->cmd($user, $project, $service, '--stateless-rpc', $rep_dir);
Expand Down
4 changes: 3 additions & 1 deletion templates/smart-http/static.html.ep
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<%
my $api = gitprep_api;

my $path = param('Path');
my $user = param('user');
my $project = param('project');
Expand Down Expand Up @@ -31,7 +33,7 @@
$content_type = 'application/x-git-packed-objects-toc';
}

my $rep_home = app->git->rep_home;
my $rep_home = $api->git->rep_home;
my $file = "$rep_home/$user/$project.git/$path";
if (-f $file) {
my $asset = Mojo::Asset::File->new(path => $file);
Expand Down
Loading

0 comments on commit f5f13e4

Please sign in to comment.