Browse files

Refactor the CLI into a class.

Speeds up the simple cases where invalid arguments/environment are used.
  • Loading branch information...
1 parent 8990bfd commit b6e38c538dd33eec206fc9a176ece67310598d87 Brian Donovan committed Apr 16, 2010
Showing with 70 additions and 52 deletions.
  1. +70 −52 bin/update
View
122 bin/update
@@ -11,48 +11,84 @@
# will be allowed in the repository. By default they won't be.
#
-this = File.expand_path(__FILE__)
-while File.symlink?(this)
- this = File.expand_path(File.readlink(this), File.dirname(this))
-end
+class ReviewGuardHook
+ attr_reader :refname, :oldrev, :newrev
-ENV['BUNDLE_GEMFILE'] = File.expand_path("../../Gemfile", this)
-require 'rubygems'
-require 'bundler'
-Bundler.setup
-Bundler.require
+ def initialize(args)
+ @refname = args[0]
+ @oldrev = args[1]
+ @newrev = args[2]
+ end
-# --- Command line
-refname = ARGV[0]
-oldrev = ARGV[1]
-newrev = ARGV[2]
+ def self.run(args)
+ new(args).run
+ end
-# --- Safety check
-def blank?(object)
- object == nil || object == ""
-end
+ def run
+ if blank?(git_dir)
+ $stderr.puts "Don't run this script from the command line."
+ $stderr.puts " (if you want, you could supply GIT_DIR then run"
+ $stderr.puts " #{$0} <ref> <oldrev> <newrev>)"
+ exit 1
+ end
-if blank?(ENV['GIT_DIR'])
- $stderr.puts "Don't run this script from the command line."
- $stderr.puts " (if you want, you could supply GIT_DIR then run"
- $stderr.puts " #{$0} <ref> <oldrev> <newrev>)"
- exit 1
-end
+ if blank?(refname) || blank?(oldrev) || blank?(newrev)
+ $stderr.puts "Usage: #{$0} <ref> <oldrev> <newrev>"
+ exit 1
+ end
-if blank?(refname) || blank?(oldrev) || blank?(newrev)
- $stderr.puts "Usage: #{$0} <ref> <oldrev> <newrev>"
- exit 1
-end
+ exit 0 if allow_direct_update?
-# --- Config
-allowdirectupdate = `git config --bool hooks.allowdirectupdate`
+ if unreviewed.any?
+ $stderr.puts "\n\033[1mSomeone else must merge these before they're pushed to master:\033[0m"
+ $stderr.puts
+ unreviewed.each do |commit|
+ $stderr.puts " \033[0;33m#{commit.id_abbrev}\033[0m #{commit.short_message}"
+ end
+ $stderr.puts
+ $stderr.puts "If these commits were from a merge, did you forget to use --no-ff?"
+ $stderr.puts
+ exit 1
+ end
+
+ exit 0
+ end
+
+ def unreviewed
+ @unreviewed ||= ReviewFilter.new(commits).unreviewed
+ end
+
+ def commits
+ @commits ||= repo.commits_between(oldrev, newrev)
+ end
-if allowdirectupdate == 'true'
- # no restrictions, so just exit
- exit 0
+ def repo
+ @repo ||= begin
+ require 'pathname'
+ ENV['BUNDLE_GEMFILE'] = File.expand_path("../../Gemfile", Pathname.new(__FILE__).realpath)
+ require 'rubygems'
+ require 'bundler'
+ Bundler.setup
+ Bundler.require
+
+ Grit::Repo.new(git_dir, :bare => true)
+ end
+ end
+
+ def git_dir
+ ENV['GIT_DIR']
+ end
+
+ private
+ def allow_direct_update?
+ `git config --bool hooks.allowdirectupdate` == 'true'
+ end
+
+ def blank?(object)
+ object == nil || object == ""
+ end
end
-# --- Check for merges
class ReviewFilter
attr_reader :commits, :merged, :reviewed, :unreviewed
@@ -93,22 +129,4 @@ class ReviewFilter
end
end
-repo = Grit::Repo.new(File.expand_path(ENV['GIT_DIR']), :is_bare => true)
-commits = repo.commits_between(oldrev, newrev)
-
-filter = ReviewFilter.new(commits)
-
-if filter.unreviewed.any?
- $stderr.puts "\n\033[1mSomeone else must merge these before they're pushed to master:\033[0m"
- $stderr.puts
- filter.unreviewed.each do |commit|
- $stderr.puts " \033[0;33m#{commit.id_abbrev}\033[0m #{commit.short_message}"
- end
- $stderr.puts
- $stderr.puts "If these commits were from a merge, did you forget to use --no-ff?"
- $stderr.puts
- exit 1
-end
-
-# --- Finished
-exit 0
+ReviewGuardHook.run(ARGV)

0 comments on commit b6e38c5

Please sign in to comment.