Sha256: 211674e7cb9b05f8d48dc95f4b7d32887b84d054c1c77211fcb9fa26c0c3d343

Contents?: true

Size: 1.68 KB

Versions: 1

Compression:

Stored size: 1.68 KB

Contents

# -*- encoding: utf-8 -*-

module GitShizzle::Git
  class GitExecuteError < GitShizzle::Error;
  end

  class Git
    def initialize(repo_location)
      @repo_location = repo_location
    end

    def status
      Dir.chdir(@repo_location) do
        status = command 'status --porcelain -z', [], :verbose => false, :redirect_io => true
        status
          .each_line("\x00")
          .select { |line| line =~ /^[\p{Lu}\x20\?!]{2}\s/ }
          .each_with_index.map do |line, index|
            File.new(:index => index,
                     :status => line[0..1],
                     :path => line[3..-1].delete("\000"),
                     :status_line => line)
        end
      end
    end

    def command(cmd, opts = [], params = { }, &block)
      opts = [opts].flatten.map { |s| escape(s) }.join(' ')

      git_cmd = "git #{cmd} #{opts}"
      echo git_cmd, params.fetch(:verbose, true)

      out = run_command(git_cmd, params, &block)

      if $?.exitstatus > 0
        if $?.exitstatus == 1 && out == ''
          return ''
        end
        raise GitShizzle::Git::GitExecuteError.new(git_cmd + ':' + out.to_s)
      end
      out
    end

    private
    def escape(s)
      escaped = s.to_s.gsub('\'', '\'\\\'\'')
      %Q{"#{escaped}"}
    end

    def run_command(git_cmd, params = { }, &block)
      if block_given?
        IO.popen(git_cmd, &block)
      else
        if params.fetch(:redirect_io, false)
          git_cmd += ' 2>&1'
          `#{git_cmd}`.chomp
        else
          system git_cmd
        end
      end
    end

    def echo(msg, verbose)
      puts(msg) if verbose && defined?(Thor)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
git_shizzle-0.2.6 lib/git_shizzle/git/git.rb