Commit 576cec6c67dcc4ee00b8220ca1a45385583e25b2

Authored by Robert Speicher
1 parent 39c65793

Add BlobController, remove Refs#blob

app/controllers/blob_controller.rb 0 → 100644
... ... @@ -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 &lt; ApplicationController
18 18 respond_to do |format|
19 19 format.html do
20 20 new_path = if params[:destination] == "tree"
21   - project_tree_path(@project, params[:ref])
  21 + project_tree_path(@project, @ref)
22 22 else
23   - project_commits_path(@project, ref: params[:ref])
  23 + project_commits_path(@project, ref: @ref)
24 24 end
25 25  
26   - redirect_to new_path
  26 + redirect_to new_path
27 27 end
28   - format.js do
  28 + format.js do
29 29 @ref = params[:ref]
30 30 define_tree_vars
31 31 render "tree"
... ... @@ -33,19 +33,6 @@ class RefsController &lt; ApplicationController
33 33 end
34 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 36 def logs_tree
50 37 contents = @tree.contents
51 38 @logs = contents.map do |content|
... ... @@ -53,32 +40,12 @@ class RefsController &lt; ApplicationController
53 40 last_commit = @project.commits(@commit.id, file, 1).last
54 41 last_commit = CommitDecorator.decorate(last_commit)
55 42 {
56   - file_name: content.name,
  43 + file_name: content.name,
57 44 commit: last_commit
58 45 }
59 46 end
60 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 49 protected
83 50  
84 51 def define_tree_vars
... ... @@ -93,15 +60,15 @@ class RefsController &lt; ApplicationController
93 60  
94 61 if params[:path]
95 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 64 else
98 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 67 end
101 68 rescue
102 69 return render_404
103 70 end
104   -
  71 +
105 72 def ref
106 73 @ref = params[:id]
107 74 end
... ...
app/views/blame/show.html.haml
... ... @@ -18,7 +18,7 @@
18 18 = @tree.name
19 19 %small blame
20 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 22 = link_to "history", project_commits_path(@project, path: params[:path], ref: @ref), class: "btn very_small"
23 23 = link_to "source", project_tree_path(@project, @id), class: "btn very_small"
24 24 .file_content.blame
... ...
app/views/commits/_diffs.html.haml
... ... @@ -24,7 +24,7 @@
24 24 %i.icon-file
25 25 %span{id: "#{diff.old_path}"}= diff.old_path
26 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 28 %i.icon-file
29 29 %span{id: "#{diff.new_path}"}= diff.new_path
30 30 %br/
... ...
app/views/tree/_tree_file.html.haml
... ... @@ -5,8 +5,8 @@
5 5 = name.force_encoding('utf-8')
6 6 %small #{file.mode}
7 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 10 = link_to "blame", project_blame_path(@project, @id), class: "btn very_small"
11 11 - if file.text?
12 12 - if gitlab_markdown?(name)
... ... @@ -32,7 +32,7 @@
32 32 - else
33 33 .file_content.blob_file
34 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 36 %div.padded
37 37 %br
38 38 = image_tag "download.png", width: 64
... ...
config/routes.rb
... ... @@ -122,12 +122,6 @@ Gitlab::Application.routes.draw do
122 122 end
123 123  
124 124 member do
125   - get "blob",
126   - constraints: {
127   - id: /[a-zA-Z.0-9\/_\-]+/,
128   - path: /.*/
129   - }
130   -
131 125 # tree viewer logs
132 126 get "logs_tree", constraints: { id: /[a-zA-Z.\/0-9_\-]+/ }
133 127 get "logs_tree/:path" => "refs#logs_tree",
... ... @@ -197,7 +191,7 @@ Gitlab::Application.routes.draw do
197 191  
198 192 # XXX: WIP
199 193 resources :blame, only: [:show], constraints: {id: /.+/}
200   - # resources :blob, only: [:show], constraints: {id: /.+/}
  194 + resources :blob, only: [:show], constraints: {id: /.+/}
201 195 # resources :raw, only: [:show], constraints: {id: /.+/}
202 196 resources :tree, only: [:show], constraints: {id: /.+/}
203 197 end
... ...
spec/requests/security/project_access_spec.rb
... ... @@ -95,7 +95,7 @@ describe &quot;Application access&quot; do
95 95 before do
96 96 commit = @project.commit
97 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 99 end
100 100  
101 101 it { @blob_path.should be_allowed_for @u1 }
... ...
spec/routing/project_routing_spec.rb
... ... @@ -192,7 +192,6 @@ describe ProtectedBranchesController, &quot;routing&quot; do
192 192 end
193 193  
194 194 # switch_project_refs GET /:project_id/switch(.:format) refs#switch
195   -# blob_project_ref GET /:project_id/:id/blob(.:format) refs#blob
196 195 # logs_tree_project_ref GET /:project_id/:id/logs_tree(.:format) refs#logs_tree
197 196 # logs_file_project_ref GET /:project_id/:id/logs_tree/:path(.:format) refs#logs_tree
198 197 describe RefsController, "routing" do
... ... @@ -204,10 +203,6 @@ describe RefsController, &quot;routing&quot; do
204 203 get("/gitlabhq/stable/logs_tree").should route_to('refs#logs_tree', project_id: 'gitlabhq', id: 'stable')
205 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 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 206 end
212 207  
213 208 # diffs_project_merge_request GET /:project_id/merge_requests/:id/diffs(.:format) merge_requests#diffs
... ... @@ -400,6 +395,12 @@ describe BlameController, &quot;routing&quot; do
400 395 end
401 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 404 describe TreeController, "routing" do
404 405 it "to #show" do
405 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 436 describe "pending routing" do
436 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 439 describe "/:project_id/commit/:id" do
445 440 it "routes to a specific commit" do
446 441 get("/gitlabhq/commit/f4b1449").should route_to('commit#show', project_id: 'gitlabhq', id: 'f4b1449')
... ...