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-05-02 17:15:12 -04:00
|
|
|
attr_reader :project, :identifier, :changes
|
2016-03-16 23:24:12 -04:00
|
|
|
|
2017-05-02 17:15:12 -04:00
|
|
|
def initialize(project, identifier, changes)
|
|
|
|
@project = project
|
2016-03-16 23:24:12 -04:00
|
|
|
@identifier = identifier
|
|
|
|
@changes = deserialize_changes(changes)
|
|
|
|
end
|
|
|
|
|
|
|
|
def identify(revision)
|
|
|
|
super(identifier, project, revision)
|
|
|
|
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
|