Skip to content

Commit 04dc06d

Browse files
alienczffcollonval
authored andcommitted
gitApi: add corner case handling for deleted remote branches (#400)
* gitApi: add corner case handling for deleted remote branches * compare git messages in lower * add unit test for get_upstream_branch corner case
1 parent f094a71 commit 04dc06d

File tree

2 files changed

+30
-7
lines changed

2 files changed

+30
-7
lines changed

jupyterlab_git/git.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -693,6 +693,8 @@ def get_upstream_branch(self, current_path, branch_name):
693693
return output.decode("utf-8").strip()
694694
elif "fatal: no upstream configured for branch" in error.decode("utf-8").lower():
695695
return None
696+
elif "unknown revision or path not in the working tree" in error.decode("utf-8").lower():
697+
return None
696698
else:
697699
raise Exception(
698700
"Error [{}] occurred while executing [{}] command to get upstream branch.".format(

jupyterlab_git/tests/test_branch.py

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -239,43 +239,64 @@ def test_get_upstream_branch_failure(mock_subproc_popen):
239239
process_mock = Mock(returncode=128)
240240
process_mock.communicate.side_effect = [
241241
('', "fatal: no such branch: 'blah'".encode('utf-8')),
242-
('', "fatal: no upstream configured for branch".encode('utf-8'))
242+
('', "fatal: no upstream configured for branch".encode('utf-8')),
243+
('', "fatal: ambiguous argument 'blah@origin': unknown revision or path not in the working tree.".encode('utf-8'))
243244
]
244245
mock_subproc_popen.return_value = process_mock
245246

246-
# When
247+
# When: fatal: no such branch: 'blah'
247248
with pytest.raises(Exception) as error:
248249
Git(root_dir='/bin').get_upstream_branch(
249250
current_path='test_curr_path', branch_name='blah')
250251

252+
# Then
253+
mock_subproc_popen.assert_has_calls([
254+
call(
255+
['git', 'rev-parse', '--abbrev-ref',
256+
'blah@{upstream}'],
257+
stdout=PIPE,
258+
stderr=PIPE,
259+
cwd='/bin/test_curr_path'
260+
),
261+
call().communicate()
262+
], any_order=False)
251263
assert "Error [fatal: no such branch: 'blah'] " \
252264
"occurred while executing [git rev-parse --abbrev-ref blah@{upstream}] command to get upstream branch." == str(
253265
error.value)
254266

267+
# When: fatal: no upstream configured for branch
255268
actual_response = Git(root_dir='/bin').get_upstream_branch(
256269
current_path='test_curr_path', branch_name='test')
257270

258-
assert None == actual_response
259-
260271
# Then
261272
mock_subproc_popen.assert_has_calls([
262273
call(
263274
['git', 'rev-parse', '--abbrev-ref',
264-
'blah@{upstream}'],
275+
'test@{upstream}'],
265276
stdout=PIPE,
266277
stderr=PIPE,
267278
cwd='/bin/test_curr_path'
268279
),
269-
call().communicate(),
280+
call().communicate()
281+
], any_order=False)
282+
assert None == actual_response
283+
284+
# When: "fatal: ambiguous argument 'blah@origin': unknown revision or path not in the working tree.
285+
actual_response = Git(root_dir='/bin').get_upstream_branch(
286+
current_path='test_curr_path', branch_name='blah')
287+
288+
# Then
289+
mock_subproc_popen.assert_has_calls([
270290
call(
271291
['git', 'rev-parse', '--abbrev-ref',
272-
'test@{upstream}'],
292+
'blah@{upstream}'],
273293
stdout=PIPE,
274294
stderr=PIPE,
275295
cwd='/bin/test_curr_path'
276296
),
277297
call().communicate()
278298
], any_order=False)
299+
assert None == actual_response
279300

280301

281302
@patch('subprocess.Popen')

0 commit comments

Comments
 (0)