Created
June 10, 2024 18:32
-
-
Save cmpadden/c9ec0d38c54a3073f6f5aa9af53fbe2b to your computer and use it in GitHub Desktop.
Extracts git origin remote URLs for all repositories in a project directory.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """Extracts git origin remote URLs for all git repositories in `TARGET_DIRECTORY`. | |
| This is useful if you would like to get a list of repositories from a project directory that you | |
| would like to clone elsewhere. | |
| """ | |
| import configparser | |
| import os | |
| from pathlib import Path | |
| TARGET_DIRECTORY = "~/src" | |
| top_level_directories = [ | |
| path | |
| for path in | |
| os.listdir(os.path.expanduser(TARGET_DIRECTORY)) | |
| if os.path.isdir(path) | |
| ] | |
| for top_level_directory in top_level_directories: | |
| git_config_path = Path(top_level_directory) / ".git" / "config" | |
| if not git_config_path.exists(): | |
| continue | |
| config = configparser.ConfigParser() | |
| _ = config.read(git_config_path) | |
| if config.has_section('remote "origin"'): | |
| remote_origin_url = config.get('remote "origin"', 'url') | |
| print(remote_origin_url) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment