diff options
author | Douwe Maan <douwe@gitlab.com> | 2015-03-10 13:51:36 +0300 |
---|---|---|
committer | Douwe Maan <douwe@gitlab.com> | 2015-03-10 15:39:31 +0300 |
commit | 383c56efa1882d9cab956de5b5b72e51691c3f0c (patch) | |
tree | 1eebe2c35351d44a9fa54f53658346f86d9566a3 | |
parent | e0caed91e2cd6b959f808139df7c40f3644f88fd (diff) |
Use Gitlab::Git helper methods and constants as much as possible.
21 files changed, 73 insertions, 78 deletions
diff --git a/app/controllers/projects/tags_controller.rb b/app/controllers/projects/tags_controller.rb index 08c7ce3f37d..03fface2d2a 100644 --- a/app/controllers/projects/tags_controller.rb +++ b/app/controllers/projects/tags_controller.rb @@ -27,7 +27,7 @@ class Projects::TagsController < Projects::ApplicationController tag = @repository.find_tag(params[:id]) if tag && @repository.rm_tag(tag.name) - EventCreateService.new.push_ref(@project, current_user, tag, 'rm', 'refs/tags') + EventCreateService.new.push_ref(@project, current_user, tag, 'rm', Gitlab::Git::TAG_REF_PREFIX) end respond_to do |format| diff --git a/app/mailers/emails/projects.rb b/app/mailers/emails/projects.rb index 9ea121d83a4..b55129de292 100644 --- a/app/mailers/emails/projects.rb +++ b/app/mailers/emails/projects.rb @@ -23,7 +23,7 @@ module Emails @compare = compare @commits = Commit.decorate(compare.commits) @diffs = compare.diffs - @branch = branch.gsub("refs/heads/", "") + @branch = Gitlab::Git.ref_name(branch) @disable_diffs = disable_diffs @subject = "[#{@project.path_with_namespace}][#{@branch}] " diff --git a/app/models/event.rb b/app/models/event.rb index 5579ab1dbb0..8d20d7ef252 100644 --- a/app/models/event.rb +++ b/app/models/event.rb @@ -190,19 +190,19 @@ class Event < ActiveRecord::Base end def tag? - data[:ref]["refs/tags"] + Gitlab::Git.tag_ref?(data[:ref]) end def branch? - data[:ref]["refs/heads"] + Gitlab::Git.branch_ref?(data[:ref]) end def new_ref? - commit_from =~ /^00000/ + Gitlab::Git.blank_ref?(commit_from) end def rm_ref? - commit_to =~ /^00000/ + Gitlab::Git.blank_ref?(commit_to) end def md_ref? @@ -226,11 +226,11 @@ class Event < ActiveRecord::Base end def branch_name - @branch_name ||= data[:ref].gsub("refs/heads/", "") + @branch_name ||= Gitlab::Git.ref_name(data[:ref]) end def tag_name - @tag_name ||= data[:ref].gsub("refs/tags/", "") + @tag_name ||= Gitlab::Git.ref_name(data[:ref]) end # Max 20 commits from push DESC diff --git a/app/models/project_services/asana_service.rb b/app/models/project_services/asana_service.rb index 6a622207385..d52214cdd69 100644 --- a/app/models/project_services/asana_service.rb +++ b/app/models/project_services/asana_service.rb @@ -77,7 +77,7 @@ automatically inspected. Leave blank to include all branches.' end user = data[:user_name] - branch = data[:ref].gsub('refs/heads/', '') + branch = Gitlab::Git.ref_name(data[:ref]) branch_restriction = restrict_to_branch.to_s diff --git a/app/models/project_services/campfire_service.rb b/app/models/project_services/campfire_service.rb index 1c63444fbf9..e591afdda64 100644 --- a/app/models/project_services/campfire_service.rb +++ b/app/models/project_services/campfire_service.rb @@ -64,7 +64,7 @@ class CampfireService < Service end def build_message(push) - ref = push[:ref].gsub("refs/heads/", "") + ref = Gitlab::Git.ref_name(push[:ref]) before = push[:before] after = push[:after] @@ -72,9 +72,9 @@ class CampfireService < Service message << "[#{project.name_with_namespace}] " message << "#{push[:user_name]} " - if before.include?('000000') + if Gitlab::Git.blank_ref?(before) message << "pushed new branch #{ref} \n" - elsif after.include?('000000') + elsif Gitlab::Git.blank_ref?(after) message << "removed branch #{ref} \n" else message << "pushed #{push[:total_commits_count]} commits to #{ref}. " diff --git a/app/models/project_services/hipchat_service.rb b/app/models/project_services/hipchat_service.rb index 90ba7e080f1..d264a56ebdf 100644 --- a/app/models/project_services/hipchat_service.rb +++ b/app/models/project_services/hipchat_service.rb @@ -79,24 +79,19 @@ class HipchatService < Service end def create_push_message(push) - if push[:ref].starts_with?('refs/tags/') - ref_type = 'tag' - ref = push[:ref].gsub('refs/tags/', '') - else - ref_type = 'branch' - ref = push[:ref].gsub('refs/heads/', '') - end + ref_type = Gitlab::Git.tag_ref?(push[:ref]) ? 'tag' : 'branch' + ref = Gitlab::Git.ref_name(push[:ref]) before = push[:before] after = push[:after] message = "" message << "#{push[:user_name]} " - if before.include?('000000') + if Gitlab::Git.blank_ref?(before) message << "pushed new #{ref_type} <a href=\""\ "#{project_url}/commits/#{URI.escape(ref)}\">#{ref}</a>"\ " to #{project_link}\n" - elsif after.include?('000000') + elsif Gitlab::Git.blank_ref?(after) message << "removed #{ref_type} <b>#{ref}</b> from <a href=\"#{project.web_url}\">#{project_name}</a> \n" else message << "pushed to #{ref_type} <a href=\""\ diff --git a/app/models/project_services/pushover_service.rb b/app/models/project_services/pushover_service.rb index 0ce324434db..53edf522e9a 100644 --- a/app/models/project_services/pushover_service.rb +++ b/app/models/project_services/pushover_service.rb @@ -88,13 +88,13 @@ class PushoverService < Service def execute(data) return unless supported_events.include?(data[:object_kind]) - ref = data[:ref].gsub('refs/heads/', '') + ref = Gitlab::Git.ref_name(data[:ref]) before = data[:before] after = data[:after] - if before.include?('000000') + if Gitlab::Git.blank_ref?(before) message = "#{data[:user_name]} pushed new branch \"#{ref}\"." - elsif after.include?('000000') + elsif Gitlab::Git.blank_ref?(after) message = "#{data[:user_name]} deleted branch \"#{ref}\"." else message = "#{data[:user_name]} push to branch \"#{ref}\"." diff --git a/app/models/project_services/slack_service/push_message.rb b/app/models/project_services/slack_service/push_message.rb index 3dc2df04764..b26f3e9ddce 100644 --- a/app/models/project_services/slack_service/push_message.rb +++ b/app/models/project_services/slack_service/push_message.rb @@ -15,13 +15,8 @@ class SlackService @commits = params.fetch(:commits, []) @project_name = params[:project_name] @project_url = params[:project_url] - if params[:ref].starts_with?('refs/tags/') - @ref_type = 'tag' - @ref = params[:ref].gsub('refs/tags/', '') - else - @ref_type = 'branch' - @ref = params[:ref].gsub('refs/heads/', '') - end + @ref_type = Gitlab::Git.tag_ref?(params[:ref]) ? 'tag' : 'branch' + @ref = Gitlab::Git.ref_name(params[:ref]) @user_name = params[:user_name] end @@ -81,11 +76,11 @@ class SlackService end def new_branch? - before.include?('000000') + Gitlab::Git.blank_ref?(before) end def removed_branch? - after.include?('000000') + Gitlab::Git.blank_ref?(after) end def branch_url diff --git a/app/models/project_services/teamcity_service.rb b/app/models/project_services/teamcity_service.rb index 038c200adc7..7403e19da9a 100644 --- a/app/models/project_services/teamcity_service.rb +++ b/app/models/project_services/teamcity_service.rb @@ -132,7 +132,7 @@ class TeamcityService < CiService password: password, } - branch = data[:ref].gsub('refs/heads/', '') + branch = Gitlab::Git.ref_name(data[:ref]) self.class.post("#{teamcity_url}/httpAuth/app/rest/buildQueue", body: "<build branchName=\"#{branch}\">"\ diff --git a/app/services/create_tag_service.rb b/app/services/create_tag_service.rb index 8cd65724cb9..dfc5677c9d4 100644 --- a/app/services/create_tag_service.rb +++ b/app/services/create_tag_service.rb @@ -21,7 +21,7 @@ class CreateTagService < BaseService new_tag = repository.find_tag(tag_name) if new_tag - EventCreateService.new.push_ref(project, current_user, new_tag, 'add', 'refs/tags') + EventCreateService.new.push_ref(project, current_user, new_tag, 'add', Gitlab::Git::TAG_REF_PREFIX) push_data = create_push_data(project, current_user, new_tag) project.execute_hooks(push_data.dup, :tag_push_hooks) @@ -41,7 +41,7 @@ class CreateTagService < BaseService def create_push_data(project, user, tag) data = Gitlab::PushDataBuilder. - build(project, user, Gitlab::Git::BLANK_SHA, tag.target, 'refs/tags/' + tag.name, []) + build(project, user, Gitlab::Git::BLANK_SHA, tag.target, "#{Gitlab::Git::TAG_REF_PREFIX}#{tag.name}", []) data[:object_kind] = "tag_push" data end diff --git a/app/services/event_create_service.rb b/app/services/event_create_service.rb index ba9547b9242..dc52d6d89df 100644 --- a/app/services/event_create_service.rb +++ b/app/services/event_create_service.rb @@ -62,19 +62,19 @@ class EventCreateService create_event(project, current_user, Event::CREATED) end - def push_ref(project, current_user, ref, action = 'add', prefix = 'refs/heads') + def push_ref(project, current_user, ref, action = 'add', prefix = Gitlab::Git::BRANCH_REF_PREFIX) commit = project.repository.commit(ref.target) if action.to_s == 'add' - before = '00000000' + before = Gitlab::Git::BLANK_SHA after = commit.id else before = commit.id - after = '00000000' + after = Gitlab::Git::BLANK_SHA end data = { - ref: "#{prefix}/#{ref.name}", + ref: "#{prefix}#{ref.name}", before: before, after: after } diff --git a/app/services/git_push_service.rb b/app/services/git_push_service.rb index 4e1afea6d50..bfabfd7ade3 100644 --- a/app/services/git_push_service.rb +++ b/app/services/git_push_service.rb @@ -107,30 +107,24 @@ class GitPushService end def push_to_existing_branch?(ref, oldrev) - ref_parts = ref.split('/') - # Return if this is not a push to a branch (e.g. new commits) - ref_parts[1].include?('heads') && oldrev != Gitlab::Git::BLANK_SHA + Gitlab::Git.branch_ref?(ref) && oldrev != Gitlab::Git::BLANK_SHA end def push_to_new_branch?(ref, oldrev) - ref_parts = ref.split('/') - - ref_parts[1].include?('heads') && oldrev == Gitlab::Git::BLANK_SHA + Gitlab::Git.branch_ref?(ref) && Gitlab::Git.blank_ref?(oldrev) end def push_remove_branch?(ref, newrev) - ref_parts = ref.split('/') - - ref_parts[1].include?('heads') && newrev == Gitlab::Git::BLANK_SHA + Gitlab::Git.branch_ref?(ref) && Gitlab::Git.blank_ref?(newrev) end def push_to_branch?(ref) - ref.include?('refs/heads') + Gitlab::Git.branch_ref?(ref) end def is_default_branch?(ref) - ref == "refs/heads/#{project.default_branch}" + Gitlab::Git.branch_ref?(ref) && Gitlab::Git.ref_name(ref) == project.default_branch end def commit_user(commit) diff --git a/app/services/merge_requests/refresh_service.rb b/app/services/merge_requests/refresh_service.rb index ea846472766..cab8a1e880e 100644 --- a/app/services/merge_requests/refresh_service.rb +++ b/app/services/merge_requests/refresh_service.rb @@ -1,10 +1,10 @@ module MergeRequests class RefreshService < MergeRequests::BaseService def execute(oldrev, newrev, ref) - return true unless ref =~ /heads/ + return true unless Gitlab::Git.branch_ref?(ref) @oldrev, @newrev = oldrev, newrev - @branch_name = ref.gsub("refs/heads/", "") + @branch_name = Gitlab::Git.ref_name(ref) @fork_merge_requests = @project.fork_merge_requests.opened @commits = @project.repository.commits_between(oldrev, newrev) diff --git a/app/workers/emails_on_push_worker.rb b/app/workers/emails_on_push_worker.rb index 2e783814824..e59ca81defe 100644 --- a/app/workers/emails_on_push_worker.rb +++ b/app/workers/emails_on_push_worker.rb @@ -8,7 +8,7 @@ class EmailsOnPushWorker branch = push_data["ref"] author_id = push_data["user_id"] - if before_sha =~ /^000000/ || after_sha =~ /^000000/ + if Gitlab::Git.blank_ref?(before_sha) || Gitlab::Git.blank_ref?(after_sha) # skip if new branch was pushed or branch was removed return true end diff --git a/app/workers/irker_worker.rb b/app/workers/irker_worker.rb index 613bae351d8..e1a99d9cad8 100644 --- a/app/workers/irker_worker.rb +++ b/app/workers/irker_worker.rb @@ -57,9 +57,9 @@ class IrkerWorker end def send_branch_updates(push_data, project, repo_name, committer, branch) - if push_data['before'] =~ /^000000/ + if push_data['before'] == Gitlab::Git::BLANK_SHA send_new_branch project, repo_name, committer, branch - elsif push_data['after'] =~ /^000000/ + elsif push_data['after'] == Gitlab::Git::BLANK_SHA send_del_branch repo_name, committer, branch end end @@ -83,7 +83,7 @@ class IrkerWorker return if push_data['total_commits_count'] == 0 # Next message is for number of commit pushed, if any - if push_data['before'] =~ /^000000/ + if push_data['before'] == Gitlab::Git::BLANK_SHA # Tweak on push_data["before"] in order to have a nice compare URL push_data['before'] = before_on_new_branch push_data, project end diff --git a/app/workers/post_receive.rb b/app/workers/post_receive.rb index 1406cba2db3..ecc6c8e53a3 100644 --- a/app/workers/post_receive.rb +++ b/app/workers/post_receive.rb @@ -33,7 +33,7 @@ class PostReceive return false end - if tag?(ref) + if Gitlab::Git.tag_ref?(ref) GitTagPushService.new.execute(project, @user, oldrev, newrev, ref) else GitPushService.new.execute(project, @user, oldrev, newrev, ref) @@ -44,10 +44,4 @@ class PostReceive def log(message) Gitlab::GitLogger.error("POST-RECEIVE: #{message}") end - - private - - def tag?(ref) - !!(/refs\/tags\/(.*)/.match(ref)) - end end diff --git a/lib/gitlab/git.rb b/lib/gitlab/git.rb index 4a712c6345f..0c350d7c675 100644 --- a/lib/gitlab/git.rb +++ b/lib/gitlab/git.rb @@ -1,9 +1,25 @@ module Gitlab module Git BLANK_SHA = '0' * 40 + TAG_REF_PREFIX = "refs/tags/" + BRANCH_REF_PREFIX = "refs/heads/" - def self.extract_ref_name(ref) - ref.gsub(/\Arefs\/(tags|heads)\//, '') + class << self + def ref_name(ref) + ref.gsub(/\Arefs\/(tags|heads)\//, '') + end + + def tag_ref?(ref) + ref.start_with?(TAG_REF_PREFIX) + end + + def branch_ref?(ref) + ref.start_with?(BRANCH_REF_PREFIX) + end + + def blank_ref?(ref) + ref == BLANK_SHA + end end end end diff --git a/lib/gitlab/git_access.rb b/lib/gitlab/git_access.rb index 9b31190a882..cb69e4b13d3 100644 --- a/lib/gitlab/git_access.rb +++ b/lib/gitlab/git_access.rb @@ -115,7 +115,7 @@ module Gitlab # we dont allow force push to protected branch if forced_push?(project, oldrev, newrev) :force_push_code_to_protected_branches - elsif newrev == Gitlab::Git::BLANK_SHA + elsif Gitlab::Git.blank_ref?(newrev) # and we dont allow remove of protected branch :remove_protected_branches elsif project.developers_can_push_to_protected_branch?(branch_name) @@ -135,8 +135,8 @@ module Gitlab def branch_name(ref) ref = ref.to_s - if ref.start_with?('refs/heads') - ref.sub(%r{\Arefs/heads/}, '') + if Gitlab::Git.branch_ref?(ref) + Gitlab::Git.ref_name(ref) else nil end @@ -144,8 +144,8 @@ module Gitlab def tag_name(ref) ref = ref.to_s - if ref.start_with?('refs/tags') - ref.sub(%r{\Arefs/tags/}, '') + if Gitlab::Git.tag_ref?(ref) + Gitlab::Git.ref_name(ref) else nil end diff --git a/lib/gitlab/push_data_builder.rb b/lib/gitlab/push_data_builder.rb index 5cefa67d3ab..9fb0bf65949 100644 --- a/lib/gitlab/push_data_builder.rb +++ b/lib/gitlab/push_data_builder.rb @@ -65,12 +65,13 @@ module Gitlab # existing project and commits to test web hooks def build_sample(project, user) commits = project.repository.commits(project.default_branch, nil, 3) - build(project, user, commits.last.id, commits.first.id, "refs/heads/#{project.default_branch}", commits) + ref = "#{Gitlab::Git::BRANCH_REF_PREFIX}#{project.default_branch}" + build(project, user, commits.last.id, commits.first.id, ref, commits) end def checkout_sha(repository, newrev, ref) - if newrev != Gitlab::Git::BLANK_SHA && ref.start_with?('refs/tags/') - tag_name = Gitlab::Git.extract_ref_name(ref) + if newrev != Gitlab::Git::BLANK_SHA && Gitlab::Git.tag_ref?(ref) + tag_name = Gitlab::Git.ref_name(ref) tag = repository.find_tag(tag_name) if tag diff --git a/spec/models/project_services/hipchat_service_spec.rb b/spec/models/project_services/hipchat_service_spec.rb index b9f2bee148d..8ab847e6432 100644 --- a/spec/models/project_services/hipchat_service_spec.rb +++ b/spec/models/project_services/hipchat_service_spec.rb @@ -63,7 +63,7 @@ describe HipchatService do end context 'tag_push events' do - let(:push_sample_data) { Gitlab::PushDataBuilder.build(project, user, '000000', '111111', 'refs/tags/test', []) } + let(:push_sample_data) { Gitlab::PushDataBuilder.build(project, user, Gitlab::Git::BLANK_SHA, '1' * 40, 'refs/tags/test', []) } it "should call Hipchat API for tag push events" do hipchat.execute(push_sample_data) diff --git a/spec/models/project_services/slack_service/push_message_spec.rb b/spec/models/project_services/slack_service/push_message_spec.rb index 3ef065459d8..10963481a12 100644 --- a/spec/models/project_services/slack_service/push_message_spec.rb +++ b/spec/models/project_services/slack_service/push_message_spec.rb @@ -43,7 +43,7 @@ describe SlackService::PushMessage do let(:args) { { after: 'after', - before: '000000', + before: Gitlab::Git::BLANK_SHA, project_name: 'project_name', ref: 'refs/tags/new_tag', user_name: 'user_name', @@ -61,7 +61,7 @@ describe SlackService::PushMessage do context 'new branch' do before do - args[:before] = '000000' + args[:before] = Gitlab::Git::BLANK_SHA end it 'returns a message regarding a new branch' do @@ -75,7 +75,7 @@ describe SlackService::PushMessage do context 'removed branch' do before do - args[:after] = '000000' + args[:after] = Gitlab::Git::BLANK_SHA end it 'returns a message regarding a removed branch' do |