Take the 2008 Git User's Survey and help out! [ hide ]

public
Description: Piston is a utility that eases vendor branch management. This repository is a complete reimplementation of Piston to provide different backends, depending on the repositories and working copies you pistonize from.
Homepage: http://piston.rubyforge.org/
Clone URL: git://github.com/francois/piston.git
Search Repo:
commit  02a591a67bc50e748ef1dad08674f579b75c2565
tree    db63a67cdd7f01ac3b2ce7034efe64b018b90885
parent  6aebe681a85685860e5a7d5d33728a33799f3188
piston / lib / piston / git / client.rb
100644 73 lines (62 sloc) 2.098 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
require "singleton"
 
module Piston
  module Git
    class Client
      include Singleton
 
      class CommandError < RuntimeError; end
      class Failed < CommandError; end
      class BadCommand < CommandError; end
 
      def logger
        @logger ||= Log4r::Logger["handler::client"]
      end
 
      def git(*args)
        run_cmd :git, *args
      end
 
      private
      def run_cmd(executable, *args)
        args.collect! {|arg| arg =~ /\s|\*|\?|"|\n|\r/ ? %Q('#{arg}') : arg}
        args.collect! {|arg| arg ? arg : '""'}
        cmd = %Q|#{executable} #{args.join(' ')}|
        logger.debug {"> " + cmd}
 
        original_language = ENV["LANGUAGE"]
        begin
          ENV["LANGUAGE"] = "C"
          value = run_real(cmd)
          logger.debug {"< " + value} unless (value || "").strip.empty?
          return value
        ensure
          ENV["LANGUAGE"] = original_language
        end
      end
 
      begin
        raise LoadError, "Not implemented on Win32 machines" if RUBY_PLATFORM =~ /mswin32/
 
          begin
            require "rubygems"
          rescue LoadError
            # NOP -- attempt to load without Rubygems
          end
 
        require "open4"
 
        def run_real(cmd)
          begin
            pid, stdin, stdout, stderr = Open4::popen4(cmd)
            _, cmdstatus = Process.waitpid2(pid)
            return stdout.read if cmd =~ /status/ && cmdstatus.exitstatus == 1
            raise CommandError, "#{cmd.inspect} exited with status: #{cmdstatus.exitstatus}\n#{stderr.read}" unless cmdstatus.success?
            return stdout.read
          rescue Errno::ENOENT
            raise BadCommand, cmd.inspect
          end
        end
 
      rescue LoadError
        # On platforms where open4 is unavailable, we fallback to running using
        # the backtick method of Kernel.
        def run_real(cmd)
          out = `#{cmd}`
          raise BadCommand, cmd.inspect if $?.exitstatus == 127
          raise Failed, "#{cmd.inspect} exited with status: #{$?.exitstatus}" unless $?.success?
          out
        end
      end
    end
  end
end