← Back to team overview

launchpad-reviewers team mailing list archive

[Merge] ~cjwatson/git-build-recipe:handle-git-2.9.0 into git-build-recipe:master

 

Colin Watson has proposed merging ~cjwatson/git-build-recipe:handle-git-2.9.0 into git-build-recipe:master.

Commit message:
Handle git 2.9.0

Requested reviews:
  Launchpad code reviewers (launchpad-reviewers)

For more details, see:
https://code.launchpad.net/~cjwatson/git-build-recipe/+git/git-build-recipe/+merge/304105

Handle git 2.9.0

git 2.9.0 forbids merging unrelated histories by default, but has an
option to override this.  Pass it where appropriate, since we explicitly
want to allow such merges (as demonstrated by a test carried over from
bzr-builder).

Debian: #835526
-- 
Your team Launchpad code reviewers is requested to review the proposed merge of ~cjwatson/git-build-recipe:handle-git-2.9.0 into git-build-recipe:master.
diff --git a/gitbuildrecipe/recipe.py b/gitbuildrecipe/recipe.py
index 98ce897..3275775 100644
--- a/gitbuildrecipe/recipe.py
+++ b/gitbuildrecipe/recipe.py
@@ -15,6 +15,7 @@
 """Handle recipe parsing and substitution variables."""
 
 import datetime
+from functools import lru_cache
 import logging
 import os
 import subprocess
@@ -22,6 +23,7 @@ import sys
 from urllib.parse import urlparse
 
 import dateutil.parser
+from debian import debian_support
 
 
 MERGE_INSTRUCTION = "merge"
@@ -332,6 +334,14 @@ def fetch_branches(child_branch):
         "refs/heads/*:refs/remotes/%s/*" % child_branch.remote_name)
 
 
+@lru_cache(maxsize=1)
+def _git_version():
+    raw_git_version = subprocess.check_output(
+        ["dpkg-query", "-W", "-f", "${Version}", "git"],
+        universal_newlines=True)
+    return debian_support.Version(raw_git_version)
+
+
 def merge_branch(child_branch, target_path):
     """Merge the branch specified by `child_branch`.
 
@@ -343,9 +353,12 @@ def merge_branch(child_branch, target_path):
     fetch_branches(child_branch)
     try:
         child_branch.resolve_commit()
-        child_branch.git_call(
-            "merge", "--commit", "-m", "Merge %s" % child_branch.get_revspec(),
-            child_branch.commit)
+        cmd = ["merge", "--commit"]
+        if _git_version() >= "1:2.9.0":
+            cmd.append("--allow-unrelated-histories")
+        cmd.extend(["-m", "Merge %s" % child_branch.get_revspec()])
+        cmd.append(child_branch.commit)
+        child_branch.git_call(*cmd)
     except subprocess.CalledProcessError as e:
         raise MergeFailed(e.output)