Class: PodsOrz::GitOperator
- Inherits:
-
Object
- Object
- PodsOrz::GitOperator
- Defined in:
- lib/podsorz/util/git_operator.rb
Instance Method Summary collapse
- #check_merge(path, condition) ⇒ Object
- #checkout(path, branch_name) ⇒ Object
- #commit(path, message) ⇒ Object
- #compare_branch(path, branch, compare_branch) ⇒ Object
- #current_branch(path) ⇒ Object
- #del_local(path, branch_name) ⇒ Object
- #del_remote(path, branch_name) ⇒ Object
- #discard(path) ⇒ Object
- #fetch(path, branch) ⇒ Object
- #has_branch(path, branch_name) ⇒ Object
- #has_changes(path) ⇒ Object
- #has_commits(path, branch_name) ⇒ Object
- #has_diverged(path) ⇒ Object
- #has_local_branch(path, branch_name) ⇒ Object
- #has_remote_branch(path, branch_name) ⇒ Object
- #merge(path, parent_branch, son_branch) ⇒ Object
- #pull(path, branch, is_merge) ⇒ Object
- #push_personal_force_to_remote(path, branch_name) ⇒ Object
- #push_to_remote(path, branch_name) ⇒ Object
- #rebase(path, branch) ⇒ Object
- #tag(path, version) ⇒ Object
- #tag_list(path) ⇒ Object
- #user ⇒ Object
Instance Method Details
#check_merge(path, condition) ⇒ Object
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 |
# File 'lib/podsorz/util/git_operator.rb', line 222 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 io.close end if (unmerged_branch.size > 0) unmerged_branch.map { |item| Logger.default(item) } Logger.error("Still has unmerged feature branch, please check") end end |
#checkout(path, branch_name) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/podsorz/util/git_operator.rb', line 47 def checkout(path, branch_name) is_checkout_success = true Dir.chdir(path) do IO.popen("git checkout #{branch_name}") do |io| io.each do |line| if line.include? 'error' Logger.error("Checkout #{branch_name} failed.") is_checkout_success = false end if line.include? 'fatal' Logger.error("Checkout #{branch_name} failed.") is_checkout_success = false end end io.close end end is_checkout_success end |
#commit(path, message) ⇒ Object
71 72 73 74 75 76 |
# File 'lib/podsorz/util/git_operator.rb', line 71 def commit(path, ) Dir.chdir(path) do `git add .` `git commit -m "#{}"` end end |
#compare_branch(path, branch, compare_branch) ⇒ Object
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 |
# File 'lib/podsorz/util/git_operator.rb', line 251 def compare_branch(path, branch, compare_branch) diff_commit_list = [] compare_cmd_list = [] compare_cmd_list << "cd #{path}" compare_cmd_list << "git log --left-right #{branch}...#{compare_branch} --pretty=oneline" IO.popen(compare_cmd_list.join(";")) do |io| io.each do |line| diff_commit_list << line unless line.strip.chomp.empty? end io.close end diff_commit_list end |
#current_branch(path) ⇒ Object
5 6 7 8 9 |
# File 'lib/podsorz/util/git_operator.rb', line 5 def current_branch(path) Dir.chdir(path) do `git rev-parse --abbrev-ref HEAD`.chop end end |
#del_local(path, branch_name) ⇒ Object
165 166 167 168 169 |
# File 'lib/podsorz/util/git_operator.rb', line 165 def del_local(path, branch_name) Dir.chdir(path) do `git branch -D #{branch_name}` end end |
#del_remote(path, branch_name) ⇒ Object
171 172 173 174 175 |
# File 'lib/podsorz/util/git_operator.rb', line 171 def del_remote(path, branch_name) Dir.chdir(path) do `git push origin --delete #{branch_name}` end end |
#discard(path) ⇒ Object
159 160 161 162 163 |
# File 'lib/podsorz/util/git_operator.rb', line 159 def discard(path) Dir.chdir(path) do `git checkout . && git clean -df` end end |
#fetch(path, branch) ⇒ Object
240 241 242 243 244 245 246 247 248 249 |
# File 'lib/podsorz/util/git_operator.rb', line 240 def fetch(path, branch) cmd_line = [] cmd_line << "cd #{path}" cmd_line << "git fetch origin #{branch}" IO.popen(cmd_line.join(";")) do |io| io.close end end |
#has_branch(path, branch_name) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/podsorz/util/git_operator.rb', line 35 def has_branch(path, branch_name) has_branch = false IO.popen("cd '#{path}';git fetch --all;git branch -a") do |io| io.each do |line| has_branch = true if line.strip.eql? branch_name end io.close end has_branch end |
#has_changes(path) ⇒ Object
145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/podsorz/util/git_operator.rb', line 145 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 io.close end has_changes end |
#has_commits(path, branch_name) ⇒ Object
132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/podsorz/util/git_operator.rb', line 132 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? "commit" end io.close end has_commits end |
#has_diverged(path) ⇒ Object
372 373 374 375 376 377 378 379 380 381 382 383 384 |
# File 'lib/podsorz/util/git_operator.rb', line 372 def has_diverged(path) has_div = false IO.popen("cd #{path};git status") do |io| rebase_cmd_lines = io.readlines rebase_cmd_lines.each do |line| has_div = true if line.include? "diverged" end io.close end has_div end |
#has_local_branch(path, branch_name) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/podsorz/util/git_operator.rb', line 23 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.strip.eql? branch_name end io.close end has_branch end |
#has_remote_branch(path, branch_name) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/podsorz/util/git_operator.rb', line 11 def has_remote_branch(path, branch_name) has_branch = false IO.popen("cd '#{path}';git fetch --all;git branch -r") do |io| io.each do |line| has_branch = true if line.include? branch_name end io.close end has_branch end |
#merge(path, parent_branch, son_branch) ⇒ Object
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 |
# File 'lib/podsorz/util/git_operator.rb', line 307 def merge(path, parent_branch, son_branch) is_merge_success = true merge_cmd_list = [] merge_cmd_list << "cd #{path}" merge_cmd_list << "git checkout #{parent_branch}" merge_cmd_list << "git fetch origin #{parent_branch}" merge_cmd_list << "git reset --hard origin/#{parent_branch}" merge_cmd_list << "git merge #{son_branch} --no-ff --no-squash --no-edit" merge_cmd_lines = [] has_conflicts = false IO.popen(merge_cmd_list.join(";")) do |io| merge_cmd_lines = io.readlines merge_cmd_lines.each do |line| has_conflicts = true if line.include? "Merge conflict" end io.close end if has_conflicts Logger.error("branch: \"#{parent_branch}\" Merge conflict, please manual fix conflicts.(fix conflicts and run \"git commit\")(use \"git merge --abort\" to abort the merge)") is_merge_success = false return is_merge_success end is_merge_success end |
#pull(path, branch, is_merge) ⇒ Object
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 |
# File 'lib/podsorz/util/git_operator.rb', line 269 def pull(path, branch, is_merge) is_pull_success = true has_conflicts = false pull_cmd_list = [] pull_cmd_list << "cd #{path}" pull_cmd_list << "git fetch origin #{branch}" if is_merge pull_cmd_list << "git pull --no-ff --no-squash --no-edit" else pull_cmd_list << "git pull --rebase" end pull_io_lines = [] IO.popen(pull_cmd_list.join(";")) do |io| pull_io_lines = io.readlines pull_io_lines.each do |line| has_conflicts = true if line.include? "Merge conflict" end io.close end if has_conflicts if is_merge Logger.error("【#{path}】\n on branch: \"#{branch}\" (pull)Merge conflict, please manual fix conflicts.(fix conflicts and run \"git commit\")(use \"git merge --abort\" to abort the merge)") else Logger.error("【#{path}】\n on branch: \"#{branch}\" (pull)Rebase conflict, please manual fix conflicts.(fix conflicts and run \"git rebase --continue\")(use \"git rebase --abort\" to abort the merge)") end is_pull_success = false end is_pull_success end |
#push_personal_force_to_remote(path, branch_name) ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/podsorz/util/git_operator.rb', line 107 def push_personal_force_to_remote(path, branch_name) is_push_success = true push_lines = [] IO.popen("cd #{path};git push -u -f origin #{branch_name}") do |io| push_lines = io.readlines io.each do |line| if line.include? "error" is_push_success = false end end io.close end unless is_push_success Logger.error("#{branch_name} push to remote failed!") push_lines.each do |line| puts(line) end end is_push_success end |
#push_to_remote(path, branch_name) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/podsorz/util/git_operator.rb', line 78 def push_to_remote(path, branch_name) is_push_success = true push_lines = [] IO.popen("cd #{path};git push -u origin #{branch_name}") do |io| push_lines = io.readlines io.each do |line| if line.include? "error" is_push_success = false end if line.include? "fatal" is_push_success = false end end io.close end unless is_push_success Logger.error("#{branch_name} push to remote failed!") push_lines.each do |line| puts(line) end end is_push_success end |
#rebase(path, branch) ⇒ Object
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 |
# File 'lib/podsorz/util/git_operator.rb', line 337 def rebase(path, branch) is_rebase_success = true is_remote_branch = has_remote_branch(path, branch) rebase_cmd_list = [] rebase_cmd_list << "cd #{path}" if is_remote_branch rebase_cmd_list << "git fetch origin #{branch}" rebase_cmd_list << "git rebase origin/#{branch}" else rebase_cmd_list << "git rebase #{branch}" end has_conflicts = false rebase_cmd_lines = [] IO.popen(rebase_cmd_list.join(";")) do |io| rebase_cmd_lines = io.readlines rebase_cmd_lines.each do |line| has_conflicts = true if line.include? "Merge conflict" end io.close end if has_conflicts c_branch = current_branch(path) Logger.error("【#{path}】\n on branch: \"#{c_branch}\" Merge conflict, please manual fix conflicts.(fix conflicts and run \"git rebase --continue\")(use \"git rebase --abort\" to abort the merge)") is_rebase_success = false end is_rebase_success end |
#tag(path, version) ⇒ Object
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/podsorz/util/git_operator.rb', line 188 def tag(path, version) = Array.new IO.popen("cd '#{path}'; git tag") do |io| io.each do |line| << line end io.close 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
209 210 211 212 213 214 215 216 217 218 219 220 |
# File 'lib/podsorz/util/git_operator.rb', line 209 def tag_list(path) tag_list = Array.new IO.popen("cd '#{path}'; git tag -l --sort=-version:refname") do |io| io.each do |line| tag_list << line end io.close end tag_list end |
#user ⇒ Object
177 178 179 180 181 182 183 184 185 186 |
# File 'lib/podsorz/util/git_operator.rb', line 177 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 |