1111from urllib .parse import urlsplit , urlunsplit
1212
1313from github import Github , GithubException
14+ from git import Repo
1415
1516from .git_tools import (
1617 clone_to_path as _git_clone_to_path ,
@@ -166,11 +167,12 @@ def get_or_create_pull(github_repo, title, body, head, base, *, none_if_no_commi
166167def clone_to_path (gh_token , folder , sdk_git_id , branch_or_commit = None , * , pr_number = None ):
167168 """Clone the given repo_id to the folder.
168169
169- If branch is specified, checkout this branch or commit.
170- If PR number is specified, don't checkout "branch", but instead the magic branches
170+ If PR number is specified fetch the magic branches
171171 pull/<id>/head or pull/<id>/merge from Github. "merge" is tried first, and fallback to "head".
172172 Beware that pr_number implies detached head, and then no push is possible.
173173
174+ If branch is specified, checkout this branch or commit finally.
175+
174176 :param str branch_or_commit: If specified, switch to this branch/commit.
175177 :param int pr_number: PR number.
176178 """
@@ -194,17 +196,20 @@ def clone_to_path(gh_token, folder, sdk_git_id, branch_or_commit=None, *, pr_num
194196 credentials = credentials_part ,
195197 sdk_git_id = sdk_git_id
196198 )
197- if not pr_number :
198- _git_clone_to_path (https_authenticated_url , folder , branch_or_commit )
199- else :
200- _git_clone_to_path ( https_authenticated_url , folder )
199+ # Clone the repo
200+ _git_clone_to_path (https_authenticated_url , folder )
201+ # If this is a PR, do some fetch to improve the number of SHA1 available
202+ if pr_number :
201203 try :
202204 checkout_with_fetch (folder , "pull/{}/merge" .format (pr_number ))
203205 return
204206 except Exception : # pylint: disable=broad-except
205207 pass # Assume "merge" doesn't exist anymore, fetch "head"
206208 checkout_with_fetch (folder , "pull/{}/head" .format (pr_number ))
207-
209+ # If there is SHA1, checkout it. If PR number was given, SHA1 could be inside that PR.
210+ if branch_or_commit :
211+ repo = Repo (str (folder ))
212+ repo .git .checkout (branch_or_commit )
208213
209214def do_pr (gh_token , sdk_git_id , sdk_pr_target_repo_id , branch_name , base_branch , pr_body = "" ): # pylint: disable=too-many-arguments
210215 "Do the PR"
0 commit comments