Skip to content

Commit e479436

Browse files
committed
fix: 'vcs.get_changed_files' with null base revision
This case can happen when you push a new branch to Github. Issue: #864
1 parent 38dac7f commit e479436

File tree

2 files changed

+42
-3
lines changed

2 files changed

+42
-3
lines changed

src/taskgraph/util/vcs.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,11 @@ def get_changed_files(self, diff_filter=None, mode=None, rev=None, base=None):
505505
cmd.append("--cached")
506506
elif mode == "all":
507507
cmd.append("HEAD")
508+
elif base == self.NULL_REVISION:
509+
# When base is NULL_REVISION (e.g new branches on Github), diff
510+
# from the empty tree to show all files present at rev. Use Git's
511+
# well-known empty tree hash.
512+
cmd = ["diff", "4b825dc642cb6eb9a060e54bf8d69288fbee4904", rev]
508513
elif self.is_shallow:
509514
# In shallow clones, `git log` won't have the history necessary to
510515
# determine the files changed. Using `git diff` finds the

test/test_util_vcs.py

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -597,9 +597,6 @@ def test_get_changed_files_shallow_clone(git_repo, tmp_path, default_git_branch)
597597

598598

599599
def test_get_changed_files_with_null_base_revision(repo):
600-
if repo.tool == "git":
601-
pytest.xfail()
602-
603600
second_file = os.path.join(repo.path, "second_file")
604601
with open(second_file, "w") as f:
605602
f.write("second file content")
@@ -614,3 +611,40 @@ def test_get_changed_files_with_null_base_revision(repo):
614611
)
615612

616613
assert isinstance(changed_files, list)
614+
# When base is NULL_REVISION, we should get all files from the beginning
615+
# of history up to head_rev. This includes both the initial "first_file"
616+
# and the newly added "second_file".
617+
assert "first_file" in changed_files
618+
assert "second_file" in changed_files
619+
620+
621+
def test_get_changed_files_with_null_base_revision_shallow_clone(
622+
git_repo, tmp_path, default_git_branch
623+
):
624+
tmp_repo = Path(git_repo)
625+
626+
(tmp_repo / "file1.txt").write_text("content 1")
627+
(tmp_repo / "file2.txt").write_text("content 2")
628+
subprocess.check_call(["git", "add", "."], cwd=tmp_repo)
629+
subprocess.check_call(["git", "commit", "-m", "Add files"], cwd=tmp_repo)
630+
631+
commit_hash = subprocess.check_output(
632+
["git", "rev-parse", "HEAD"], cwd=tmp_repo, text=True
633+
).strip()
634+
635+
shallow_path = tmp_path / "shallow_null_test"
636+
subprocess.check_call(
637+
["git", "clone", "--depth=1", f"file://{tmp_repo}", str(shallow_path)],
638+
cwd=tmp_path,
639+
)
640+
641+
shallow_repo = get_repository(str(shallow_path))
642+
assert shallow_repo.is_shallow
643+
644+
changed_files = shallow_repo.get_changed_files(
645+
"AMD", "all", rev=commit_hash, base=Repository.NULL_REVISION
646+
)
647+
648+
assert "first_file" in changed_files
649+
assert "file1.txt" in changed_files
650+
assert "file2.txt" in changed_files

0 commit comments

Comments
 (0)