Commit 03f6d1251087f1f04c267221d69cb7a63af95745

use Gitto in the BrowserController

Commit diff

app/controllers/browse_controller.rb

 
77 def index
88 @git = Gitorious::Gitto.new(@repository.full_repository_path)
99 @commits = @git.log(LOGS_PER_PAGE)
10 @tags_per_sha = @git.tags_by_sha
11 # TODO: Patch rails to keep track of what it responds to so we can DRY this up
12 @atom_auto_discovery_url = project_repository_formatted_browse_path(@project, @repository, :atom)
13 respond_to do |format|
14 format.html
15 format.atom
16 end
1017 end
1118
12 # def index
13 # @git = Git.bare(@repository.full_repository_path)
14 # @commits = @git.log(LOGS_PER_PAGE)
15 # @tags_per_sha = returning({}) do |hash|
16 # @git.tags.each do |tag|
17 # hash[tag.sha] ||= []
18 # hash[tag.sha] << tag.name
19 # end
20 # end
21 # # TODO: Patch rails to keep track of what it responds to so we can DRY this up
22 # @atom_auto_discovery_url = project_repository_formatted_browse_path(@project, @repository, :atom)
23 # respond_to do |format|
24 # format.html
25 # format.atom
26 # end
27 # end
28
2919 def tree
30 @git = Git.bare(@repository.full_repository_path)
31 @tree = @git.gtree(params[:sha])
20 @git = Gitorious::Gitto.new(@repository.full_repository_path)
21 @tree = @git.tree(params[:sha])
3222 end
3323
3424 def commit
3525 @diffmode = params[:diffmode] == "sidebyside" ? "sidebyside" : "inline"
36 @git = Git.bare(@repository.full_repository_path)
37 @commit = @git.gcommit(params[:sha])
26 @git = Gitorious::Gitto.new(@repository.full_repository_path)
27 @commit = @git.commit(params[:sha])
3828 if @commit.parent
3929 @diff = @git.diff(@commit.parent.sha || "", @commit.sha)
4030 else
3535 end
3636
3737 def diff
38 @git = Git.bare(@repository.full_repository_path)
38 @git = Gitorious::Gitto.new(@repository.full_repository_path)
3939 @diff = @git.diff(params[:sha], params[:other_sha])
4040 end
4141
4242 def blob
43 @git = Git.bare(@repository.full_repository_path)
44 @blob = @git.gblob(params[:sha])
43 @git = Gitorious::Gitto.new(@repository.full_repository_path)
44 @blob = @git.blob(params[:sha])
4545 end
4646
4747 def raw
48 @git = Git.bare(@repository.full_repository_path)
49 @blob = @git.gblob(params[:sha])
48 @git = Gitorious::Gitto.new(@repository.full_repository_path)
49 @blob = @git.blob(params[:sha])
5050 render :text => @blob.contents, :content_type => "text/plain"
5151 end
5252
5353 def log
54 @git = Git.bare(@repository.full_repository_path)
54 @git = Gitorious::Gitto.new(@repository.full_repository_path)
5555 skip = params[:page].blank? ? 0 : (params[:page].to_i-1) * LOGS_PER_PAGE
5656 @commits = @git.log(LOGS_PER_PAGE, skip)
57 @tags_per_sha = returning({}) do |hash|
58 @git.tags.each do |tag|
59 hash[tag.sha] ||= []
60 hash[tag.sha] << tag.name
61 end
62 end
57 @tags_per_sha = @git.tags_by_sha
6358 # TODO: Patch rails to keep track of what it responds to so we can DRY this up
6459 @atom_auto_discovery_url = project_repository_formatted_browse_path(@project, @repository, :atom)
6560 respond_to do |format|
toggle raw diff

lib/gitorious/gitto.rb

 
4040
4141 def tags
4242 @git.tags
43 end
43 end
44
45 def tags_by_sha
46 tags_by_sha = {}
47 tags.each do |tag|
48 tags_by_sha[tag.sha] ||= []
49 tags_by_sha[tag.sha] << tag.name
50 end
51 tags_by_sha
52 end
4453
4554 def remotes
4655 @git.remotes
toggle raw diff

