Class: Cbt::Girc
- Inherits:
-
Object
- Object
- Cbt::Girc
- Defined in:
- lib/cbt/girc.rb
Instance Attribute Summary collapse
-
#git ⇒ Object
Returns the value of attribute git.
Instance Method Summary collapse
-
#branches ⇒ Object
all branches include remote branches.
-
#config ⇒ Object
return config list as a hash.
-
#current_branch ⇒ Object
the branch currently working on.
-
#del_branch!(branch, remote = nil) ⇒ Object
delete a branch.
-
#in_git_dir? ⇒ Boolean
whether the current directory is a git working directory.
- #info(msg) ⇒ Object
-
#initialize(cmd = 'git', v = true) ⇒ Girc
constructor
A new instance of Girc.
-
#me ⇒ Object
return the value of user.name in configuration.
-
#modified_files ⇒ Object
general informations ———————– return modified files (without additions/deletions).
-
#new_branch!(branch, remote = nil) ⇒ Object
create a new branch, if remote set, push it to remote too then switch to that branch.
-
#pull!(remote = 'origin') ⇒ Object
pull from the remote use fetch/merge.
-
#rebase!(remote = 'origin', branch = 'develop') ⇒ Object
remote from a specified remote ref.
- #remote_list ⇒ Object
- #stash! ⇒ Object
- #stash_pop! ⇒ Object
-
#wd_clean? ⇒ Boolean
is the working directory has modified file.
Constructor Details
#initialize(cmd = 'git', v = true) ⇒ Girc
Returns a new instance of Girc.
5 6 7 8 |
# File 'lib/cbt/girc.rb', line 5 def initialize cmd = 'git', v = true @git = cmd @v = v end |
Instance Attribute Details
#git ⇒ Object
Returns the value of attribute git.
3 4 5 |
# File 'lib/cbt/girc.rb', line 3 def git @git end |
Instance Method Details
#branches ⇒ Object
all branches include remote branches
44 45 46 47 48 49 50 51 52 |
# File 'lib/cbt/girc.rb', line 44 def branches branch_list = Array.new `#{@git} branch -a`.split("\n").each do |line| line.gsub!('* ', '') line.gsub!(/\s/, '') branch_list << line unless branch_list.include? line end branch_list end |
#config ⇒ Object
return config list as a hash
29 30 31 32 33 34 35 36 |
# File 'lib/cbt/girc.rb', line 29 def config h = Hash.new `#{@git} config --list`.split("\n").each do |line| key, value = line.split("=") h[key] = value end h end |
#current_branch ⇒ Object
the branch currently working on
55 56 57 58 59 60 61 62 |
# File 'lib/cbt/girc.rb', line 55 def current_branch `#{@git} branch`.split("\n").each do |line| if /\*/.match line return line.gsub('* ', '') end end nil end |
#del_branch!(branch, remote = nil) ⇒ Object
delete a branch
110 111 112 113 114 |
# File 'lib/cbt/girc.rb', line 110 def del_branch! branch, remote=nil rslt = `#{@git} branch -d #{branch}` raise "Cat not delete branch #{branch}: #{rslt}" unless $?.success? `#{@git} push #{remote} :#{branch}` if remote end |
#in_git_dir? ⇒ Boolean
whether the current directory is a git working directory
83 84 85 |
# File 'lib/cbt/girc.rb', line 83 def in_git_dir? `#{@git} status` =~ /fatal/ ? false : true end |
#info(msg) ⇒ Object
10 11 12 13 |
# File 'lib/cbt/girc.rb', line 10 def info msg return unless @v puts "[GITC] #{msg}" if msg.size > 0 end |
#me ⇒ Object
return the value of user.name in configuration
39 40 41 |
# File 'lib/cbt/girc.rb', line 39 def me config["user.name"] || "?" end |
#modified_files ⇒ Object
general informations
return modified files (without additions/deletions)
18 19 20 21 22 23 24 25 26 |
# File 'lib/cbt/girc.rb', line 18 def modified_files files = Array.new `#{@git} status`.split("\n").each do |line| if /modified\:\s+(?<file_>.+)/ =~ line files << File.(file_) end end files end |
#new_branch!(branch, remote = nil) ⇒ Object
create a new branch, if remote set, push it to remote too then switch to that branch
102 103 104 105 106 107 |
# File 'lib/cbt/girc.rb', line 102 def new_branch! branch, remote=nil raise "You need clean up you working directory" unless wd_clean? raise "Branch #{branch} already exists" if self.branches.include? branch `#{@git} checkout -b #{branch}` `#{@git} push #{remote} #{branch}` if remote end |
#pull!(remote = 'origin') ⇒ Object
pull from the remote use fetch/merge
91 92 93 94 95 96 97 98 |
# File 'lib/cbt/girc.rb', line 91 def pull! remote = 'origin' cb = self.current_branch info "Fetch from #{remote}" rslt = `#{@git} fetch #{remote}` raise "fetch failed with message: #{rslt}" unless $?.success? info rslt info `#{@git} merge #{remote}/#{cb}` end |
#rebase!(remote = 'origin', branch = 'develop') ⇒ Object
remote from a specified remote ref
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/cbt/girc.rb', line 131 def rebase! remote = 'origin', branch = 'develop' cb = self.current_branch stashed = false unless self.wd_clean? self.stash! stashed = true end if branch == self.current_branch info "Pull from remote" # `#{@git} pull --rebase #{remote} #{branch}` `#{@git} pull #{remote} #{branch}` else info "Switch to branch #{branch}" `#{@git} fetch #{remote}` rslt = `#{@git} checkout #{branch}` raise "Checkout failed: #{rslt}" unless $?.success? info "Update branch from remote" # rslt = `#{@git} pull --rebase #{remote} #{branch}` rslt = `#{@git} pull #{remote} #{branch}` raise "Pull for #{branch} failed: #{rslt}" unless $?.success? info "Switch back to branch #{cb}" `#{@git} checkout #{cb}` info "Merge from #{branch}" rslt = `#{@git} merge #{branch}` raise "Merge with #{branch} failed: #{rslt}" unless $?.success? info "pull branch #{self.current_branch} from remote" `#{@git} pull #{remote} #{self.current_branch}` end self.stash_pop! if stashed end |
#remote_list ⇒ Object
64 65 66 67 68 69 70 71 |
# File 'lib/cbt/girc.rb', line 64 def remote_list lst = Array.new `#{@git} remote -v`.split("\n").each do |line| rn = line.split(/\s+/)[0] lst << rn unless lst.include? rn end lst end |
#stash! ⇒ Object
116 117 118 119 120 121 122 |
# File 'lib/cbt/girc.rb', line 116 def stash! unless wd_clean? info "Stash your local changes" `#{@git} add .` `#{@git} stash` end end |
#stash_pop! ⇒ Object
124 125 126 127 128 |
# File 'lib/cbt/girc.rb', line 124 def stash_pop! raise "You may clean up you work directroy first before pop out from the stash" unless wd_clean? info "Pop out from you last stash" `#{@git} stash pop` end |
#wd_clean? ⇒ Boolean
is the working directory has modified file
74 75 76 77 78 79 80 |
# File 'lib/cbt/girc.rb', line 74 def wd_clean? clean = true `#{@git} status`.split("\n").each do |line| clean = false if /Changes/.match line end clean end |