Commit 383c56ef authored by Douwe Maan's avatar Douwe Maan

Use Gitlab::Git helper methods and constants as much as possible.

parent e0caed91
...@@ -27,7 +27,7 @@ class Projects::TagsController < Projects::ApplicationController ...@@ -27,7 +27,7 @@ class Projects::TagsController < Projects::ApplicationController
tag = @repository.find_tag(params[:id]) tag = @repository.find_tag(params[:id])
if tag && @repository.rm_tag(tag.name) 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 end
respond_to do |format| respond_to do |format|
......
...@@ -23,7 +23,7 @@ module Emails ...@@ -23,7 +23,7 @@ module Emails
@compare = compare @compare = compare
@commits = Commit.decorate(compare.commits) @commits = Commit.decorate(compare.commits)
@diffs = compare.diffs @diffs = compare.diffs
@branch = branch.gsub("refs/heads/", "") @branch = Gitlab::Git.ref_name(branch)
@disable_diffs = disable_diffs @disable_diffs = disable_diffs
@subject = "[#{@project.path_with_namespace}][#{@branch}] " @subject = "[#{@project.path_with_namespace}][#{@branch}] "
......
...@@ -190,19 +190,19 @@ class Event < ActiveRecord::Base ...@@ -190,19 +190,19 @@ class Event < ActiveRecord::Base
end end
def tag? def tag?
data[:ref]["refs/tags"] Gitlab::Git.tag_ref?(data[:ref])
end end
def branch? def branch?
data[:ref]["refs/heads"] Gitlab::Git.branch_ref?(data[:ref])
end end
def new_ref? def new_ref?
commit_from =~ /^00000/ Gitlab::Git.blank_ref?(commit_from)
end end
def rm_ref? def rm_ref?
commit_to =~ /^00000/ Gitlab::Git.blank_ref?(commit_to)
end end
def md_ref? def md_ref?
...@@ -226,11 +226,11 @@ class Event < ActiveRecord::Base ...@@ -226,11 +226,11 @@ class Event < ActiveRecord::Base
end end
def branch_name def branch_name
@branch_name ||= data[:ref].gsub("refs/heads/", "") @branch_name ||= Gitlab::Git.ref_name(data[:ref])
end end
def tag_name def tag_name
@tag_name ||= data[:ref].gsub("refs/tags/", "") @tag_name ||= Gitlab::Git.ref_name(data[:ref])
end end
# Max 20 commits from push DESC # Max 20 commits from push DESC
......
...@@ -77,7 +77,7 @@ automatically inspected. Leave blank to include all branches.' ...@@ -77,7 +77,7 @@ automatically inspected. Leave blank to include all branches.'
end end
user = data[:user_name] user = data[:user_name]
branch = data[:ref].gsub('refs/heads/', '') branch = Gitlab::Git.ref_name(data[:ref])
branch_restriction = restrict_to_branch.to_s branch_restriction = restrict_to_branch.to_s
......
...@@ -64,7 +64,7 @@ class CampfireService < Service ...@@ -64,7 +64,7 @@ class CampfireService < Service
end end
def build_message(push) def build_message(push)
ref = push[:ref].gsub("refs/heads/", "") ref = Gitlab::Git.ref_name(push[:ref])
before = push[:before] before = push[:before]
after = push[:after] after = push[:after]
...@@ -72,9 +72,9 @@ class CampfireService < Service ...@@ -72,9 +72,9 @@ class CampfireService < Service
message << "[#{project.name_with_namespace}] " message << "[#{project.name_with_namespace}] "
message << "#{push[:user_name]} " message << "#{push[:user_name]} "
if before.include?('000000') if Gitlab::Git.blank_ref?(before)
message << "pushed new branch #{ref} \n" message << "pushed new branch #{ref} \n"
elsif after.include?('000000') elsif Gitlab::Git.blank_ref?(after)
message << "removed branch #{ref} \n" message << "removed branch #{ref} \n"
else else
message << "pushed #{push[:total_commits_count]} commits to #{ref}. " message << "pushed #{push[:total_commits_count]} commits to #{ref}. "
......
...@@ -79,24 +79,19 @@ class HipchatService < Service ...@@ -79,24 +79,19 @@ class HipchatService < Service
end end
def create_push_message(push) def create_push_message(push)
if push[:ref].starts_with?('refs/tags/') ref_type = Gitlab::Git.tag_ref?(push[:ref]) ? 'tag' : 'branch'
ref_type = 'tag' ref = Gitlab::Git.ref_name(push[:ref])
ref = push[:ref].gsub('refs/tags/', '')
else
ref_type = 'branch'
ref = push[:ref].gsub('refs/heads/', '')
end
before = push[:before] before = push[:before]
after = push[:after] after = push[:after]
message = "" message = ""
message << "#{push[:user_name]} " message << "#{push[:user_name]} "
if before.include?('000000') if Gitlab::Git.blank_ref?(before)
message << "pushed new #{ref_type} <a href=\""\ message << "pushed new #{ref_type} <a href=\""\
"#{project_url}/commits/#{URI.escape(ref)}\">#{ref}</a>"\ "#{project_url}/commits/#{URI.escape(ref)}\">#{ref}</a>"\
" to #{project_link}\n" " 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" message << "removed #{ref_type} <b>#{ref}</b> from <a href=\"#{project.web_url}\">#{project_name}</a> \n"
else else
message << "pushed to #{ref_type} <a href=\""\ message << "pushed to #{ref_type} <a href=\""\
......
...@@ -88,13 +88,13 @@ class PushoverService < Service ...@@ -88,13 +88,13 @@ class PushoverService < Service
def execute(data) def execute(data)
return unless supported_events.include?(data[:object_kind]) return unless supported_events.include?(data[:object_kind])
ref = data[:ref].gsub('refs/heads/', '') ref = Gitlab::Git.ref_name(data[:ref])
before = data[:before] before = data[:before]
after = data[:after] after = data[:after]
if before.include?('000000') if Gitlab::Git.blank_ref?(before)
message = "#{data[:user_name]} pushed new branch \"#{ref}\"." 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}\"." message = "#{data[:user_name]} deleted branch \"#{ref}\"."
else else
message = "#{data[:user_name]} push to branch \"#{ref}\"." message = "#{data[:user_name]} push to branch \"#{ref}\"."
......
...@@ -15,13 +15,8 @@ class SlackService ...@@ -15,13 +15,8 @@ class SlackService
@commits = params.fetch(:commits, []) @commits = params.fetch(:commits, [])
@project_name = params[:project_name] @project_name = params[:project_name]
@project_url = params[:project_url] @project_url = params[:project_url]
if params[:ref].starts_with?('refs/tags/') @ref_type = Gitlab::Git.tag_ref?(params[:ref]) ? 'tag' : 'branch'
@ref_type = 'tag' @ref = Gitlab::Git.ref_name(params[:ref])
@ref = params[:ref].gsub('refs/tags/', '')
else
@ref_type = 'branch'
@ref = params[:ref].gsub('refs/heads/', '')
end
@user_name = params[:user_name] @user_name = params[:user_name]
end end
...@@ -81,11 +76,11 @@ class SlackService ...@@ -81,11 +76,11 @@ class SlackService
end end
def new_branch? def new_branch?
before.include?('000000') Gitlab::Git.blank_ref?(before)
end end
def removed_branch? def removed_branch?
after.include?('000000') Gitlab::Git.blank_ref?(after)
end end
def branch_url def branch_url
......
...@@ -132,7 +132,7 @@ class TeamcityService < CiService ...@@ -132,7 +132,7 @@ class TeamcityService < CiService
password: password, password: password,
} }
branch = data[:ref].gsub('refs/heads/', '') branch = Gitlab::Git.ref_name(data[:ref])
self.class.post("#{teamcity_url}/httpAuth/app/rest/buildQueue", self.class.post("#{teamcity_url}/httpAuth/app/rest/buildQueue",
body: "<build branchName=\"#{branch}\">"\ body: "<build branchName=\"#{branch}\">"\
......
...@@ -21,7 +21,7 @@ class CreateTagService < BaseService ...@@ -21,7 +21,7 @@ class CreateTagService < BaseService
new_tag = repository.find_tag(tag_name) new_tag = repository.find_tag(tag_name)
if new_tag 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) push_data = create_push_data(project, current_user, new_tag)
project.execute_hooks(push_data.dup, :tag_push_hooks) project.execute_hooks(push_data.dup, :tag_push_hooks)
...@@ -41,7 +41,7 @@ class CreateTagService < BaseService ...@@ -41,7 +41,7 @@ class CreateTagService < BaseService
def create_push_data(project, user, tag) def create_push_data(project, user, tag)
data = Gitlab::PushDataBuilder. 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[:object_kind] = "tag_push"
data data
end end
......
...@@ -62,19 +62,19 @@ class EventCreateService ...@@ -62,19 +62,19 @@ class EventCreateService
create_event(project, current_user, Event::CREATED) create_event(project, current_user, Event::CREATED)
end 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) commit = project.repository.commit(ref.target)
if action.to_s == 'add' if action.to_s == 'add'
before = '00000000' before = Gitlab::Git::BLANK_SHA
after = commit.id after = commit.id
else else
before = commit.id before = commit.id
after = '00000000' after = Gitlab::Git::BLANK_SHA
end end
data = { data = {
ref: "#{prefix}/#{ref.name}", ref: "#{prefix}#{ref.name}",
before: before, before: before,
after: after after: after
} }
......
...@@ -107,30 +107,24 @@ class GitPushService ...@@ -107,30 +107,24 @@ class GitPushService
end end
def push_to_existing_branch?(ref, oldrev) 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) # 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 end
def push_to_new_branch?(ref, oldrev) def push_to_new_branch?(ref, oldrev)
ref_parts = ref.split('/') Gitlab::Git.branch_ref?(ref) && Gitlab::Git.blank_ref?(oldrev)
ref_parts[1].include?('heads') && oldrev == Gitlab::Git::BLANK_SHA
end end
def push_remove_branch?(ref, newrev) def push_remove_branch?(ref, newrev)
ref_parts = ref.split('/') Gitlab::Git.branch_ref?(ref) && Gitlab::Git.blank_ref?(newrev)
ref_parts[1].include?('heads') && newrev == Gitlab::Git::BLANK_SHA
end end
def push_to_branch?(ref) def push_to_branch?(ref)
ref.include?('refs/heads') Gitlab::Git.branch_ref?(ref)
end end
def is_default_branch?(ref) 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 end
def commit_user(commit) def commit_user(commit)
......
module MergeRequests module MergeRequests
class RefreshService < MergeRequests::BaseService class RefreshService < MergeRequests::BaseService
def execute(oldrev, newrev, ref) def execute(oldrev, newrev, ref)
return true unless ref =~ /heads/ return true unless Gitlab::Git.branch_ref?(ref)
@oldrev, @newrev = oldrev, newrev @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 @fork_merge_requests = @project.fork_merge_requests.opened
@commits = @project.repository.commits_between(oldrev, newrev) @commits = @project.repository.commits_between(oldrev, newrev)
......
...@@ -8,7 +8,7 @@ class EmailsOnPushWorker ...@@ -8,7 +8,7 @@ class EmailsOnPushWorker
branch = push_data["ref"] branch = push_data["ref"]
author_id = push_data["user_id"] 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 # skip if new branch was pushed or branch was removed
return true return true
end end
......
...@@ -57,9 +57,9 @@ class IrkerWorker ...@@ -57,9 +57,9 @@ class IrkerWorker
end end
def send_branch_updates(push_data, project, repo_name, committer, branch) 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 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 send_del_branch repo_name, committer, branch
end end
end end
...@@ -83,7 +83,7 @@ class IrkerWorker ...@@ -83,7 +83,7 @@ class IrkerWorker
return if push_data['total_commits_count'] == 0 return if push_data['total_commits_count'] == 0
# Next message is for number of commit pushed, if any # 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 # Tweak on push_data["before"] in order to have a nice compare URL
push_data['before'] = before_on_new_branch push_data, project push_data['before'] = before_on_new_branch push_data, project
end end
......
...@@ -33,7 +33,7 @@ class PostReceive ...@@ -33,7 +33,7 @@ class PostReceive
return false return false
end end
if tag?(ref) if Gitlab::Git.tag_ref?(ref)
GitTagPushService.new.execute(project, @user, oldrev, newrev, ref) GitTagPushService.new.execute(project, @user, oldrev, newrev, ref)
else else
GitPushService.new.execute(project, @user, oldrev, newrev, ref) GitPushService.new.execute(project, @user, oldrev, newrev, ref)
...@@ -44,10 +44,4 @@ class PostReceive ...@@ -44,10 +44,4 @@ class PostReceive
def log(message) def log(message)
Gitlab::GitLogger.error("POST-RECEIVE: #{message}") Gitlab::GitLogger.error("POST-RECEIVE: #{message}")
end end
private
def tag?(ref)
!!(/refs\/tags\/(.*)/.match(ref))
end
end end
module Gitlab module Gitlab
module Git module Git
BLANK_SHA = '0' * 40 BLANK_SHA = '0' * 40
TAG_REF_PREFIX = "refs/tags/"
BRANCH_REF_PREFIX = "refs/heads/"
def self.extract_ref_name(ref) class << self
ref.gsub(/\Arefs\/(tags|heads)\//, '') 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 end
end end
...@@ -115,7 +115,7 @@ module Gitlab ...@@ -115,7 +115,7 @@ module Gitlab
# we dont allow force push to protected branch # we dont allow force push to protected branch
if forced_push?(project, oldrev, newrev) if forced_push?(project, oldrev, newrev)
:force_push_code_to_protected_branches :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 # and we dont allow remove of protected branch
:remove_protected_branches :remove_protected_branches
elsif project.developers_can_push_to_protected_branch?(branch_name) elsif project.developers_can_push_to_protected_branch?(branch_name)
...@@ -135,8 +135,8 @@ module Gitlab ...@@ -135,8 +135,8 @@ module Gitlab
def branch_name(ref) def branch_name(ref)
ref = ref.to_s ref = ref.to_s
if ref.start_with?('refs/heads') if Gitlab::Git.branch_ref?(ref)
ref.sub(%r{\Arefs/heads/}, '') Gitlab::Git.ref_name(ref)
else else
nil nil
end end
...@@ -144,8 +144,8 @@ module Gitlab ...@@ -144,8 +144,8 @@ module Gitlab
def tag_name(ref) def tag_name(ref)
ref = ref.to_s ref = ref.to_s
if ref.start_with?('refs/tags') if Gitlab::Git.tag_ref?(ref)
ref.sub(%r{\Arefs/tags/}, '') Gitlab::Git.ref_name(ref)
else else
nil nil
end end
......
...@@ -65,12 +65,13 @@ module Gitlab ...@@ -65,12 +65,13 @@ module Gitlab
# existing project and commits to test web hooks # existing project and commits to test web hooks
def build_sample(project, user) def build_sample(project, user)
commits = project.repository.commits(project.default_branch, nil, 3) 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 end
def checkout_sha(repository, newrev, ref) def checkout_sha(repository, newrev, ref)
if newrev != Gitlab::Git::BLANK_SHA && ref.start_with?('refs/tags/') if newrev != Gitlab::Git::BLANK_SHA && Gitlab::Git.tag_ref?(ref)
tag_name = Gitlab::Git.extract_ref_name(ref) tag_name = Gitlab::Git.ref_name(ref)
tag = repository.find_tag(tag_name) tag = repository.find_tag(tag_name)
if tag if tag
......
...@@ -63,7 +63,7 @@ describe HipchatService do ...@@ -63,7 +63,7 @@ describe HipchatService do
end end
context 'tag_push events' do 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 it "should call Hipchat API for tag push events" do
hipchat.execute(push_sample_data) hipchat.execute(push_sample_data)
......
...@@ -43,7 +43,7 @@ describe SlackService::PushMessage do ...@@ -43,7 +43,7 @@ describe SlackService::PushMessage do
let(:args) { let(:args) {
{ {
after: 'after', after: 'after',
before: '000000', before: Gitlab::Git::BLANK_SHA,
project_name: 'project_name', project_name: 'project_name',
ref: 'refs/tags/new_tag', ref: 'refs/tags/new_tag',
user_name: 'user_name', user_name: 'user_name',
...@@ -61,7 +61,7 @@ describe SlackService::PushMessage do ...@@ -61,7 +61,7 @@ describe SlackService::PushMessage do
context 'new branch' do context 'new branch' do
before do before do
args[:before] = '000000' args[:before] = Gitlab::Git::BLANK_SHA
end end
it 'returns a message regarding a new branch' do it 'returns a message regarding a new branch' do
...@@ -75,7 +75,7 @@ describe SlackService::PushMessage do ...@@ -75,7 +75,7 @@ describe SlackService::PushMessage do
context 'removed branch' do context 'removed branch' do
before do before do
args[:after] = '000000' args[:after] = Gitlab::Git::BLANK_SHA
end end
it 'returns a message regarding a removed branch' do it 'returns a message regarding a removed branch' do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment