Created
January 13, 2018 14:35
-
-
Save JonNorman/6bf0db4b5b24203a3b42abafac9da8a8 to your computer and use it in GitHub Desktop.
Find local Git branches that haven't got remotes
This file contains 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
import os | |
import subprocess | |
# only look in home and subdirectories | |
home_dir = os.path.expanduser('~') | |
git_repos = [root for root, dirs, _ in os.walk(home_dir) if '.git' in dirs ] | |
row_format = '{:30s} {:40s} ({})' | |
print(row_format.format('Repository', 'Branch', 'Path')) | |
def cd_run_report(directory, commands): | |
os.chdir(directory) | |
process = subprocess.Popen(commands, stdout=subprocess.PIPE) | |
return (process.communicate()) | |
for repo in git_repos: | |
# list all the local branches in the repository | |
commands = ['git', 'branch'] | |
out, err = cd_run_report(repo, commands) | |
if err: | |
print('Error: {}'.format(err)) | |
break; | |
# ignore first 2 chars (either whitespace or *) and ignore final empty string | |
branches = [ branch[2:].strip() for branch in out.split('\n') ][:-1] | |
# check each branch for its remote | |
for branch in branches: | |
commands = ['git', 'rev-parse', '--symbolic-full-name', branch + '@{u}'] | |
out, err = cd_run_report(repo, commands) | |
remote_branch = out.strip() | |
if not remote_branch: | |
print(row_format.format(repo.split(os.sep)[-1], branch, repo)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment