8784: Fix test for latest firefox.
[arvados.git] / services / api / app / models / commit_ancestor.rb
1 # Usage:
2 #
3 # x = CommitAncestor.find_or_create_by_descendant_and_ancestor(a, b)
4 # "b is an ancestor of a" if x.is
5 #
6
7 class CommitAncestor < ActiveRecord::Base
8   before_create :ask_git_whether_is
9
10   class CommitNotFoundError < ArgumentError
11   end
12
13   protected
14
15   def ask_git_whether_is
16     @gitdirbase = Rails.configuration.git_repositories_dir
17     self.is = nil
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|
24         self.is = false
25         sha1, _ = line.strip.split(" ", 2)
26         if sha1 == self.ancestor
27           self.is = true
28           break
29         end
30       end
31       if !self.is.nil?
32         self.repository_name = repo_name
33         break
34       end
35     end
36     if self.is.nil?
37       raise CommitNotFoundError.new "Specified commit was not found"
38     end
39   end
40 end