(
color: bool,
args: Sequence[str],
stdin: bytes,
)
| 118 | |
| 119 | |
| 120 | def _pre_push_ns( |
| 121 | color: bool, |
| 122 | args: Sequence[str], |
| 123 | stdin: bytes, |
| 124 | ) -> argparse.Namespace | None: |
| 125 | remote_name = args[0] |
| 126 | remote_url = args[1] |
| 127 | |
| 128 | for line in stdin.decode().splitlines(): |
| 129 | parts = line.rsplit(maxsplit=3) |
| 130 | local_branch, local_sha, remote_branch, remote_sha = parts |
| 131 | if local_sha == Z40: |
| 132 | continue |
| 133 | elif remote_sha != Z40 and _rev_exists(remote_sha): |
| 134 | return _ns( |
| 135 | 'pre-push', color, |
| 136 | from_ref=remote_sha, to_ref=local_sha, |
| 137 | remote_branch=remote_branch, |
| 138 | local_branch=local_branch, |
| 139 | remote_name=remote_name, remote_url=remote_url, |
| 140 | ) |
| 141 | else: |
| 142 | # ancestors not found in remote |
| 143 | ancestors = subprocess.check_output(( |
| 144 | 'git', 'rev-list', local_sha, '--topo-order', '--reverse', |
| 145 | '--not', f'--remotes={remote_name}', |
| 146 | )).decode().strip() |
| 147 | if not ancestors: |
| 148 | continue |
| 149 | else: |
| 150 | first_ancestor = ancestors.splitlines()[0] |
| 151 | cmd = ('git', 'rev-list', '--max-parents=0', local_sha) |
| 152 | roots = set(subprocess.check_output(cmd).decode().splitlines()) |
| 153 | if first_ancestor in roots: |
| 154 | # pushing the whole tree including root commit |
| 155 | return _ns( |
| 156 | 'pre-push', color, |
| 157 | all_files=True, |
| 158 | remote_name=remote_name, remote_url=remote_url, |
| 159 | remote_branch=remote_branch, |
| 160 | local_branch=local_branch, |
| 161 | ) |
| 162 | else: |
| 163 | rev_cmd = ('git', 'rev-parse', f'{first_ancestor}^') |
| 164 | source = subprocess.check_output(rev_cmd).decode().strip() |
| 165 | return _ns( |
| 166 | 'pre-push', color, |
| 167 | from_ref=source, to_ref=local_sha, |
| 168 | remote_name=remote_name, remote_url=remote_url, |
| 169 | remote_branch=remote_branch, |
| 170 | local_branch=local_branch, |
| 171 | ) |
| 172 | |
| 173 | # nothing to push |
| 174 | return None |
| 175 | |
| 176 | |
| 177 | _EXPECTED_ARG_LENGTH_BY_HOOK = { |
no test coverage detected