Class: Opsk::Git
- Inherits:
-
Object
- Object
- Opsk::Git
- Extended by:
- Forwardable
- Defined in:
- lib/opskeleton/git.rb
Instance Method Summary collapse
- #add_writable(options) ⇒ Object
- #changed? ⇒ Boolean
- #git_run(cmd, parent = nil) ⇒ Object
-
#initialize(d, thor) ⇒ Git
constructor
A new instance of Git.
-
#local_ahead? ⇒ Boolean
ruby-git cannot do this (lame).
- #master_commit(options) ⇒ Object
- #normalize_url(readonly, options) ⇒ Object
- #remaster ⇒ Object
- #report ⇒ Object
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() readonly = @g.remotes.find{|r|r.name.eql?('origin')}.url writable = normalize_url(readonly,) 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
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)
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() resp = @thor.yes? "Commit the changes under #{@d}? (y/n)" unless ['all'] if(['all'] or resp) remaster if ['message'] @g.commit_all(['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,) url = GitCloneUrl.parse(readonly) proto = ['protocol'] || 'ssh' port = ['port']? ":#{['port']}" : '' user = ['user']? "#{['user']}@" : '' "#{proto}://#{user}#{url.host}#{port}#{url.path}" end |
#remaster ⇒ Object
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 |
#report ⇒ Object
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 |