Permalink
Browse files

👕 remove a bunch of reeks

  • Loading branch information...
bronson committed Oct 7, 2016
1 parent 48d1a74 commit 2c3a7508eed9f3dc4ecc5358e9c132da73f086be
Showing with 34 additions and 32 deletions.
  1. +34 −32 lib/gitrepo.rb
View
@@ -1,12 +1,11 @@
-# interface for working on git repos, insulates app from underlying implementation
-
require 'gitrb'
require 'retryable'
-
+# helps working with git repos, insulates app from underlying implementation
class GitRepo
- include Retryable # only for network operations
+ include Retryable
+ # common failures should be GitErrors
class GitError < RuntimeError; end
attr_reader :root, :bare
@@ -20,13 +19,13 @@ def initialize opts
retryable_options opts[:retryable_options] if opts[:retryable_options]
if opts[:clone]
- retryable(:task => "cloning #{opts[:clone]}") do
+ retryable(task: "cloning #{opts[:clone]}") do
args = [opts[:clone], opts[:root]]
args.push '--bare' if opts[:bare]
git_exec :clone, *args
end
elsif opts[:create]
- Dir.mkdir opts[:root] unless test ?d, opts[:root]
+ Dir.mkdir opts[:root] unless File.directory? opts[:root]
if opts[:bare]
git :init, '--bare'
else
@@ -35,19 +34,25 @@ def initialize opts
end
# a little sanity checking, catch simple errors early
- raise GitError.new "#{@root} does not exist" unless test ?d, @root
+ raise GitError, "#{@root} does not exist" unless File.directory? @root
if opts[:bare]
- raise GitError.new "#{@root} does not appear to be a bare repo" unless test(?d, File.join(@root, 'objects'))
+ unless File.directory? File.join(@root, 'objects')
+ raise GitError, "#{@root} does not appear to be a bare repo"
+ end
else
- raise GitError.new "#{@root}/.git does not exist" unless test(?d, File.join(@root, '.git'))
- raise GitError.new "#{@root}/.git does not appear to be a git repo" unless test(?d, File.join(@root, '.git', 'objects'))
+ unless File.directory? File.join(@root, '.git')
+ raise GitError, "#{@root}/.git does not exist"
+ end
+ unless File.directory? File.join(@root, '.git', 'objects')
+ raise GitError, "#{@root}/.git does not appear to be a git repo"
+ end
end
end
def git_exec *args
- options = { :before_exec => lambda { } }
+ options = { before_exec: -> {} }
options.merge! args.pop if args[-1].is_a? Hash
- args = args.map { |a| a.to_s }
+ args = args.map(&:to_s)
out = IO.popen('-', 'r') do |io|
if io # parent
@@ -60,15 +65,15 @@ def git_exec *args
end
if $?.exitstatus > 0
- raise GitError.new("git #{args.join(' ')}: #{out}")
+ raise GitError, "git #{args.join(' ')}: #{out}"
end
out
end
def git *args
Dir.chdir(@root) do
- git_exec *args
+ git_exec(*args)
end
end
@@ -80,38 +85,35 @@ def remote_remove name
git :remote, :rm, name
end
-
def pull *args
# Can we tell the difference between a network error, which we want to retry,
# and a merge error, which we want to fail immediately?
- retryable(:task => "pulling #{args.join ' '}") do
- git :pull, '--no-rebase', *args
+ retryable(task: "pulling #{args.join ' '}") do
+ git :pull, '--no-rebase', *args
end
end
def push *args
# like pull, can we tell the difference between a network error and local error?
- retryable(:task => "pushing #{args.join ' '}") do
+ retryable(task: "pushing #{args.join ' '}") do
git :push, *args
end
end
-
def create_tag name, message, committer
# gitrb doesn't handle annotated tags so we call git directly
- git :tag, '-a', name, '-m', message, :before_exec => lambda {
+ git :tag, '-a', name, '-m', message, before_exec: lambda {
ENV['GIT_COMMITTER_NAME'] = committer[:name]
ENV['GIT_COMMITTER_EMAIL'] = committer[:email]
- ENV['GIT_COMMITTER_DATE'] = (committer[:date] || Time.now).strftime("%s %z")
+ ENV['GIT_COMMITTER_DATE'] = (committer[:date] || Time.now).strftime('%s %z')
}
end
def find_tag tagname
tag = git :tag, '-l', tagname
- return !tag || tag =~ /^\s*$/ ? nil : tag.chomp
+ !tag || tag =~ /^\s*$/ ? nil : tag.chomp
end
-
# all the things you can do while committing
class CommitHelper
def initialize repo
@@ -129,30 +131,30 @@ def remove name
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)
+ raise GitError, "no data in #{name}: #{contents.inspect}" unless contents
+ @repo.root[name] = Gitrb::Blob.new(data: contents)
end
def entries
- @repo.root.to_a.map { |name,value| name }
+ @repo.root.to_a.map { |name, _| name }
end
# If the entry is a tree, returns an array of the names in the tree.
# If a blob, returns the blob data. Othewise returns nil.
def entry name, type=nil
data = @repo.root[name]
- raise GitError.new "type was #{data.type} not #{type}" if type && type != data.type
- return data.map { |n,v| n } if data && data.type == :tree
+ raise GitError, "type was #{data.type} not #{type}" if type && type != data.type
+ return data.map { |n, _| n } if data && data.type == :tree
return data.dump if data && data.type == :blob
end
end
-
def commit message, author, committer=author
- author = Gitrb::User.new(author[:name], author[:email], author[:date] || Time.now)
- committer = Gitrb::User.new(committer[:name], committer[:email], committer[:date] || Time.now)
+ now = Time.now
+ author = Gitrb::User.new(author[:name], author[:email], author[:date] || now)
+ committer = Gitrb::User.new(committer[:name], committer[:email], committer[:date] || now)
# gitrb has a bug where it will complain about frozen strings unless you dup the path
- repo = Gitrb::Repository.new(:path => root.dup, :bare => bare, :create => false)
+ repo = Gitrb::Repository.new(path: root.dup, bare: bare, create: false)
repo.transaction(message, author, committer) do
yield CommitHelper.new(repo)
end

0 comments on commit 2c3a750

Please sign in to comment.