Repository: smarkets/marge-bot

Issue:
When merging batches, fast fordwarding is enabled
Fast forward only is enabled when merging batches. This is problematic as when reverting a change included in a batch, every commit associated with the change must be reverted rather than just the merge commit.

There should be some configuration so that fast-forwarding can be disabled when merging batches.

https://github.com/smarkets/marge-bot/blob/master/marge/batch_job.py#L145

Comments:

Patch:
diff --git a/README.md b/README.md
index bbcb532..27ee749 100644
--- a/README.md
+++ b/README.md
@@ -87,6 +87,8 @@ optional arguments:
                            [env var: MARGE_ADD_TESTED] (default: False)
   --batch               Enable processing MRs in batches
                            [env var: MARGE_BATCH] (default: False)
+  --use-no-ff-batches      Disable fast forwarding when merging MR batches.
+                           [env var: MARGE_USE_NO_FF_BATCHES] (default: False)
   --add-part-of         Add "Part-of: <$MR_URL>" to each commit in MR.
                            [env var: MARGE_ADD_PART_OF] (default: False)
   --add-reviewers       Add "Reviewed-by: $approver" for each approver of MR to each commit in MR.
diff --git a/marge/app.py b/marge/app.py
index 0748643..a991811 100644
--- a/marge/app.py
+++ b/marge/app.py
@@ -208,6 +208,11 @@ def _parse_config(args):
         action='store_true',
         help='Debug logging (includes all HTTP requests etc).\n',
     )
+    parser.add_argument(
+        '--use-no-ff-batches',
+        action='store_true',
+        help='Disable fast forwarding when merging MR batches'
+    )
     config = parser.parse_args(args)
 
     if config.use_merge_strategy and config.batch:
diff --git a/marge/batch_job.py b/marge/batch_job.py
index b99fd87..2f4cae8 100644
--- a/marge/batch_job.py
+++ b/marge/batch_job.py
@@ -108,6 +108,19 @@ class BatchMergeJob(MergeJob):
             if getattr(changed_mr, attr) != getattr(merge_request, attr):
                 raise CannotMerge(error_message.format(attr.replace('_', ' ')))
 
+    def merge_batch(self, target_branch, source_branch, no_ff=False):
+        if no_ff:
+            return self._repo.merge(
+                    target_branch,
+                    source_branch,
+                    '--no-ff',
+            )
+
+        return self._repo.fast_forward(
+            target_branch,
+            source_branch,
+        )
+
     def accept_mr(
         self,
         merge_request,
@@ -142,9 +155,10 @@ class BatchMergeJob(MergeJob):
         self.maybe_reapprove(merge_request, approvals)
 
         # This switches git to <target_branch>
-        final_sha = self._repo.fast_forward(
+        final_sha = self.merge_batch(
             merge_request.target_branch,
             merge_request.source_branch,
+            self._options.use_no_ff_batches,
         )
 
         # Don't force push in case the remote has changed.


Test patch:
diff --git a/tests/test_batch_job.py b/tests/test_batch_job.py
index 109b905..4ac2c5b 100644
--- a/tests/test_batch_job.py
+++ b/tests/test_batch_job.py
@@ -138,6 +138,28 @@ class TestBatchJob:
             force=True,
         )
 
+    def test_merge_batch(self, api, mocklab):
+        batch_merge_job = self.get_batch_merge_job(api, mocklab)
+        target_branch = 'master'
+        source_branch = mocklab.merge_request_info['source_branch']
+        batch_merge_job.merge_batch(target_branch, source_branch, no_ff=False)
+        batch_merge_job._repo.fast_forward.assert_called_once_with(
+            target_branch,
+            source_branch,
+        )
+
+    def test_merge_batch_with_no_ff_enabled(self, api, mocklab):
+        batch_merge_job = self.get_batch_merge_job(api, mocklab)
+        target_branch = 'master'
+        source_branch = mocklab.merge_request_info['source_branch']
+        batch_merge_job.merge_batch(target_branch, source_branch, no_ff=True)
+        batch_merge_job._repo.merge.assert_called_once_with(
+            target_branch,
+            source_branch,
+            '--no-ff'
+        )
+        batch_merge_job._repo.fast_forward.assert_not_called()
+
     def test_ensure_mr_not_changed(self, api, mocklab):
         with patch('marge.batch_job.MergeRequest') as mr_class:
             batch_merge_job = self.get_batch_merge_job(api, mocklab)