From f54e0d120d66e5f4239dcba7f5a07105cd5f0182 Mon Sep 17 00:00:00 2001 From: Darragh Bailey Date: Sat, 10 Mar 2018 10:17:04 +0000 Subject: [PATCH] Support recent git changes to merge options Git 2.9+ requires an explicit option to merge unrelated histories together. Switch all the merge commands to try to merge without this option and call it if the stderr indicates that it is explicitly needed. Change-Id: I929b310126ed0ae4d6c9726e81d86c0609c3b6e2 --- git_upstream/lib/importupstream.py | 10 +++++++++- git_upstream/tests/base.py | 27 ++++++++++++++++++++++++--- 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/git_upstream/lib/importupstream.py b/git_upstream/lib/importupstream.py index 6058269..9bd8cc7 100644 --- a/git_upstream/lib/importupstream.py +++ b/git_upstream/lib/importupstream.py @@ -219,7 +219,15 @@ class ImportUpstream(LogDedentMixin, GitMixin): # as normal merge (with octopus strategy) will refuse if you # are merging in more than one branch without a common # ancestor to the current tree. - self.git.merge(*self.extra_branches, s="ours", no_commit=True) + try: + self.git.merge(*self.extra_branches, s="ours", no_commit=True) + except GitCommandError as exc: + if 'refusing to merge unrelated histories' in exc.stderr: + self.git.merge(*self.extra_branches, s="ours", + no_commit=True, + allow_unrelated_histories=True) + else: + raise self.git.read_tree(empty=True) self.git.read_tree("HEAD", *self.extra_branches) self.git.checkout("--", ".") diff --git a/git_upstream/tests/base.py b/git_upstream/tests/base.py index ba07704..2f4170b 100644 --- a/git_upstream/tests/base.py +++ b/git_upstream/tests/base.py @@ -224,16 +224,37 @@ class BuildTree(object): # trees of the nodes prefixed with '=' use = [str(self.graph[p.lstrip("=")]) for p in parents if p.startswith("=")] - self.git.merge(*commits, s="ours", no_commit=True) + try: + self.git.merge(*commits, s="ours", no_commit=True) + except git.exc.GitCommandError as exc: + if 'refusing to merge unrelated histories' in exc.stderr: + self.git.merge(*commits, s="ours", no_commit=True, + allow_unrelated_histories=True) + else: + raise self.git.read_tree(empty=True) self.git.read_tree(*use, u=True, reset=True) elif len(commits) < 2: # standard merge - self.git.merge(*commits, no_commit=True) + try: + self.git.merge(*commits, no_commit=True) + except git.exc.GitCommandError as exc: + if 'refusing to merge unrelated histories' in exc.stderr: + self.git.merge(*commits, no_commit=True, + allow_unrelated_histories=True) + else: + raise else: # multi-branch merge, git is not great at handling # merging multiple orphaned branches - self.git.merge(*commits, s="ours", no_commit=True) + try: + self.git.merge(*commits, s="ours", no_commit=True) + except git.exc.GitCommandError as exc: + if 'refusing to merge unrelated histories' in exc.stderr: + self.git.merge(*commits, s="ours", no_commit=True, + allow_unrelated_histories=True) + else: + raise self.git.read_tree(empty=True) self.git.read_tree("HEAD", *commits) self.git.checkout("--", ".")