1 # Copyright (C) The Arvados Authors. All rights reserved.
3 # SPDX-License-Identifier: AGPL-3.0
7 # x = CommitAncestor.find_or_create_by_descendant_and_ancestor(a, b)
8 # "b is an ancestor of a" if x.is
11 class CommitAncestor < ActiveRecord::Base
12 before_create :ask_git_whether_is
14 class CommitNotFoundError < ArgumentError
19 def ask_git_whether_is
20 @gitdirbase = Rails.configuration.git_repositories_dir
22 Dir.foreach @gitdirbase do |repo|
23 next if repo.match(/^\./)
24 git_dir = repo.match(/\.git$/) ? repo : File.join(repo, '.git')
25 repo_name = repo.sub(/\.git$/, '')
26 ENV['GIT_DIR'] = File.join(@gitdirbase, git_dir)
27 IO.foreach("|git rev-list --format=oneline '#{self.descendant.gsub(/[^0-9a-f]/,"")}'") do |line|
29 sha1, _ = line.strip.split(" ", 2)
30 if sha1 == self.ancestor
36 self.repository_name = repo_name
41 raise CommitNotFoundError.new "Specified commit was not found"