|
3 | 3 | # file, You can obtain one at http://mozilla.org/MPL/2.0/. |
4 | 4 |
|
5 | 5 | import os |
| 6 | +import shutil |
6 | 7 | import subprocess |
7 | 8 | from pathlib import Path |
8 | 9 | from textwrap import dedent |
@@ -495,6 +496,58 @@ def test_find_latest_common_revision(repo_with_remote): |
495 | 496 | ) |
496 | 497 |
|
497 | 498 |
|
| 499 | +def test_find_latest_common_revision_shallow_clone( |
| 500 | + tmpdir, git_repo, default_git_branch |
| 501 | +): |
| 502 | + """Test finding common revision in a shallow clone that requires deepening.""" |
| 503 | + remote_path = str(tmpdir / "remote_repo") |
| 504 | + shutil.copytree(git_repo, remote_path) |
| 505 | + |
| 506 | + # Add several commits to the remote repository to create depth |
| 507 | + remote_repo = get_repository(remote_path) |
| 508 | + |
| 509 | + # Create multiple commits to establish depth |
| 510 | + for i in range(5): |
| 511 | + test_file = os.path.join(remote_path, f"file_{i}.txt") |
| 512 | + with open(test_file, "w") as f: |
| 513 | + f.write(f"content {i}") |
| 514 | + remote_repo.run("add", test_file) |
| 515 | + remote_repo.run("commit", "-m", f"Commit {i}") |
| 516 | + |
| 517 | + # Store the head revision of remote for comparison |
| 518 | + remote_head = remote_repo.head_rev |
| 519 | + |
| 520 | + # Create a shallow clone with depth 1 |
| 521 | + # Need to use file:// protocol for --depth to work with local repos |
| 522 | + shallow_clone_path = str(tmpdir / "shallow_clone") |
| 523 | + subprocess.check_call( |
| 524 | + ["git", "clone", "--depth", "1", f"file://{remote_path}", shallow_clone_path] |
| 525 | + ) |
| 526 | + |
| 527 | + shallow_repo = get_repository(shallow_clone_path) |
| 528 | + assert shallow_repo.is_shallow |
| 529 | + |
| 530 | + remote_name = "origin" |
| 531 | + |
| 532 | + # Create a new commit in the shallow clone to diverge from remote |
| 533 | + new_file = os.path.join(shallow_clone_path, "local_file.txt") |
| 534 | + with open(new_file, "w") as f: |
| 535 | + f.write("local content") |
| 536 | + shallow_repo.run("add", new_file) |
| 537 | + shallow_repo.run("commit", "-m", "Local commit") |
| 538 | + |
| 539 | + # Now try to find the common revision - this should trigger deepening |
| 540 | + # because the shallow clone doesn't have enough history |
| 541 | + base_ref = f"{remote_name}/{default_git_branch}" |
| 542 | + result = shallow_repo.find_latest_common_revision(base_ref, shallow_repo.head_rev) |
| 543 | + |
| 544 | + # The result should be the remote's head (the common ancestor) |
| 545 | + assert result == remote_head |
| 546 | + |
| 547 | + # Verify the repository has been deepened |
| 548 | + assert shallow_repo.does_revision_exist_locally(result) |
| 549 | + |
| 550 | + |
498 | 551 | def test_does_revision_exist_locally(repo): |
499 | 552 | first_revision = repo.head_rev |
500 | 553 |
|
|
0 commit comments