diff --git a/pre_commit/commands/hook_impl.py b/pre_commit/commands/hook_impl.py index de5c8f34..6a563b60 100644 --- a/pre_commit/commands/hook_impl.py +++ b/pre_commit/commands/hook_impl.py @@ -114,6 +114,12 @@ def _rev_exists(rev: str) -> bool: return not subprocess.call(('git', 'rev-list', '--quiet', rev)) +def _is_ancestor(ancestor: str, rev: str) -> bool: + return not subprocess.call( + ('git', 'merge-base', '--is-ancestor', ancestor, rev), + ) + + def _pre_push_ns( color: bool, args: Sequence[str], @@ -127,7 +133,8 @@ def _pre_push_ns( local_branch, local_sha, remote_branch, remote_sha = parts if local_sha == Z40: continue - elif remote_sha != Z40 and _rev_exists(remote_sha): + elif remote_sha != Z40 and _rev_exists(remote_sha) \ + and _is_ancestor(remote_sha, local_sha): return _ns( 'pre-push', color, from_ref=remote_sha, to_ref=local_sha,