Created
July 11, 2019 14:56
-
-
Save lavoiesl/a48e7d791c684e8b983bc7c8a3f7cc44 to your computer and use it in GitHub Desktop.
This file contains 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 'open3' | |
module CheckGitNoModif | |
class << self | |
def call(*argv) | |
case argv.size | |
when 1 | |
if system("git rev-parse --git-dir >/dev/null 2>&1") | |
out, stat = Open3.capture2('git', 'status', '-s') | |
unless stat.success? && out.chomp.empty? | |
bail("git index/workdir must be clean before running") | |
end | |
else | |
info("setting up repo...") | |
success = system("git init . 2>/dev/null && git add . && git -c gc.auto=0 -c user.name=test -c [email protected] commit -m 'init commit for CI' >/dev/null 2>&1") | |
bail("failed to set up repo") unless success | |
end | |
bail("command(s) failed") unless system("/bin/bash", "-c", argv.first) | |
when 0 | |
else | |
bail("pass only one argument") | |
end | |
out, stat = Open3.capture2('git', 'status', '-s') | |
bail("git failed") unless stat.success? | |
unless out.chomp.empty? | |
Dir.mkdir("check-git-no-modif") unless Dir.exist?("check-git-no-modif") | |
system("git diff > check-git-no-modif/diff") | |
bail("There are modified files:\n#{out}") | |
end | |
end | |
def info(msg) | |
puts("[check-git-no-modif] " + msg) | |
end | |
def bail(msg) | |
abort("[check-git-no-modif] " + msg) | |
end | |
end | |
end | |
__FILE__ == $PROGRAM_NAME and CheckGitNoModif.call(*ARGV) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment