Skip to content

Commit

Permalink
Merge pull request #57 from cs50/develop
Browse files Browse the repository at this point in the history
v2.0.8
  • Loading branch information
Kareem Zidane committed Mar 22, 2020
2 parents ccdf4af + 45bee1e commit f563b44
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
24 changes: 18 additions & 6 deletions lib50/_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,30 +78,39 @@ def push(tool, slug, config_loader, repo=None, data=None, prompt=lambda included
raise Error(_("No files were submitted."))


def local(slug, offline=False):
def local(slug, offline=False, remove_origin=False, github_token=None):
"""
Create/update local copy of github.com/org/repo/branch.
Returns path to local copy
"""

# Parse slug
slug = Slug(slug, offline=offline)
slug = Slug(slug, offline=offline, github_token=github_token)

local_path = get_local_path() / slug.org / slug.repo

git = Git().set("-C {path}", path=str(local_path))
if not local_path.exists():
_run(Git()("init {path}", path=str(local_path)))
_run(git(f"remote add origin https://github.com/{slug.org}/{slug.repo}"))
_run(git(f"remote add origin {slug.origin}"))

if not offline:
# Get latest version of checks
_run(git("fetch origin {branch}", branch=slug.branch))


# Tolerate checkout failure (e.g., when origin doesn't exist)
try:
_run(git("checkout -f -B {branch} origin/{branch}", branch=slug.branch))
except Error:
pass

# Ensure that local copy of the repo is identical to remote copy
_run(git("checkout -f -B {branch} origin/{branch}", branch=slug.branch))
_run(git("reset --hard HEAD"))

if remove_origin:
_run(git(f"remote remove origin"))

problem_path = (local_path / slug.problem).absolute()

if not problem_path.exists():
Expand Down Expand Up @@ -518,7 +527,7 @@ def __call__(self, command, **format_args):


class Slug:
def __init__(self, slug, offline=False):
def __init__(self, slug, offline=False, github_token=None):
"""Parse <org>/<repo>/<branch>/<problem_dir> from slug."""
self.slug = self.normalize_case(slug)
self.offline = offline
Expand All @@ -535,6 +544,9 @@ def __init__(self, slug, offline=False):
remainder = self.slug[idx + 1:]
self.org, self.repo = self.slug.split("/")[:2]

credentials = f"{github_token}:x-oauth-basic@" if github_token else ""
self.origin = f"https://{credentials}github.com/{self.org}/{self.repo}"

# Gather all branches
try:
branches = self._get_branches()
Expand Down Expand Up @@ -572,7 +584,7 @@ def _get_branches(self):
local_path = get_local_path() / self.org / self.repo
output = _run(f"git -C {shlex.quote(str(local_path))} show-ref --heads").split("\n")
else:
cmd = f"git ls-remote --heads https://github.com/{self.org}/{self.repo}"
cmd = f"git ls-remote --heads {self.origin}"
try:
with _spawn(cmd, timeout=3) as child:
output = child.read().strip().split("\r\n")
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@
python_requires=">= 3.6",
packages=["lib50"],
url="https://github.com/cs50/lib50",
version="2.0.7",
version="2.0.8",
include_package_data=True
)

0 comments on commit f563b44

Please sign in to comment.