|
53 | 53 | import edgshell |
54 | 54 | import edgutil |
55 | 55 |
|
| 56 | +def read_push_refs(): |
| 57 | + '''Return the lines git supplied on standard input describing what is to be |
| 58 | + pushed. A relaunched delegate (see below) can't read them from standard |
| 59 | + input, so its parent passes them in the environment.''' |
| 60 | + if os.environ.get('EDG_PREPUSH_DELEGATE') == '1': |
| 61 | + refs_text = os.environ.get('EDG_PREPUSH_REFS', '') |
| 62 | + elif sys.stdin.isatty(): |
| 63 | + # Run by hand rather than by git. |
| 64 | + refs_text = '' |
| 65 | + else: |
| 66 | + refs_text = sys.stdin.read() |
| 67 | + return refs_text.splitlines() |
| 68 | + |
| 69 | +# Git supplies the remote name and location as arguments (both are the |
| 70 | +# location if the push doesn't use a named remote). Read the refs before |
| 71 | +# standard input is replaced below. |
| 72 | +PUSH_REMOTE_NAME = sys.argv[1] if len(sys.argv) > 1 else None |
| 73 | +PUSH_REMOTE_LOCATION = sys.argv[2] if len(sys.argv) > 2 else None |
| 74 | +PUSH_REFS = read_push_refs() |
| 75 | + |
56 | 76 | if platform.system() == 'Windows': |
57 | 77 | # The standard input of this hook carries the push ref list supplied by |
58 | 78 | # git, so it cannot be used to prompt the user. When a console is |
|
68 | 88 | delegate_proc = subprocess.run( |
69 | 89 | [ |
70 | 90 | sys.executable, |
71 | | - __file__ |
| 91 | + __file__, |
| 92 | + *sys.argv[1:] |
72 | 93 | ], |
73 | 94 | creationflags = subprocess.CREATE_NEW_CONSOLE, |
74 | | - env = {**os.environ, 'EDG_PREPUSH_DELEGATE': '1'} |
| 95 | + env = { |
| 96 | + **os.environ, |
| 97 | + 'EDG_PREPUSH_DELEGATE': '1', |
| 98 | + 'EDG_PREPUSH_REFS': '\n'.join(PUSH_REFS) |
| 99 | + } |
75 | 100 | ) |
76 | 101 | sys.exit(delegate_proc.returncode) |
77 | 102 | else: |
@@ -150,6 +175,37 @@ def git_show_blob(revision_path: str) -> bytes: |
150 | 175 | return b'' |
151 | 176 | return completed_process.stdout |
152 | 177 |
|
| 178 | +ZERO_OBJECT_NAME_CHARS = set('0') |
| 179 | + |
| 180 | +def describe_push_ref(line): |
| 181 | + '''Return a description of one of the ref lines git supplies to the hook |
| 182 | + (<local ref> <local name> <remote ref> <remote name>), or None if line |
| 183 | + isn't of that form.''' |
| 184 | + fields = line.split() |
| 185 | + if len(fields) != 4: |
| 186 | + return None |
| 187 | + local_ref, local_name, remote_ref, remote_name = fields |
| 188 | + if set(local_name) <= ZERO_OBJECT_NAME_CHARS: |
| 189 | + return f"delete {remote_ref}" |
| 190 | + description = f"{local_ref} -> {remote_ref}" |
| 191 | + if set(remote_name) <= ZERO_OBJECT_NAME_CHARS: |
| 192 | + description += ' (new)' |
| 193 | + return description |
| 194 | + |
| 195 | +def print_push_target(): |
| 196 | + '''Print where the push is targeted.''' |
| 197 | + if PUSH_REMOTE_NAME is None: |
| 198 | + return |
| 199 | + target = PUSH_REMOTE_NAME |
| 200 | + if PUSH_REMOTE_LOCATION not in (None, PUSH_REMOTE_NAME): |
| 201 | + target += f" ({PUSH_REMOTE_LOCATION})" |
| 202 | + print(f"Pushing to {target}:\n") |
| 203 | + for line in PUSH_REFS: |
| 204 | + description = describe_push_ref(line) |
| 205 | + if description is not None: |
| 206 | + print(f" {description}") |
| 207 | + print('') |
| 208 | + |
153 | 209 | class PrePushChecker: |
154 | 210 | '''Runs EDG pre-push policy checks against new commits and changed files.''' |
155 | 211 |
|
@@ -418,6 +474,7 @@ def check_changed_files(self, first_new_commit): |
418 | 474 |
|
419 | 475 | def run(self) -> int: |
420 | 476 | '''Execute the full pre-push check sequence. Return the exit status.''' |
| 477 | + print_push_target() |
421 | 478 | first_new_commit = self.check_commits() |
422 | 479 |
|
423 | 480 | # Skip per file checks if we didn't actually detect a new commit. |
|
0 commit comments