Introduce git_action and normalize previous_path

Feedback:
https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/7237#note_19747456
This commit is contained in:
Lin Jen-Shin 2016-12-09 20:05:35 +08:00
parent e7599eb0b7
commit 56f697466d
1 changed files with 46 additions and 26 deletions

View File

@ -862,32 +862,8 @@ class Repository
[]
end
actions.each do |action|
path = Gitlab::Git::PathHelper.normalize_path(action[:file_path]).to_s
raise Gitlab::Git::Repository::InvalidBlobName.new("Invalid path") if
path.split('/').include?('..')
case action[:action]
when :create, :update, :move
mode =
case action[:action]
when :update
index.get(path)[:mode]
when :move
index.get(action[:previous_path])[:mode]
end
mode ||= 0o100644
index.remove(action[:previous_path]) if action[:action] == :move
content = action[:encoding] == 'base64' ? Base64.decode64(action[:content]) : action[:content]
oid = rugged.write(content, :blob)
index.add(path: path, oid: oid, mode: mode)
when :delete
index.remove(path)
end
actions.each do |act|
git_action(index, act)
end
options = {
@ -1181,6 +1157,50 @@ class Repository
private
def git_action(index, action)
path = normalize_path(action[:file_path])
if action[:action] == :move
previous_path = normalize_path(action[:previous_path])
end
case action[:action]
when :create, :update, :move
mode =
case action[:action]
when :update
index.get(path)[:mode]
when :move
index.get(previous_path)[:mode]
end
mode ||= 0o100644
index.remove(previous_path) if action[:action] == :move
content = if action[:encoding] == 'base64'
Base64.decode64(action[:content])
else
action[:content]
end
oid = rugged.write(content, :blob)
index.add(path: path, oid: oid, mode: mode)
when :delete
index.remove(path)
end
end
def normalize_path(path)
pathname = Gitlab::Git::PathHelper.normalize_path(path)
if pathname.each_filename.include?('..')
raise Gitlab::Git::Repository::InvalidBlobName.new('Invalid path')
end
pathname.to_s
end
def refs_directory_exists?
return false unless path_with_namespace