11from __future__ import annotations
22
33import argparse
4+ import logging
45import os .path
56import subprocess
67import sys
78from collections .abc import Sequence
89
10+ from pre_commit .clientlib import load_config
911from pre_commit .commands .run import run
1012from pre_commit .envcontext import envcontext
1113from pre_commit .parse_shebang import normalize_cmd
1214from pre_commit .store import Store
1315
16+ logger = logging .getLogger ('pre_commit' )
17+
1418Z40 = '0' * 40
1519
1620
@@ -118,6 +122,7 @@ def _pre_push_ns(
118122 color : bool ,
119123 args : Sequence [str ],
120124 stdin : bytes ,
125+ skip_remote_sha_check : bool = False ,
121126) -> argparse .Namespace | None :
122127 remote_name = args [0 ]
123128 remote_url = args [1 ]
@@ -127,7 +132,7 @@ def _pre_push_ns(
127132 local_branch , local_sha , remote_branch , remote_sha = parts
128133 if local_sha == Z40 :
129134 continue
130- elif remote_sha != Z40 and _rev_exists (remote_sha ):
135+ elif not skip_remote_sha_check and remote_sha != Z40 and _rev_exists (remote_sha ):
131136 return _ns (
132137 'pre-push' , color ,
133138 from_ref = remote_sha , to_ref = local_sha ,
@@ -213,10 +218,11 @@ def _run_ns(
213218 color : bool ,
214219 args : Sequence [str ],
215220 stdin : bytes ,
221+ skip_remote_sha_check : bool = False ,
216222) -> argparse .Namespace | None :
217223 _check_args_length (hook_type , args )
218224 if hook_type == 'pre-push' :
219- return _pre_push_ns (color , args , stdin )
225+ return _pre_push_ns (color , args , stdin , skip_remote_sha_check )
220226 elif hook_type in 'commit-msg' :
221227 return _ns (hook_type , color , commit_msg_filename = args [0 ])
222228 elif hook_type == 'prepare-commit-msg' and len (args ) == 1 :
@@ -265,7 +271,20 @@ def hook_impl(
265271) -> int :
266272 retv , stdin = _run_legacy (hook_type , hook_dir , args )
267273 _validate_config (retv , config , skip_on_missing_config )
268- ns = _run_ns (hook_type , color , args , stdin )
274+
275+ # Load config to get skip-remote-sha-check setting (only for pre-push)
276+ skip_remote_sha_check = False
277+ if hook_type == 'pre-push' :
278+ try :
279+ cfg = load_config (config )
280+ skip_remote_sha_check = cfg .get ('skip-remote-sha-check' , False )
281+ if skip_remote_sha_check :
282+ logger .info ('skip-remote-sha-check is enabled' )
283+ except Exception :
284+ # If config loading fails, use safe default (False)
285+ skip_remote_sha_check = False
286+
287+ ns = _run_ns (hook_type , color , args , stdin , skip_remote_sha_check )
269288 if ns is None :
270289 return retv
271290 else :
0 commit comments