Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Reduce to docs-only change per maintainer review
Per @Byron's feedback: dropping the is_shallow property, since a
per-commit property re-reads the shallow file on every call, which
is IO-heavy when checking many commits. Keeping just the stats()
docstring note about the limitation for a quick merge. Happy to
follow up with a Repo-level implementation (e.g. returning the set
of shallow-boundary hashes) as a separate PR if useful.
  • Loading branch information
harshitayadavv committed Jul 12, 2026
commit 2f49a66fe4dc52b742002cb88d7c254ffede589b
28 changes: 3 additions & 25 deletions git/objects/commit.py
Original file line number Diff line number Diff line change
Expand Up @@ -369,37 +369,15 @@ def iter_parents(self, paths: Union[PathLike, Sequence[PathLike]] = "", **kwargs

return self.iter_items(self.repo, self, paths, **kwargs)

@property
def is_shallow(self) -> bool:
"""Check whether this commit is a shallow boundary (graft) commit.

A commit at the boundary of a shallow clone appears to have no
parents from Git's perspective, even though the underlying commit
object still references a parent SHA that was never fetched. Calling
:attr:`stats` (or anything else that diffs against the parent) on
such a commit will raise :exc:`~git.exc.GitCommandError` because the
parent object does not exist locally.

:return:
True if this commit's hexsha appears in the repository's
``shallow`` file, False otherwise (including for non-shallow
repositories).
"""
shallow_file = os.path.join(self.repo.git_dir, "shallow")
if not os.path.isfile(shallow_file):
return False
with open(shallow_file, "r") as f:
return self.hexsha in f.read().split()

@property
def stats(self) -> Stats:
"""Create a git stat from changes between this commit and its first parent
or from all changes done if this is the very first commit.

:note:
If this commit is at the boundary of a shallow clone (see
:attr:`is_shallow`), this will raise :exc:`~git.exc.GitCommandError`
because the parent object was never fetched.
If this commit is at the boundary of a shallow clone, this will
raise :exc:`~git.exc.GitCommandError`, since the parent object
was never fetched and only exists as a reference on this commit.

:return:
:class:`Stats`
Expand Down
19 changes: 0 additions & 19 deletions test/test_commit.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,25 +164,6 @@ def check_entries(d, has_change_type=False):
self.assertEqual(commit.committer_tz_offset, 14400, commit.committer_tz_offset)
self.assertEqual(commit.message, "initial project\n")

@with_rw_directory
def test_is_shallow(self, rw_dir):
"""A commit at the shallow boundary should report is_shallow, and
accessing its stats should raise GitCommandError."""
full_repo = self.rorepo
shallow_path = osp.join(rw_dir, "shallow_clone")
shallow_repo = Repo.clone_from(full_repo.git_dir, shallow_path, depth=2, no_local=True)

commits = list(shallow_repo.iter_commits())
boundary_commit = commits[-1]

self.assertTrue(boundary_commit.is_shallow)
with self.assertRaises(GitCommandError):
boundary_commit.stats

if len(commits) > 1:
non_boundary_commit = commits[0]
self.assertFalse(non_boundary_commit.is_shallow)

def test_renames(self):
commit = self.rorepo.commit("185d847ec7647fd2642a82d9205fb3d07ea71715")
files = commit.stats.files
Expand Down
Loading