Don't show timeout message for empty diffs (ie. blank file added)
[gitorious.git] / lib / push_commit_extractor.rb
blob91973fb0a6fb9f63a9177b785383f2e7319bdec2
1 # encoding: utf-8
2 #--
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/>.
17 #++
18 class PushCommitExtractor
19   def initialize(repository_path, spec)
20     @spec = spec
21     @rugged_repo = Rugged::Repository.new(repository_path)
22   end
24   def existing_refs
25     @existing_refs ||= @rugged_repo.refs
26   end
28   def existing_ref_names
29     existing_refs.map(&:name).map {|r| r.split("/").last}
30   end
32   def new_commits
33     @new_commits ||= fetch_new_commits
34   end
36   def 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)
43     end
45     if @spec.action_create?
46       heads.each { |head| walker.hide(head.target) }
47     else
48       walker.hide(@spec.from_sha.sha)
49     end
51     new_shas = walker.map {|c| c}
52     walker.reset
53     return new_shas
54   end
56   def newest_known_commit
57     if new_commits.empty? || new_commits.last.parents.empty?
58       @rugged_repo.lookup(@spec.to_sha.sha)
59     else
60       new_commits.last.parents.first
61     end
62   end
63 end