Skip to content
This repository was archived by the owner on Oct 12, 2023. It is now read-only.

Commit 2bf87b1

Browse files
authored
Allow clone_to_path to have both PR and SHA1 (#49)
* Allow clone_to_path to have both PR and SHA1 * Fix test
1 parent d4f6fc4 commit 2bf87b1

File tree

4 files changed

+63
-22
lines changed

4 files changed

+63
-22
lines changed

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from setuptools import setup
1010

1111

12-
VERSION = "1.1.0"
12+
VERSION = "1.1.1"
1313

1414

1515
CLASSIFIERS = [

src/azure_devtools/ci_tools/github_tools.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from urllib.parse import urlsplit, urlunsplit
1212

1313
from github import Github, GithubException
14+
from git import Repo
1415

1516
from .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
166167
def 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

209214
def 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

Comments
 (0)