|
| 1 | +import os |
| 2 | +import shutil |
| 3 | +import sys |
| 4 | +import subprocess |
| 5 | + |
| 6 | +def read_file_list(list_path): |
| 7 | + """ |
| 8 | + Reads a file containing file paths, ignoring empty lines and lines starting with '#'. |
| 9 | + Returns a list of relative file paths. |
| 10 | + """ |
| 11 | + with open(list_path, "r") as f: |
| 12 | + lines = [line.strip() for line in f] |
| 13 | + return [line for line in lines if line and not line.startswith("#")] |
| 14 | + |
| 15 | +def copy_files(file_list, dest_dir): |
| 16 | + """ |
| 17 | + Copy files listed in file_list to dest_dir, preserving their relative paths. |
| 18 | +
|
| 19 | + :param file_list: List of file paths (relative to current working directory) |
| 20 | + :param dest_dir: Destination directory where files will be copied |
| 21 | + """ |
| 22 | + for rel_path in file_list: |
| 23 | + abs_src = os.path.abspath(rel_path) |
| 24 | + abs_dest = os.path.abspath(os.path.join(dest_dir, rel_path)) |
| 25 | + os.makedirs(os.path.dirname(abs_dest), exist_ok=True) |
| 26 | + shutil.copy2(abs_src, abs_dest) |
| 27 | + print(f"Copied {abs_src} -> {abs_dest}") |
| 28 | + |
| 29 | +def ensure_git_repo(dest_dir): |
| 30 | + """ |
| 31 | + Initializes a git repository in dest_dir if it's not already a git repo. |
| 32 | + Sets the main branch to 'main'. |
| 33 | + """ |
| 34 | + git_dir = os.path.join(dest_dir, ".git") |
| 35 | + if not os.path.isdir(git_dir): |
| 36 | + try: |
| 37 | + subprocess.run(["git", "init", "-b", "main"], cwd=dest_dir, check=True) |
| 38 | + print(f"Initialized new git repository in {dest_dir} with 'main' as the default branch") |
| 39 | + except subprocess.CalledProcessError as e: |
| 40 | + print(f"Failed to initialize git repository in {dest_dir}: {e}") |
| 41 | + sys.exit(1) |
| 42 | + else: |
| 43 | + # Ensure main branch exists and is checked out |
| 44 | + try: |
| 45 | + branches = subprocess.check_output(["git", "branch"], cwd=dest_dir, text=True) |
| 46 | + if "main" not in branches: |
| 47 | + subprocess.run(["git", "checkout", "-b", "main"], cwd=dest_dir, check=True) |
| 48 | + print("Created and switched to 'main' branch.") |
| 49 | + else: |
| 50 | + subprocess.run(["git", "checkout", "main"], cwd=dest_dir, check=True) |
| 51 | + print("Switched to 'main' branch.") |
| 52 | + except subprocess.CalledProcessError as e: |
| 53 | + print(f"Failed to ensure 'main' branch in {dest_dir}: {e}") |
| 54 | + sys.exit(1) |
| 55 | + |
| 56 | +def git_add_files(file_list, dest_dir): |
| 57 | + """ |
| 58 | + Runs 'git add' on each file in file_list within dest_dir. |
| 59 | + """ |
| 60 | + cwd = os.getcwd() |
| 61 | + os.chdir(dest_dir) |
| 62 | + try: |
| 63 | + for rel_path in file_list: |
| 64 | + try: |
| 65 | + subprocess.run(["git", "add", "-f", rel_path], check=True) |
| 66 | + print(f"git add {rel_path}") |
| 67 | + except subprocess.CalledProcessError as e: |
| 68 | + print(f"Failed to git add {rel_path}: {e}") |
| 69 | + finally: |
| 70 | + os.chdir(cwd) |
| 71 | + |
| 72 | +if __name__ == "__main__": |
| 73 | + if len(sys.argv) != 3: |
| 74 | + print("Usage: python copy_files.py <file_list.txt> <dest_dir>") |
| 75 | + sys.exit(1) |
| 76 | + file_list_path = sys.argv[1] |
| 77 | + dest_dir = sys.argv[2] |
| 78 | + file_list = read_file_list(file_list_path) |
| 79 | + copy_files(file_list, dest_dir) |
| 80 | + ensure_git_repo(dest_dir) |
| 81 | + git_add_files(file_list, dest_dir) |
0 commit comments