Showing 7 changed files with 181 additions and 204 deletions
+7 -3
lib/Gitprep.pm
... ...
@@ -205,9 +205,13 @@ sub startup {
205 205
       table => 'pull_request',
206 206
       primary_key => 'row_id',
207 207
       join => [
208
-        'left join user on pull_request.open_user = user.row_id',
209
-        'left join project on pull_request.project = project.row_id',
210
-        'left join user as project_user on project.user = project_user.row_id'
208
+        'left join user as pull_request__open_user on pull_request.open_user = pull_request__open_user.row_id',
209
+        'left join project as pull_request__base_project on pull_request.base_project = pull_request__base_project.row_id',
210
+        'left join user as pull_request__base_project__user'
211
+          . ' on pull_request__base_project.user = pull_request__base_project__user.row_id',
212
+        'left join project as pull_request__target_project on pull_request.target_project = pull_request__target_project.row_id',
213
+        'left join user as pull_request__target_project__user'
214
+          . ' on pull_request__target_project.user = pull_request__target_project__user.row_id'
211 215
       ]
212 216
     },
213 217
     {
+15 -1
lib/Gitprep/Git.pm
... ...
@@ -19,6 +19,21 @@ has text_exts => sub { ['txt'] };
19 19
 has 'time_zone_second';
20 20
 has 'app';
21 21
 
22
+sub ref_to_object_id {
23
+  my ($self, $rep_info, $ref) = @_;
24
+  
25
+  my @cmd = $self->cmd($rep_info, 'show-ref', $ref);
26
+  open my $fh, '-|', @cmd
27
+    or croak "Can't execute git show-ref: @cmd";
28
+  my $result = <$fh>;
29
+  
30
+  return unless defined $result;
31
+  
32
+  my ($object_id) = split /\s+/, $result;
33
+  
34
+  return $object_id;
35
+}
36
+
22 37
 sub current_branch {
23 38
   my ($self, $rep_info) = @_;
24 39
   
... ...
@@ -745,7 +760,6 @@ sub path_to_hash {
745 760
   return $id;
746 761
 }
747 762
 
748
-
749 763
 sub last_activity {
750 764
   my ($self, $rep) = @_;
751 765
   
+6 -100
lib/Gitprep/Manager.pm
... ...
@@ -17,59 +17,17 @@ has 'authorized_keys_file';
17 17
 
18 18
 has '_tmp_branch' => '__gitprep_tmp_branch__';
19 19
 
20
-sub merge_and_push {
21
-  my ($self, $work_rep_info, $rep_info1, $base_branch, $rep_info2, $target_branch) = @_;
22
-  
23
-  # Merge
24
-  my $target_user_id = $rep_info2->{user};
25
-  my @git_merge_cmd = $self->app->git->cmd(
26
-    $work_rep_info,
27
-    'merge',
28
-    "--message=merge $target_user_id/$target_branch",
29
-    "$target_user_id/$target_branch"
30
-  );
31
-  Gitprep::Util::run_command(@git_merge_cmd)
32
-    or Carp::croak "Can't execute git merge: @git_merge_cmd";
33
-  
34
-  # Push
35
-  my @git_push_cmd = $self->app->git->cmd($work_rep_info, 'push', 'origin', $base_branch);
36
-  Gitprep::Util::run_command(@git_push_cmd)
37
-    or Carp::croak "Can't execute git push: @git_push_cmd";
38
-}
39
-
40 20
 sub prepare_merge {
41 21
   my ($self, $work_rep_info, $rep_info1, $base_branch, $rep_info2, $target_branch) = @_;
42 22
   
43 23
   # Fetch base repository
44 24
   my $base_user_id = $rep_info1->{user};
45
-  my @git_fetch_base_cmd = $self->app->git->cmd($work_rep_info, 'fetch', 'origin', $base_branch);
25
+  my @git_fetch_base_cmd = $self->app->git->cmd($work_rep_info, 'fetch', 'origin');
46 26
   Gitprep::Util::run_command(@git_fetch_base_cmd)
47 27
     or Carp::croak "Can't execute git fetch: @git_fetch_base_cmd";
48 28
   
49
-  # Remeve remote
50
-  my $target_user_id = $rep_info2->{user};
51
-  my @git_remote_remove_cmd =  $self->app->git->cmd(
52
-    $work_rep_info,
53
-    'remote',
54
-    'remove',
55
-    $target_user_id
56
-  );
57
-  Gitprep::Util::run_command(@git_remote_remove_cmd);
58
-  
59
-  # Set remote add target repository
60
-  my $target_git_dir = $rep_info2->{git_dir};
61
-  my @git_remote_add_cmd = $self->app->git->cmd(
62
-    $work_rep_info,
63
-    'remote',
64
-    'add',
65
-    $target_user_id,
66
-    $target_git_dir
67
-  );
68
-  Gitprep::Util::run_command(@git_remote_add_cmd)
69
-    or Carp::croak "Can't execute git remote add: @git_remote_add_cmd";
70
-  
71 29
   # Fetch target repository
72
-  my @git_fetch_target_cmd = $self->app->git->cmd($work_rep_info, 'fetch', $target_user_id, $target_branch);
30
+  my @git_fetch_target_cmd = $self->app->git->cmd($work_rep_info, 'fetch', $rep_info2->{git_dir});
73 31
   Gitprep::Util::run_command(@git_fetch_target_cmd)
74 32
     or Carp::croak "Can't execute git fetch: @git_fetch_target_cmd";
75 33
 
... ...
@@ -144,13 +102,15 @@ sub prepare_merge {
144 102
 sub merge {
145 103
   my ($self, $work_rep_info, $rep_info1, $base_branch, $rep_info2, $target_branch) = @_;
146 104
   
105
+  my $object_id = $self->app->git->ref_to_object_id($rep_info2, $target_branch);
106
+  
147 107
   # Merge
148 108
   my $target_user_id = $rep_info2->{user};
149 109
   my @git_merge_cmd = $self->app->git->cmd(
150 110
     $work_rep_info,
151 111
     'merge',
152
-    "--message=merge $target_user_id/$target_branch",
153
-    "$target_user_id/$target_branch"
112
+    "--message=[merge]$target_user_id/$target_branch",
113
+    $object_id
154 114
   );
155 115
   
156 116
   my $success = Gitprep::Util::run_command(@git_merge_cmd);
... ...
@@ -169,64 +129,10 @@ sub push {
169 129
     'origin',
170 130
     "$tmp_branch:$base_branch"
171 131
   );
172
-  warn "@git_push_cmd";
173 132
   Gitprep::Util::run_command(@git_push_cmd)
174 133
     or Carp::croak "Can't execute git push: @git_push_cmd";
175 134
 }
176 135
 
177
-=pod
178
-sub check_merge_automatical {
179
-  my ($self, $work_rep_info, $rep_info1, $base_branch, $rep_info2, $target_branch) = @_;
180
-  
181
-  # Create patch
182
-  my $tmp_branch = $self->_tmp_branch;
183
-  my $target_user_id = $rep_info2->{user};
184
-  my @git_format_patch_cmd = $self->app->git->cmd(
185
-    $work_rep_info,
186
-    'format-patch',
187
-    "origin/$base_branch..$target_user_id/$target_branch",
188
-    "--stdout"
189
-  );
190
-  open my $git_format_patch_fh, '-|', @git_format_patch_cmd
191
-    or Carp::croak "Can't execute git format-patch: @git_format_patch_cmd";
192
-  my $patch_str = do { local $/; <$git_format_patch_fh> };
193
-  
194
-  warn "@git_format_patch_cmd";
195
-  
196
-  # Write patch to file
197
-  my $tmp_dir = File::Temp->newdir(DIR => $self->app->home->rel_file('/tmp'));
198
-  my $patch_file = "$tmp_dir/test.patch";
199
-  open my $patch_fh, '>', $patch_file
200
-    or Carp::croak "Can't open patch file $patch_file: $!";
201
-  print $patch_fh $patch_str;
202
-  close $patch_fh;
203
-  
204
-  # Check if this patch can be applied
205
-  use Cwd;
206
-  my $original_cwd = getcwd;
207
-  chdir $work_rep_info->{work_tree}
208
-    or croak "Can't change working directory: $work_rep_info->{work_tree}";
209
-  warn "aaaaaaaaaaaaaaaaa $work_rep_info->{work_tree}";
210
-  my @git_apply_cmd = $self->app->git->cmd(
211
-    $work_rep_info,
212
-    'apply',
213
-    $patch_file,
214
-    '--check'
215
-  );
216
-  chdir $original_cwd
217
-    or croak "Can't change working directory: $original_cwd";
218
-  
219
-  warn "@git_apply_cmd";
220
-  
221
-  # sleep 300;
222
-  my $automatical = Gitprep::Util::run_command(@git_apply_cmd);
223
-  
224
-  warn "bbbbbbbbbbbbbbbbbbbb $automatical";
225
-  
226
-  return $automatical;
227
-}
228
-=cut
229
-
230 136
 sub lock_rep {
231 137
   my ($self, $rep_info) = @_;
232 138
   
+6 -5
setup_database
... ...
@@ -157,10 +157,11 @@ EOS
157 157
     my $sql = <<"EOS";
158 158
 create table pull_request (
159 159
   row_id integer primary key autoincrement,
160
-  project integer not null default 0,
161
-  branch1 not null default '',
162
-  branch2 not null default '',
163
-  unique(project, branch1, branch2)
160
+  base_project integer not null default 0,
161
+  base_branch not null default '',
162
+  target_project integer not null default 0,
163
+  target_branch not null default '',
164
+  unique(base_project, base_branch, target_project, target_branch)
164 165
 );
165 166
 EOS
166 167
     $dbi->execute($sql);
... ...
@@ -178,7 +179,7 @@ EOS
178 179
   }
179 180
 
180 181
   # Check pull_request table
181
-  eval { $dbi->select([qw/row_id project branch1 branch2 title open open_time open_user/], table => 'pull_request') };
182
+  eval { $dbi->select([qw/row_id base_project base_branch target_project target_branch title open open_time open_user/], table => 'pull_request') };
182 183
   if ($@) {
183 184
     my $error = "Can't create pull_request table properly: $@";
184 185
     die $error;
+4 -4
templates/branches.html.ep
... ...
@@ -232,10 +232,10 @@
232 232
                 my $pull_request = app->dbi->model('pull_request')->select(
233 233
                   {__MY__ => ['row_id', 'open']},
234 234
                   where => {
235
-                    'project.id' => $project_id,
236
-                    'project_user.id' => $user_id,
237
-                    branch1 => $default_branch_name,
238
-                    branch2 => $branch_name
235
+                    'pull_request__base_project.id' => $project_id,
236
+                    'pull_request__base_project__user.id' => $user_id,
237
+                    base_branch => $default_branch_name,
238
+                    target_branch => $branch_name
239 239
                   }
240 240
                 )->one;
241 241
               %>
+61 -42
templates/compare.html.ep
... ...
@@ -3,25 +3,35 @@
3 3
   my $api = gitprep_api;
4 4
 
5 5
   # Parameters
6
-  my $user_id = param('user');
7
-  my $project_id = param('project');
8
-  my $from_rev = param('rev1');
9
-  my $rev = param('rev2');
6
+  my $base_user_id = param('user');
7
+  my $base_project_id = param('project');
8
+  my $base_branch = param('rev1');
9
+  my $user_id_and_target_branch = param('rev2');
10 10
   my $page = param('page') || 0;
11 11
   my $expand = param('expand');
12 12
   
13
+  # Default base branch
14
+  $base_branch //= app->manager->default_branch($base_user_id, $base_project_id);
15
+  
16
+  # Base project
17
+  my $base_project = app->dbi->model('project')->select(
18
+    {__MY__ => '*'},
19
+    where => {'project.id' => $base_project_id, 'user.id' => $base_user_id}
20
+  )->one;
21
+  
13 22
   # Get target user, project, branch
14 23
   my $target_user_id;
15 24
   my $target_branch;
16 25
   my $target_project;
17
-  if ($rev =~ /^([^:]+):(.+)/) {
26
+  if ($user_id_and_target_branch =~ /^([^:]+):(.+)/) {
18 27
     $target_user_id = $1;
19 28
     $target_branch = $2;
20
-    $target_project = $self->app->manager->child_project($user_id, $project_id, $target_user_id);
29
+    $target_project = $self->app->manager->child_project($base_user_id, $base_project_id, $target_user_id);
21 30
   }
22
-  
23
-  unless ($from_rev) {
24
-    $from_rev = app->manager->default_branch($user_id, $project_id);
31
+  else {
32
+    $target_user_id = $base_user_id;
33
+    $target_branch = $user_id_and_target_branch;
34
+    $target_project = $base_project
25 35
   }
26 36
   
27 37
   # Git
... ...
@@ -36,15 +46,20 @@
36 46
       
37 47
       my $project_row_id = app->dbi->model('project')->select(
38 48
         'project.row_id',
39
-        where => {'user.id' => $user_id, 'project.id' => $project_id}
49
+        where => {'user.id' => $base_user_id, 'project.id' => $base_project_id}
40 50
       )->value;
41 51
       
42 52
       my $pull_request = app->dbi->model('pull_request')->select(
43
-        where => {project => $project_row_id, branch1 => $from_rev, branch2 => $rev}
53
+        where => {
54
+          base_project => $project_row_id,
55
+          base_branch => $base_branch,
56
+          target_project => $target_project->{id},
57
+          target_branch => $target_branch
58
+        }
44 59
       )->one;
45 60
       
46 61
       if ($pull_request) {
47
-        $self->redirect_to("/$user_id/$project_id/pull/$pull_request->{row_id}");
62
+        $self->redirect_to("/$base_user_id/$base_project_id/pull/$pull_request->{row_id}");
48 63
         return;
49 64
       }
50 65
       else {
... ...
@@ -52,16 +67,17 @@
52 67
         my $now_epoch = $now_tm->epoch;
53 68
         my $user_row_id = app->dbi->model('user')->select(
54 69
           'row_id',
55
-          where => {id => $user_id}
70
+          where => {id => $base_user_id}
56 71
         )->value;
57 72
         
58 73
         my $new_pull_request_row_id;
59 74
         app->dbi->connector->txn(sub {
60 75
           # New pull request
61 76
           my $new_pull_request_params = {
62
-            project => $project_row_id,
63
-            branch1 => $from_rev,
64
-            branch2 => $rev,
77
+            base_project => $project_row_id,
78
+            base_branch => $base_branch,
79
+            target_project => $target_project->{row_id},
80
+            target_branch => $target_branch,
65 81
             title => $title,
66 82
             open => 1,
67 83
             open_time => $now_epoch,
... ...
@@ -73,9 +89,10 @@
73 89
           $new_pull_request_row_id = app->dbi->model('pull_request')->select(
74 90
             'row_id',
75 91
             where => {
76
-              project => $project_row_id,
77
-              branch1 => $from_rev,
78
-              branch2 => $rev
92
+              base_project => $project_row_id,
93
+              base_branch => $base_branch,
94
+              target_project => $target_project->{row_id},
95
+              target_branch => $target_branch
79 96
             }
80 97
           )->value;
81 98
           
... ...
@@ -100,17 +117,19 @@
100 117
             user => $user_row_id
101 118
           };
102 119
           
120
+          use D;d $new_pull_request_message_params;
121
+          
103 122
           app->dbi->model('pull_request_message')->insert($new_pull_request_message_params);
104 123
         });
105 124
         
106
-        $self->redirect_to("/$user_id/$project_id/pull/$new_pull_request_row_id");
125
+        $self->redirect_to("/$base_user_id/$base_project_id/pull/$new_pull_request_row_id");
107 126
         return;
108 127
       }
109 128
     }
110 129
   }
111 130
 
112 131
   # Can merge
113
-  my $rep_info = app->rep_info($user_id, $project_id);
132
+  my $rep_info = app->rep_info($base_user_id, $base_project_id);
114 133
   my $merge_success;
115 134
   my $rep_info2;
116 135
   if ($target_project) {
... ...
@@ -121,10 +140,10 @@
121 140
   }
122 141
     
123 142
   # Create working repository if it don't exist
124
-  $self->app->manager->create_work_rep($user_id, $project_id);
143
+  $self->app->manager->create_work_rep($base_user_id, $base_project_id);
125 144
   
126 145
   # Lock working repository
127
-  my $work_rep_info = app->work_rep_info($user_id, $project_id);
146
+  my $work_rep_info = app->work_rep_info($base_user_id, $base_project_id);
128 147
   {
129 148
     my $lock_fh = $self->app->manager->lock_rep($work_rep_info);
130 149
     
... ...
@@ -132,23 +151,23 @@
132 151
     $self->app->manager->prepare_merge(
133 152
       $work_rep_info,
134 153
       $rep_info,
135
-      $from_rev,
154
+      $base_branch,
136 155
       $rep_info2,
137
-      $rev
156
+      $target_branch
138 157
     );
139 158
     
140 159
     # Check merge automatically
141 160
     $merge_success = $self->app->manager->merge(
142 161
       $work_rep_info,
143 162
       $rep_info,
144
-      $from_rev,
163
+      $base_branch,
145 164
       $rep_info2,
146
-      $rev
165
+      $target_branch
147 166
     );
148 167
   }
149 168
   
150 169
   # Commits
151
-  my $commits = $git->forward_commits($rep_info, $from_rev, $rev);
170
+  my $commits = $git->forward_commits($rep_info, $base_branch, $target_branch);
152 171
   my $commits_count = @$commits;
153 172
   my $commits_date = {};
154 173
   my $authors = {};
... ...
@@ -161,10 +180,10 @@
161 180
   my $authors_count = keys %$authors;
162 181
 
163 182
   # Start commit
164
-  my $start_commit = $git->separated_commit($rep_info, $from_rev, $rev);
183
+  my $start_commit = $git->separated_commit($rep_info, $base_branch, $target_branch);
165 184
 
166 185
   # End commit
167
-  my $end_commit = $git->get_commit($rep_info2, $rev);
186
+  my $end_commit = $git->get_commit($rep_info2, $target_branch);
168 187
   
169 188
   if (!$start_commit || !$end_commit) {
170 189
     $self->reply->not_found;
... ...
@@ -189,7 +208,7 @@
189 208
     from_rev => $start_commit->{id}
190 209
   );
191 210
 
192
-  layout 'common', title => "Comparing $from_rev...$rev \x{30fb} $user_id/$project_id";
211
+  layout 'common', title => "Comparing $base_branch...$target_branch \x{30fb} $base_user_id/$base_project_id";
193 212
 %>
194 213
 
195 214
 
... ...
@@ -210,7 +229,7 @@
210 229
     $('[name=base-branch]').on('keypress', function (e) {
211 230
       // Enter
212 231
       if (e.which == 13) {
213
-        var href = '<%= url_for("/$user_id/$project_id/compare/") %>' + $(this).val() + '...<%= $rev %>';
232
+        var href = '<%= url_for("/$base_user_id/$base_project_id/compare/") %>' + $(this).val() + '...<%= $target_branch %>';
214 233
         if (<%= $expand ? 1 : 0 %>) {
215 234
           href = href + '?expand=1';
216 235
         }
... ...
@@ -232,7 +251,7 @@
232 251
     $('[name=compare-branch]').on('keypress', function (e) {
233 252
       // Enter
234 253
       if (e.which == 13) {
235
-        var href = '<%= url_for("/$user_id/$project_id/compare/") %>' + '<%= $from_rev %>...' + $(this).val();
254
+        var href = '<%= url_for("/$base_user_id/$base_project_id/compare/") %>' + '<%= $base_branch %>...' + $(this).val();
236 255
         if (<%= $expand ? 1 : 0 %>) {
237 256
           href = href + '?expand=1';
238 257
         }
... ...
@@ -256,11 +275,11 @@
256 275
     <div>
257 276
       <div>
258 277
         <button id="base-branch-btn" class="btn btn-small">
259
-          <span>base:</span><b> <%= $from_rev %></b><i class="icon-arrow-down"></i>
278
+          <span>base:</span><b> <%= $base_branch %></b><i class="icon-arrow-down"></i>
260 279
         </button>
261 280
         ...
262 281
         <button id="compare-branch-btn" class="btn btn-small">
263
-          <span>compare:</span> <b><%= $rev %></b><i class="icon-arrow-down"></i>
282
+          <span>compare:</span> <b><%= $target_branch %></b><i class="icon-arrow-down"></i>
264 283
         </button>
265 284
         
266 285
         % if ($can_open_pull_request) {
... ...
@@ -297,7 +316,7 @@
297 316
             % my $branch = $branches->[$i];
298 317
               <li>
299 318
                 <a style="border-top-left-radius:0px;border-top-right-radius:0px;"
300
-                  href="<%= url_with("/$user_id/$project_id/compare/$branch->{name}...$rev") %>">
319
+                  href="<%= url_with("/$base_user_id/$base_project_id/compare/$branch->{name}...$target_branch") %>">
301 320
                   <%= $branch->{name} %>
302 321
                 </a>
303 322
               </li>
... ...
@@ -326,7 +345,7 @@
326 345
           % my $branch = $branches->[$i];
327 346
             <li>
328 347
               <a style="border-top-left-radius:0px;border-top-right-radius:0px;"
329
-                href="<%= url_with("/$user_id/$project_id/compare/$from_rev...$branch->{name}") %>">
348
+                href="<%= url_with("/$base_user_id/$base_project_id/compare/$base_branch...$branch->{name}") %>">
330 349
                 <%= $branch->{name} %>
331 350
               </a>
332 351
             </li>
... ...
@@ -397,12 +416,12 @@
397 416
                 </span>
398 417
               </div>
399 418
               <div class="compare-commits-commit-title">
400
-                <a style="color:#333" href="<%= url_for("/$user_id/$project_id/commit/$commit->{id}") %>">
419
+                <a style="color:#333" href="<%= url_for("/$base_user_id/$base_project_id/commit/$commit->{id}") %>">
401 420
                   <%= $commit->{title_short} %>
402 421
                 </a>
403 422
               </div>
404 423
               <div class="compare-commits-commit-id">
405
-                <a href="<%= url_for("/$user_id/$project_id/commit/$commit->{id}") %>">
424
+                <a href="<%= url_for("/$base_user_id/$base_project_id/commit/$commit->{id}") %>">
406 425
                   <%= substr($commit->{id}, 0, 7) %>
407 426
                 </a>
408 427
               </div>
... ...
@@ -420,9 +439,9 @@
420 439
       </div>
421 440
       <div>
422 441
         <b>
423
-          <%= $from_rev %></b> is up to date with all commits from
424
-          <b><%= $rev %></b>.
425
-          Try <a href="<%= url_for("/$user_id/$project_id/compare/$rev...$from_rev") %>">switching the base</a> for your comparison.
442
+          <%= $base_branch %></b> is up to date with all commits from
443
+          <b><%= $target_branch %></b>.
444
+          Try <a href="<%= url_for("/$base_user_id/$base_project_id/compare/$target_branch...$base_branch") %>">switching the base</a> for your comparison.
426 445
       </div>
427 446
     </div>
428 447
   % }
+82 -49
templates/pull.html.ep
... ...
@@ -3,38 +3,67 @@
3 3
   my $api = gitprep_api;
4 4
 
5 5
   # Parameters
6
-  my $user_id = param('user');
7
-  my $project_id = param('project');
6
+  my $base_user_id = param('user');
7
+  my $base_project_id = param('project');
8 8
   my $row_id = param('row_id');
9
-  
10
-  # Session
11
-  my $session_user_id = $api->session_user_id;
12
-  
13
-  # Repository information
14
-  my $rep_info = app->rep_info($user_id, $project_id);
15
-  
16
-  # Working repository information
17
-  my $work_rep_info = app->work_rep_info($user_id, $project_id);
18
-  
19
-  # Git
20
-  my $git = $self->app->git;
21 9
 
22 10
   # Pull requests
23 11
   my $pull_request = app->dbi->model('pull_request')->select(
24 12
     [
25 13
       {__MY__ => '*'},
26
-      {user => ['id']}
14
+      {'pull_request__open_user' => ['id']}
27 15
     ],
28 16
     where => {'pull_request.row_id' => $row_id}
29 17
   )->one;
30 18
   
31
-  my $branch1 = $pull_request->{branch1};
32
-  my $branch2 = $pull_request->{branch2};
19
+  # Base information
20
+  my $base_project_row_id = $pull_request->{base_project};
21
+  my $base_project = app->dbi->model('project')->select(
22
+    [
23
+      {__MY__ => '*'},
24
+      {user => ['id']}
25
+    ],
26
+    where => {'project.row_id' => $base_project_row_id}
27
+  )->one;
28
+  my $base_branch = $pull_request->{base_branch};
29
+  
30
+  Carp::croak "pull_request invalid user id"
31
+    if $base_user_id ne $base_project->{'user.id'};
32
+  Carp::croak "pull_request invalid project id"
33
+    if $base_project_id ne $base_project->{id};
34
+  
35
+  # Target information
36
+  my $target_project_row_id = $pull_request->{target_project};
37
+  my $target_project = app->dbi->model('project')->select(
38
+    [
39
+      {__MY__ => '*'},
40
+      {user => ['id']}
41
+    ],
42
+    where => {'project.row_id' => $target_project_row_id}
43
+  )->one;
44
+  my $target_branch = $pull_request->{target_branch};
45
+  my $target_user_id = $target_project->{'user.id'};
46
+  my $taregt_project_id = $target_project->{id};
47
+  
48
+  # Session
49
+  my $session_user_id = $api->session_user_id;
50
+  
51
+  # Base repository information
52
+  my $base_rep_info = app->rep_info($base_user_id, $base_project_id);
53
+  
54
+  # Target repository information
55
+  my $target_rep_info = app->rep_info($target_user_id, $base_project_id);
56
+  
57
+  # Working repository information
58
+  my $work_rep_info = app->work_rep_info($base_user_id, $base_project_id);
59
+  
60
+  # Git
61
+  my $git = $self->app->git;
33 62
   
34 63
   if (lc $self->req->method eq 'post') {
35 64
     
36 65
     # Access controll
37
-    unless ($api->can_write_access($session_user_id, $user_id, $project_id)) {
66
+    unless ($api->can_write_access($session_user_id, $base_user_id, $base_project_id)) {
38 67
       $self->reply->exception('Forbbiden');
39 68
       return;
40 69
     }
... ...
@@ -69,38 +98,38 @@
69 98
       # Prepare merge
70 99
       $self->app->manager->prepare_merge(
71 100
         $work_rep_info,
72
-        $rep_info,
73
-        $branch1,
74
-        $rep_info,
75
-        $branch2
101
+        $base_rep_info,
102
+        $base_branch,
103
+        $target_rep_info,
104
+        $target_branch
76 105
       );
77 106
       
78 107
       # Merge
79 108
       my $merge_success = $self->app->manager->merge(
80 109
         $work_rep_info,
81
-        $rep_info,
82
-        $branch1,
83
-        $rep_info,
84
-        $branch2
110
+        $base_rep_info,
111
+        $base_branch,
112
+        $target_rep_info,
113
+        $target_branch
85 114
       );
86 115
       
87 116
       
88 117
       if ($merge_success) {
89 118
         # Push
90
-        app->manager->push($work_rep_info, $branch1);
119
+        app->manager->push($work_rep_info, $base_branch);
91 120
         
92 121
         app->dbi->model('pull_request')->update(
93 122
           {open => 0},
94 123
           where => {row_id => $row_id}
95 124
         );
96 125
         
97
-        $self->redirect_to("/$user_id/$project_id/tree/$branch1");
126
+        $self->redirect_to("/$base_user_id/$base_project_id/tree/$base_branch");
98 127
       }
99 128
     }
100 129
   }
101 130
   
102 131
   # Commits
103
-  my $commits = $git->forward_commits($rep_info, $branch1, $branch2);
132
+  my $commits = $git->forward_commits($base_rep_info, $base_branch, $target_branch);
104 133
   my $commits_count = @$commits;
105 134
   my $commits_date = {};
106 135
   my $authors = {};
... ...
@@ -113,10 +142,10 @@
113 142
   my $authors_count = keys %$authors;
114 143
 
115 144
   # Start commit
116
-  my $start_commit = $git->separated_commit($rep_info, $branch1, $branch2);
145
+  my $start_commit = $git->separated_commit($base_rep_info, $base_branch, $target_branch);
117 146
 
118 147
   # End commit
119
-  my $end_commit = $git->get_commit($rep_info, $branch2);
148
+  my $end_commit = $git->get_commit($target_rep_info, $target_branch);
120 149
   
121 150
   # Pull request first message 
122 151
   my $pull_request_message_first = app->dbi->model('pull_request_message')->select(
... ...
@@ -129,26 +158,26 @@
129 158
   
130 159
   # Check merge automatically
131 160
   my $merge_success;
132
-  if ($api->can_write_access($session_user_id, $user_id, $project_id) && $pull_request->{open}) {
133
-  
161
+  if ($api->can_write_access($session_user_id, $base_user_id, $base_project_id) && $pull_request->{open}) {
162
+    
134 163
     my $lock_fh = $self->app->manager->lock_rep($work_rep_info);
135 164
     
136 165
     # Prepare merge
137 166
     $self->app->manager->prepare_merge(
138 167
       $work_rep_info,
139
-      $rep_info,
140
-      $branch1,
141
-      $rep_info,
142
-      $branch2
168
+      $base_rep_info,
169
+      $base_branch,
170
+      $target_rep_info,
171
+      $target_branch
143 172
     );
144 173
     
145 174
     # Check merge automatical
146 175
     $merge_success = $self->app->manager->merge(
147 176
       $work_rep_info,
148
-      $rep_info,
149
-      $branch1,
150
-      $rep_info,
151
-      $branch2
177
+      $base_rep_info,
178
+      $base_branch,
179
+      $target_rep_info,
180
+      $target_branch
152 181
     );
153 182
   }
154 183
 
... ...
@@ -161,7 +190,7 @@
161 190
   );
162 191
 %>
163 192
 
164
-% layout 'common', title => "Pull Requests Tags \x{30fb} $user_id/$project_id";
193
+% layout 'common', title => "Pull Requests Tags \x{30fb} $base_user_id/$base_project_id";
165 194
   
166 195
   %= include '/include/header';
167 196
   
... ...
@@ -187,17 +216,21 @@
187 216
           </div>
188 217
         % }
189 218
       </div>
190
-      % my $open_user_id = $pull_request->{'user.id'};
219
+      % my $open_user_id = $pull_request->{'pull_request__open_user.id'};
191 220
       <a style="color:#333333;font-weight:bold" href="<%= url_for("/$open_user_id") %>"><%= $open_user_id %></a> 
192 221
       <span style="color:#767676">
193 222
         wants to merge <%= $commits_count %> commits
194 223
         into
195 224
         <span style="display:inline-block;padding:2px 4px;background:#e8f0f8;color:#336479;border-radius:3px;">
196
-          <%= $pull_request->{branch1} %>
225
+          <%= $pull_request->{base_branch} %>
197 226
         </span>
198 227
         from
199 228
         <span style="display:inline-block;padding:2px 4px;background:#e8f0f8;color:#336479;border-radius:3px;">
200
-          <%= $pull_request->{branch2} %>
229
+          % if ($base_user_id ne $target_user_id) {
230
+            <%= $target_user_id %>
231
+            /
232
+          % }
233
+          <%= $pull_request->{target_branch} %>
201 234
         </span>
202 235
       </span>
203 236
     </div>
... ...
@@ -225,7 +258,7 @@
225 258
         </ul>
226 259
         <div class="pull-comment">
227 260
           <div class="pull-comment-header">
228
-            <b><%= $pull_request_message_first->{'user.id'} %></b>
261
+            <b><%= $pull_request_message_first->{'pull_request__open_user.id'} %></b>
229 262
             <span style="color:#767676">
230 263
               commented
231 264
               <%= $api->age_string($pull_request_message_first->{update_time}) %></b>
... ...
@@ -264,12 +297,12 @@
264 297
                     </span>
265 298
                   </div>
266 299
                   <div class="compare-commits-commit-title">
267
-                    <a style="color:#333" href="<%= url_for("/$user_id/$project_id/commit/$commit->{id}") %>">
300
+                    <a style="color:#333" href="<%= url_for("/$base_user_id/$base_project_id/commit/$commit->{id}") %>">
268 301
                       <%= $commit->{title_short} %>
269 302
                     </a>
270 303
                   </div>
271 304
                   <div class="compare-commits-commit-id">
272
-                    <a href="<%= url_for("/$user_id/$project_id/commit/$commit->{id}") %>">
305
+                    <a href="<%= url_for("/$base_user_id/$base_project_id/commit/$commit->{id}") %>">
273 306
                       <%= substr($commit->{id}, 0, 7) %>
274 307
                     </a>
275 308
                   </div>
... ...
@@ -283,7 +316,7 @@
283 316
         %= include '/include/commit_body', %commit_body_args;
284 317
       % }
285 318
       
286
-      % if ($api->can_write_access($session_user_id, $user_id, $project_id)) {
319
+      % if ($api->can_write_access($session_user_id, $base_user_id, $base_project_id)) {
287 320
         % if ($commits_count && $merge_success && $pull_request->{open}) {
288 321
           <form action="<%= url_for %>" method="post">
289 322
             <%= hidden_field op => 'merge' %>