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
|
|
|
|
@changes = deserialize_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
|
|
|
|
|
2017-05-02 22:36:13 -04:00
|
|
|
def changes_refs
|
2018-07-31 10:07:24 -04:00
|
|
|
return changes unless block_given?
|
2017-05-02 22:36:13 -04:00
|
|
|
|
|
|
|
changes.each do |change|
|
2018-07-31 10:07:24 -04:00
|
|
|
change.strip!
|
|
|
|
oldrev, newrev, ref = change.split(' ')
|
2017-05-02 22:36:13 -04:00
|
|
|
|
|
|
|
yield oldrev, newrev, ref
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-03-16 23:24:12 -04:00
|
|
|
private
|
|
|
|
|
|
|
|
def deserialize_changes(changes)
|
2018-07-31 10:07:24 -04:00
|
|
|
utf8_encode_changes(changes).each_line
|
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
|