2018-11-18 11:00:15 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-06-02 11:05:42 +05:30
|
|
|
module Gitlab
|
|
|
|
class GitPostReceive
|
|
|
|
include Gitlab::Identifier
|
2019-02-15 15:39:39 +05:30
|
|
|
attr_reader :project, :identifier, :changes, :push_options
|
2016-06-02 11:05:42 +05:30
|
|
|
|
2019-07-07 11:18:12 +05:30
|
|
|
def initialize(project, identifier, changes, push_options = {})
|
2017-08-17 22:00:37 +05:30
|
|
|
@project = project
|
2016-06-02 11:05:42 +05:30
|
|
|
@identifier = identifier
|
2019-12-21 20:55:43 +05:30
|
|
|
@changes = parse_changes(changes)
|
2019-02-15 15:39:39 +05:30
|
|
|
@push_options = push_options
|
2016-06-02 11:05:42 +05:30
|
|
|
end
|
|
|
|
|
2018-12-13 13:39:08 +05:30
|
|
|
def identify
|
|
|
|
super(identifier)
|
2016-06-02 11:05:42 +05:30
|
|
|
end
|
|
|
|
|
2019-10-12 21:52:04 +05:30
|
|
|
def includes_branches?
|
2019-12-21 20:55:43 +05:30
|
|
|
changes.includes_branches?
|
2019-10-12 21:52:04 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def includes_tags?
|
2019-12-21 20:55:43 +05:30
|
|
|
changes.includes_tags?
|
2019-10-12 21:52:04 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def includes_default_branch?
|
|
|
|
# If the branch doesn't have a default branch yet, we presume the
|
|
|
|
# first branch pushed will be the default.
|
|
|
|
return true unless project.default_branch.present?
|
|
|
|
|
2019-12-21 20:55:43 +05:30
|
|
|
changes.branch_changes.any? do |change|
|
|
|
|
Gitlab::Git.branch_name(change[:ref]) == project.default_branch
|
2019-10-12 21:52:04 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-06-02 11:05:42 +05:30
|
|
|
private
|
|
|
|
|
2019-12-21 20:55:43 +05:30
|
|
|
def parse_changes(changes)
|
|
|
|
deserialized_changes = utf8_encode_changes(changes).each_line
|
|
|
|
|
|
|
|
Git::Changes.new.tap do |collection|
|
|
|
|
deserialized_changes.each_with_index do |raw_change, index|
|
|
|
|
oldrev, newrev, ref = raw_change.strip.split(' ')
|
|
|
|
change = { index: index, oldrev: oldrev, newrev: newrev, ref: ref }
|
|
|
|
|
|
|
|
if Git.branch_ref?(ref)
|
|
|
|
collection.add_branch_change(change)
|
|
|
|
elsif Git.tag_ref?(ref)
|
|
|
|
collection.add_tag_change(change)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-06-02 11:05:42 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def utf8_encode_changes(changes)
|
|
|
|
changes.force_encoding('UTF-8')
|
|
|
|
return changes if changes.valid_encoding?
|
|
|
|
|
|
|
|
# Convert non-UTF-8 branch/tag names to UTF-8 so they can be dumped as JSON.
|
|
|
|
detection = CharlockHolmes::EncodingDetector.detect(changes)
|
|
|
|
return changes unless detection && detection[:encoding]
|
|
|
|
|
|
|
|
CharlockHolmes::Converter.convert(changes, detection[:encoding], 'UTF-8')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|