Last active
March 22, 2018 16:46
-
-
Save petekinnecom/9b5a8d4a923caf1dc7fcaab7ac9a48e9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #! /usr/bin/env ruby | |
| require "work_queue" | |
| require "etc" | |
| module Color | |
| CODES = { | |
| red: 31, | |
| green: 32, | |
| yellow: 33, | |
| pink: 35, | |
| }.freeze | |
| CODES.each do |color, code| | |
| self.send(:define_method, color) do |string| | |
| colorize(string, code) | |
| end | |
| end | |
| private | |
| # colorization | |
| def colorize(string, color_code) | |
| "\e[#{color_code}m#{string}\e[0m" | |
| end | |
| end | |
| class GitPull | |
| include Color | |
| def run | |
| succeeded = [] | |
| skipped = [] | |
| failed = [] | |
| not_on_master = [] | |
| dirs = | |
| Dir.entries(".") | |
| .reject {|d| d.match(/^\./)} | |
| .select {|d| File.directory?(d)} | |
| .select {|d| File.directory?(File.join(d, ".git"))} | |
| queue = WorkQueue.new(Etc.nprocessors) | |
| dirs.each do |dir| | |
| queue.enqueue_b do | |
| is_clean = begin | |
| shell(dir, "git diff --quiet HEAD") | |
| $? == 0 | |
| end | |
| if !is_clean | |
| skipped << dir | |
| next | |
| end | |
| branch = shell(dir, "git rev-parse --abbrev-ref HEAD").chomp("\n") | |
| if branch != "master" | |
| not_on_master << dir | |
| end | |
| output = shell(dir, "git pull --ff-only origin #{branch}") | |
| if $? == 0 | |
| print '.' | |
| succeeded << dir | |
| else | |
| failed << <<~ERROR | |
| cd #{dir} && git pull --ff-only origin #{branch} | |
| #{output.chomp} | |
| ERROR | |
| end | |
| end | |
| end | |
| queue.join | |
| puts "" | |
| puts green("Succeeded in #{succeeded.size} dirs") | |
| if (not_on_master - failed).size > 0 | |
| puts "---" | |
| puts yellow("The following branches pulled successfuly but are not on master:") | |
| puts yellow((not_on_master - failed).join("\n")) | |
| end | |
| if skipped.size > 0 | |
| puts "---" | |
| puts yellow("Skipped the following dirs due to uncommitted changes:") | |
| puts yellow(skipped.join("\n")) | |
| end | |
| if failed.size > 0 | |
| puts "---" | |
| puts red("Failed commands:") | |
| puts "" | |
| puts red(failed.join("---")) | |
| puts "---" | |
| end | |
| end | |
| def shell(dir, cmd, log: false) | |
| cmd = "cd #{dir} && #{cmd}" | |
| puts cmd if log | |
| `#{cmd} 2>&1` | |
| end | |
| end | |
| GitPull.new.run |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment