1 #!/usr/bin/env python 2 # Copyright 2015 The Chromium Authors. All rights reserved. 3 # Use of this source code is governed by a BSD-style license that can be 4 # found in the LICENSE file. 5 6 import sys 7 import subprocess 8 9 """Detect forced pushes (on the client) and prompt the user before going on.""" 10 11 12 def read_from_tty(): 13 try: 14 import posix # No way to do this on Windows, just give up there. 15 with open('/dev/tty') as tty_fd: 16 return tty_fd.readline().strip() 17 except: 18 return None 19 20 21 def Main(): 22 # Allow force pushes in repos forked elsewhere (e.g. googlesource). 23 remote_url = sys.argv[2] if len(sys.argv) >= 2 else '' 24 if 'github.com' not in remote_url: 25 return 0 26 27 parts = sys.stdin.readline().split() 28 if len(parts) < 4: 29 return 0 30 local_ref, local_sha, remote_ref, remote_sha = parts 31 cmd = ['git', 'rev-list', '--count', remote_sha, '--not', local_sha, 32 '--max-count=1'] 33 34 is_force_push = '0' 35 try: 36 is_force_push = subprocess.check_output(cmd).strip() 37 except(subprocess.CalledProcessError): 38 return 0 39 40 if is_force_push != '0': 41 sys.stderr.write('\033[31mWARNING: Force pushing will break the ' + 42 'github.com -> googlesource.com mirroring.\033[0m\n' + 43 'This is almost certainly a bad idea.\n') 44 45 sys.stderr.write('Type y to continue: ') 46 if read_from_tty() != 'y': 47 return 1 48 49 return 0 50 51 52 if __name__ == '__main__': 53 sys.exit(Main()) 54