Skip to content

tool

dandy.tool.git.tool

GitTool

Bases: BaseTool

Source code in dandy/tool/tool.py
5
6
7
8
9
def __init__(self) -> None:
    if not self.setup():
        print(f"Failed to setup {self.__class__.__name__}")

    self.__post_init__()

setup

Source code in dandy/tool/git/tool.py
def setup(self) -> bool:
    try:
        subprocess.run(
            ['git', '--version'],
            capture_output=True,
            text=True,
            check=True
        )

        return True

    except (subprocess.CalledProcessError, FileNotFoundError):
        return False

diff_file staticmethod

Source code in dandy/tool/git/tool.py
@staticmethod
def diff_file(file_path: Path | str) -> dict:
    file_path_str = str(file_path)

    try:
        result = subprocess.run(
            ['git', 'diff', file_path_str],
            capture_output=True,
            text=True,
            check=True
        )

        return {
            'file_path': file_path_str,
            'diff': result.stdout,
            'has_changes': bool(result.stdout.strip()),
            'error': None
        }

    except subprocess.CalledProcessError as e:
        return {
            'error': f'Git command failed: {e.stderr}',
            'file_path': file_path_str,
            'diff': None
        }

    except Exception as e:
        return {
            'error': f'Unexpected error: {str(e)}',
            'file_path': file_path_str,
            'diff': None
        }