debian-mirror-gitlab/lib/gitlab/git_post_receive.rb

48 lines
1.2 KiB
Ruby
Raw Normal View History

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
@changes = deserialize_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
2017-09-10 17:25:29 +05:30
def changes_refs
2018-11-18 11:00:15 +05:30
return changes unless block_given?
2017-09-10 17:25:29 +05:30
changes.each do |change|
2018-11-18 11:00:15 +05:30
change.strip!
oldrev, newrev, ref = change.split(' ')
2017-09-10 17:25:29 +05:30
yield oldrev, newrev, ref
end
end
2016-06-02 11:05:42 +05:30
private
def deserialize_changes(changes)
2018-11-18 11:00:15 +05:30
utf8_encode_changes(changes).each_line
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