Showing 6 changed files with 150 additions and 42 deletions
+14 -8
lib/Gitprep/Git.pm
... ...
@@ -155,6 +155,8 @@ sub branches_count {
155 155
 sub cmd {
156 156
   my ($self, $rep_info, @command) = @_;
157 157
   
158
+  $rep_info //= {};
159
+  
158 160
   my $git_dir = $rep_info->{git_dir};
159 161
   my $work_tree = $rep_info->{work_tree};
160 162
   
... ...
@@ -538,16 +540,20 @@ sub commits_number {
538 540
 }
539 541
 
540 542
 sub exists_branch {
541
-  my ($self, $rep_info) = @_;
543
+  my ($self, $rep_info, $branch_name) = @_;
542 544
   
543
-  # Exists branch
544
-  my @cmd = $self->cmd($rep_info, 'branch');
545
-  open my $fh, "-|", @cmd
546
-    or croak 'git branch failed';
547
-  local $/;
548
-  my $branch = <$fh>;
545
+  my $branch_names = $self->branch_names($rep_info);
546
+  
547
+  
548
+  my $exists_branch;
549
+  if (defined $branch_name) {
550
+    $exists_branch = grep { $_ eq $branch_name } @$branch_names;
551
+  }
552
+  else {
553
+    $exists_branch = @$branch_names ? 1 : 0;
554
+  }
549 555
   
550
-  return $branch ne '' ? 1 : 0;
556
+  return $exists_branch;
551 557
 }
552 558
 
553 559
 sub delete_branch {
+97 -11
lib/Gitprep/Manager.pm
... ...
@@ -22,7 +22,12 @@ sub merge_and_push {
22 22
   
23 23
   # Merge
24 24
   my $target_user_id = $rep_info2->{user};
25
-  my @git_merge_cmd = $self->app->git->cmd($work_rep_info, 'merge', "$target_user_id/$target_branch");
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
+  );
26 31
   Gitprep::Util::run_command(@git_merge_cmd)
27 32
     or Carp::croak "Can't execute git merge: @git_merge_cmd";
28 33
   
... ...
@@ -67,12 +72,60 @@ sub prepare_merge {
67 72
   my @git_fetch_target_cmd = $self->app->git->cmd($work_rep_info, 'fetch', $target_user_id, $target_branch);
68 73
   Gitprep::Util::run_command(@git_fetch_target_cmd)
69 74
     or Carp::croak "Can't execute git fetch: @git_fetch_target_cmd";
75
+
76
+  # Ensure no diff
77
+  my @git_reset_hard_cmd = $self->app->git->cmd(
78
+    $work_rep_info,
79
+    'reset',
80
+    '--hard'
81
+  );
82
+  Gitprep::Util::run_command(@git_reset_hard_cmd)
83
+    or Carp::croak "Can't execute git reset --hard: @git_reset_hard_cmd";
84
+
85
+  # Checkout first branch
86
+  my $tmp_branch = $self->_tmp_branch;
87
+  my $branch_names = $self->app->git->branch_names($work_rep_info);
88
+  my $first_branch;
89
+  for my $branch_name (@$branch_names) {
90
+    if ($branch_name ne $tmp_branch) {
91
+      $first_branch = $branch_name;
92
+      last;
93
+    }
94
+  }
95
+  my @git_checkout_first_branch = $self->app->git->cmd(
96
+    $work_rep_info,
97
+    'checkout',
98
+    $first_branch
99
+  );
100
+  Gitprep::Util::run_command(@git_checkout_first_branch)
101
+    or Carp::croak "Can't execute git checkout: @git_checkout_first_branch";
102
+  
103
+  # Delete temparary branch if it eixsts
104
+  if (grep { $_ eq $tmp_branch } @$branch_names) {
105
+    my @git_branch_remove_cmd = $self->app->git->cmd(
106
+      $work_rep_info,
107
+      'branch',
108
+      '-D',
109
+      $tmp_branch
110
+    );
111
+    Gitprep::Util::run_command(@git_branch_remove_cmd)
112
+      or Carp::croak "Can't execute git branch: @git_branch_remove_cmd";
113
+  }
114
+
115
+  # Create temparary branch
116
+  my @git_branch_cmd = $self->app->git->cmd(
117
+    $work_rep_info,
118
+    'branch',
119
+    $tmp_branch
120
+  );
121
+  Gitprep::Util::run_command(@git_branch_cmd)
122
+    or Carp::croak "Can't execute git branch: @git_branch_cmd";
70 123
   
71 124
   # Checkout tmp branch and git reset --hard from my remote branch
72 125
   my @git_checkout_tmp_branch = $self->app->git->cmd(
73 126
     $work_rep_info,
74 127
     'checkout',
75
-    $self->_tmp_branch
128
+    $tmp_branch
76 129
   );
77 130
   Gitprep::Util::run_command(@git_checkout_tmp_branch)
78 131
     or Carp::croak "Can't execute git checkout: @git_checkout_tmp_branch";
... ...
@@ -88,6 +141,34 @@ sub prepare_merge {
88 141
     or Carp::croak "Can't execute git reset --hard: @git_reset_hard_cmd";
89 142
 }
90 143
 
144
+sub merge {
145
+  my ($self, $work_rep_info, $rep_info1, $base_branch, $rep_info2, $target_branch) = @_;
146
+  
147
+  # Merge
148
+  my $target_user_id = $rep_info2->{user};
149
+  my @git_merge_cmd = $self->app->git->cmd(
150
+    $work_rep_info,
151
+    'merge',
152
+    "--message=merge $target_user_id/$target_branch",
153
+    "$target_user_id/$target_branch"
154
+  );
155
+  
156
+  my $success = Gitprep::Util::run_command(@git_merge_cmd);
157
+  
158
+  return $success;
159
+}
160
+
161
+sub push {
162
+  my ($self, $work_rep_info, $rep_info1, $base_branch, $rep_info2, $target_branch) = @_;
163
+  
164
+  # Push
165
+  my @git_push_cmd = $self->app->git->cmd($work_rep_info, 'push', 'origin', $base_branch);
166
+  Gitprep::Util::run_command(@git_push_cmd)
167
+    or Carp::croak "Can't execute git push: @git_push_cmd";
168
+}
169
+
170
+
171
+=pod
91 172
 sub check_merge_automatical {
92 173
   my ($self, $work_rep_info, $rep_info1, $base_branch, $rep_info2, $target_branch) = @_;
93 174
   
... ...
@@ -104,6 +185,8 @@ sub check_merge_automatical {
104 185
     or Carp::croak "Can't execute git format-patch: @git_format_patch_cmd";
105 186
   my $patch_str = do { local $/; <$git_format_patch_fh> };
106 187
   
188
+  warn "@git_format_patch_cmd";
189
+  
107 190
   # Write patch to file
108 191
   my $tmp_dir = File::Temp->newdir(DIR => $self->app->home->rel_file('/tmp'));
109 192
   my $patch_file = "$tmp_dir/test.patch";
... ...
@@ -113,18 +196,30 @@ sub check_merge_automatical {
113 196
   close $patch_fh;
114 197
   
115 198
   # Check if this patch can be applied
199
+  use Cwd;
200
+  my $original_cwd = getcwd;
201
+  chdir $work_rep_info->{work_tree}
202
+    or croak "Can't change working directory: $work_rep_info->{work_tree}";
203
+  warn "aaaaaaaaaaaaaaaaa $work_rep_info->{work_tree}";
116 204
   my @git_apply_cmd = $self->app->git->cmd(
117 205
     $work_rep_info,
118 206
     'apply',
119 207
     $patch_file,
120 208
     '--check'
121 209
   );
210
+  chdir $original_cwd
211
+    or croak "Can't change working directory: $original_cwd";
212
+  
213
+  warn "@git_apply_cmd";
122 214
   
215
+  # sleep 300;
123 216
   my $automatical = Gitprep::Util::run_command(@git_apply_cmd);
124 217
   
218
+  warn "bbbbbbbbbbbbbbbbbbbb $automatical";
125 219
   
126 220
   return $automatical;
127 221
 }
222
+=cut
128 223
 
129 224
 sub lock_rep {
130 225
   my ($self, $rep_info) = @_;
... ...
@@ -160,15 +255,6 @@ sub create_work_rep {
160 255
     Gitprep::Util::run_command(@git_clone_cmd)
161 256
       or croak "Can't git clone: @git_clone_cmd";
162 257
     
163
-    # Create temparary branch
164
-    my @git_branch_cmd = $self->app->git->cmd(
165
-      $work_rep_info,
166
-      'branch',
167
-      $self->_tmp_branch
168
-    );
169
-    Gitprep::Util::run_command(@git_branch_cmd)
170
-      or Carp::croak "Can't execute git branch: @git_branch_cmd";
171
-
172 258
     # Set user name
173 259
     my @git_config_user_name = $self->app->git->cmd(
174 260
       $work_rep_info,
+14
public/css/common.css
... ...
@@ -49,6 +49,20 @@
49 49
   text-decoration:none;
50 50
 }
51 51
 
52
+.branches-close {
53
+  color:white;
54
+  background:#bd2c00;
55
+  padding:5px 8px;
56
+  border-radius:3px;
57
+  margin-right:4px;
58
+  border-radius:3px;
59
+  text-align:center;
60
+}
61
+.branches-close:hover {
62
+  color:white;
63
+  text-decoration:none;
64
+}
65
+
52 66
 .pull-comment {
53 67
   border:1px solid #eee;
54 68
   margin-top:15px;
+3 -1
templates/branches.html.ep
... ...
@@ -313,7 +313,9 @@
313 313
                                 open
314 314
                               </a>
315 315
                             % } else {
316
-                              closed
316
+                              <a class="branches-close" href="<%= url_for("/$user/$project/pull/$pull_request->{row_id}") %>">
317
+                                closed
318
+                              </a>
317 319
                             % }
318 320
                           % } else {
319 321
                             <% 
+14 -15
templates/compare.html.ep
... ...
@@ -129,12 +129,6 @@
129 129
   my $branches = $git->branches($rep_info);
130 130
   @$branches = sort { $a->{commit}{age} <=> $b->{commit}{age} } @$branches;
131 131
   
132
-  # Variables
133
-  stash id => $end_commit->{id};
134
-  stash from_id => $start_commit->{id};
135
-  stash rev => $end_commit->{id};
136
-  stash from_rev => $start_commit->{id};
137
-  
138 132
   # Can open pull request
139 133
   my $can_open_pull_request;
140 134
   if (keys %$commits_date && $expand) {
... ...
@@ -142,7 +136,7 @@
142 136
   }
143 137
   
144 138
   # Can merge
145
-  my $merge_automatical;
139
+  my $merge_success;
146 140
   if ($can_open_pull_request) {
147 141
     
148 142
     # Create working repository if it don't exist
... ...
@@ -162,18 +156,23 @@
162 156
     );
163 157
     
164 158
     # Check merge automatically
165
-    $merge_automatical = $self->app->manager->check_merge_automatical(
159
+    $merge_success = $self->app->manager->merge(
166 160
       $work_rep_info,
167 161
       $rep_info,
168 162
       $from_rev,
169 163
       $rep_info,
170 164
       $rev
171 165
     );
172
-    
173
-    # Unlock explicitly
174
-    close $lock_fh;
175 166
   }
176
-  
167
+
168
+  # commit_body args
169
+  my %commit_body_args = (
170
+    id => $end_commit->{id},
171
+    from_id => $start_commit->{id},
172
+    rev => $end_commit->{id},
173
+    from_rev => $start_commit->{id}
174
+  );
175
+
177 176
   layout 'common', title => "Comparing $from_rev...$rev \x{30fb} $user_id/$project_id";
178 177
 %>
179 178
 
... ...
@@ -249,7 +248,7 @@
249 248
         </button>
250 249
         
251 250
         % if ($can_open_pull_request) {
252
-          % if ($merge_automatical) {
251
+          % if ($merge_success) {
253 252
             <span style="margin-left:10px">
254 253
               <span style="color:green;font-weight:bold"><%= "\x{2714}" %>Able to merge.</span> These branches can be automatically merged.
255 254
             </span>
... ...
@@ -321,7 +320,7 @@
321 320
     </div>
322 321
   </div>
323 322
   
324
-  % if ($merge_automatical) {
323
+  % if ($merge_success) {
325 324
     <div class="compare-open-pull-request">
326 325
       <form action="<%= url_for %>" method="post">
327 326
         <%= hidden_field op => 'create-pull-request' %>
... ...
@@ -397,7 +396,7 @@
397 396
       % }
398 397
     </div>
399 398
   
400
-    %= include '/include/commit_body';
399
+    %= include '/include/commit_body', %commit_body_args;
401 400
   % } else {
402 401
     <div class="compare-nothing">
403 402
       <div>
+8 -7
templates/pull.html.ep
... ...
@@ -75,8 +75,8 @@
75 75
         $branch2
76 76
       );
77 77
       
78
-      # Check merge automatical
79
-      my $merge_automatical = $self->app->manager->check_merge_automatical(
78
+      # Merge
79
+      my $merge_success = $self->app->manager->merge(
80 80
         $work_rep_info,
81 81
         $rep_info,
82 82
         $branch1,
... ...
@@ -84,8 +84,9 @@
84 84
         $branch2
85 85
       );
86 86
       
87
-      if ($merge_automatical) {
88
-        app->manager->merge_and_push(
87
+      if ($merge_success) {
88
+        # Push
89
+        app->manager->push(
89 90
           $work_rep_info,
90 91
           $rep_info,
91 92
           $branch1,
... ...
@@ -132,7 +133,7 @@
132 133
   )->one;
133 134
   
134 135
   # Check merge automatically
135
-  my $merge_automatical;
136
+  my $merge_success;
136 137
   if ($api->can_write_access($session_user_id, $user_id, $project_id) && $pull_request->{open}) {
137 138
   
138 139
     my $lock_fh = $self->app->manager->lock_rep($work_rep_info);
... ...
@@ -147,7 +148,7 @@
147 148
     );
148 149
     
149 150
     # Check merge automatical
150
-    $merge_automatical = $self->app->manager->check_merge_automatical(
151
+    $merge_success = $self->app->manager->merge(
151 152
       $work_rep_info,
152 153
       $rep_info,
153 154
       $branch1,
... ...
@@ -280,7 +281,7 @@
280 281
       %= include '/include/commit_body', %commit_body_args;
281 282
       
282 283
       % if ($api->can_write_access($session_user_id, $user_id, $project_id)) {
283
-        % if ($merge_automatical && $pull_request->{open}) {
284
+        % if ($merge_success && $pull_request->{open}) {
284 285
           <form action="<%= url_for %>" method="post">
285 286
             <%= hidden_field op => 'merge' %>
286 287
             <div class="pull-request-form">