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



65
66
67
68
69
70
71
72
# File 'lib/opskeleton/git.rb', line 65

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
20
# File 'lib/opskeleton/git.rb', line 17

def changed?
	res = git_run('status')
	%w(modified deleted untracked).detect{|c| res.include?(c)}
end

#git_run(cmd, parent = nil) ⇒ Object



79
80
81
82
83
84
85
86
# File 'lib/opskeleton/git.rb', line 79

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)


75
76
77
# File 'lib/opskeleton/git.rb', line 75

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

#master_commit(options) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/opskeleton/git.rb', line 43

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



57
58
59
60
61
62
63
# File 'lib/opskeleton/git.rb', line 57

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



35
36
37
38
39
40
41
# File 'lib/opskeleton/git.rb', line 35

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

#reportObject



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/opskeleton/git.rb', line 22

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