import osimport reimport sysimport subprocessimport shutilimport platformfrom common import get_git_root, find_submodule_paths, get_project_rootdef remove_dependency(pkg_path=None):"""Remove the specified submodule (if pkg_path is provided) or all submoduleswithin the current Git repository.Args:pkg_path (str, optional): Relative path of the submodule to remove. If None, all submodules will be removed.Raises:FileNotFoundError: If the .gitmodules file is not found in the repository root.ValueError: If the provided pkg_path is not a valid submodule or is not within the current Git repository."""try:repo_root = get_git_root()submodule_paths_rel = find_submodule_paths(repo_root)submodule_paths_abs = [os.path.abspath(os.path.join(repo_root, rel_path)) for rel_path in submodule_paths_rel]if not submodule_paths_abs:print("No submodules found in the current repository.")returngitmodules_path = os.path.join(repo_root, '.gitmodules')# Keep track of removed submodule relative pathsremoved_submodule_paths = []if pkg_path:# Make pkg_path absolute based on the current working directorypkg_path_abs = os.path.abspath(os.path.join(repo_root, pkg_path))# Check if pkg_path is within the current Git repositoryif not pkg_path_abs.startswith(repo_root):print(f"Error: '{pkg_path}' is not within the current Git repository.")sys.exit(1)# Remove a single submoduleif pkg_path_abs not in submodule_paths_abs:print(f"Error: '{pkg_path}' is not a valid submodule in the current repository.")sys.exit(1)# Verify that the provided pkg_path is a valid Git submodulesubmodule_git_file = os.path.join(pkg_path_abs, '.git')if not os.path.isfile(submodule_git_file):print(f"Error: '{pkg_path}' is not a valid Git submodule.")sys.exit(1)print(f"Removing submodule '{pkg_path}'...")subprocess.run(['git', 'submodule', 'deinit', '--force', pkg_path_abs])subprocess.run(['git', 'rm', '--cached', '-r', pkg_path_abs])shutil.rmtree(pkg_path_abs)# Calculate the relative path of the submodule from the repository rootpkg_path_rel = os.path.relpath(pkg_path_abs, start=repo_root)# Add the removed submodule's relative path to the listremoved_submodule_paths.append(pkg_path_rel)else:# Remove all submodulesprint("Removing all submodules...")for submodule_path in submodule_paths_abs:submodule_git_file = os.path.join(submodule_path, '.git')if not os.path.isfile(submodule_git_file):print(f"Error: '{submodule_path}' is not a valid Git submodule.")sys.exit(1)subprocess.run(['git', 'submodule', 'deinit', '--force', submodule_path])subprocess.run(['git', 'rm', '--cached', '-r', submodule_path])shutil.rmtree(submodule_path)# Add the removed submodule's relative path to the listremoved_submodule_paths.append(os.path.relpath(submodule_path, start=repo_root))# Compute the relative path of the current Git repository to the top-level Git repositorycurrent_repo_to_top_level_rel = os.path.relpath(repo_root, start=get_project_root())# Iterate over the removed submodule relative pathsfor submodule_path_rel in removed_submodule_paths:# Compute the relative path of the submodule to the top-level Git repositorysubmodule_to_top_level_rel = os.path.join(current_repo_to_top_level_rel, submodule_path_rel)# Compute the absolute path of the submodule directory in the top-level Git repository's .git/modulestop_level_submodule_dir = os.path.join(get_project_root(), '.git', 'modules', submodule_to_top_level_rel)# Ensure the directory exists before attempting to delete itif os.path.isdir(top_level_submodule_dir):print(f"Deleting top-level submodule directory: {top_level_submodule_dir}")if platform.system() == 'Windows':subprocess.check_output(['cmd.exe', '/c', 'rmdir', '/s', '/q', top_level_submodule_dir])else:shutil.rmtree(top_level_submodule_dir)# Update .gitmodules content for all removed submoduleswith open(gitmodules_path, 'r') as f:content = f.read()for submodule_path_rel in removed_submodule_paths:pattern = r'^\[submodule\s+"{}"\](?:(?!^\[submodule\b).)*$'.format(submodule_path_rel)content = re.sub(pattern, '', content, flags=re.MULTILINE | re.DOTALL)with open(gitmodules_path, 'w') as f:print("Updating .gitmodules file in current git repository...")f.write(content)subprocess.run(['git', 'add', gitmodules_path])print("Removed submodule(s) successfully.")except Exception as e:print(f"Error: Failed to remove submodule(s): {e}")sys.exit(1)
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。