Sha256: db5b94cac34650d43bde8231a7b54da6bcbd51fa320b8d390a56b4f689fdf316

Contents?: true

Size: 1.81 KB

Versions: 2

Compression:

Stored size: 1.81 KB

Contents

require 'git_clone_url'

module  Opsk
  class Git
    extend ::Forwardable


    def initialize(d,thor)
	@d = d
	@g = ::Git.open(d)
	@thor = thor
    end

    def_delegator :@g, :push, :push
    def_delegator :@g, :pull, :pull

    def changed?
	git_run('status').include?('modified')
    end

    def report
	%i(changed added untracked).each do |state|
	  @thor.say "#{state} files:\n\n"
	  @g.status.send(state).each do |k,v|
	    @thor.say "- #{k}"
	  end
	  @thor.say "\n"
	end
    end

    def remaster
	Dir.chdir @d do
	  git_run('stash','.')
	  git_run('checkout master','.')
	  git_run('stash apply stash@\{0\}','.')
	end
    end

    def master_commit(options)
	resp = @thor.yes? "Commit the changes under #{@d}? (y/n)" unless options['all']
	if(options['all'] or resp)
	  remaster
	  if options['message']
	    @g.commit_all(options['message']) 
	  else 
	    @thor.say 'Commit message:'
	    @g.commit_all(STDIN.gets.chomp) 
	  end
	end

    end

    def normalize_url(readonly,options)
	url = GitCloneUrl.parse(readonly)
	proto = options['protocol'] || 'ssh'
	port = options['port']? ":#{options['port']}" : ''
	user = options['user']? "#{options['user']}@" : ''
	"#{proto}://#{user}#{url.host}#{port}#{url.path}"
    end

    def add_writable(options)
	readonly = @g.remotes.find{|r|r.name.eql?('origin')}.url
	writable = normalize_url(readonly,options)
	remote_exists = @g.remotes.map {|r| r.name}.include?('writable')
	unless readonly.eql?(writable) or remote_exists
	  @g.add_remote('writable',writable) 
	end
    end

    # ruby-git cannot do this (lame)
    def local_ahead?
	git_run('status').include?('ahead')
    end

    def git_run(cmd,parent=nil)
	dir = parent || @d
	res = %x{git --git-dir=#{dir}/.git --work-tree=#{dir} #{cmd}}
	if $? != 0	
	  raise Exception.new("Failed to run #{cmd} due to #{res}")
	end
	res
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
opskeleton-0.9.7 lib/opskeleton/git.rb
opskeleton-0.9.6 lib/opskeleton/git.rb