Skip to content

filechanges

Class info

Classes

Name Children Inherits
FileChange
githarbor.core.filechanges
Represents a file change in a diff.

    🛈 DocStrings

    FileChange dataclass

    Represents a file change in a diff.

    Source code in src/githarbor/core/filechanges.py
    @dataclass
    class FileChange:
        """Represents a file change in a diff."""
    
        path: str
        content: str | None  # None means file deletion
        mode: Literal["add", "modify", "delete"]
        old_path: str | None = None  # For renamed files
    

    parse_diff

    parse_diff(diff_str: str) -> list[FileChange]
    

    Parse a unified diff string into a list of file changes.

    Uses the unidiff library for robust diff parsing.

    Parameters:

    Name Type Description Default
    diff_str str

    Unified diff string

    required

    Returns:

    Type Description
    list[FileChange]

    List of FileChange objects

    Source code in src/githarbor/core/filechanges.py
    def parse_diff(diff_str: str) -> list[FileChange]:
        """Parse a unified diff string into a list of file changes.
    
        Uses the unidiff library for robust diff parsing.
    
        Args:
            diff_str: Unified diff string
    
        Returns:
            List of FileChange objects
        """
        import unidiff
    
        patch_set = unidiff.PatchSet(diff_str.splitlines(keepends=True))
        changes: list[FileChange] = []
    
        for patched_file in patch_set:
            if patched_file.is_rename:
                # Handle renamed files
                lines = [ln.value for hunk in patched_file for ln in hunk if not ln.is_removed]
                changes.append(
                    FileChange(
                        path=patched_file.path,
                        old_path=patched_file.source_file.removeprefix("a/"),
                        content="".join(lines) if lines else None,
                        mode="modify",
                    )
                )
                continue
    
            # Determine the change type
            if patched_file.is_added_file:
                mode = "add"
            elif patched_file.is_removed_file:
                mode = "delete"
            else:
                mode = "modify"
    
            # For deletions, we don't need content
            if mode == "delete":
                change = FileChange(path=patched_file.path, content=None, mode="delete")
                changes.append(change)
                continue
            # Reconstruct the final content
            lines = [ln.value for hunk in patched_file for ln in hunk if not ln.is_removed]
            change = FileChange(patched_file.path, content="".join(lines), mode=mode)
            changes.append(change)
    
        return changes