2018-07-31 10:07:24 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-03-16 23:24:12 -04:00
|
|
|
module Gitlab
|
|
|
|
class GitPostReceive
|
|
|
|
include Gitlab::Identifier
|
2017-11-28 22:50:29 -05:00
|
|
|
attr_reader :project, :identifier, :changes, :push_options
|
2016-03-16 23:24:12 -04:00
|
|
|
|
2019-04-05 00:19:30 -04:00
|
|
|
def initialize(project, identifier, changes, push_options = {})
|
2017-05-02 17:15:12 -04:00
|
|
|
@project = project
|
2016-03-16 23:24:12 -04:00
|
|
|
@identifier = identifier
|
2019-10-03 11:07:07 -04:00
|
|
|
@changes = parse_changes(changes)
|
2017-11-28 22:50:29 -05:00
|
|
|
@push_options = push_options
|
2016-03-16 23:24:12 -04:00
|
|
|
end
|
|
|
|
|
2018-10-25 06:09:53 -04:00
|
|
|
def identify
|
|
|
|
super(identifier)
|
2016-03-16 23:24:12 -04:00
|
|
|
end
|
|
|
|
|
2019-08-09 18:41:07 -04:00
|
|
|
def includes_branches?
|
2019-10-03 11:07:07 -04:00
|
|
|
changes.includes_branches?
|
2019-08-09 06:09:06 -04:00
|
|
|
end
|
|
|
|
|
2019-08-13 12:04:30 -04:00
|
|
|
def includes_tags?
|
2019-10-03 11:07:07 -04:00
|
|
|
changes.includes_tags?
|
2019-08-13 12:04:30 -04:00
|
|
|
end
|
|
|
|
|
2019-08-16 15:53:56 -04:00
|
|
|
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-10-03 11:07:07 -04:00
|
|
|
changes.branch_changes.any? do |change|
|
|
|
|
Gitlab::Git.branch_name(change[:ref]) == project.default_branch
|
2019-08-16 15:53:56 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-03-16 23:24:12 -04:00
|
|
|
private
|
|
|
|
|
2019-10-03 11:07:07 -04:00
|
|
|
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-03-16 23:24:12 -04:00
|
|
|
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
|