Skip to content

Commit a8f8226

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

File tree

2 files changed

+201
-7
lines changed

2 files changed

+201
-7
lines changed

cycode/cli/files_collector/commit_range_documents.py

Lines changed: 26 additions & 7 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()
@@ -199,8 +207,7 @@ def parse_pre_receive_input() -> str:
199207
200208
:return: First branch update details (input's first line)
201209
"""
202-
# FIXME(MarshalX): this blocks main thread forever if called outside of pre-receive hook
203-
pre_receive_input = sys.stdin.read().strip()
210+
pre_receive_input = _read_hook_input_from_stdin()
204211
if not pre_receive_input:
205212
raise ValueError(
206213
'Pre receive input was not found. Make sure that you are using this command only in pre-receive hook'
@@ -222,7 +229,7 @@ def parse_pre_push_input() -> str:
222229
223230
:return: First, push update details (input's first line)
224231
""" # noqa: E501
225-
pre_push_input = sys.stdin.read().strip()
232+
pre_push_input = _read_hook_input_from_stdin()
226233
if not pre_push_input:
227234
raise ValueError(
228235
'Pre push input was not found. Make sure that you are using this command only in pre-push hook'
@@ -232,6 +239,18 @@ def parse_pre_push_input() -> str:
232239
return pre_push_input.splitlines()[0]
233240

234241

242+
def _read_hook_input_from_stdin() -> str:
243+
"""Read input from stdin when called from a hook.
244+
245+
If called manually from the command line, return an empty string so it doesn't block the main thread.
246+
247+
Returns:
248+
Input from stdin
249+
"""
250+
if sys.stdin.isatty():
251+
return ''
252+
return sys.stdin.read().strip()
253+
235254
def _get_default_branches_for_merge_base(repo: 'Repo') -> list[str]:
236255
"""Get a list of default branches to try for merge base calculation.
237256

tests/cli/files_collector/test_commit_range_documents.py

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,21 @@
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

24+
DUMMY_SHA_0 = '0' * 40
25+
DUMMY_SHA_1 = '1' * 40
26+
DUMMY_SHA_2 = '2' * 40
27+
DUMMY_SHA_A = 'a' * 40
28+
DUMMY_SHA_B = 'b' * 40
29+
DUMMY_SHA_C = 'c' * 40
2230

2331
@contextmanager
2432
def git_repository(path: str) -> Generator[Repo, None, None]:
@@ -871,3 +879,170 @@ def test_single_commit_spec(self) -> None:
871879

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

0 commit comments

Comments
 (0)