Class: BigKeeper::GitOperator
- Inherits:
-
Object
- Object
- BigKeeper::GitOperator
- Defined in:
- lib/big_keeper/util/git_operator.rb
Overview
Operator for got
Class Method Summary collapse
Instance Method Summary collapse
-
#check_diff(path, branch, compare_branch) ⇒ Object
TODO: 需要改造,util方法不应该有业务逻辑.
- #check_merge(path, condition) ⇒ Object
- #check_push_success(path, branch, compare_branch) ⇒ Object
- #check_remote_branch_diff(path, branch, compare_branch) ⇒ Object
- #checkout(path, branch_name) ⇒ Object
- #clone(path, git_base) ⇒ Object
- #commit(path, message) ⇒ Object
- #current_branch(path) ⇒ Object
- #del_local(path, branch_name) ⇒ Object
- #del_remote(path, branch_name) ⇒ Object
- #discard(path) ⇒ Object
- #fetch(path) ⇒ Object
- #has_branch(path, branch_name) ⇒ Object
- #has_changes(path) ⇒ Object
- #has_commits(path, branch_name) ⇒ Object
- #has_local_branch(path, branch_name) ⇒ Object
- #has_remote_branch(path, branch_name) ⇒ Object
- #merge(path, branch_name) ⇒ Object
- #merge_no_ff(path, branch_name) ⇒ Object
- #pull(path) ⇒ Object
- #push_to_remote(path, branch_name) ⇒ Object
- #rebase(path, branch_name) ⇒ Object
- #tag(path, version) ⇒ Object
- #tag_list(path) ⇒ Object
- #user ⇒ Object
Class Method Details
.remote_local_name(path) ⇒ Object
200 201 202 203 204 205 206 207 208 209 |
# File 'lib/big_keeper/util/git_operator.rb', line 200 def self.remote_local_name(path) Dir.chdir(path) do IO.popen("git remote") do |io| io.each do |line| Logger.error("Check git remote setting.") if line.length == 0 return line.chomp end end end end |
Instance Method Details
#check_diff(path, branch, compare_branch) ⇒ Object
TODO: 需要改造,util方法不应该有业务逻辑
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 |
# File 'lib/big_keeper/util/git_operator.rb', line 212 def check_diff(path, branch, compare_branch) compare_branch_commits = Array.new IO.popen("cd '#{path}'; git log --left-right #{branch}...#{compare_branch} --pretty=oneline") do |io| io.each do |line| compare_branch_commits.push(line) if (line.include? '>') && (line.include? "Merge branch #{branch} into #{compare_branch}") end end if compare_branch_commits.size > 0 compare_branch_commits.map { |item| Logger.default(item) } Logger.highlight("#{compare_branch} branch has commit doesn't committed in #{branch}, please check") return false else Logger.highlight("#{compare_branch} branch doesn't have commit before #{branch}") return true end end |
#check_merge(path, condition) ⇒ Object
170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/big_keeper/util/git_operator.rb', line 170 def check_merge(path, condition) unmerged_branch = Array.new IO.popen("cd '#{path}'; git branch --no-merged") do |io| io.each do |line| unmerged_branch.push(line) if line.include? "#{condition}" end end if (unmerged_branch.size > 0) unmerged_branch.map { |item| Logger.default(item) } Logger.error("Still has unmerged feature branch, please check") end end |
#check_push_success(path, branch, compare_branch) ⇒ Object
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 |
# File 'lib/big_keeper/util/git_operator.rb', line 247 def check_push_success(path, branch, compare_branch) compare_branch_commits = Array.new IO.popen("cd '#{path}'; git log --left-right #{branch}...#{compare_branch} --pretty=oneline") do |io| io.each do |line| compare_branch_commits.push(line) if (line.include? '>') || (line.include? 'fatal') end end if compare_branch_commits.size > 0 compare_branch_commits.map { |item| Logger.default(item) } Logger.error("#{branch} branch push unsuccess, please check") else Logger.highlight("#{branch} branch push success") end end |
#check_remote_branch_diff(path, branch, compare_branch) ⇒ Object
185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
# File 'lib/big_keeper/util/git_operator.rb', line 185 def check_remote_branch_diff(path, branch, compare_branch) fetch(path) compare_branch_commits = Array.new IO.popen("cd '#{path}';git log --left-right #{branch}...#{GitOperator.remote_local_name(path)}/#{compare_branch} --pretty=oneline") do |io| io.each do |line| compare_branch_commits.push(line) unless (line.include? '>') && (line.include? "Merge branch \'#{branch}\'") end end if compare_branch_commits.size > 0 return true else return false end end |
#checkout(path, branch_name) ⇒ Object
42 43 44 45 46 47 48 49 50 |
# File 'lib/big_keeper/util/git_operator.rb', line 42 def checkout(path, branch_name) Dir.chdir(path) do IO.popen("git checkout #{branch_name}") do |io| io.each do |line| Logger.error("Checkout #{branch_name} failed.") if line.include? 'error' end end end end |
#clone(path, git_base) ⇒ Object
64 65 66 67 68 |
# File 'lib/big_keeper/util/git_operator.rb', line 64 def clone(path, git_base) Dir.chdir(path) do `git clone #{git_base}` end end |
#commit(path, message) ⇒ Object
70 71 72 73 74 75 |
# File 'lib/big_keeper/util/git_operator.rb', line 70 def commit(path, ) Dir.chdir(path) do `git add .` `git commit -m "#{Logger.formatter_output()}"` end end |
#current_branch(path) ⇒ Object
6 7 8 9 10 |
# File 'lib/big_keeper/util/git_operator.rb', line 6 def current_branch(path) Dir.chdir(path) do `git rev-parse --abbrev-ref HEAD`.chop end end |
#del_local(path, branch_name) ⇒ Object
118 119 120 121 122 |
# File 'lib/big_keeper/util/git_operator.rb', line 118 def del_local(path, branch_name) Dir.chdir(path) do `git branch -D #{branch_name}` end end |
#del_remote(path, branch_name) ⇒ Object
124 125 126 127 128 |
# File 'lib/big_keeper/util/git_operator.rb', line 124 def del_remote(path, branch_name) Dir.chdir(path) do `git push #{GitOperator.remote_local_name(path)} --delete #{branch_name}` end end |
#discard(path) ⇒ Object
112 113 114 115 116 |
# File 'lib/big_keeper/util/git_operator.rb', line 112 def discard(path) Dir.chdir(path) do `git checkout . && git clean -df` end end |
#fetch(path) ⇒ Object
52 53 54 55 56 |
# File 'lib/big_keeper/util/git_operator.rb', line 52 def fetch(path) Dir.chdir(path) do `git fetch #{GitOperator.remote_local_name(path)}` end end |
#has_branch(path, branch_name) ⇒ Object
32 33 34 35 36 37 38 39 40 |
# File 'lib/big_keeper/util/git_operator.rb', line 32 def has_branch(path, branch_name) has_branch = false IO.popen("cd '#{path}'; git branch -a") do |io| io.each do |line| has_branch = true if line.include? branch_name end end has_branch end |
#has_changes(path) ⇒ Object
101 102 103 104 105 106 107 108 109 110 |
# File 'lib/big_keeper/util/git_operator.rb', line 101 def has_changes(path) has_changes = true clear_flag = 'nothing to commit, working tree clean' IO.popen("cd '#{path}'; git status") do |io| io.each do |line| has_changes = false if line.include? clear_flag end end has_changes end |
#has_commits(path, branch_name) ⇒ Object
91 92 93 94 95 96 97 98 99 |
# File 'lib/big_keeper/util/git_operator.rb', line 91 def has_commits(path, branch_name) has_commits = false IO.popen("cd '#{path}'; git log --branches --not --remotes") do |io| io.each do |line| has_commits = true if line.include? "(#{branch_name})" end end has_commits end |
#has_local_branch(path, branch_name) ⇒ Object
22 23 24 25 26 27 28 29 30 |
# File 'lib/big_keeper/util/git_operator.rb', line 22 def has_local_branch(path, branch_name) has_branch = false IO.popen("cd '#{path}'; git branch") do |io| io.each do |line| has_branch = true if line.include? branch_name end end has_branch end |
#has_remote_branch(path, branch_name) ⇒ Object
12 13 14 15 16 17 18 19 20 |
# File 'lib/big_keeper/util/git_operator.rb', line 12 def has_remote_branch(path, branch_name) has_branch = false IO.popen("cd '#{path}'; git branch -r") do |io| io.each do |line| has_branch = true if line.include? branch_name end end has_branch end |
#merge(path, branch_name) ⇒ Object
231 232 233 234 235 236 237 |
# File 'lib/big_keeper/util/git_operator.rb', line 231 def merge(path, branch_name) IO.popen("cd '#{path}'; git merge #{branch_name}") do |io| io.each do |line| Logger.error("Merge conflict in #{line}") if line.include? 'Merge conflict' end end end |
#merge_no_ff(path, branch_name) ⇒ Object
239 240 241 242 243 244 245 |
# File 'lib/big_keeper/util/git_operator.rb', line 239 def merge_no_ff(path, branch_name) IO.popen("cd '#{path}'; git merge #{branch_name} --no-ff") do |io| io.each do |line| Logger.error("Merge conflict in #{line}") if line.include? 'Merge conflict' end end end |
#pull(path) ⇒ Object
84 85 86 87 88 89 |
# File 'lib/big_keeper/util/git_operator.rb', line 84 def pull(path) Dir.chdir(path) do # git pull <remote> <branch> `git pull #{GitOperator.remote_local_name(path)} #{GitOperator.new.current_branch(path)}` end end |
#push_to_remote(path, branch_name) ⇒ Object
77 78 79 80 81 82 |
# File 'lib/big_keeper/util/git_operator.rb', line 77 def push_to_remote(path, branch_name) Dir.chdir(path) do `git push -u #{GitOperator.remote_local_name(path)} #{branch_name}` end GitOperator.new.check_push_success(path, branch_name, "#{GitOperator.remote_local_name(path)}/#{branch_name}") end |
#rebase(path, branch_name) ⇒ Object
58 59 60 61 62 |
# File 'lib/big_keeper/util/git_operator.rb', line 58 def rebase(path, branch_name) Dir.chdir(path) do `git rebase #{GitOperator.remote_local_name(path)}/#{branch_name}` end end |
#tag(path, version) ⇒ Object
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/big_keeper/util/git_operator.rb', line 141 def tag(path, version) = Array.new IO.popen("cd '#{path}'; git tag") do |io| io.each do |line| << line end end unless .include? "#{version}\n" Dir.chdir(path) do `git tag -a #{version} -m "release: V #{version}" master;` `git push --tags` end return end Logger.highlight("tag already exists in the remote, skip this step") end |
#tag_list(path) ⇒ Object
158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/big_keeper/util/git_operator.rb', line 158 def tag_list(path) tag_list = Array.new IO.popen("cd '#{path}'; git tag -l") do |io| io.each do |line| unless line=~(/[a-zA-Z]/) tag_list << line end end end tag_list end |
#user ⇒ Object
130 131 132 133 134 135 136 137 138 139 |
# File 'lib/big_keeper/util/git_operator.rb', line 130 def user name = `git config user.name`.chop cn_reg = /[\u4e00-\u9fa5]{1}/ cn_arr = name.scan(cn_reg) if cn_arr.count > 0 Logger.error("git config user.name has Chinese character") else name.gsub(/[^0-9A-Za-z]/, '').downcase end end |