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("--", ".")