2

I'm currently writing a little zsh function that checks all of my git repositories to see if they're dirty or not and then prints out the ones that need a commit. Thus far, I've figured out that the quickest way to figure out a git repository's clean/dirty status is via git-diff and git-ls-files:

if ! git diff --quiet || git ls-files --others --exclude-standard; then
  state=":dirty"
fi

I have two questions for you folks:

  1. Does anyone know of a quicker, more efficient way to check for file changes/additions in a git repo?
  2. I want my zsh function to be handed a file path (say ~/Code/git-repos/) and check all of the repositories in it. Is there a way to do without having to cd into each directory and run those commands? Something like git-diff --quiet --git-dir="~/Code/git-repos/..." would be fantastic.

Thanks! :)

ABach
  • 3,743
  • 5
  • 25
  • 33
  • Would http://stackoverflow.com/questions/2657935/checking-for-a-dirty-index-or-untracked-files-with-git help? – VonC May 05 '10 at 19:25
  • @VonC - that looks great. I was hoping that I wouldn't have to use git-status, since it's a bit heavier, but it it allows me to specify "remote" git directories to look in, it may be my only choice. – ABach May 05 '10 at 19:39

1 Answers1

3

If $DIR holds your directory name and it's a standard layout (i.e. the .git dir is $DIR/.git), then git --git-dir $DIR/.git --work-tree $DIR status -s -uno will list all the files that have uncommitted changes. Checking if the list is empty should give you what you want.

Oblomov
  • 761
  • 3
  • 10
  • Running that command gives "fatal: Invalid untracked files mode '=none'." The man page specifies 'no' as a valid option for -u, but that gives a similar error. – ABach May 05 '10 at 19:44
  • You're right, sorry, the syntax was slightly off. Use -uno instead of -u=none – Oblomov May 06 '10 at 06:16
  • (I fixed the post accordingly) – Oblomov May 06 '10 at 06:16
  • A combination of this and @VonC's link above (in particular, the --porcelain flag for git status) is what did the trick. – ABach May 06 '10 at 15:10