Class: Opsk::Git

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/opskeleton/git.rb

Instance Method Summary collapse

Constructor Details

#initialize(d, thor) ⇒ Git

Returns a new instance of Git.



8
9
10
11
12
# File 'lib/opskeleton/git.rb', line 8

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

Instance Method Details

#add_writable(options) ⇒ Object



61
62
63
64
65
66
67
68
# File 'lib/opskeleton/git.rb', line 61

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

#changed?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/opskeleton/git.rb', line 17

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

#git_run(cmd, parent = nil) ⇒ Object



75
76
77
78
79
80
81
82
# File 'lib/opskeleton/git.rb', line 75

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

#local_ahead?Boolean

ruby-git cannot do this (lame)

Returns:

  • (Boolean)


71
72
73
# File 'lib/opskeleton/git.rb', line 71

def local_ahead?
	git_run('status').include?('ahead')
end

#master_commit(options) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/opskeleton/git.rb', line 39

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

#normalize_url(readonly, options) ⇒ Object



53
54
55
56
57
58
59
# File 'lib/opskeleton/git.rb', line 53

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

#remasterObject



31
32
33
34
35
36
37
# File 'lib/opskeleton/git.rb', line 31

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

#reportObject



21
22
23
24
25
26
27
28
29
# File 'lib/opskeleton/git.rb', line 21

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