3 # Copyright (C) 2013 Gitorious AS
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU Affero General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU Affero General Public License for more details.
15 # You should have received a copy of the GNU Affero General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18 class PushCommitExtractor
19 def initialize(repository_path, spec)
21 @rugged_repo = Rugged::Repository.new(repository_path)
25 @existing_refs ||= @rugged_repo.refs
28 def existing_ref_names
29 existing_refs.map(&:name).map {|r| r.split("/").last}
33 @new_commits ||= fetch_new_commits
37 walker = Rugged::Walker.new(@rugged_repo)
38 walker.push(@spec.to_sha.sha)
39 candidates = existing_refs
41 heads = candidates.reject do |head|
42 head.name.split("/").last == @spec.ref_name || /^refs\/tags/.match(head.name)
45 if @spec.action_create?
46 heads.each { |head| walker.hide(head.target) }
48 walker.hide(@spec.from_sha.sha)
51 new_shas = walker.map {|c| c}
56 def newest_known_commit
57 if new_commits.empty? || new_commits.last.parents.empty?
58 @rugged_repo.lookup(@spec.to_sha.sha)
60 new_commits.last.parents.first