#!/usr/bin/env python3
# Written by retoor@molodetz.nl
# This script checks for the current status of Git repositories within a specified directory, identifying which repositories have uncommitted changes ("dirty" status).
# Imports:
# - pathlib: to handle and manipulate filesystem paths
# - subprocess: to run shell commands from within Python
# - sys: to access command-line arguments
# MIT License
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import pathlib
import subprocess
import sys
def is_git_repo(path):
return pathlib.Path(path).joinpath(".git").exists()
def is_dirty(path):
if not is_git_repo(path):
return None
command = ['bash', '-c', f'cd \'{path}\' && git status']
output = subprocess.check_output(command).decode()
if 'tree clean' in output:
return False
if 'Changes not' in output:
return True
if 'not a git':
return None
print("HOE DAN")
return output
try:
src_path = sys.argv[1]
except IndexError:
src_path = "."
dirty_count = 0
for folder in pathlib.Path(src_path).glob("*"):
if not folder.is_dir():
continue
if is_dirty(folder.absolute()):
dirty_count += 1
print(f'{dirty_count}\t',folder.name, "is dirty!")
print(f"Found {dirty_count} dirty repositories.")