Skip to content

Commit 46f7362

Browse files
committed
CM-55207: improve testing and cover some edge cases in commit pre-receive
1 parent aac1446 commit 46f7362

File tree

2 files changed

+185
-4
lines changed

2 files changed

+185
-4
lines changed

cycode/cli/files_collector/commit_range_documents.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,19 +104,27 @@ def collect_commit_range_diff_documents(
104104
return commit_documents_to_scan
105105

106106

107-
def calculate_pre_receive_commit_range(branch_update_details: str) -> Optional[str]:
107+
def calculate_pre_receive_commit_range(repo_path: str, branch_update_details: str) -> Optional[str]:
108108
end_commit = _get_end_commit_from_branch_update_details(branch_update_details)
109109

110110
# branch is deleted, no need to perform scan
111111
if end_commit == consts.EMPTY_COMMIT_SHA:
112112
return None
113113

114-
start_commit = _get_oldest_unupdated_commit_for_branch(end_commit)
114+
repo = git_proxy.get_repo(repo_path)
115+
start_commit = _get_oldest_unupdated_commit_for_branch(repo, end_commit)
115116

116117
# no new commit to update found
117118
if not start_commit:
118119
return None
119120

121+
# If the oldest not-yet-updated commit has no parent (root commit or orphaned history),
122+
# using '~1' will fail. In that case, scan from the end commit, which effectively
123+
# includes the entire history reachable from it (which is exactly what we need here).
124+
125+
if not bool(repo.commit(start_commit).parents):
126+
return f'{end_commit}'
127+
120128
return f'{start_commit}~1...{end_commit}'
121129

122130

@@ -126,10 +134,10 @@ def _get_end_commit_from_branch_update_details(update_details: str) -> str:
126134
return end_commit
127135

128136

129-
def _get_oldest_unupdated_commit_for_branch(commit: str) -> Optional[str]:
137+
def _get_oldest_unupdated_commit_for_branch(repo: 'Repo', commit: str) -> Optional[str]:
130138
# get a list of commits by chronological order that are not in the remote repository yet
131139
# more info about rev-list command: https://git-scm.com/docs/git-rev-list
132-
repo = git_proxy.get_repo(os.getcwd())
140+
133141
not_updated_commits = repo.git.rev_list(commit, '--topo-order', '--reverse', '--not', '--all')
134142

135143
commits = not_updated_commits.splitlines()

tests/cli/files_collector/test_commit_range_documents.py

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@
1212
from cycode.cli.files_collector.commit_range_documents import (
1313
_get_default_branches_for_merge_base,
1414
calculate_pre_push_commit_range,
15+
calculate_pre_receive_commit_range,
1516
get_diff_file_path,
1617
get_safe_head_reference_for_diff,
1718
parse_commit_range,
1819
parse_pre_push_input,
20+
parse_pre_receive_input,
1921
)
2022
from cycode.cli.utils.path_utils import get_path_by_os
2123

@@ -871,3 +873,174 @@ def test_single_commit_spec(self) -> None:
871873

872874
parsed_from, parsed_to = parse_commit_range(a, temp_dir)
873875
assert (parsed_from, parsed_to) == (a, c)
876+
877+
878+
class TestParsePreReceiveInput:
879+
"""Test the parse_pre_receive_input function with various pre-receive hook input scenarios."""
880+
881+
def test_parse_single_update_input(self) -> None:
882+
"""Test parsing a single branch update input."""
883+
pre_receive_input = '1111111111111111111111111111111111111111 2222222222222222222222222222222222222222 refs/heads/main'
884+
885+
with patch('sys.stdin', StringIO(pre_receive_input)):
886+
result = parse_pre_receive_input()
887+
assert result == pre_receive_input
888+
889+
def test_parse_multiple_update_input_returns_first_line(self) -> None:
890+
"""Test parsing multiple branch updates returns only the first line."""
891+
pre_receive_input = """0000000000000000000000000000000000000000 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa refs/heads/main
892+
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb cccccccccccccccccccccccccccccccccccccccc refs/heads/feature"""
893+
894+
with patch('sys.stdin', StringIO(pre_receive_input)):
895+
result = parse_pre_receive_input()
896+
assert result == '0000000000000000000000000000000000000000 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa refs/heads/main'
897+
898+
def test_parse_empty_input_raises_error(self) -> None:
899+
"""Test that empty input raises ValueError."""
900+
with patch('sys.stdin', StringIO('')), pytest.raises(ValueError, match='Pre receive input was not found'):
901+
parse_pre_receive_input()
902+
903+
def test_parse_whitespace_only_input_raises_error(self) -> None:
904+
"""Test that whitespace-only input raises ValueError."""
905+
with patch('sys.stdin', StringIO(' \n\t ')), pytest.raises(ValueError, match='Pre receive input was not found'):
906+
parse_pre_receive_input()
907+
908+
909+
class TestCalculatePreReceiveCommitRange:
910+
"""Test the calculate_pre_receive_commit_range function with representative scenarios."""
911+
912+
def test_branch_deletion_returns_none(self) -> None:
913+
"""When end commit is all zeros (deletion), no scan is needed."""
914+
update_details = f'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa {consts.EMPTY_COMMIT_SHA} refs/heads/feature'
915+
assert calculate_pre_receive_commit_range(os.getcwd(), update_details) is None
916+
917+
def test_no_new_commits_returns_none(self) -> None:
918+
"""When there are no commits not in remote, return None."""
919+
with tempfile.TemporaryDirectory() as server_dir:
920+
server_repo = Repo.init(server_dir, bare=True)
921+
try:
922+
with tempfile.TemporaryDirectory() as work_dir:
923+
work_repo = Repo.init(work_dir, b='main')
924+
try:
925+
# Create a single commit and push it to the server as main (end commit is already on a ref)
926+
test_file = os.path.join(work_dir, 'file.txt')
927+
with open(test_file, 'w') as f:
928+
f.write('base')
929+
work_repo.index.add(['file.txt'])
930+
end_commit = work_repo.index.commit('initial')
931+
932+
work_repo.create_remote('origin', server_dir)
933+
work_repo.remotes.origin.push('main:main')
934+
935+
update_details = f'{"a"*40} {end_commit.hexsha} refs/heads/main'
936+
assert calculate_pre_receive_commit_range(server_dir, update_details) is None
937+
finally:
938+
work_repo.close()
939+
finally:
940+
server_repo.close()
941+
942+
def test_returns_triple_dot_range_from_oldest_unupdated(self) -> None:
943+
"""Returns '<oldest>~1...<end>' when there are new commits to scan."""
944+
with tempfile.TemporaryDirectory() as server_dir:
945+
server_repo = Repo.init(server_dir, bare=True)
946+
try:
947+
with tempfile.TemporaryDirectory() as work_dir:
948+
work_repo = Repo.init(work_dir, b='main')
949+
try:
950+
# Create commit A and push it to server as main (server has A on a ref)
951+
a_path = os.path.join(work_dir, 'a.txt')
952+
with open(a_path, 'w') as f:
953+
f.write('A')
954+
work_repo.index.add(['a.txt'])
955+
a_commit = work_repo.index.commit('A')
956+
957+
work_repo.create_remote('origin', server_dir)
958+
work_repo.remotes.origin.push('main:main')
959+
960+
# Create commits B and C locally (not yet on server ref)
961+
b_path = os.path.join(work_dir, 'b.txt')
962+
with open(b_path, 'w') as f:
963+
f.write('B')
964+
work_repo.index.add(['b.txt'])
965+
b_commit = work_repo.index.commit('B')
966+
967+
c_path = os.path.join(work_dir, 'c.txt')
968+
with open(c_path, 'w') as f:
969+
f.write('C')
970+
work_repo.index.add(['c.txt'])
971+
end_commit = work_repo.index.commit('C')
972+
973+
# Push the objects to a temporary ref and then delete that ref on server,
974+
# so the objects exist but are not reachable from any ref.
975+
work_repo.remotes.origin.push(f'{end_commit.hexsha}:refs/tmp/hold')
976+
Repo(server_dir).git.update_ref('-d', 'refs/tmp/hold')
977+
978+
update_details = f'{"a"*40} {end_commit.hexsha} refs/heads/main'
979+
result = calculate_pre_receive_commit_range(server_dir, update_details)
980+
assert result == f'{b_commit.hexsha}~1...{end_commit.hexsha}'
981+
finally:
982+
work_repo.close()
983+
finally:
984+
server_repo.close()
985+
986+
def test_initial_oldest_commit_without_parent_returns_single_commit_range(self) -> None:
987+
"""If oldest commit has no parent, avoid '~1' and scan from end commit only."""
988+
with tempfile.TemporaryDirectory() as server_dir:
989+
server_repo = Repo.init(server_dir, bare=True)
990+
try:
991+
with tempfile.TemporaryDirectory() as work_dir:
992+
work_repo = Repo.init(work_dir, b='main')
993+
try:
994+
# Create a single root commit locally
995+
p = os.path.join(work_dir, 'root.txt')
996+
with open(p, 'w') as f:
997+
f.write('root')
998+
work_repo.index.add(['root.txt'])
999+
end_commit = work_repo.index.commit('root')
1000+
1001+
work_repo.create_remote('origin', server_dir)
1002+
# Push objects to a temporary ref and delete it so server has objects but no refs
1003+
work_repo.remotes.origin.push(f'{end_commit.hexsha}:refs/tmp/hold')
1004+
Repo(server_dir).git.update_ref('-d', 'refs/tmp/hold')
1005+
1006+
update_details = f'{"a"*40} {end_commit.hexsha} refs/heads/main'
1007+
result = calculate_pre_receive_commit_range(server_dir, update_details)
1008+
assert result == end_commit.hexsha
1009+
finally:
1010+
work_repo.close()
1011+
finally:
1012+
server_repo.close()
1013+
1014+
def test_initial_oldest_commit_without_parent_with_two_commits_returns_single_commit_range(self) -> None:
1015+
"""If there are two new commits and the oldest has no parent, avoid '~1' and scan from end commit only."""
1016+
with tempfile.TemporaryDirectory() as server_dir:
1017+
server_repo = Repo.init(server_dir, bare=True)
1018+
try:
1019+
with tempfile.TemporaryDirectory() as work_dir:
1020+
work_repo = Repo.init(work_dir, b='main')
1021+
try:
1022+
# Create two commits locally: oldest has no parent, second on top
1023+
a_path = os.path.join(work_dir, 'a.txt')
1024+
with open(a_path, 'w') as f:
1025+
f.write('A')
1026+
work_repo.index.add(['a.txt'])
1027+
_a_commit = work_repo.index.commit('A')
1028+
1029+
d_path = os.path.join(work_dir, 'd.txt')
1030+
with open(d_path, 'w') as f:
1031+
f.write('D')
1032+
work_repo.index.add(['d.txt'])
1033+
end_commit = work_repo.index.commit('D')
1034+
1035+
work_repo.create_remote('origin', server_dir)
1036+
# Push objects to a temporary ref and delete it so server has objects but no refs
1037+
work_repo.remotes.origin.push(f'{end_commit.hexsha}:refs/tmp/hold')
1038+
Repo(server_dir).git.update_ref('-d', 'refs/tmp/hold')
1039+
1040+
update_details = f'{consts.EMPTY_COMMIT_SHA} {end_commit.hexsha} refs/heads/main'
1041+
result = calculate_pre_receive_commit_range(server_dir, update_details)
1042+
assert result == end_commit.hexsha
1043+
finally:
1044+
work_repo.close()
1045+
finally:
1046+
server_repo.close()

0 commit comments

Comments
 (0)