Implemented repository status
I am thinking use it for both graphs and tarball generators
Signed-off-by: David A. Cuadrado <krawek@gmail.com>
| |   |
| 219 | 219 | cloners.create(:ip => ip, :date => Time.now.utc, :country_code => country_code, :country => country_name) |
| 220 | 220 | end |
| 221 | 221 | |
| 222 | def status_file(action, count = nil) |
| 223 | count = git.commit_count("HEAD") if !count |
| 224 | File.join(RAILS_ROOT, "tmp", "repos", |
| 225 | "#{project.slug}_#{self.name}_#{count}_#{action}.status") |
| 226 | end |
| 227 | |
| 228 | def has_changed?(action, head = "master") |
| 229 | count = git.commit_count(head) |
| 230 | !File.exist?(status_file(action, count)) |
| 231 | end |
| 232 | |
| 233 | def keep_status!(action, head = "master") |
| 234 | count = git.commit_count(head) |
| 235 | filepath = status_file(action, count) |
| 236 | if !File.directory?(File.dirname(filepath)) |
| 237 | FileUtils.mkpath(File.dirname(filepath)) |
| 238 | end |
| 239 | |
| 240 | Dir[status_file(action, "*")].each do |st| |
| 241 | File.unlink(st) |
| 242 | end |
| 243 | |
| 244 | File.open(filepath, "w") do |file| |
| 245 | file << Time.now.to_i |
| 246 | end |
| 247 | end |
| 248 | |
| 222 | 249 | protected |
| 223 | 250 | def set_as_mainline_if_first |
| 224 | 251 | unless project.repositories.size >= 1 |
| toggle raw diff |
--- a/app/models/repository.rb
+++ b/app/models/repository.rb
@@ -219,6 +219,33 @@ class Repository < ActiveRecord::Base
cloners.create(:ip => ip, :date => Time.now.utc, :country_code => country_code, :country => country_name)
end
+ def status_file(action, count = nil)
+ count = git.commit_count("HEAD") if !count
+ File.join(RAILS_ROOT, "tmp", "repos",
+ "#{project.slug}_#{self.name}_#{count}_#{action}.status")
+ end
+
+ def has_changed?(action, head = "master")
+ count = git.commit_count(head)
+ !File.exist?(status_file(action, count))
+ end
+
+ def keep_status!(action, head = "master")
+ count = git.commit_count(head)
+ filepath = status_file(action, count)
+ if !File.directory?(File.dirname(filepath))
+ FileUtils.mkpath(File.dirname(filepath))
+ end
+
+ Dir[status_file(action, "*")].each do |st|
+ File.unlink(st)
+ end
+
+ File.open(filepath, "w") do |file|
+ file << Time.now.to_i
+ end
+ end
+
protected
def set_as_mainline_if_first
unless project.repositories.size >= 1 |
| |   |
| 307 | 307 | users.values.map(&:login).sort.should == users(:johan, :moe).map(&:login).sort |
| 308 | 308 | end |
| 309 | 309 | |
| 310 | it "should keep the status file" do |
| 311 | mock_path = Dir.tmpdir+"/repo_status" |
| 312 | |
| 313 | my_git = Grit::Repo.new(@repository.full_repository_path) |
| 314 | @repository.should_receive(:git).and_return(my_git) |
| 315 | |
| 316 | my_git.should_receive(:commit_count).with("master").and_return(10) |
| 317 | @repository.should_receive(:status_file).with("spec", 10).and_return(mock_path) |
| 318 | @repository.should_receive(:status_file).with("spec", "*").and_return("") |
| 319 | Dir.should_receive("[]").and_return([]) |
| 320 | @repository.keep_status!("spec") |
| 321 | end |
| 322 | |
| 323 | it "should return the status file path" do |
| 324 | @repository.status_file("spec", 10).should =~ /_10_spec\.status$/ |
| 325 | end |
| 326 | |
| 327 | it "should know if the repository was changed" do |
| 328 | my_git = Grit::Repo.new(@repository.full_repository_path) |
| 329 | @repository.should_receive(:git).and_return(my_git) |
| 330 | my_git.should_receive(:commit_count).with("master").and_return(10) |
| 331 | |
| 332 | @repository.has_changed?("spec", "master").should == true |
| 333 | end |
| 334 | |
| 310 | 335 | describe "observers" do |
| 311 | 336 | it "sends an email to the admin if there's a parent" do |
| 312 | 337 | Mailer.should_receive(:deliver_new_repository_clone).with(@repository).and_return(true) |
| toggle raw diff |
--- a/spec/models/repository_spec.rb
+++ b/spec/models/repository_spec.rb
@@ -307,6 +307,31 @@ describe Repository do
users.values.map(&:login).sort.should == users(:johan, :moe).map(&:login).sort
end
+ it "should keep the status file" do
+ mock_path = Dir.tmpdir+"/repo_status"
+
+ my_git = Grit::Repo.new(@repository.full_repository_path)
+ @repository.should_receive(:git).and_return(my_git)
+
+ my_git.should_receive(:commit_count).with("master").and_return(10)
+ @repository.should_receive(:status_file).with("spec", 10).and_return(mock_path)
+ @repository.should_receive(:status_file).with("spec", "*").and_return("")
+ Dir.should_receive("[]").and_return([])
+ @repository.keep_status!("spec")
+ end
+
+ it "should return the status file path" do
+ @repository.status_file("spec", 10).should =~ /_10_spec\.status$/
+ end
+
+ it "should know if the repository was changed" do
+ my_git = Grit::Repo.new(@repository.full_repository_path)
+ @repository.should_receive(:git).and_return(my_git)
+ my_git.should_receive(:commit_count).with("master").and_return(10)
+
+ @repository.has_changed?("spec", "master").should == true
+ end
+
describe "observers" do
it "sends an email to the admin if there's a parent" do
Mailer.should_receive(:deliver_new_repository_clone).with(@repository).and_return(true) |