Skip to content

Commit

Permalink
Updates to handling default or non-existing branch
Browse files Browse the repository at this point in the history
- removed requirements.txt - misguided moment!
- added exception handling to subprocess.run
- added tests for exception handling
- added tests for checking that branch does exist
  • Loading branch information
sean-morris committed Jun 3, 2021
1 parent 79dd180 commit 8423af4
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 35 deletions.
72 changes: 42 additions & 30 deletions nbgitpuller/pull.py
Expand Up @@ -87,42 +87,54 @@ def branch_exists(self, branch):
This checks to make sure the branch we are told to access
exists in the repo
"""
p_heads = subprocess.run(
["git", "ls-remote", "--heads", self.git_url],
capture_output=True,
text=True,
)
p_tags = subprocess.run(
["git", "ls-remote", "--tags", self.git_url],
capture_output=True,
text=True,
)
lines = p_heads.stdout.splitlines() + p_tags.stdout.splitlines()
branches = []
for line in lines:
_, ref = line.split()
refs, heads, branch_name = ref.split("/", 2)
branches.append(branch_name)
return branch in branches
try:
heads = subprocess.run(
["git", "ls-remote", "--heads", self.git_url],
capture_output=True,
text=True,
check=True
)
tags = subprocess.run(
["git", "ls-remote", "--tags", self.git_url],
capture_output=True,
text=True,
check=True
)
lines = heads.stdout.splitlines() + tags.stdout.splitlines()
branches = []
for line in lines:
_, ref = line.split()
refs, heads, branch_name = ref.split("/", 2)
branches.append(branch_name)
return branch in branches
except subprocess.CalledProcessError:
m = f"Problem accessing list of branches and/or tags: {self.git_url}"
logging.exception(m)
raise ValueError(m)

def resolve_default_branch(self):
"""
This will resolve the default branch of the repo in
the case where the branch given does not exist
"""
p = subprocess.run(
["git", "ls-remote", "--symref", self.git_url, "HEAD"],
capture_output=True,
text=True,
)

for line in p.stdout.splitlines():
if line.startswith("ref:"):
# line resembles --> ref: refs/heads/main HEAD
_, ref, head = line.split()
refs, heads, branch_name = ref.split("/", 2)
return branch_name
raise ValueError(f"default branch not found in {self.git_url}")
try:
head_branch = subprocess.run(
["git", "ls-remote", "--symref", self.git_url, "HEAD"],
capture_output=True,
text=True,
check=True
)
for line in head_branch.stdout.splitlines():
if line.startswith("ref:"):
# line resembles --> ref: refs/heads/main HEAD
_, ref, head = line.split()
refs, heads, branch_name = ref.split("/", 2)
return branch_name
raise ValueError(f"default branch not found in {self.git_url}")
except subprocess.CalledProcessError:
m = f"Problem accessing HEAD branch: {self.git_url}"
logging.exception(m)
raise ValueError(m)

def pull(self):
"""
Expand Down
3 changes: 0 additions & 3 deletions requirements.txt

This file was deleted.

31 changes: 29 additions & 2 deletions tests/test_gitpuller.py
Expand Up @@ -97,19 +97,46 @@ def test_initialize():


def test_branch_exists():
with Remote() as remote, Pusher(remote) as pusher:
with Remote() as remote, Pusher(remote) as pusher:
pusher.push_file('README.md', '1')
with Puller(remote, 'puller') as puller:
assert not puller.gp.branch_exists("wrong")
assert puller.gp.branch_exists("master")


def test_exception_branch_exists():
with Remote() as remote, Pusher(remote) as pusher:
pusher.push_file('README.md', '1')
with Puller(remote, 'puller') as puller:
orig_url = puller.gp.git_url
puller.gp.git_url = ""
try:
puller.gp.branch_exists("wrong")
except Exception as e:
assert type(e) == ValueError
puller.gp.git_url = orig_url


def test_resolve_default_branch():
with Remote() as remote, Pusher(remote) as pusher:
with Remote() as remote, Pusher(remote) as pusher:
pusher.push_file('README.md', '1')
with Puller(remote, 'puller') as puller:
assert puller.gp.resolve_default_branch() == "master"


def test_exception_resolve_default_branch():
with Remote() as remote, Pusher(remote) as pusher:
pusher.push_file('README.md', '1')
with Puller(remote, 'puller') as puller:
orig_url = puller.gp.git_url
puller.gp.git_url = ""
try:
puller.gp.resolve_default_branch()
except Exception as e:
assert type(e) == ValueError
puller.gp.git_url = orig_url


def test_simple_push_pull():
"""
Test the 'happy path' push/pull interaction
Expand Down

0 comments on commit 8423af4

Please sign in to comment.