Skip to content

Commit 8423af4

Browse files
committed
Updates to handling default or non-existing branch
- removed requirements.txt - misguided moment! - added exception handling to subprocess.run - added tests for exception handling - added tests for checking that branch does exist
1 parent 79dd180 commit 8423af4

File tree

3 files changed

+71
-35
lines changed

3 files changed

+71
-35
lines changed

nbgitpuller/pull.py

Lines changed: 42 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -87,42 +87,54 @@ def branch_exists(self, branch):
8787
This checks to make sure the branch we are told to access
8888
exists in the repo
8989
"""
90-
p_heads = subprocess.run(
91-
["git", "ls-remote", "--heads", self.git_url],
92-
capture_output=True,
93-
text=True,
94-
)
95-
p_tags = subprocess.run(
96-
["git", "ls-remote", "--tags", self.git_url],
97-
capture_output=True,
98-
text=True,
99-
)
100-
lines = p_heads.stdout.splitlines() + p_tags.stdout.splitlines()
101-
branches = []
102-
for line in lines:
103-
_, ref = line.split()
104-
refs, heads, branch_name = ref.split("/", 2)
105-
branches.append(branch_name)
106-
return branch in branches
90+
try:
91+
heads = subprocess.run(
92+
["git", "ls-remote", "--heads", self.git_url],
93+
capture_output=True,
94+
text=True,
95+
check=True
96+
)
97+
tags = subprocess.run(
98+
["git", "ls-remote", "--tags", self.git_url],
99+
capture_output=True,
100+
text=True,
101+
check=True
102+
)
103+
lines = heads.stdout.splitlines() + tags.stdout.splitlines()
104+
branches = []
105+
for line in lines:
106+
_, ref = line.split()
107+
refs, heads, branch_name = ref.split("/", 2)
108+
branches.append(branch_name)
109+
return branch in branches
110+
except subprocess.CalledProcessError:
111+
m = f"Problem accessing list of branches and/or tags: {self.git_url}"
112+
logging.exception(m)
113+
raise ValueError(m)
107114

108115
def resolve_default_branch(self):
109116
"""
110117
This will resolve the default branch of the repo in
111118
the case where the branch given does not exist
112119
"""
113-
p = subprocess.run(
114-
["git", "ls-remote", "--symref", self.git_url, "HEAD"],
115-
capture_output=True,
116-
text=True,
117-
)
118-
119-
for line in p.stdout.splitlines():
120-
if line.startswith("ref:"):
121-
# line resembles --> ref: refs/heads/main HEAD
122-
_, ref, head = line.split()
123-
refs, heads, branch_name = ref.split("/", 2)
124-
return branch_name
125-
raise ValueError(f"default branch not found in {self.git_url}")
120+
try:
121+
head_branch = subprocess.run(
122+
["git", "ls-remote", "--symref", self.git_url, "HEAD"],
123+
capture_output=True,
124+
text=True,
125+
check=True
126+
)
127+
for line in head_branch.stdout.splitlines():
128+
if line.startswith("ref:"):
129+
# line resembles --> ref: refs/heads/main HEAD
130+
_, ref, head = line.split()
131+
refs, heads, branch_name = ref.split("/", 2)
132+
return branch_name
133+
raise ValueError(f"default branch not found in {self.git_url}")
134+
except subprocess.CalledProcessError:
135+
m = f"Problem accessing HEAD branch: {self.git_url}"
136+
logging.exception(m)
137+
raise ValueError(m)
126138

127139
def pull(self):
128140
"""

requirements.txt

Lines changed: 0 additions & 3 deletions
This file was deleted.

tests/test_gitpuller.py

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,19 +97,46 @@ def test_initialize():
9797

9898

9999
def test_branch_exists():
100-
with Remote() as remote, Pusher(remote) as pusher:
100+
with Remote() as remote, Pusher(remote) as pusher:
101101
pusher.push_file('README.md', '1')
102102
with Puller(remote, 'puller') as puller:
103103
assert not puller.gp.branch_exists("wrong")
104+
assert puller.gp.branch_exists("master")
105+
106+
107+
def test_exception_branch_exists():
108+
with Remote() as remote, Pusher(remote) as pusher:
109+
pusher.push_file('README.md', '1')
110+
with Puller(remote, 'puller') as puller:
111+
orig_url = puller.gp.git_url
112+
puller.gp.git_url = ""
113+
try:
114+
puller.gp.branch_exists("wrong")
115+
except Exception as e:
116+
assert type(e) == ValueError
117+
puller.gp.git_url = orig_url
104118

105119

106120
def test_resolve_default_branch():
107-
with Remote() as remote, Pusher(remote) as pusher:
121+
with Remote() as remote, Pusher(remote) as pusher:
108122
pusher.push_file('README.md', '1')
109123
with Puller(remote, 'puller') as puller:
110124
assert puller.gp.resolve_default_branch() == "master"
111125

112126

127+
def test_exception_resolve_default_branch():
128+
with Remote() as remote, Pusher(remote) as pusher:
129+
pusher.push_file('README.md', '1')
130+
with Puller(remote, 'puller') as puller:
131+
orig_url = puller.gp.git_url
132+
puller.gp.git_url = ""
133+
try:
134+
puller.gp.resolve_default_branch()
135+
except Exception as e:
136+
assert type(e) == ValueError
137+
puller.gp.git_url = orig_url
138+
139+
113140
def test_simple_push_pull():
114141
"""
115142
Test the 'happy path' push/pull interaction

0 commit comments

Comments
 (0)