Skip to content

Instantly share code, notes, and snippets.

@cmpadden
Created June 10, 2024 18:32
Show Gist options
  • Select an option

  • Save cmpadden/c9ec0d38c54a3073f6f5aa9af53fbe2b to your computer and use it in GitHub Desktop.

Select an option

Save cmpadden/c9ec0d38c54a3073f6f5aa9af53fbe2b to your computer and use it in GitHub Desktop.
Extracts git origin remote URLs for all repositories in a project directory.
"""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