Skip to content

Commit 83b6259

Browse files
Add a notice of where the push is going to the hook and make the script take at least 3 seconds to run []
1 parent fe25315 commit 83b6259

1 file changed

Lines changed: 76 additions & 2 deletions

File tree

‎.git-hooks/pre-push.py‎

Lines changed: 76 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import subprocess
4141
import sys
4242
import tempfile
43+
import time
4344
from pathlib import Path
4445

4546
# Git invokes this hook with the ambient environment, which does not
@@ -53,6 +54,33 @@
5354
import edgshell
5455
import edgutil
5556

57+
START_TIME = time.monotonic()
58+
59+
# The push isn't allowed to proceed until the hook has run for this long, so
60+
# that there's time to read the target of the push and cancel it (e.g., with
61+
# Ctrl-C) before it happens.
62+
MIN_RUN_SECONDS = 3.0
63+
64+
def read_push_refs():
65+
'''Return the lines git supplied on standard input describing what is to be
66+
pushed. A relaunched delegate (see below) can't read them from standard
67+
input, so its parent passes them in the environment.'''
68+
if os.environ.get('EDG_PREPUSH_DELEGATE') == '1':
69+
refs_text = os.environ.get('EDG_PREPUSH_REFS', '')
70+
elif sys.stdin.isatty():
71+
# Run by hand rather than by git.
72+
refs_text = ''
73+
else:
74+
refs_text = sys.stdin.read()
75+
return refs_text.splitlines()
76+
77+
# Git supplies the remote name and location as arguments (both are the
78+
# location if the push doesn't use a named remote). Read the refs before
79+
# standard input is replaced below.
80+
PUSH_REMOTE_NAME = sys.argv[1] if len(sys.argv) > 1 else None
81+
PUSH_REMOTE_LOCATION = sys.argv[2] if len(sys.argv) > 2 else None
82+
PUSH_REFS = read_push_refs()
83+
5684
if platform.system() == 'Windows':
5785
# The standard input of this hook carries the push ref list supplied by
5886
# git, so it cannot be used to prompt the user. When a console is
@@ -68,10 +96,15 @@
6896
delegate_proc = subprocess.run(
6997
[
7098
sys.executable,
71-
__file__
99+
__file__,
100+
*sys.argv[1:]
72101
],
73102
creationflags = subprocess.CREATE_NEW_CONSOLE,
74-
env = {**os.environ, 'EDG_PREPUSH_DELEGATE': '1'}
103+
env = {
104+
**os.environ,
105+
'EDG_PREPUSH_DELEGATE': '1',
106+
'EDG_PREPUSH_REFS': '\n'.join(PUSH_REFS)
107+
}
75108
)
76109
sys.exit(delegate_proc.returncode)
77110
else:
@@ -150,6 +183,37 @@ def git_show_blob(revision_path: str) -> bytes:
150183
return b''
151184
return completed_process.stdout
152185

186+
ZERO_OBJECT_NAME_CHARS = set('0')
187+
188+
def describe_push_ref(line):
189+
'''Return a description of one of the ref lines git supplies to the hook
190+
(<local ref> <local name> <remote ref> <remote name>), or None if line
191+
isn't of that form.'''
192+
fields = line.split()
193+
if len(fields) != 4:
194+
return None
195+
local_ref, local_name, remote_ref, remote_name = fields
196+
if set(local_name) <= ZERO_OBJECT_NAME_CHARS:
197+
return f"delete {remote_ref}"
198+
description = f"{local_ref} -> {remote_ref}"
199+
if set(remote_name) <= ZERO_OBJECT_NAME_CHARS:
200+
description += ' (new)'
201+
return description
202+
203+
def print_push_target():
204+
'''Print where the push is targeted.'''
205+
if PUSH_REMOTE_NAME is None:
206+
return
207+
target = PUSH_REMOTE_NAME
208+
if PUSH_REMOTE_LOCATION not in (None, PUSH_REMOTE_NAME):
209+
target += f" ({PUSH_REMOTE_LOCATION})"
210+
print(f"Pushing to {target}:\n")
211+
for line in PUSH_REFS:
212+
description = describe_push_ref(line)
213+
if description is not None:
214+
print(f" {description}")
215+
print('')
216+
153217
class PrePushChecker:
154218
'''Runs EDG pre-push policy checks against new commits and changed files.'''
155219

@@ -418,6 +482,7 @@ def check_changed_files(self, first_new_commit):
418482

419483
def run(self) -> int:
420484
'''Execute the full pre-push check sequence. Return the exit status.'''
485+
print_push_target()
421486
first_new_commit = self.check_commits()
422487

423488
# Skip per file checks if we didn't actually detect a new commit.
@@ -437,11 +502,20 @@ def pause_delegate_window() -> None:
437502
except EOFError:
438503
pass
439504

505+
def wait_for_minimum_run_time() -> None:
506+
'''Sleep until the hook has been running for MIN_RUN_SECONDS.'''
507+
remaining = MIN_RUN_SECONDS - (time.monotonic() - START_TIME)
508+
if remaining > 0:
509+
print(f"Pushing in {remaining:.0f}s (Ctrl-C to cancel)...")
510+
time.sleep(remaining)
511+
440512
def main():
441513
checker = PrePushChecker()
442514
returncode = checker.run()
443515
if returncode != 0:
444516
pause_delegate_window()
517+
else:
518+
wait_for_minimum_run_time()
445519
sys.exit(returncode)
446520

447521
main()

0 commit comments

Comments
 (0)