... | ... |
@@ -77,7 +77,16 @@ sub params { |
77 | 77 |
|
78 | 78 |
my $c = $self->cntl; |
79 | 79 |
|
80 |
- my %params = map { $_ => $c->param($_) } $c->param; |
|
80 |
+ my %params; |
|
81 |
+ for my $name ($c->param) { |
|
82 |
+ my @values = $c->param($name); |
|
83 |
+ if (@values > 1) { |
|
84 |
+ $params{$name} = \@values; |
|
85 |
+ } |
|
86 |
+ elsif (@values) { |
|
87 |
+ $params{$name} = $values[0]; |
|
88 |
+ } |
|
89 |
+ } |
|
81 | 90 |
|
82 | 91 |
return \%params; |
83 | 92 |
} |
... | ... |
@@ -11,89 +11,63 @@ |
11 | 11 |
# Git |
12 | 12 |
my $git = $self->app->git; |
13 | 13 |
|
14 |
- # Default branch |
|
15 |
- my $base_branch; |
|
16 |
- |
|
17 |
- # Branch |
|
18 |
- my $branches; |
|
19 |
- my $branches_count; |
|
20 |
- my $no_merged_branches_count; |
|
21 |
- my $base_branch_name; |
|
22 |
- my $merged_branches_count; |
|
23 |
- my $max; |
|
24 |
- |
|
25 | 14 |
# Delete |
26 | 15 |
my $errors; |
27 |
- if ($op eq 'delete') { |
|
16 |
+ if ($op eq 'delete' && lc $self->req->method eq 'post') { |
|
17 |
+ |
|
18 |
+ # Forbbiden |
|
19 |
+ unless ($api->logined($user)) { |
|
20 |
+ $self->redirect_to('/'); |
|
21 |
+ $self->finish_rendering; |
|
22 |
+ return; |
|
23 |
+ } |
|
28 | 24 |
|
29 | 25 |
# Validation |
30 | 26 |
my $params = $api->params; |
31 | 27 |
my $validator = $self->app->validator; |
32 | 28 |
my $rule = [ |
33 |
- user => [ |
|
34 |
- ['not_blank' => 'User name is empty.'] |
|
35 |
- ], |
|
36 |
- project => [ |
|
37 |
- ['not_blank' => 'Repository name is empty'] |
|
38 |
- ], |
|
39 | 29 |
branch => [ |
40 |
- ['not_blank' => 'Branch name is empty'] |
|
30 |
+ [not_blank => 'Branch name is empty'] |
|
41 | 31 |
] |
42 | 32 |
]; |
43 | 33 |
my $vresult = $validator->validate($params, $rule); |
44 | 34 |
|
45 | 35 |
if ($vresult->is_ok) { |
46 |
- |
|
47 | 36 |
# Valid parameters |
48 | 37 |
my $params = $vresult->data; |
49 |
- my $user = $params->{user}; |
|
50 |
- my $project = $params->{project}; |
|
51 | 38 |
my $branch = $params->{branch}; |
52 | 39 |
|
53 | 40 |
# Delete branch |
54 |
- if ($api->logined($user)) { |
|
55 |
- |
|
56 |
- # Delete user |
|
57 |
- eval { $git->delete_branch($user, $project, $branch) }; |
|
58 |
- if ($@) { |
|
59 |
- app->log->error($@); |
|
60 |
- $errors = ['Internal Error']; |
|
61 |
- } |
|
62 |
- else { |
|
63 |
- $self->flash(branch_deleted => 1); |
|
64 |
- $self->flash(branch => $branch); |
|
65 |
- $self->redirect_to; |
|
66 |
- $self->finish_rendering; |
|
67 |
- return; |
|
68 |
- } |
|
41 |
+ eval { $git->delete_branch($user, $project, $branch) }; |
|
42 |
+ if ($@) { |
|
43 |
+ app->log->error(url_with . ":$@"); |
|
44 |
+ $errors = ['Internal Error']; |
|
69 | 45 |
} |
70 |
- # Forbidden |
|
71 | 46 |
else { |
72 |
- $self->res->code('403'); |
|
47 |
+ $self->flash(message => "Branch $branch is deleted."); |
|
48 |
+ $self->redirect_to; |
|
73 | 49 |
$self->finish_rendering; |
74 | 50 |
return; |
75 | 51 |
} |
76 | 52 |
} |
77 | 53 |
else { $errors = $vresult->messages } |
78 | 54 |
} |
79 |
- # List |
|
80 |
- else { |
|
81 |
- # Default branch |
|
82 |
- $base_branch_name = stash('base_branch') || app->manager->default_branch($user, $project); |
|
83 |
- $base_branch = $git->branch($user, $project, $base_branch_name); |
|
84 |
- |
|
85 |
- # No merged branches |
|
86 |
- $branches = $git->branches($user, $project); |
|
87 |
- $max = 0; |
|
88 |
- for my $branch (@$branches) { |
|
89 |
- $branch->{status} = $git->branch_status($user, $project, $base_branch->{name}, $branch->{name}); |
|
90 |
- $max = $branch->{status}{ahead} if $max < $branch->{status}{ahead}; |
|
91 |
- $max = $branch->{status}{behind} if $max < $branch->{status}{behind}; |
|
92 |
- } |
|
93 |
- $branches_count = $git->branches_count($user, $project); |
|
94 |
- $no_merged_branches_count = $git->no_merged_branches_count($user, $project); |
|
95 |
- $merged_branches_count = $branches_count - $no_merged_branches_count - 1; |
|
55 |
+ |
|
56 |
+ # Default branch |
|
57 |
+ my $base_branch_name = param('base_branch') || app->manager->default_branch($user, $project); |
|
58 |
+ my $base_branch = $git->branch($user, $project, $base_branch_name); |
|
59 |
+ |
|
60 |
+ # No merged branches |
|
61 |
+ my $branches = $git->branches($user, $project); |
|
62 |
+ my $max = 0; |
|
63 |
+ for my $branch (@$branches) { |
|
64 |
+ $branch->{status} = $git->branch_status($user, $project, $base_branch->{name}, $branch->{name}); |
|
65 |
+ $max = $branch->{status}{ahead} if $max < $branch->{status}{ahead}; |
|
66 |
+ $max = $branch->{status}{behind} if $max < $branch->{status}{behind}; |
|
96 | 67 |
} |
68 |
+ my $branches_count = $git->branches_count($user, $project); |
|
69 |
+ my $no_merged_branches_count = $git->no_merged_branches_count($user, $project); |
|
70 |
+ my $merged_branches_count = $branches_count - $no_merged_branches_count - 1; |
|
97 | 71 |
|
98 | 72 |
# Global variable |
99 | 73 |
stash(rev => $base_branch_name); |
... | ... |
@@ -139,15 +113,8 @@ |
139 | 113 |
%= include '/include/header'; |
140 | 114 |
|
141 | 115 |
<div class="container" style="padding-bottom:30px"> |
142 |
- |
|
143 |
- % if ($errors) { |
|
144 |
- <div class="alert alert-error"> |
|
145 |
- <button type="button" class="close" data-dismiss="alert">×</button> |
|
146 |
- % for my $error (@$errors) { |
|
147 |
- <p><%= $error %></p> |
|
148 |
- % } |
|
149 |
- </div> |
|
150 |
- % } |
|
116 |
+ %= include '/include/errors', errors => $errors; |
|
117 |
+ %= include '/include/message', message => flash('message'); |
|
151 | 118 |
|
152 | 119 |
%= include '/include/project_header'; |
153 | 120 |
%= include '/include/code_menu', display => 'branches', branches => $branches; |
... | ... |
@@ -409,3 +409,29 @@ note 'fork'; |
409 | 409 |
$t->content_like(qr#kimoto1/t2#); |
410 | 410 |
$t->content_unlike(qr/Repository is forked from/); |
411 | 411 |
} |
412 |
+ |
|
413 |
+note 'Delete branch'; |
|
414 |
+{ |
|
415 |
+ my $app = Gitprep->new; |
|
416 |
+ my $t = Test::Mojo->new($app); |
|
417 |
+ $t->ua->max_redirects(3); |
|
418 |
+ |
|
419 |
+ # No delete branch button |
|
420 |
+ $t->get_ok("/kimoto1/t2/branches"); |
|
421 |
+ $t->content_like(qr/Branches/); |
|
422 |
+ $t->content_unlike(qr/Delete branch/); |
|
423 |
+ |
|
424 |
+ # Login as kimoto1 |
|
425 |
+ $t->post_ok('/_login?op=login', form => {id => 'kimoto1', password => 'a'}); |
|
426 |
+ my $cmd = "git --git-dir=$rep_home/kimoto1/t2.git branch tmp_branch"; |
|
427 |
+ system($cmd) == 0 or die "Can't execute git branch"; |
|
428 |
+ $t->get_ok("/kimoto1/t2/branches"); |
|
429 |
+ $t->content_like(qr/Delete branch/); |
|
430 |
+ $t->content_like(qr/tmp_branch/); |
|
431 |
+ |
|
432 |
+ # Delete branch |
|
433 |
+ $t->post_ok('/kimoto1/t2/branches?op=delete', form => {branch => 'tmp_branch'}); |
|
434 |
+ $t->content_like(qr/Branch tmp_branch is deleted/); |
|
435 |
+ $t->get_ok('/kimoto1/t2/branches'); |
|
436 |
+ $t->content_unlike(qr/tmp_branch/); |
|
437 |
+} |