Permalink
Browse files

test committing with gitrepo

  • Loading branch information...
1 parent 2b0d542 commit 0bf3768cc00184653e4ded15b892a629bee17325 @bronson bronson committed Nov 1, 2011
Showing with 93 additions and 16 deletions.
  1. +8 −6 lib/gitrepo.rb
  2. +2 −5 scraper
  3. +83 −5 spec/gitrepo_spec.rb
View
@@ -1,7 +1,7 @@
# interface for working on git repos, insulates app from underlying implementation
# todo: only call git via array so no shell interp issues
-# todo: make a way for caller to tell Repo to indent all messages
+# todo: make a way for caller to tell GitRepo to indent all log messages?
require 'gitrb'
require 'retryable'
@@ -31,6 +31,7 @@ def initialize opts
raise "#{@root} doesn't exist" unless test ?d, @root
end
+ # todo: move gitrb so it only exists in CommitHelper
# gitrb has a bug where it will complain about frozen strings unless you dup the path
@repo = Gitrb::Repository.new(:path => @root.dup, :bare => opts[:bare], :create => opts[:create])
end
@@ -129,22 +130,23 @@ def initialize repo
end
# this empties out the commit tree so you can start fresh
- def empty
- @repo.root.to_a.map { |name,value| remove name }
+ def empty_index
+ entries.each { |name| remove name }
end
# to test: returns the value of the deleted object
def remove name
- @repo.root.delete name
+ @repo.root.delete(name).data
end
def add name, contents
+ # an empty file is represented by the empty string so contents==nil is an error
+ raise GitError.new "no data in #{name}: #{contents.inspect}" unless contents
@repo.root[name] = Gitrb::Blob.new(:data => contents)
end
- # todo: this returns |name,value| ????
def entries
- @repo.root.to_a
+ @repo.root.to_a.map { |name,value| name }
end
end
View
@@ -740,7 +740,7 @@ def mark_repo_as_duplicate dupe, new_script
repo = open_repo(script_extract_id(dupe), script_extract_name(dupe))
committer = { :name => $vimscripts_name, :email => $vimscripts_email }
repo.commit("Renamed to #{new_script['display_name']}", committer, committer) do |commit|
- commit.empty
+ commit.empty_index
commit.add 'README', "This script has been renamed to #{new_script['display_name']}.\n\n#{repo_url new_script}\n"
end
end
@@ -825,9 +825,6 @@ end
def copy_file commit, filename, contents
- # an empty file is represented by the empty string. contents==nil indicates an internal error.
- raise "no data in #{filename}: #{contents.inspect}" unless contents
-
# skip swapfiles or crap Apple files that authors accidentally check in
unless filename =~ /\.[^\/]+\.sw[n-p]$/ || filename =~ /~$/ || filename =~ /\.(?:_\.)?DS_Store$/ || filename =~ /(?:^|\/)\._/
commit.add filename, contents
@@ -1268,7 +1265,7 @@ def store_versions_in_repo repo, script
catch :corrupt do
puts " adding #{version['filename']} #{version['date']} #{script_version(version)} to branch #{branch}"
repo.commit(fix_release_notes(version), author, committer) do |commit|
- commit.empty
+ commit.empty_index
add_version commit, version, script
check_for_keymap_helper repo, script
check_for_readme_file commit
View
@@ -38,6 +38,7 @@ def with_git_commit *args
end
end
+
it "should allow remotes to be added and removed" do
with_git_commit(:bare => true) do |repo|
repo.remote_add :origin, 'http://example.com/'
@@ -47,6 +48,7 @@ def with_git_commit *args
end
end
+
it "should allow pulling" do
with_git_repo do |repo|
repo.should_receive(:git).once.with(:pull, '--no-rebase', :origin, :master)
@@ -92,9 +94,85 @@ def with_git_commit *args
end
- # testing commits:
- # committer same as author
- # omit committer
- # empty commit
- # -700 in commits
+ it "should commit with correct dates" do
+ with_git_repo(:bare => true) do |repo|
+ authordate = Time.new 2010, 9, 5, 15,30,0, '-07:00'
+ committerdate = Time.new 2010,10,10, 16,40,0, '-07:00'
+ author = { :name => "the author", :email => "auemail@example.com", :date => authordate }
+ committer = { :name => "the committer", :email => "comemail@example.com", :date => committerdate }
+ repo.commit("date commit", author, committer) do |commit|
+ commit.add "README", "This is a test readme file\n"
+ end
+
+ # This ensures the time is represented as -0700, not -700. http://vim-scripts.org/news/2011/06/23/picky-about-timezones.html
+ repo.git(:log, '--pretty=raw').should match /^commit [0-9a-f]+\ntree [0-9a-f]+\nauthor the author <auemail@example.com> 1283725800 -0700\ncommitter the committer <comemail@example.com> 1286754000 -0700\n\n\s*date commit/
+ end
+ end
+
+ it "should replace files" do
+ with_git_commit(:bare => true) do |repo|
+ author = { :name => "replace author", :email => "auemail@example.com" }
+ repo.commit("replace commit", author) do |commit|
+ commit.entries.should == ['README']
+ commit.remove('README').should == "This is a test readme file\n"
+ commit.add 'zarg one', 'some content for zarg\n'
+ commit.entries.should == ['zarg one']
+ end
+ repo.git('log', '--pretty=oneline').split("\n").length.should == 2 # 2 commits
+ repo.git('ls-tree', 'HEAD').should match /^[^\n]+zarg one$/ # only one file
+ end
+ end
+
+ def git_tree repo, spec
+ # returns an array of all entries in the tree in the format ["blob r", "tree s"]
+ repo.git('ls-tree', spec).split("\n").map { |s| s =~ /^[0-9]+ (\S+) [0-9a-f]+\t(.*)$/; "#{$1} #{$2}" }.sort
+ end
+
+ it "should handle trees" do
+ with_git_commit(:bare => true) do |repo|
+ author = { :name => "tree author", :email => "auemail@example.com" }
+ repo.commit("tree commit", author) do |commit|
+ commit.add 'a/b/c dir/d/e.txt', "e text file contents\n"
+ commit.add 'a/b/c dir/d/f.txt', "f text file contents\n"
+ commit.entries.sort.should == ['README', 'a']
+ end
+ repo.git('log', '--pretty=oneline').split("\n").length.should == 2
+ git_tree(repo, 'HEAD').should == ['blob README', 'tree a']
+ git_tree(repo, 'HEAD:a/b/c dir/d').should == ['blob e.txt', 'blob f.txt']
+ end
+ end
+
+ it "should not commit null blob contents" do
+ with_git_commit(:bare => true) do |repo|
+ author = { :name => "tree author", :email => "auemail@example.com" }
+ repo.commit("tree commit", author) do |commit|
+ lambda {
+ commit.add "nilbog", nil
+ }.should raise_error(GitRepo::GitError, /no data in nilbog: nil/)
+ end
+ # ensure we didn't create another commit
+ repo.git('log', '--pretty=oneline').split("\n").length.should == 1
+ end
+ end
+
+ it "should create empty commits" do
+ with_git_commit(:bare => true) do |repo|
+ author = { :name => "an author", :email => "auemail@example.com" }
+ repo.commit("empty commit", author) do |commit|
+ commit.empty_index
+ commit.entries.empty?.should == true
+ end
+ repo.git('ls-tree', 'HEAD').should == ''
+ end
+ end
+
+ it "should not create nop commits" do
+ # dunno why not but it's the current behavior
+ with_git_commit(:bare => true) do |repo|
+ author = { :name => "nop author", :email => "nop@example.com" }
+ repo.commit("nop commit", author) { |commit| 'do nothing' }
+ repo.git(:log, '--pretty=raw').should match /^commit [0-9a-f]+\ntree [0-9a-f]+\nauthor test author <testemail@example.com>/
+ repo.git('ls-tree', 'HEAD').should match /^[^\n]+README$/
+ end
+ end
end

0 comments on commit 0bf3768

Please sign in to comment.