Skip to content

Commit

Permalink
test gitrepo's tagging methods
Browse files Browse the repository at this point in the history
  • Loading branch information
bronson committed Oct 31, 2011
1 parent 03244ec commit 2b0d542
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 27 deletions.
42 changes: 17 additions & 25 deletions lib/gitrepo.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -61,21 +61,15 @@ def git *args
end end
end end


# i.e. remote_add 'rails', 'http://github.com/rails/rails.git'
def remote_add name, remote def remote_add name, remote
Dir.chdir(@root) { git :remote, :add, name, remote
output = `git remote add #{name} #{remote} 2>&1`
raise GitError.new("git remote add #{name} failed: #{output}") unless $?.success?
}
end end


def remote_remove name def remote_remove name
Dir.chdir(@root) { git :remote, :rm, name
output = `git remote rm #{name} 2>&1`
raise GitError.new("git remote add #{name} failed: #{output}") unless $?.success?
}
end end



# todo: get rid of this call, should be regular git add / git commit # todo: get rid of this call, should be regular git add / git commit
def commit_all message def commit_all message
Dir.chdir(@root) { Dir.chdir(@root) {
Expand Down Expand Up @@ -105,28 +99,26 @@ def push *args
end end




def create_tag name, message, committer, branch = 'master' # todo: get rid of branch since we should only ever produce commits on master.
# todo: this blows away the environment, is there a better way of doing this? def create_tag name, message, committer, branch='master'
# gitrb doesn't handle annotated tags so we call git directly
# todo: this blows away the environment, should set env after forking & before execing
ENV['GIT_COMMITTER_NAME'] = committer[:name] ENV['GIT_COMMITTER_NAME'] = committer[:name]
ENV['GIT_COMMITTER_EMAIL'] = committer[:email] ENV['GIT_COMMITTER_EMAIL'] = committer[:email]
puts " tagging with #{gittagify(name)} from #{name}" ENV['GIT_COMMITTER_DATE'] = (committer[:date] || Time.now).strftime("%s %z")
Dir.chdir(@root) {
output = `git tag -a #{gittagify(name)} -m 'tag #{name}' #{branch} 2>&1` result = git :tag, '-a', name, '-m', message, branch
raise GitError.new("create git tag failed: #{output}") unless $?.success?
}
ENV.delete 'GIT_COMMITTER_NAME' ENV.delete 'GIT_COMMITTER_NAME'
ENV.delete 'GIT_COMMITTER_EMAIL' ENV.delete 'GIT_COMMITTER_EMAIL'
end ENV.delete 'GIT_COMMITTER_DATE'


result
end


def read_tag tagname def find_tag tagname
# gitrb doesn't handle annotated tags so we call git directly tag = git :tag, '-l', tagname
Dir.chdir(@root) { return !tag || tag =~ /^\s*$/ ? nil : tag.chomp
output = `git tag -l #{tagname} 2>&1`
raise GitError.new("read git tag failed: #{output}") unless $?.success?
output = nil if tag =~ /^\s*$/
return output
}
end end




Expand Down
3 changes: 2 additions & 1 deletion scraper
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -1257,7 +1257,7 @@ def store_versions_in_repo repo, script


# todo: rather than bailing, we should bit-for-bit verify # todo: rather than bailing, we should bit-for-bit verify
tagname = gittagify(script_version(version)) tagname = gittagify(script_version(version))
next if repo.read_tag(tagname) next if repo.find_tag(tagname)


next if corrupted_package(script, version) next if corrupted_package(script, version)


Expand All @@ -1275,6 +1275,7 @@ def store_versions_in_repo repo, script
copy_file(commit, 'README', "This is a mirror of #{script_id_to_url(script['script_id'])}\n\n" + script['description'] + "\n") unless repo.root['README'] copy_file(commit, 'README', "This is a mirror of #{script_id_to_url(script['script_id'])}\n\n" + script['description'] + "\n") unless repo.root['README']
end end
sver = script_version(version) sver = script_version(version)
puts " tagging with #{gittagify(sver)} from #{sver}"
repo.create_tag gittagify(sver), "tag #{sver}", committer, branch repo.create_tag gittagify(sver), "tag #{sver}", committer, branch
count += 1 count += 1
end end
Expand Down
43 changes: 42 additions & 1 deletion spec/gitrepo_spec.rb
Original file line number Original file line Diff line number Diff line change
@@ -1,5 +1,7 @@
# Ensures git operations can occur on repos. # Ensures git operations can occur on repos.
# Avoids stubbing as much as possible so the testfile doesn't prescribe the implementation. # Avoids stubbing as much as possible so the testfile doesn't prescribe the implementation.
#
# to run specs with 'date' in the name and preserve the results: PRESERVE=1 bundle exec rspec spec/* -e date


require File.dirname(File.absolute_path(__FILE__)) + '/../lib/gitrepo' require File.dirname(File.absolute_path(__FILE__)) + '/../lib/gitrepo'


Expand All @@ -12,7 +14,9 @@ def with_git_repo args={}, &block
stub = lambda { |tmpdir| stub = lambda { |tmpdir|
options = { :root => "#{tmpdir}/repo", :create => true } options = { :root => "#{tmpdir}/repo", :create => true }
options.merge! args options.merge! args
block.call GitRepo.new options repo = GitRepo.new options
repo.retryable_options :logger => lambda { |task,retries,error| } # turn off logging when testing
block.call repo
} }


if ENV['PRESERVE'] if ENV['PRESERVE']
Expand Down Expand Up @@ -56,4 +60,41 @@ def with_git_commit *args
repo.push :origin, :master repo.push :origin, :master
end end
end end


it "should create a tag at the right date" do
with_git_commit(:bare => true) do |repo|
date = Time.new 2010,10,10, 16,40,0, '-07:00'
committer = { :name => "test committer", :email => "testemail@example.com", :date => date }
repo.create_tag '1.2', 'tag 1.2', committer
# wish there was a better way of checking this but git show doesn't support --format for tags.
repo.git(:show, '1.2').should match /^tag 1.2\nTagger: test committer <testemail@example.com>\nDate:\s*Sun Oct 10 16:40:00 2010 -0700\n\ntag 1.2\n/
end
end

it "should not create an invalid tag" do
with_git_commit(:bare => true) do |repo|
committer = { :name => "test committer", :email => "testemail@example.com" }
lambda {
repo.create_tag '5 4.3', 'tag 5.4.3', committer
}.should raise_error(GitRepo::GitError, /is not a valid tag name/)
end
end

it "should find tags" do
with_git_commit(:bare => true) do |repo|
committer = { :name => "test committer", :email => "testemail@example.com" }
repo.create_tag '5.4.3', 'tag message for 5.4.3', committer
repo.find_tag('5.4.3').should == '5.4.3' # find an existing tag
repo.find_tag('5.4.4').should == nil # find a nonexistent tag
repo.find_tag('5 4.4').should == nil # find an invalid tag
end
end


# testing commits:
# committer same as author
# omit committer
# empty commit
# -700 in commits
end end

0 comments on commit 2b0d542

Please sign in to comment.