|  | 
|  | 1 | +#!/usr/bin/env python3 | 
|  | 2 | + | 
|  | 3 | +import logging | 
|  | 4 | +import os | 
|  | 5 | +import pathlib | 
|  | 6 | + | 
|  | 7 | +from ..common import normalize_file_path | 
|  | 8 | +from ..git import commit_changes, get_repository_root | 
|  | 9 | +from ..shell import run_command | 
|  | 10 | + | 
|  | 11 | +__all__ = [ | 
|  | 12 | +    "mv_file", | 
|  | 13 | +] | 
|  | 14 | + | 
|  | 15 | + | 
|  | 16 | +async def mv_file( | 
|  | 17 | +    source_path: str, | 
|  | 18 | +    target_path: str, | 
|  | 19 | +    description: str, | 
|  | 20 | +    chat_id: str = "", | 
|  | 21 | +) -> str: | 
|  | 22 | +    """Move a file using git mv. | 
|  | 23 | +
 | 
|  | 24 | +    Args: | 
|  | 25 | +        source_path: The path to the source file to move (can be absolute or relative to repository root) | 
|  | 26 | +        target_path: The path to the target location (can be absolute or relative to repository root) | 
|  | 27 | +        description: Short description of why the file is being moved | 
|  | 28 | +        chat_id: The unique ID of the current chat session | 
|  | 29 | +
 | 
|  | 30 | +    Returns: | 
|  | 31 | +        A string containing the result of the move operation | 
|  | 32 | +    """ | 
|  | 33 | +    # Use the directory from the path as our starting point for source | 
|  | 34 | +    source_path = normalize_file_path(source_path) | 
|  | 35 | +    source_dir_path = ( | 
|  | 36 | +        os.path.dirname(source_path) if os.path.dirname(source_path) else "." | 
|  | 37 | +    ) | 
|  | 38 | + | 
|  | 39 | +    # Normalize target path as well | 
|  | 40 | +    target_path = normalize_file_path(target_path) | 
|  | 41 | + | 
|  | 42 | +    # Validations for source file | 
|  | 43 | +    if not os.path.exists(source_path): | 
|  | 44 | +        raise FileNotFoundError(f"Source file does not exist: {source_path}") | 
|  | 45 | + | 
|  | 46 | +    if not os.path.isfile(source_path): | 
|  | 47 | +        raise ValueError(f"Source path is not a file: {source_path}") | 
|  | 48 | + | 
|  | 49 | +    # Get git repository root | 
|  | 50 | +    git_root = await get_repository_root(source_dir_path) | 
|  | 51 | +    # Ensure paths are absolute and resolve any symlinks | 
|  | 52 | +    source_path_resolved = os.path.realpath(source_path) | 
|  | 53 | +    git_root_resolved = os.path.realpath(git_root) | 
|  | 54 | +    target_path_resolved = ( | 
|  | 55 | +        os.path.realpath(target_path) | 
|  | 56 | +        if os.path.exists(os.path.dirname(target_path)) | 
|  | 57 | +        else target_path | 
|  | 58 | +    ) | 
|  | 59 | + | 
|  | 60 | +    # Use pathlib to check if the source file is within the git repo | 
|  | 61 | +    # This handles path traversal correctly on all platforms | 
|  | 62 | +    try: | 
|  | 63 | +        # Convert to Path objects | 
|  | 64 | +        source_path_obj = pathlib.Path(source_path_resolved) | 
|  | 65 | +        git_root_obj = pathlib.Path(git_root_resolved) | 
|  | 66 | + | 
|  | 67 | +        # Check if file is inside the git repo using Path.relative_to | 
|  | 68 | +        # This will raise ValueError if source_path is not inside git_root | 
|  | 69 | +        source_path_obj.relative_to(git_root_obj) | 
|  | 70 | +    except ValueError: | 
|  | 71 | +        msg = ( | 
|  | 72 | +            f"Source path {source_path} is not within the git repository at {git_root}" | 
|  | 73 | +        ) | 
|  | 74 | +        logging.error(msg) | 
|  | 75 | +        raise ValueError(msg) | 
|  | 76 | + | 
|  | 77 | +    # Check if target directory exists and is within the git repo | 
|  | 78 | +    target_dir = os.path.dirname(target_path) | 
|  | 79 | +    if target_dir and not os.path.exists(target_dir): | 
|  | 80 | +        raise FileNotFoundError(f"Target directory does not exist: {target_dir}") | 
|  | 81 | + | 
|  | 82 | +    try: | 
|  | 83 | +        # Convert to Path objects | 
|  | 84 | +        target_dir_obj = pathlib.Path( | 
|  | 85 | +            os.path.realpath(target_dir) if target_dir else git_root_resolved | 
|  | 86 | +        ) | 
|  | 87 | +        # Check if target directory is inside the git repo | 
|  | 88 | +        target_dir_obj.relative_to(git_root_obj) | 
|  | 89 | +    except ValueError: | 
|  | 90 | +        msg = f"Target directory {target_dir} is not within the git repository at {git_root}" | 
|  | 91 | +        logging.error(msg) | 
|  | 92 | +        raise ValueError(msg) | 
|  | 93 | + | 
|  | 94 | +    # Get the relative paths using pathlib | 
|  | 95 | +    source_rel_path = os.path.relpath(source_path_resolved, git_root_resolved) | 
|  | 96 | +    target_rel_path = os.path.relpath( | 
|  | 97 | +        target_path_resolved | 
|  | 98 | +        if os.path.exists(os.path.dirname(target_path)) | 
|  | 99 | +        else os.path.join(git_root_resolved, os.path.basename(target_path)), | 
|  | 100 | +        git_root_resolved, | 
|  | 101 | +    ) | 
|  | 102 | + | 
|  | 103 | +    logging.info(f"Using relative paths: {source_rel_path} -> {target_rel_path}") | 
|  | 104 | + | 
|  | 105 | +    # Check if the source file is tracked by git from the git root | 
|  | 106 | +    await run_command( | 
|  | 107 | +        ["git", "ls-files", "--error-unmatch", source_rel_path], | 
|  | 108 | +        cwd=git_root_resolved, | 
|  | 109 | +        check=True, | 
|  | 110 | +        capture_output=True, | 
|  | 111 | +        text=True, | 
|  | 112 | +    ) | 
|  | 113 | + | 
|  | 114 | +    # If we get here, the file is tracked by git, so we can move it | 
|  | 115 | +    await run_command( | 
|  | 116 | +        ["git", "mv", source_rel_path, target_rel_path], | 
|  | 117 | +        cwd=git_root_resolved, | 
|  | 118 | +        check=True, | 
|  | 119 | +        capture_output=True, | 
|  | 120 | +        text=True, | 
|  | 121 | +    ) | 
|  | 122 | + | 
|  | 123 | +    # Commit the changes | 
|  | 124 | +    logging.info(f"Committing move of file: {source_rel_path} -> {target_rel_path}") | 
|  | 125 | +    success, commit_message = await commit_changes( | 
|  | 126 | +        git_root_resolved, | 
|  | 127 | +        f"Move {source_rel_path} -> {target_rel_path}: {description}", | 
|  | 128 | +        chat_id, | 
|  | 129 | +        commit_all=False,  # No need for commit_all since git mv already stages the change | 
|  | 130 | +    ) | 
|  | 131 | + | 
|  | 132 | +    if success: | 
|  | 133 | +        return f"Successfully moved file from {source_rel_path} to {target_rel_path}." | 
|  | 134 | +    else: | 
|  | 135 | +        return f"File was moved from {source_rel_path} to {target_rel_path} but failed to commit: {commit_message}" | 
0 commit comments