Skip to content

Commit

Permalink
revert encoding support
Browse files Browse the repository at this point in the history
  • Loading branch information
yuki-kimoto committed Nov 22, 2013
1 parent 4330aae commit 7233306
Show file tree
Hide file tree
Showing 43 changed files with 112 additions and 296 deletions.
3 changes: 1 addition & 2 deletions CHANGES
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
1.4
- fix markdown bugs (*em*, _em_)
- default readme file is changed. README is changed to README.md
- add private repository and collaborator feature
- add project encoding support
- add private repository feature
1.3
- add README.md support
- add submodule support
Expand Down
27 changes: 16 additions & 11 deletions lib/Gitprep.pm
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@ use Validator::Custom;
eval {require Digest::SHA; import Digest::SHA qw(sha1 sha1_hex)};
}

our $VERSION = '1.04';
our $VERSION = '1.0301';

has 'dbi';
has 'git';
has 'manager';
has 'validator';
has 'git';

use constant BUFFER_SIZE => 8192;

sub startup {
my $self = shift;
Expand All @@ -43,7 +45,7 @@ sub startup {
$listen = [split /,/, $listen] unless ref $listen eq 'ARRAY';
$conf->{hypnotoad}{listen} = $listen;

# Git settings
# Git
my $git = Gitprep::Git->new;
my $git_bin
= $conf->{basic}{git_bin} ? $conf->{basic}{git_bin} : $git->search_bin;
Expand All @@ -55,20 +57,23 @@ sub startup {
croak $error;
}
$git->bin($git_bin);


# Repository Manager
my $manager = Gitprep::Manager->new(app => $self);
weaken $manager->{app};
$self->manager($manager);

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

# Repository Manager
my $manager = Gitprep::Manager->new(app => $self, git => $git);
weaken $manager->{app};
$self->manager($manager);
# Added public path
# push @{$self->static->paths}, $rep_home;

# DBI
my $db_file = $ENV{GITPREP_DB_FILE}
Expand Down Expand Up @@ -237,10 +242,10 @@ sub startup {

# API
my $api = $self->gitprep_api;
my $user = $self->param('user');
my $project = $self->param('project');

# Private
my $user = $self->param('user');
my $project = $self->param('project');
my $private = $self->app->manager->is_private_project($user, $project);
if ($private) {
if ($api->can_access_private_project($user, $project)) {
Expand Down
30 changes: 0 additions & 30 deletions lib/Gitprep/API.pm
Original file line number Diff line number Diff line change
Expand Up @@ -42,26 +42,6 @@ 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 All @@ -78,7 +58,6 @@ sub can_access_private_project {
my ($self, $user, $project) = @_;

my $session_user = $self->cntl->session('user');
return unless $session_user;

my $is_valid =
($user eq $session_user || $self->is_collaborator($user, $project))
Expand All @@ -87,15 +66,6 @@ sub can_access_private_project {
return $is_valid;
}

sub manager {
my $self = shift;

my $manager = $self->app->manager->clone;
$manager->git($self->git);

return $manager;
}

sub new {
my ($class, $cntl) = @_;

Expand Down
26 changes: 11 additions & 15 deletions lib/Gitprep/Git.pm
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,6 @@ 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 Expand Up @@ -559,7 +547,7 @@ sub description {
else {
# Read description
return unless -f $file;
my $description = $self->_dec($self->_slurp($file) || '', 'UTF-8');
my $description = $self->_dec($self->_slurp($file) || '');
return $description;
}
}
Expand Down Expand Up @@ -1669,16 +1657,24 @@ sub _chop_str {
}

sub _dec {
my ($self, $str, $encoding) = @_;
my ($self, $str) = @_;

my $enc = $encoding || $self->encoding;
my $enc = $self->encoding;

my $new_str;
eval { $new_str = decode($enc, $str) };

return $@ ? $str : $new_str;
}

sub _enc {
my ($self, $str) = @_;

my $enc = $self->encoding;

return encode($enc, $str);
}

sub _mode_str {
my $self = shift;
my $mode = oct shift;
Expand Down
52 changes: 16 additions & 36 deletions lib/Gitprep/Manager.pm
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use File::Path qw/mkpath rmtree/;
use File::Temp ();

has 'app';
has 'git';

sub admin_user {
my $self = shift;
Expand All @@ -20,16 +19,6 @@ sub admin_user {
return $admin_user;
}

sub clone {
my $self = shift;

my $clone = __PACKAGE__->new;
$clone->app($self->app);
$clone->git($self->git);

return $clone;
}

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

Expand Down Expand Up @@ -61,12 +50,8 @@ sub fork_project {
$dbi->connector->txn(sub {

# Original project id
my $project_info = $dbi->model('project')->select(
['original_pid', 'private', 'encoding'],
id => [$original_user, $project]
)->one;

my $original_pid = $project_info->{original_pid};
my $original_pid = $dbi->model('project')
->select('original_pid', id => [$original_user, $project])->value;

croak "Can't get original project id"
unless defined $original_pid && $original_pid > 0;
Expand All @@ -78,9 +63,7 @@ sub fork_project {
$project,
{
original_user => $original_user,
original_pid => $original_pid,
private => $project_info->{private},
encoding => $project_info->{encoding}
original_pid => $original_pid
}
);
};
Expand Down Expand Up @@ -283,7 +266,7 @@ sub rename_project {
my ($self, $user, $project, $to_project) = @_;

# Rename project
my $git = $self->git;
my $git = $self->app->git;
my $dbi = $self->app->dbi;
my $error;
eval {
Expand Down Expand Up @@ -349,8 +332,7 @@ EOS
"default_branch not null default 'master'",
"original_user not null default ''",
"original_pid integer not null default 0",
"private not null default 0",
"encoding not null default ''"
"private not null default 0"
];
for my $column (@$project_columns) {
eval { $dbi->execute("alter table project add column $column") };
Expand Down Expand Up @@ -440,7 +422,6 @@ sub _create_project {
$dbi->model('number')->update({value => $number}, where => {key => 'original_pid'});
$params->{original_pid} = $number;
}
use Data::Dumper;
$dbi->model('project')->insert($params, id => [$user, $project]);
});
}
Expand All @@ -449,7 +430,7 @@ sub _create_rep {
my ($self, $user, $project, $opts) = @_;

# Create repository directory
my $git = $self->git;
my $git = $self->app->git;
my $rep = $git->rep($user, $project);
mkdir $rep
or croak "Can't create directory $rep: $!";
Expand Down Expand Up @@ -483,9 +464,9 @@ sub _create_rep {
or croak "Can't move post-update";

# Description
my $description = $opts->{description};
$description = '' unless defined $description;
{
my $description = $opts->{description};
$description = '' unless defined $description;
my $file = "$rep/description";
open my $fh, '>', $file
or croak "Can't open $file: $!";
Expand All @@ -512,8 +493,7 @@ sub _create_rep {
my $file = "$temp_work/README.md";
open my $readme_fh, '>', $file
or croak "Can't create $file: $!";
print $readme_fh "# $project\n";
print $readme_fh "\n" . encode('UTF-8', $description) . "\n";
print $readme_fh "$project\n=====\n";
close $readme_fh;

my @git_add_cmd = $git->cmd_rep(
Expand Down Expand Up @@ -576,7 +556,7 @@ sub _create_user_dir {
my ($self, $user) = @_;

# Create user directory
my $rep_home = $self->git->rep_home;
my $rep_home = $self->app->git->rep_home;
my $user_dir = "$rep_home/$user";
mkpath $user_dir;
}
Expand All @@ -594,7 +574,7 @@ sub _delete_user_dir {
my ($self, $user) = @_;

# Delete user directory
my $rep_home = $self->git->rep_home;
my $rep_home = $self->app->git->rep_home;
my $user_dir = "$rep_home/$user";
rmtree $user_dir;
}
Expand All @@ -611,7 +591,7 @@ sub _delete_rep {
my ($self, $user, $project) = @_;

# Delete repository
my $rep_home = $self->git->rep_home;
my $rep_home = $self->app->git->rep_home;
croak "Can't remove repository. repository home is empty"
if !defined $rep_home || $rep_home eq '';
my $rep = "$rep_home/$user/$project.git";
Expand Down Expand Up @@ -643,7 +623,7 @@ sub _exists_rep {
my ($self, $user, $project) = @_;

# Exists repository
my $rep = $self->git->rep($user, $project);
my $rep = $self->app->git->rep($user, $project);

return -e $rep;
}
Expand All @@ -652,7 +632,7 @@ sub _fork_rep {
my ($self, $user, $project, $to_user, $to_project) = @_;

# Fork repository
my $git = $self->git;
my $git = $self->app->git;
my $rep = $git->rep($user, $project);
my $to_rep = $git->rep($to_user, $to_project);
my @cmd = (
Expand Down Expand Up @@ -694,8 +674,8 @@ sub _rename_rep {
unless defined $user && defined $project && defined $renamed_project;

# Rename repository
my $rep = $self->git->rep($user, $project);
my $renamed_rep = $self->git->rep($user, $renamed_project);
my $rep = $self->app->git->rep($user, $project);
my $renamed_rep = $self->app->git->rep($user, $renamed_project);
move($rep, $renamed_rep)
or croak "Can't move $rep to $renamed_rep: $!";
}
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 = $api->git;
my $git = app->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 = gitprep_api;
my $api = $self->gitprep_api;

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

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

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

# Create user
eval { $api->manager->create_user($id, $params) };
eval { app->manager->create_user($id, $params) };
if ($@) {
app->log->error($@);
$errors = ['Internal Error'];
Expand Down
Loading

0 comments on commit 7233306

Please sign in to comment.