Skip to content

Commit 9286236

Browse files
Add a notice of where the push is going to the hook []
1 parent f971ffc commit 9286236

1 file changed

Lines changed: 59 additions & 2 deletions

File tree

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

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,26 @@
5353
import edgshell
5454
import edgutil
5555

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+
5676
if platform.system() == 'Windows':
5777
# The standard input of this hook carries the push ref list supplied by
5878
# git, so it cannot be used to prompt the user. When a console is
@@ -68,10 +88,15 @@
6888
delegate_proc = subprocess.run(
6989
[
7090
sys.executable,
71-
__file__
91+
__file__,
92+
*sys.argv[1:]
7293
],
7394
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+
}
75100
)
76101
sys.exit(delegate_proc.returncode)
77102
else:
@@ -150,6 +175,37 @@ def git_show_blob(revision_path: str) -> bytes:
150175
return b''
151176
return completed_process.stdout
152177

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+
153209
class PrePushChecker:
154210
'''Runs EDG pre-push policy checks against new commits and changed files.'''
155211

@@ -418,6 +474,7 @@ def check_changed_files(self, first_new_commit):
418474

419475
def run(self) -> int:
420476
'''Execute the full pre-push check sequence. Return the exit status.'''
477+
print_push_target()
421478
first_new_commit = self.check_commits()
422479

423480
# Skip per file checks if we didn't actually detect a new commit.

0 commit comments

Comments
 (0)