Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
631 views
in Technique[技术] by (71.8m points)

scripting - git: Find all uncommitted locals repos in a directory tree

I have a bunch(10-15) of local git repositories somewhere on my filesystem, but all in the folder /data/

I want to find all/any folder that has uncommitted changes. How can I do that? Kind of like a recursive global git status variant.

All the answers got it wrong I think. Any git command only works within the folder that's under git control. I need something to search for such folders.

So instead I wrote this script that does this:

#!/usr/bin/env ruby
require 'find'
require 'fileutils'

#supply directory to search in as argument

@pat = ARGV[0]
(puts "directory argument required"; exit) unless @pat
Dir.chdir(@pat)
Find.find(@pat) do |path|
  if FileTest.directory?(path)
    Dir.chdir(path)
    resp = `git status 2>&1`
    unless resp =~ /fatal|nothing to commit (working directory clean)/i
      puts "#{'#'*10}
#{Dir.pwd}#{'#'*10}
#{resp}"
      Find.prune
    end

    Dir.chdir(@pat)
  end
end
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Adapted from this gist on how to print git status of all repositories under the current folder:

find . -type d -name '.git' | while read dir ; do sh -c "cd $dir/../ && echo -e "
GIT STATUS IN ${dir//.git/}" && git status -s" ; done

The find command is your friend, along with some shell magic.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...