lib/braid/operations.rb in braid-0.6.2 vs lib/braid/operations.rb in braid-0.7.0

- old
+ new

@@ -1,8 +1,8 @@ require 'singleton' require 'rubygems' -require 'open4' +require defined?(JRUBY_VERSION) ? 'open3' : 'open4' require 'tempfile' module Braid module Operations class ShellExecutionError < BraidError @@ -14,12 +14,12 @@ @err.to_s.split("\n").first end end class VersionTooLow < BraidError def initialize(command, version, required) - @command = command - @version = version.to_s.split("\n").first + @command = command + @version = version.to_s.split("\n").first @required = required end def message "#{@command} version too low: #{@version}. #{@required} needed." @@ -43,20 +43,23 @@ # The command proxy is meant to encapsulate commands such as git, git-svn and svn, that work with subcommands. class Proxy include Singleton - def self.command; name.split('::').last.downcase; end # hax! + def self.command; + name.split('::').last.downcase; + end + # hax! def version status, out, err = exec!("#{self.class.command} --version") out.sub(/^.* version/, "").strip end def require_version(required) required = required.split(".") - actual = version.split(".") + actual = version.split(".") actual.each_with_index do |actual_piece, idx| required_piece = required[idx] return true unless required_piece @@ -77,66 +80,76 @@ def require_version!(required) require_version(required) || raise(VersionTooLow.new(self.class.command, version, required)) end private - def command(name) - # stub - name - end - def invoke(arg, *args) - exec!("#{command(arg)} #{args.join(' ')}".strip)[1].strip # return stdout - end + def command(name) + # stub + name + end - def method_missing(name, *args) - invoke(name, *args) - end + def invoke(arg, *args) + exec!("#{command(arg)} #{args.join(' ')}".strip)[1].strip # return stdout + end - def exec(cmd) - cmd.strip! + def method_missing(name, *args) + invoke(name, *args) + end - previous_lang = ENV['LANG'] - ENV['LANG'] = 'C' + def exec(cmd) + cmd.strip! - out, err = nil - log(cmd) + previous_lang = ENV['LANG'] + ENV['LANG'] = 'C' + + out, err = nil + log(cmd) + + if defined?(JRUBY_VERSION) + Open3.popen3(cmd) do |stdin, stdout, stderr| + out = stdout.read + err = stderr.read + end + status = $?.exitstatus + else status = Open4.popen4(cmd) do |pid, stdin, stdout, stderr| out = stdout.read err = stderr.read end.exitstatus - [status, out, err] - - ensure - ENV['LANG'] = previous_lang end - def exec!(cmd) - status, out, err = exec(cmd) - raise ShellExecutionError, err unless status == 0 - [status, out, err] - end + [status, out, err] + ensure + ENV['LANG'] = previous_lang + end - def sh(cmd, message = nil) - message ||= "could not fetch" if cmd =~ /fetch/ - log(cmd) - `#{cmd}` - raise ShellExecutionError, message unless $?.exitstatus == 0 - true - end + def exec!(cmd) + status, out, err = exec(cmd) + raise ShellExecutionError, err unless status == 0 + [status, out, err] + end - def msg(str) - puts "Braid: #{str}" - end + def sh(cmd, message = nil) + message ||= "could not fetch" if cmd =~ /fetch/ + log(cmd) + `#{cmd}` + raise ShellExecutionError, message unless $?.exitstatus == 0 + true + end - def log(cmd) - msg "Executing `#{cmd}`" if verbose? - end + def msg(str) + puts "Braid: #{str}" + end - def verbose? - Braid.verbose - end + def log(cmd) + msg "Executing `#{cmd}`" if verbose? + end + + def verbose? + Braid.verbose + end end class Git < Proxy def commit(message, *args) cmd = "git commit --no-verify" @@ -273,37 +286,51 @@ out[2..-1] end def apply(diff, *args) err = nil - status = Open4.popen4("git apply --index --whitespace=nowarn #{args.join(' ')} -") do |pid, stdin, stdout, stderr| - stdin.puts(diff) - stdin.close - err = stderr.read - end.exitstatus + if defined?(JRUBY_VERSION) + Open3.popen3("git apply --index --whitespace=nowarn #{args.join(' ')} -") do |stdin, stdout, stderr| + stdin.puts(diff) + stdin.close + err = stderr.read + end + status = $?.exitstatus + else + status = Open4.popen4("git apply --index --whitespace=nowarn #{args.join(' ')} -") do |pid, stdin, stdout, stderr| + stdin.puts(diff) + stdin.close + err = stderr.read + end.exitstatus + end + raise ShellExecutionError, err unless status == 0 true end def clone(*args) # overrides builtin invoke(:clone, *args) end private - def command(name) - "#{self.class.command} #{name.to_s.gsub('_', '-')}" - end + + def command(name) + "#{self.class.command} #{name.to_s.gsub('_', '-')}" + end end class GitSvn < Proxy - def self.command; "git svn"; end + def self.command; + "git svn"; + end def commit_hash(remote, revision) - out = invoke(:log, "--show-commit --oneline", "-r #{revision}", remote) - part = out.to_s.split(" | ")[1] + out = invoke(:log, "--show-commit --oneline", "-r #{revision}", remote) + part = out.to_s.split("|")[1] + part.strip! raise UnknownRevision, "r#{revision}" unless part git.rev_parse(part) end def fetch(remote) @@ -314,17 +341,18 @@ invoke(:init, "-R", remote, "--id=#{remote}", path) true end private - def command(name) - "#{self.class.command} #{name}" - end - def git - Git.instance - end + def command(name) + "#{self.class.command} #{name}" + end + + def git + Git.instance + end end class Svn < Proxy def clean_revision(revision) revision.to_i if revision @@ -362,16 +390,17 @@ def path(url) File.join(local_cache_dir, url.gsub(/[\/:@]/, "_")) end private - def local_cache_dir - Braid.local_cache_dir - end - def git - Git.instance - end + def local_cache_dir + Braid.local_cache_dir + end + + def git + Git.instance + end end module VersionControl def git Git.instance