-
-
Save dbu/2843660 to your computer and use it in GitHub Desktop.
#!/usr/bin/php | |
<?php | |
$repos = array(); | |
exec('find -type d -name .git | sed -e "s/\.git//"', $repos); | |
foreach ($repos as $repo) { | |
$status = shell_exec("cd $repo && git status"); | |
if (false == strpos($status, 'nothing to commit (working directory clean)')) { | |
echo "$repo\n" . str_repeat('-', strlen($repo)) . "\n$status\n\n"; | |
} | |
} |
hm, i print the full change log if there is a change. i think its useful to see that to better see what happened.
How about:
find . -type d | while read dir ; git status -s $dir ; done
instead?
Update to the previous code:
find . -type d | while read dir ; do git status -s $dir ; done
yet another version:
find . -name '.git' -type d | while read dir ; do sh -c "cd $dir/../ && git status" ; done
one more:
find . -type d -name '.git' | while read dir ; do sh -c "cd $dir/../ && echo -e \"\nGIT STATUS IN ${dir//\.git/}\" && git status -s" ; done
git status -uall
Output a directory name only if something has changed in that git repo:
find . -type d -name '.git' | while read dir ; do sh -c "cd $dir/../ && git status -s | grep -q [azAZ09] && echo ---- ${dir//\.git/} ---- && git status -s" ; done
I use this one:
find . -type d -depth 1 | while read dir ; do echo -e "\n\033[1m${dir}\033[m"; git -C "${dir}" status -s ; done
Key is to use -C $dir
or you'll probably get fatal: Not a git repository (or any of the parent directories): .git
From git(1):
-C path
Run as if git was started in instead of the current working directory.
I also use this command to show me which Git has not "origin" remote:
find . -type d -depth 1 | while read dir ; do echo -e "\n\033[1m${dir}\033[m"; git -C "${dir}" remote show origin > /dev/null 2>&1 || echo -e "\033[0;31mNO REMOTE\033[0m"; git -C "${dir}" status -s ; done
Output looks like:
./SplashBuddy
./Send_Message_to_M
NO REMOTE
M .DS_Store
M "Send Message to M/Base.lproj/main.xib"
probably too late. but just adding so that person ending up here has one more choice
find . -name .git -type d -execdir git status -s ';'
#! /bin/sh
BLACK=`tput setaf 0`
RED=`tput setaf 1`
GREEN=`tput setaf 2`
YELLOW=`tput setaf 3`
BLUE=`tput setaf 4`
MAGENTA=`tput setaf 5`
CYAN=`tput setaf 6`
WHITE=`tput setaf 7`
BOLD=`tput bold`
RESET=`tput sgr0`
find . -name ".git" -type d | while read dir; do echo -e ${YELLOW}${BOLD}${dir/.git/}${RESET}; sh -c "cd ${dir/.git/} && git status"; done
git status -uall
The best!
based on:
changes:
.git
is actually often not a directory, but a file (in the case of sub-modules), thus I had to remove the-type d
part- modified the
grep ...
part to not exclude the parent repo, if it contains changes
features:
- fat dir name
- only show (sub-)repos with changes
- no sub-shells (for performance)
- works for non-dir
.git
sub-modules
one-liner:
find . -name '.git' | while read repo ; do repo=${repo//\.git/}; git -C "$repo" status -s | grep -q -v "^\$" && echo -e "\n\033[1m${repo}\033[m" && git -C "$repo" status -s || true; done
as bash script file with custom extra options for status:
#!/usr/bin/env bash
# Recursive `git status` (including sub-modules)
set -e
status_ops="$*"
find . -name '.git' \
| while read repo
do
repo=${repo//\.git/}
git -C "$repo" status -s \
| grep -q -v "^\$" \
&& echo -e "\n\033[1m${repo}\033[m" \
&& git -C "$repo" status $status_ops \
|| true
done
If you store this in your PATH
as git-status-recursive
,
and use git config --global alias.rst '!git-status-recursive'
(for example in your ~/.profile
),
then you can use it like git rst -s
.
Thank you @hoijui!
One comment: the script fails if the folder contains '.git' in its name. For example, I have a Github page repo named Gabriel-p.github.io
and the script will fail with:
fatal: cannot change to './Gabriel-phub.io/': No such file or directory
@Gabriel-p strange.. it does not happen here. :-/
I tested it like this:
mkdir tmp
cd tmp
mkdir .git
mkdir bla.github
mkdir ergwergwerg
mkdir ergwergwerggit
mkdir ergwergwerg/bla.github
find . -name '.git'
Which spits out only this:
./.git
which I think.. is what should happen, according to the man page of find
; -name
should match only on the complete file name.
Is it possible that you are using some obscure version of find
? maybe OSX?
though even there it woudl be strange to diverge like this... I don't know!
@hoijui Try creating a .git
folder inside bla.github
(and remove the top .git
folder to avoid an error because it is not a valid git
folder), and then run your script. I get this:
fatal: cannot change to './blahub/': No such file or directory
The problem is not with the find . -name '.git'
line but with the repo=${repo//\.git/}
line. Using this instead
repo=${repo%".git"}
seems to do the trick.
ahh very good, thank you @Gabriel-p!
//
replaces anywhere, %
replaces only a match on the end of the string, so %
is to be used here.
fixed version of the script:
#!/usr/bin/env bash
# Recursive `git status` (including sub-modules)
set -e
status_ops="$*"
find . -name '.git' \
| while read -r repo
do
repo=${repo%".git"}
(git -C "$repo" status -s \
| grep -q -v "^\$" \
&& echo -e "\n\033[1m${repo}\033[m" \
&& git -C "$repo" status $status_ops) \
|| true
done
... and as one-liner:
find . -name '.git' | while read -r repo ; do repo=${repo%".git"}; (git -C "$repo" status -s | grep -q -v "^\$" && echo -e "\n\033[1m${repo}\033[m" && git -C "$repo" status -s) || true; done
For Windows users... try this in the root folder above the folders to search:
for /f "tokens=*" %a in ('dir .git /adh /b /s') do (echo off & cd "%~pa" & cd & git status -s)
(The echo off
helps the output look less cluttered but may wreck your prompt. If so, exit the window when done.)
@glittle , awesome,
If you want to check all git folders in "C:\some-folder" run the following (1 level, runs faster)
for /f "tokens=*" %a in ('dir /ad /b') do (echo off & cd "c:\\some-folder\\%~a" & cd & git status -s)
You don't need to search everything. Do:
find . -maxdepth 2 -name .git -type d -exec sh -c "cd {}/..;git status" \;
find . -name '.git' | while read repo; do echo -e "\n$(dirname $repo)"; git --git-dir=$repo --work-tree=$(dirname $repo) status -s -b; done
ahh very good, thank you @Gabriel-p!
//
replaces anywhere,%
replaces only a match on the end of the string, so%
is to be used here.fixed version of the script:
#!/usr/bin/env bash # Recursive `git status` (including sub-modules) set -e status_ops="$*" find . -name '.git' \ | while read -r repo do repo=${repo%".git"} (git -C "$repo" status -s \ | grep -q -v "^\$" \ && echo -e "\n\033[1m${repo}\033[m" \ && git -C "$repo" status $status_ops) \ || true done... and as one-liner:
find . -name '.git' | while read -r repo ; do repo=${repo%".git"}; (git -C "$repo" status -s | grep -q -v "^\$" && echo -e "\n\033[1m${repo}\033[m" && git -C "$repo" status -s) || true; done
I discovered from here that if you want to run that script as a git command without making an alias, you have to:
- Name your script: "git-command"
- Make your script executable
- Move your script to a directory that's in your PATH
E.g.:
If your PATH contains ~/.local/bin/ and you named your script "git-str" then:
chmod +x git-str && mv git-str ~/.local/bin/
Now you can run: git str.
To any Linux novice, beware of what script you make executable, as it may be dangerous for your system.
(The
echo off
helps the output look less cluttered but may wreck your prompt. If so, exit the window when done.)
after the command is done, typing echo on
will bring back the prompt.
This one (1) produces a slim output, and (2) starts printing the results instantly (you don't have to wait it to finish to see the final output):
find . -maxdepth 2 -name .git -type d -exec sh -c "cd {}/..; pwd; git status -s" \;
Protip:
git status -s