From c0008fc038810d56f5ffe68ee2ebf7f57eb3e2d8 Mon Sep 17 00:00:00 2001 From: neingeist Date: Thu, 19 Mar 2020 16:40:14 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20git-status-all:=20convert=20to?= =?UTF-8?q?=20python=20to=20fix=20finding=20git=20work=20trees?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- git-status-all | 55 ++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 42 insertions(+), 13 deletions(-) diff --git a/git-status-all b/git-status-all index d43f9ec..cb72c39 100755 --- a/git-status-all +++ b/git-status-all @@ -1,13 +1,42 @@ -#!/bin/sh -# Show dirty git repos - -for d in */; do - ( - cd $d - status=`git status -s 2>/dev/null` - if [ $? -ne 0 -o -n "$status" ]; then - echo "== $d" - git status -s - fi - ) -done +#!/usr/bin/env python +"""show dirty git repos""" + +from __future__ import division, print_function + +from colorama import Fore + +import contextlib +import os +import subprocess + + +def git_directories(startdir): + for dirpath, dirnames, _ in os.walk(startdir): + if '.sync' in dirpath: + continue + if '.git/modules' in dirpath: + # FIXME + continue + if set(['info', 'objects', 'refs']).issubset(set(dirnames)): + yield dirpath + + +@contextlib.contextmanager +def working_directory(directory): + saved_cwd = os.getcwd() + os.chdir(directory) + yield + os.chdir(saved_cwd) + + +for git_directory in git_directories('.'): + work_tree_directory = git_directory[:-4] + with working_directory(work_tree_directory): + try: + out = subprocess.check_output(['git', 'status', '-s'], stderr=subprocess.STDOUT) + if len(out) > 0: + print('== {}\n{}'.format(work_tree_directory, out.decode('utf-8'))) + except subprocess.CalledProcessError as e: + print((Fore.RED + 'git status is unhappy with {}' + Fore.RESET) + .format(work_tree_directory)) + print(e.output)