spec/controllers/browse_controller_spec.rb

 
22
33describe BrowseController do
44 before(:each) do
5 login_as :johan
65 @project = projects(:johans)
76 @repository = @project.repositories.first
87
1111 .with(@repository.name).and_return(@repository)
1212 @repository.stub!(:has_commits?).and_return(true)
1313
14 @git_mock = mock("Git mock", :null_object => true)
14 @gitto = mock("Gitto mock", :null_object => true)
1515 Gitorious::Gitto.should_receive(:new) \
1616 .with(@repository.full_repository_path) \
17 .and_return(@git_mock)
17 .and_return(@gitto)
1818 end
1919
20 describe "index" do
21
20 describe "#index" do
2221 def do_get
2322 get :index, :project_id => @project.slug, :repository_id => @repository.name
2423 end
2929 end
3030
3131 it "fetches the specified log entries" do
32 @git_mock.should_receive(:log).with(BrowseController::LOGS_PER_PAGE).and_return(commits=mock("logentrues"))
32 @gitto.should_receive(:log).with(BrowseController::LOGS_PER_PAGE) \
33 .and_return(commits=mock("logentrues"))
3334 do_get
3435 assigns[:commits].should == commits
3536 end
3637
3738 it "assigns the tags for easy lookup" do
38 @git_mock.should_receive(:log).with(BrowseController::LOGS_PER_PAGE).and_return(commits=mock("logentrues"))
39 @gitto.should_receive(:log).with(BrowseController::LOGS_PER_PAGE) \
40 .and_return(mock("logentrues"))
41 @gitto.should_receive(:tags_by_sha).and_return({"foo" => ["bar"]})
42 do_get
43 assigns[:tags_per_sha].should == {"foo" => ["bar"]}
44 end
45 end
46
47 describe "#tree" do
48 it "GETs successfully" do
49 tree_mock = mock("gtree")
50 @gitto.should_receive(:tree).and_return(tree_mock)
51 get :tree, :project_id => @project.slug,
52 :repository_id => @repository.name, :sha => "a"*40
53
54 response.should be_success
55 assigns[:git].should == @gitto
56 assigns[:tree].should == tree_mock
57 end
58 end
59
60 describe "#commit" do
61 before(:each) do
62 @commit_mock = mock("commit")
63 @commit_mock.stub!(:sha).and_return("a"*40)
64 @commit_mock.stub!(:parent).and_return(@commit_mock)
65 @diff_mock = mock("diff")
66 @gitto.should_receive(:diff).with(@commit_mock.parent.sha,
67 @commit_mock.sha).and_return(@diff_mock)
68 @gitto.should_receive(:commit).with("a"*40).and_return(@commit_mock)
69 end
70 def do_get(opts={})
71 get :commit, {:project_id => @project.slug,
72 :repository_id => @repository.name, :sha => "a"*40}.merge(opts)
73 end
74 it "gets the commit data" do
75 do_get
76 response.should be_success
77 assigns[:git].should == @gitto
78 assigns[:commit].should == @commit_mock
79 assigns[:diff].should == @diff_mock
80 end
81
82 it "gets the comments for the commit" do
83 do_get
84 assigns[:comment_count].should == 0
85 end
86
87 it "defaults to 'inline' diffmode" do
88 do_get
89 assigns[:diffmode].should == "inline"
90 end
91
92 it "sets sidebyside diffmode" do
93 do_get(:diffmode => "sidebyside")
94 assigns[:diffmode].should == "sidebyside"
95 end
96 end
97
98 describe "#diff" do
99 it "diffs the sha's provided" do
100 diff_mock = mock("diff")
101 @gitto.should_receive(:diff).with("a"*40, "b"*40).and_return(diff_mock)
102
103 get :diff, {:project_id => @project.slug,
104 :repository_id => @repository.name, :sha => "a"*40, :other_sha => "b"*40}
105
106 response.should be_success
107 assigns[:git].should == @gitto
108 assigns[:diff].should == diff_mock
109 end
110 end
111
112 describe "#blob" do
113 it "gets the blob data for the sha provided" do
114 blob_mock = mock("blob")
115 @gitto.should_receive(:blob).with("a"*40).and_return(blob_mock)
116
117 get :blob, {:project_id => @project.slug,
118 :repository_id => @repository.name, :sha => "a"*40}
119
120 response.should be_success
121 assigns[:git].should == @gitto
122 assigns[:blob].should == blob_mock
123 end
124 end
125
126 describe "#raw" do
127 it "gets the blob data from the sha and renders it as text/plain" do
128 blob_mock = mock("blob")
129 blob_mock.stub!(:contents).and_return("blabla")
130 @gitto.should_receive(:blob).with("a"*40).and_return(blob_mock)
131
132 get :raw, {:project_id => @project.slug,
133 :repository_id => @repository.name, :sha => "a"*40}
134
135 response.should be_success
136 assigns[:git].should == @gitto
137 assigns[:blob].should == blob_mock
138 response.body.should == "blabla"
139 response.content_type.should == "text/plain"
140 end
141 end
142
143 describe "#log" do
144 def do_get(opts = {})
145 get :log, {:project_id => @project.slug,
146 :repository_id => @repository.name, :page => nil}.merge(opts)
147 end
148
149 it "GETs page 1 successfully" do
150 @gitto.should_receive(:log).with(30, 0).and_return(mock("logentries"))
151 do_get
152 end
153
154 it "GETs page 3 successfully" do
155 @gitto.should_receive(:log).with(30, 60).and_return(mock("logentries"))
156 do_get(:page => 3)
157 end
158
159 it "GETs the commits successfully" do
160 commits = mock("logentries")
161 @gitto.should_receive(:log).with(30, 0).and_return(commits)
162 do_get
163 response.should be_success
164 assigns[:git].should == @gitto
165 assigns[:commits].should == commits
166 end
167
168 it "assigns the tags for easy lookup" do
169 @gitto.should_receive(:log).with(30, 0).and_return(mock("logentries"))
170 @gitto.should_receive(:tags_by_sha).and_return({"foo" => ["bar"]})
39171 do_get
40 assigns[:tags_per_sha].should == {:write => "me"}
172 assigns[:tags_per_sha].should == {"foo" => ["bar"]}
41173 end
42174 end
43175
toggle raw diff

spec/lib/gitorious/gitto_spec.rb

 
11require File.dirname(__FILE__) + '/../../spec_helper'
2require "ostruct"
23
34describe Gitorious::Gitto do
45
7070 @gitto.remotes
7171 end
7272
73 it "returns a list of tags grouped by sha" do
74 tag1 = OpenStruct.new(:name => "tag1", :sha => some_sha("a"))
75 tag2 = OpenStruct.new(:name => "tag2", :sha => some_sha("b"))
76 @git_mock.should_receive(:tags).exactly(3).times.and_return([tag1, tag2])
77
78 @gitto.tags_by_sha.keys.sort.should == [tag1.sha, tag2.sha]
79 @gitto.tags_by_sha[tag1.sha].should == [tag1.name]
80 @gitto.tags_by_sha[tag2.sha].should == [tag2.name]
81 end
82
7383 describe "objectish validation" do
7484 it "accepts good objectish" do
7585 proc{
toggle raw diff