|
15 | 15 | get_diff_file_path, |
16 | 16 | get_safe_head_reference_for_diff, |
17 | 17 | parse_pre_push_input, |
| 18 | + parse_commit_range, |
18 | 19 | ) |
19 | 20 | from cycode.cli.utils.path_utils import get_path_by_os |
20 | 21 |
|
@@ -539,8 +540,12 @@ def test_calculate_range_for_new_branch_with_merge_base(self) -> None: |
539 | 540 | repo.index.add(['feature.py']) |
540 | 541 | feature_commit = repo.index.commit('Add feature') |
541 | 542 |
|
542 | | - # Switch back to master to simulate we're pushing a feature branch |
543 | | - repo.heads.master.checkout() |
| 543 | + # Switch back to the default branch to simulate pushing the feature branch |
| 544 | + # Prefer 'main' when present; fallback to 'master'. |
| 545 | + if any(h.name == 'main' for h in repo.heads): |
| 546 | + repo.heads.main.checkout() |
| 547 | + else: |
| 548 | + repo.heads.master.checkout() |
544 | 549 |
|
545 | 550 | # Test new branch push |
546 | 551 | push_details = f'refs/heads/feature {feature_commit.hexsha} refs/heads/feature {consts.EMPTY_COMMIT_SHA}' |
@@ -805,3 +810,67 @@ def test_simulate_pre_push_hook_input_format(self) -> None: |
805 | 810 | parts = push_input.split() |
806 | 811 | expected_range = f'{parts[3]}..{parts[1]}' |
807 | 812 | assert commit_range == expected_range |
| 813 | + |
| 814 | + |
| 815 | +class TestParseCommitRange: |
| 816 | + """Tests to validate unified parse_commit_range behavior matches git semantics.""" |
| 817 | + |
| 818 | + def _make_linear_history(self, repo: Repo, base_dir: str) -> tuple[str, str, str]: |
| 819 | + """Create three linear commits A -> B -> C and return their SHAs.""" |
| 820 | + a_file = os.path.join(base_dir, 'a.txt') |
| 821 | + with open(a_file, 'w') as f: |
| 822 | + f.write('A') |
| 823 | + repo.index.add(['a.txt']) |
| 824 | + a = repo.index.commit('A') |
| 825 | + |
| 826 | + with open(a_file, 'a') as f: |
| 827 | + f.write('B') |
| 828 | + repo.index.add(['a.txt']) |
| 829 | + b = repo.index.commit('B') |
| 830 | + |
| 831 | + with open(a_file, 'a') as f: |
| 832 | + f.write('C') |
| 833 | + repo.index.add(['a.txt']) |
| 834 | + c = repo.index.commit('C') |
| 835 | + |
| 836 | + return a.hexsha, b.hexsha, c.hexsha |
| 837 | + |
| 838 | + def test_two_dot_linear_history(self) -> None: |
| 839 | + """For 'A..C', expect (A,C) in linear history.""" |
| 840 | + with temporary_git_repository() as (temp_dir, repo): |
| 841 | + a, b, c = self._make_linear_history(repo, temp_dir) |
| 842 | + |
| 843 | + parsed_from, parsed_to = parse_commit_range(f'{a}..{c}', temp_dir) |
| 844 | + assert (parsed_from, parsed_to) == (a, c) |
| 845 | + |
| 846 | + def test_three_dot_linear_history(self) -> None: |
| 847 | + """For 'A...C' in linear history, expect (A,C).""" |
| 848 | + with temporary_git_repository() as (temp_dir, repo): |
| 849 | + a, b, c = self._make_linear_history(repo, temp_dir) |
| 850 | + |
| 851 | + parsed_from, parsed_to = parse_commit_range(f'{a}...{c}', temp_dir) |
| 852 | + assert (parsed_from, parsed_to) == (a, c) |
| 853 | + |
| 854 | + def test_open_right_linear_history(self) -> None: |
| 855 | + """For 'A..', expect (A,HEAD=C).""" |
| 856 | + with temporary_git_repository() as (temp_dir, repo): |
| 857 | + a, b, c = self._make_linear_history(repo, temp_dir) |
| 858 | + |
| 859 | + parsed_from, parsed_to = parse_commit_range(f'{a}..', temp_dir) |
| 860 | + assert (parsed_from, parsed_to) == (a, c) |
| 861 | + |
| 862 | + def test_open_left_linear_history(self) -> None: |
| 863 | + """For '..C' where HEAD==C, expect (HEAD=C,C).""" |
| 864 | + with temporary_git_repository() as (temp_dir, repo): |
| 865 | + a, b, c = self._make_linear_history(repo, temp_dir) |
| 866 | + |
| 867 | + parsed_from, parsed_to = parse_commit_range(f'..{c}', temp_dir) |
| 868 | + assert (parsed_from, parsed_to) == (c, c) |
| 869 | + |
| 870 | + def test_single_commit_spec(self) -> None: |
| 871 | + """For 'A', expect (A,HEAD=C).""" |
| 872 | + with temporary_git_repository() as (temp_dir, repo): |
| 873 | + a, b, c = self._make_linear_history(repo, temp_dir) |
| 874 | + |
| 875 | + parsed_from, parsed_to = parse_commit_range(a, temp_dir) |
| 876 | + assert (parsed_from, parsed_to) == (a, c) |
0 commit comments