Commit 576cec6c67dcc4ee00b8220ca1a45385583e25b2
1 parent
39c65793
Exists in
master
and in
4 other branches
Add BlobController, remove Refs#blob
Showing
8 changed files
with
83 additions
and
65 deletions
Show diff stats
| @@ -0,0 +1,62 @@ | @@ -0,0 +1,62 @@ | ||
| 1 | +# Controller for viewing a file's blame | ||
| 2 | +class BlobController < ApplicationController | ||
| 3 | + # Thrown when given an invalid path | ||
| 4 | + class InvalidPathError < StandardError; end | ||
| 5 | + | ||
| 6 | + include RefExtractor | ||
| 7 | + include Gitlab::Encode | ||
| 8 | + | ||
| 9 | + layout "project" | ||
| 10 | + | ||
| 11 | + before_filter :project | ||
| 12 | + | ||
| 13 | + # Authorize | ||
| 14 | + before_filter :add_project_abilities | ||
| 15 | + before_filter :authorize_read_project! | ||
| 16 | + before_filter :authorize_code_access! | ||
| 17 | + before_filter :require_non_empty_project | ||
| 18 | + | ||
| 19 | + before_filter :define_tree_vars | ||
| 20 | + | ||
| 21 | + def show | ||
| 22 | + if @tree.is_blob? | ||
| 23 | + if @tree.text? | ||
| 24 | + encoding = detect_encoding(@tree.data) | ||
| 25 | + mime_type = encoding ? "text/plain; charset=#{encoding}" : "text/plain" | ||
| 26 | + else | ||
| 27 | + mime_type = @tree.mime_type | ||
| 28 | + end | ||
| 29 | + | ||
| 30 | + send_data( | ||
| 31 | + @tree.data, | ||
| 32 | + type: mime_type, | ||
| 33 | + disposition: 'inline', | ||
| 34 | + filename: @tree.name | ||
| 35 | + ) | ||
| 36 | + else | ||
| 37 | + not_found! | ||
| 38 | + end | ||
| 39 | + end | ||
| 40 | + | ||
| 41 | + private | ||
| 42 | + | ||
| 43 | + def define_tree_vars | ||
| 44 | + @ref, @path = extract_ref(params[:id]) | ||
| 45 | + | ||
| 46 | + @id = File.join(@ref, @path) | ||
| 47 | + @repo = @project.repo | ||
| 48 | + @commit = CommitDecorator.decorate(@project.commit(@ref)) | ||
| 49 | + | ||
| 50 | + @tree = Tree.new(@commit.tree, @project, @ref, @path) | ||
| 51 | + @tree = TreeDecorator.new(@tree) | ||
| 52 | + | ||
| 53 | + raise InvalidPathError if @tree.invalid? | ||
| 54 | + | ||
| 55 | + @hex_path = Digest::SHA1.hexdigest(@path) | ||
| 56 | + | ||
| 57 | + @history_path = project_tree_path(@project, @id) | ||
| 58 | + @logs_path = logs_file_project_ref_path(@project, @ref, @path) | ||
| 59 | + rescue NoMethodError, InvalidPathError | ||
| 60 | + not_found! | ||
| 61 | + end | ||
| 62 | +end |
app/controllers/refs_controller.rb
| @@ -18,14 +18,14 @@ class RefsController < ApplicationController | @@ -18,14 +18,14 @@ class RefsController < ApplicationController | ||
| 18 | respond_to do |format| | 18 | respond_to do |format| |
| 19 | format.html do | 19 | format.html do |
| 20 | new_path = if params[:destination] == "tree" | 20 | new_path = if params[:destination] == "tree" |
| 21 | - project_tree_path(@project, params[:ref]) | 21 | + project_tree_path(@project, @ref) |
| 22 | else | 22 | else |
| 23 | - project_commits_path(@project, ref: params[:ref]) | 23 | + project_commits_path(@project, ref: @ref) |
| 24 | end | 24 | end |
| 25 | 25 | ||
| 26 | - redirect_to new_path | 26 | + redirect_to new_path |
| 27 | end | 27 | end |
| 28 | - format.js do | 28 | + format.js do |
| 29 | @ref = params[:ref] | 29 | @ref = params[:ref] |
| 30 | define_tree_vars | 30 | define_tree_vars |
| 31 | render "tree" | 31 | render "tree" |
| @@ -33,19 +33,6 @@ class RefsController < ApplicationController | @@ -33,19 +33,6 @@ class RefsController < ApplicationController | ||
| 33 | end | 33 | end |
| 34 | end | 34 | end |
| 35 | 35 | ||
| 36 | - # | ||
| 37 | - # Repository preview | ||
| 38 | - # | ||
| 39 | - def tree | ||
| 40 | - respond_to do |format| | ||
| 41 | - format.html | ||
| 42 | - format.js do | ||
| 43 | - # disable cache to allow back button works | ||
| 44 | - no_cache_headers | ||
| 45 | - end | ||
| 46 | - end | ||
| 47 | - end | ||
| 48 | - | ||
| 49 | def logs_tree | 36 | def logs_tree |
| 50 | contents = @tree.contents | 37 | contents = @tree.contents |
| 51 | @logs = contents.map do |content| | 38 | @logs = contents.map do |content| |
| @@ -53,32 +40,12 @@ class RefsController < ApplicationController | @@ -53,32 +40,12 @@ class RefsController < ApplicationController | ||
| 53 | last_commit = @project.commits(@commit.id, file, 1).last | 40 | last_commit = @project.commits(@commit.id, file, 1).last |
| 54 | last_commit = CommitDecorator.decorate(last_commit) | 41 | last_commit = CommitDecorator.decorate(last_commit) |
| 55 | { | 42 | { |
| 56 | - file_name: content.name, | 43 | + file_name: content.name, |
| 57 | commit: last_commit | 44 | commit: last_commit |
| 58 | } | 45 | } |
| 59 | end | 46 | end |
| 60 | end | 47 | end |
| 61 | 48 | ||
| 62 | - def blob | ||
| 63 | - if @tree.is_blob? | ||
| 64 | - if @tree.text? | ||
| 65 | - encoding = detect_encoding(@tree.data) | ||
| 66 | - mime_type = encoding ? "text/plain; charset=#{encoding}" : "text/plain" | ||
| 67 | - else | ||
| 68 | - mime_type = @tree.mime_type | ||
| 69 | - end | ||
| 70 | - | ||
| 71 | - send_data( | ||
| 72 | - @tree.data, | ||
| 73 | - type: mime_type, | ||
| 74 | - disposition: 'inline', | ||
| 75 | - filename: @tree.name | ||
| 76 | - ) | ||
| 77 | - else | ||
| 78 | - head(404) | ||
| 79 | - end | ||
| 80 | - end | ||
| 81 | - | ||
| 82 | protected | 49 | protected |
| 83 | 50 | ||
| 84 | def define_tree_vars | 51 | def define_tree_vars |
| @@ -93,15 +60,15 @@ class RefsController < ApplicationController | @@ -93,15 +60,15 @@ class RefsController < ApplicationController | ||
| 93 | 60 | ||
| 94 | if params[:path] | 61 | if params[:path] |
| 95 | @history_path = project_tree_path(@project, File.join(@ref, params[:path])) | 62 | @history_path = project_tree_path(@project, File.join(@ref, params[:path])) |
| 96 | - @logs_path = logs_file_project_ref_path(@project, @ref, params[:path]) | 63 | + @logs_path = logs_file_project_ref_path(@project, @ref, params[:path]) |
| 97 | else | 64 | else |
| 98 | @history_path = project_tree_path(@project, @ref) | 65 | @history_path = project_tree_path(@project, @ref) |
| 99 | - @logs_path = logs_tree_project_ref_path(@project, @ref) | 66 | + @logs_path = logs_tree_project_ref_path(@project, @ref) |
| 100 | end | 67 | end |
| 101 | rescue | 68 | rescue |
| 102 | return render_404 | 69 | return render_404 |
| 103 | end | 70 | end |
| 104 | - | 71 | + |
| 105 | def ref | 72 | def ref |
| 106 | @ref = params[:id] | 73 | @ref = params[:id] |
| 107 | end | 74 | end |
app/views/blame/show.html.haml
| @@ -18,7 +18,7 @@ | @@ -18,7 +18,7 @@ | ||
| 18 | = @tree.name | 18 | = @tree.name |
| 19 | %small blame | 19 | %small blame |
| 20 | %span.options | 20 | %span.options |
| 21 | - = link_to "raw", blob_project_ref_path(@project, @ref, path: params[:path]), class: "btn very_small", target: "_blank" | 21 | + = link_to "raw", project_blob_path(@project, @id), class: "btn very_small", target: "_blank" |
| 22 | = link_to "history", project_commits_path(@project, path: params[:path], ref: @ref), class: "btn very_small" | 22 | = link_to "history", project_commits_path(@project, path: params[:path], ref: @ref), class: "btn very_small" |
| 23 | = link_to "source", project_tree_path(@project, @id), class: "btn very_small" | 23 | = link_to "source", project_tree_path(@project, @id), class: "btn very_small" |
| 24 | .file_content.blame | 24 | .file_content.blame |
app/views/commits/_diffs.html.haml
| @@ -24,7 +24,7 @@ | @@ -24,7 +24,7 @@ | ||
| 24 | %i.icon-file | 24 | %i.icon-file |
| 25 | %span{id: "#{diff.old_path}"}= diff.old_path | 25 | %span{id: "#{diff.old_path}"}= diff.old_path |
| 26 | - else | 26 | - else |
| 27 | - = link_to project_tree_path(@project, @commit, diff.new_path) do | 27 | + = link_to project_tree_path(@project, tree_join(@commit.id, diff.new_path)) do |
| 28 | %i.icon-file | 28 | %i.icon-file |
| 29 | %span{id: "#{diff.new_path}"}= diff.new_path | 29 | %span{id: "#{diff.new_path}"}= diff.new_path |
| 30 | %br/ | 30 | %br/ |
app/views/tree/_tree_file.html.haml
| @@ -5,8 +5,8 @@ | @@ -5,8 +5,8 @@ | ||
| 5 | = name.force_encoding('utf-8') | 5 | = name.force_encoding('utf-8') |
| 6 | %small #{file.mode} | 6 | %small #{file.mode} |
| 7 | %span.options | 7 | %span.options |
| 8 | - = link_to "raw", blob_project_ref_path(@project, @ref, path: @path), class: "btn very_small", target: "_blank" | ||
| 9 | - = link_to "history", project_commits_path(@project, path: params[:path], ref: @ref), class: "btn very_small" | 8 | + = link_to "raw", project_blob_path(@project, @id), class: "btn very_small", target: "_blank" |
| 9 | + = link_to "history", project_commits_path(@project, path: @path, ref: @ref), class: "btn very_small" | ||
| 10 | = link_to "blame", project_blame_path(@project, @id), class: "btn very_small" | 10 | = link_to "blame", project_blame_path(@project, @id), class: "btn very_small" |
| 11 | - if file.text? | 11 | - if file.text? |
| 12 | - if gitlab_markdown?(name) | 12 | - if gitlab_markdown?(name) |
| @@ -32,7 +32,7 @@ | @@ -32,7 +32,7 @@ | ||
| 32 | - else | 32 | - else |
| 33 | .file_content.blob_file | 33 | .file_content.blob_file |
| 34 | %center | 34 | %center |
| 35 | - = link_to blob_project_ref_path(@project, @ref, path: params[:path]) do | 35 | + = link_to project_blob_path(@project, @id) do |
| 36 | %div.padded | 36 | %div.padded |
| 37 | %br | 37 | %br |
| 38 | = image_tag "download.png", width: 64 | 38 | = image_tag "download.png", width: 64 |
config/routes.rb
| @@ -122,12 +122,6 @@ Gitlab::Application.routes.draw do | @@ -122,12 +122,6 @@ Gitlab::Application.routes.draw do | ||
| 122 | end | 122 | end |
| 123 | 123 | ||
| 124 | member do | 124 | member do |
| 125 | - get "blob", | ||
| 126 | - constraints: { | ||
| 127 | - id: /[a-zA-Z.0-9\/_\-]+/, | ||
| 128 | - path: /.*/ | ||
| 129 | - } | ||
| 130 | - | ||
| 131 | # tree viewer logs | 125 | # tree viewer logs |
| 132 | get "logs_tree", constraints: { id: /[a-zA-Z.\/0-9_\-]+/ } | 126 | get "logs_tree", constraints: { id: /[a-zA-Z.\/0-9_\-]+/ } |
| 133 | get "logs_tree/:path" => "refs#logs_tree", | 127 | get "logs_tree/:path" => "refs#logs_tree", |
| @@ -197,7 +191,7 @@ Gitlab::Application.routes.draw do | @@ -197,7 +191,7 @@ Gitlab::Application.routes.draw do | ||
| 197 | 191 | ||
| 198 | # XXX: WIP | 192 | # XXX: WIP |
| 199 | resources :blame, only: [:show], constraints: {id: /.+/} | 193 | resources :blame, only: [:show], constraints: {id: /.+/} |
| 200 | - # resources :blob, only: [:show], constraints: {id: /.+/} | 194 | + resources :blob, only: [:show], constraints: {id: /.+/} |
| 201 | # resources :raw, only: [:show], constraints: {id: /.+/} | 195 | # resources :raw, only: [:show], constraints: {id: /.+/} |
| 202 | resources :tree, only: [:show], constraints: {id: /.+/} | 196 | resources :tree, only: [:show], constraints: {id: /.+/} |
| 203 | end | 197 | end |
spec/requests/security/project_access_spec.rb
| @@ -95,7 +95,7 @@ describe "Application access" do | @@ -95,7 +95,7 @@ describe "Application access" do | ||
| 95 | before do | 95 | before do |
| 96 | commit = @project.commit | 96 | commit = @project.commit |
| 97 | path = commit.tree.contents.select { |i| i.is_a?(Grit::Blob)}.first.name | 97 | path = commit.tree.contents.select { |i| i.is_a?(Grit::Blob)}.first.name |
| 98 | - @blob_path = blob_project_ref_path(@project, commit.id, path: path) | 98 | + @blob_path = project_blob_path(@project, File.join(commit.id, path)) |
| 99 | end | 99 | end |
| 100 | 100 | ||
| 101 | it { @blob_path.should be_allowed_for @u1 } | 101 | it { @blob_path.should be_allowed_for @u1 } |
spec/routing/project_routing_spec.rb
| @@ -192,7 +192,6 @@ describe ProtectedBranchesController, "routing" do | @@ -192,7 +192,6 @@ describe ProtectedBranchesController, "routing" do | ||
| 192 | end | 192 | end |
| 193 | 193 | ||
| 194 | # switch_project_refs GET /:project_id/switch(.:format) refs#switch | 194 | # switch_project_refs GET /:project_id/switch(.:format) refs#switch |
| 195 | -# blob_project_ref GET /:project_id/:id/blob(.:format) refs#blob | ||
| 196 | # logs_tree_project_ref GET /:project_id/:id/logs_tree(.:format) refs#logs_tree | 195 | # logs_tree_project_ref GET /:project_id/:id/logs_tree(.:format) refs#logs_tree |
| 197 | # logs_file_project_ref GET /:project_id/:id/logs_tree/:path(.:format) refs#logs_tree | 196 | # logs_file_project_ref GET /:project_id/:id/logs_tree/:path(.:format) refs#logs_tree |
| 198 | describe RefsController, "routing" do | 197 | describe RefsController, "routing" do |
| @@ -204,10 +203,6 @@ describe RefsController, "routing" do | @@ -204,10 +203,6 @@ describe RefsController, "routing" do | ||
| 204 | get("/gitlabhq/stable/logs_tree").should route_to('refs#logs_tree', project_id: 'gitlabhq', id: 'stable') | 203 | get("/gitlabhq/stable/logs_tree").should route_to('refs#logs_tree', project_id: 'gitlabhq', id: 'stable') |
| 205 | get("/gitlabhq/stable/logs_tree/foo/bar/baz").should route_to('refs#logs_tree', project_id: 'gitlabhq', id: 'stable', path: 'foo/bar/baz') | 204 | get("/gitlabhq/stable/logs_tree/foo/bar/baz").should route_to('refs#logs_tree', project_id: 'gitlabhq', id: 'stable', path: 'foo/bar/baz') |
| 206 | end | 205 | end |
| 207 | - | ||
| 208 | - it "to #blob" do | ||
| 209 | - get("/gitlabhq/stable/blob").should route_to('refs#blob', project_id: 'gitlabhq', id: 'stable') | ||
| 210 | - end | ||
| 211 | end | 206 | end |
| 212 | 207 | ||
| 213 | # diffs_project_merge_request GET /:project_id/merge_requests/:id/diffs(.:format) merge_requests#diffs | 208 | # diffs_project_merge_request GET /:project_id/merge_requests/:id/diffs(.:format) merge_requests#diffs |
| @@ -400,6 +395,12 @@ describe BlameController, "routing" do | @@ -400,6 +395,12 @@ describe BlameController, "routing" do | ||
| 400 | end | 395 | end |
| 401 | end | 396 | end |
| 402 | 397 | ||
| 398 | +describe BlobController, "routing" do | ||
| 399 | + it "to #show" do | ||
| 400 | + get("/gitlabhq/blob/master/app/models/project.rb").should route_to('blob#show', project_id: 'gitlabhq', id: 'master/app/models/project.rb') | ||
| 401 | + end | ||
| 402 | +end | ||
| 403 | + | ||
| 403 | describe TreeController, "routing" do | 404 | describe TreeController, "routing" do |
| 404 | it "to #show" do | 405 | it "to #show" do |
| 405 | get("/gitlabhq/tree/master/app/models/project.rb").should route_to('tree#show', project_id: 'gitlabhq', id: 'master/app/models/project.rb') | 406 | get("/gitlabhq/tree/master/app/models/project.rb").should route_to('tree#show', project_id: 'gitlabhq', id: 'master/app/models/project.rb') |
| @@ -435,12 +436,6 @@ end | @@ -435,12 +436,6 @@ end | ||
| 435 | describe "pending routing" do | 436 | describe "pending routing" do |
| 436 | before { pending } | 437 | before { pending } |
| 437 | 438 | ||
| 438 | - describe "/:project_id/blob/:id" do | ||
| 439 | - it "routes to a ref with a path" do | ||
| 440 | - get("/gitlabhq/blob/master/app/models/project.rb").should route_to('blob#show', project_id: 'gitlabhq', id: 'master/app/models/project.rb') | ||
| 441 | - end | ||
| 442 | - end | ||
| 443 | - | ||
| 444 | describe "/:project_id/commit/:id" do | 439 | describe "/:project_id/commit/:id" do |
| 445 | it "routes to a specific commit" do | 440 | it "routes to a specific commit" do |
| 446 | get("/gitlabhq/commit/f4b1449").should route_to('commit#show', project_id: 'gitlabhq', id: 'f4b1449') | 441 | get("/gitlabhq/commit/f4b1449").should route_to('commit#show', project_id: 'gitlabhq', id: 'f4b1449') |