1515 r"^https://github\.com/[A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+(?:\.git)?$"
1616)
1717BRANCH_RE = re .compile (r"^[A-Za-z0-9][A-Za-z0-9._/-]{0,127}$" )
18+ REMOTE_RE = re .compile (r"^[A-Za-z0-9][A-Za-z0-9._-]{0,63}$" )
1819
1920
2021def _run (argv : list [str ], root : Path ) -> subprocess .CompletedProcess [str ]:
@@ -38,7 +39,7 @@ def _value(root: Path, *args: str) -> str:
3839 return result .stdout .strip ()
3940
4041
41- def sync (root : Path , source : str , branch : str ) -> str :
42+ def sync (root : Path , source : str , branch : str , remote_name : str = "origin" ) -> str :
4243 root = root .expanduser ()
4344 if not root .is_absolute ():
4445 raise RuntimeError ("Git checkout path must be absolute" )
@@ -52,14 +53,18 @@ def sync(root: Path, source: str, branch: str) -> str:
5253 or ".." in branch .split ("/" )
5354 ):
5455 raise RuntimeError ("Declared Git branch is invalid" )
56+ if not REMOTE_RE .fullmatch (remote_name ):
57+ raise RuntimeError ("Declared Git remote is invalid" )
5558 current_branch = _value (root , "branch" , "--show-current" )
5659 if current_branch != branch :
5760 raise RuntimeError (
5861 f"Checkout is on { current_branch or 'detached HEAD' } , expected { branch } "
5962 )
60- origin = _value (root , "remote" , "get-url" , "origin" )
61- if canonical_git_remote (origin ) != canonical_git_remote (source ):
62- raise RuntimeError ("Checkout origin does not match the declared source" )
63+ remote_url = _value (root , "remote" , "get-url" , remote_name )
64+ if canonical_git_remote (remote_url ) != canonical_git_remote (source ):
65+ raise RuntimeError (
66+ f"Checkout remote { remote_name } does not match the declared source"
67+ )
6368 dirty = _value (root , "status" , "--porcelain" , "--untracked-files=no" )
6469 if dirty :
6570 raise RuntimeError (
@@ -85,7 +90,7 @@ def sync(root: Path, source: str, branch: str) -> str:
8590 if before .lower () == remote_commit :
8691 return f"Current at { before [:7 ]} . Nothing to do."
8792 fetched = _run (
88- ["git" , "-C" , str (root ), "fetch" , "--prune" , "origin" , branch ],
93+ ["git" , "-C" , str (root ), "fetch" , "--prune" , remote_name , branch ],
8994 root ,
9095 )
9196 if fetched .returncode :
@@ -97,7 +102,7 @@ def sync(root: Path, source: str, branch: str) -> str:
97102 str (root ),
98103 "merge" ,
99104 "--ff-only" ,
100- f"origin /{ branch } " ,
105+ f"{ remote_name } /{ branch } " ,
101106 ],
102107 root ,
103108 )
@@ -114,10 +119,11 @@ def main() -> int:
114119 parser = argparse .ArgumentParser ()
115120 parser .add_argument ("--root" , type = Path , required = True )
116121 parser .add_argument ("--source" , required = True )
122+ parser .add_argument ("--remote" , default = "origin" )
117123 parser .add_argument ("--branch" , required = True )
118124 args = parser .parse_args ()
119125 try :
120- message = sync (args .root , args .source , args .branch )
126+ message = sync (args .root , args .source , args .branch , args . remote )
121127 except (OSError , RuntimeError , subprocess .SubprocessError ) as exc :
122128 print (f"Update blocked: { exc } " )
123129 return 1
0 commit comments