3 # x = CommitAncestor.find_or_create_by_descendant_and_ancestor(a, b)
4 # "b is an ancestor of a" if x.is
7 class CommitAncestor < ActiveRecord::Base
8 before_create :ask_git_whether_is
10 class CommitNotFoundError < ArgumentError
15 def ask_git_whether_is
16 @gitdirbase = Rails.configuration.git_repositories_dir
18 Dir.foreach @gitdirbase do |repo|
19 next if repo.match(/^\./)
20 git_dir = repo.match(/\.git$/) ? repo : File.join(repo, '.git')
21 repo_name = repo.sub(/\.git$/, '')
22 ENV['GIT_DIR'] = File.join(@gitdirbase, git_dir)
23 IO.foreach("|git rev-list --format=oneline '#{self.descendant.gsub(/[^0-9a-f]/,"")}'") do |line|
25 sha1, _ = line.strip.split(" ", 2)
26 if sha1 == self.ancestor
32 self.repository_name = repo_name
37 raise CommitNotFoundError.new "Specified commit was not found"