therealadam (owner)

Revisions

gist: 14004 Download_button fork
public
Public Clone URL: git://gist.github.com/14004.git
git_finder.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/usr/bin/env ruby
#
# Seeking to answer man's eternal question "I wonder how many little Git
# repositories I have just floating around in my home directory. Further, I
# wonder how many actually have a remote somewhere."
#
# Usage:
#
# Search for Git repositories in the specified directory
# > ruby git_finder.rb some_dir another_dir ...
#
# (c) Copyright 2008 Adam Keys. MIT licensed or some-such.
 
class GitFinder
  
  def self.run(dirs)
    dirs.each do |dir|
      Dir.chdir(dir) do
        gf = new
        gf.find_repos
        puts "#{dir},#{gf.repos.length},#{gf.repos.select { |r| r.has_remote? }.length}"
      end
    end
  end
  
  attr_reader :repos
  
  def initialize
    @repos = []
  end
  
  def find_repos
    @repos = Dir['**/.git'].map { |dir| print '.'; Repository.new(dir) }
    puts
  end
  
  class Repository
    
    def initialize(dir)
      @dir = dir
    end
    
    def has_remote?
      Dir.chdir(File.join(@dir, '..')) do
        puts "In #{@dir}" if ENV['DEBUG']
        `git remote`.length > 0
      end
    end
    
  end
  
end
 
GitFinder.run(ARGV)