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
|
|
|
|
|
2019-08-09 18:41:07 -04:00
|
|
|
def includes_branches?
|
|
|
|
enum_for(:changes_refs).any? do |_oldrev, _newrev, ref|
|
|
|
|
Gitlab::Git.branch_ref?(ref)
|
2019-08-09 06:09:06 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-08-13 12:04:30 -04:00
|
|
|
def includes_tags?
|
|
|
|
enum_for(:changes_refs).any? do |_oldrev, _newrev, ref|
|
|
|
|
Gitlab::Git.tag_ref?(ref)
|
|
|
|
end
|
|
|
|
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?
|
|
|
|
|
|
|
|
enum_for(:changes_refs).any? do |_oldrev, _newrev, ref|
|
|
|
|
Gitlab::Git.branch_ref?(ref) &&
|
|
|
|
Gitlab::Git.branch_name(ref) == project.default_branch
|
|
|
|
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
|