1 class Commit < ActiveRecord::Base
4 # Make sure the specified commit really exists, and return the full
7 # Accepts anything "git rev-list" accepts, optionally (and
8 # preferably) preceded by "repo_name:".
10 # Examples: "1234567", "master", "apps:1234567", "apps:master",
13 def self.find_by_commit_ish(commit_ish)
15 if commit_ish.index(':')
16 want_repo, commit_ish = commit_ish.split(':',2)
18 repositories.each do |repo_name, repo|
19 next if want_repo and want_repo != repo_name
20 ENV['GIT_DIR'] = repo[:git_dir]
21 IO.foreach("|git rev-list --max-count=1 --format=oneline 'origin/'#{commit_ish.shellescape} 2>/dev/null || git rev-list --max-count=1 --format=oneline ''#{commit_ish.shellescape}") do |line|
22 sha1, message = line.strip.split " ", 2
23 next if sha1.length != 40
25 Commit.find_or_create_by_repository_name_and_sha1_and_message(repo_name, sha1, message[0..254])
27 logger.warn "find_or_create failed: repo_name #{repo_name} sha1 #{sha1} message #{message[0..254]}"
28 # Ignore cache failure. Commit is real. We should proceed.
36 # Import all commits from configured git directory into the commits
40 repositories.each do |repo_name, repo|
41 stat = { true => 0, false => 0 }
42 ENV['GIT_DIR'] = repo[:git_dir]
43 IO.foreach("|git rev-list --format=oneline --all") do |line|
44 sha1, message = line.strip.split " ", 2
46 Commit.find_or_create_by_repository_name_and_sha1_and_message(repo_name, sha1, message[0..254]) do
50 if (stat[true] + stat[false]) % 100 == 0
51 if $stdout.tty? or ARGV[0] == '-v'
52 puts "#{$0} #{$$}: repo #{repo_name} add #{stat[true]} skip #{stat[false]}"
56 if $stdout.tty? or ARGV[0] == '-v'
57 puts "#{$0} #{$$}: repo #{repo_name} add #{stat[true]} skip #{stat[false]}"
65 return @repositories if @repositories
68 @gitdirbase = Rails.configuration.git_repositories_dir
69 Dir.foreach @gitdirbase do |repo|
70 next if repo.match /^\./
71 git_dir = File.join(@gitdirbase,
72 repo.match(/\.git$/) ? repo : File.join(repo, '.git'))
73 repo_name = repo.sub(/\.git$/, '')
74 @repositories[repo_name] = {git_dir: git_dir}