4040import subprocess
4141import sys
4242import tempfile
43+ import time
4344from pathlib import Path
4445
4546# Git invokes this hook with the ambient environment, which does not
5354import edgshell
5455import 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+
5684if 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
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 )
77110else :
@@ -162,6 +195,37 @@ def git_show_blob(revision_path: str) -> bytes:
162195 return b''
163196 return completed_process .stdout
164197
198+ ZERO_OBJECT_NAME_CHARS = set ('0' )
199+
200+ def describe_push_ref (line ):
201+ '''Return a description of one of the ref lines git supplies to the hook
202+ (<local ref> <local name> <remote ref> <remote name>), or None if line
203+ isn't of that form.'''
204+ fields = line .split ()
205+ if len (fields ) != 4 :
206+ return None
207+ local_ref , local_name , remote_ref , remote_name = fields
208+ if set (local_name ) <= ZERO_OBJECT_NAME_CHARS :
209+ return f"delete { remote_ref } "
210+ description = f"{ local_ref } -> { remote_ref } "
211+ if set (remote_name ) <= ZERO_OBJECT_NAME_CHARS :
212+ description += ' (new)'
213+ return description
214+
215+ def print_push_target ():
216+ '''Print where the push is targeted.'''
217+ if PUSH_REMOTE_NAME is None :
218+ return
219+ target = PUSH_REMOTE_NAME
220+ if PUSH_REMOTE_LOCATION not in (None , PUSH_REMOTE_NAME ):
221+ target += f" ({ PUSH_REMOTE_LOCATION } )"
222+ print (f"Pushing to { target } :\n " )
223+ for line in PUSH_REFS :
224+ description = describe_push_ref (line )
225+ if description is not None :
226+ print (f" { description } " )
227+ print ('' )
228+
165229class PrePushChecker :
166230 '''Runs EDG pre-push policy checks against new commits and changed files.'''
167231
@@ -435,6 +499,7 @@ def check_changed_files(self, first_new_commit):
435499
436500 def run (self ) -> int :
437501 '''Execute the full pre-push check sequence. Return the exit status.'''
502+ print_push_target ()
438503 first_new_commit = self .check_commits ()
439504
440505 # Skip per file checks if we didn't actually detect a new commit.
@@ -450,11 +515,20 @@ def wait_for_minimum_run_time() -> None:
450515 print (f"Pushing in { remaining :.0f} s (Ctrl-C to cancel)..." )
451516 time .sleep (remaining )
452517
518+ def wait_for_minimum_run_time () -> None :
519+ '''Sleep until the hook has been running for MIN_RUN_SECONDS.'''
520+ remaining = MIN_RUN_SECONDS - (time .monotonic () - START_TIME )
521+ if remaining > 0 :
522+ print (f"Pushing in { remaining :.0f} s (Ctrl-C to cancel)..." )
523+ time .sleep (remaining )
524+
453525def main ():
454526 checker = PrePushChecker ()
455527 returncode = checker .run ()
456528 if returncode != 0 :
457529 pause_delegate_window ()
530+ else :
531+ wait_for_minimum_run_time ()
458532 sys .exit (returncode )
459533
460534main ()
0 commit comments