Skip to content

Instantly share code, notes, and snippets.

@lavoiesl
Created July 11, 2019 14:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lavoiesl/a48e7d791c684e8b983bc7c8a3f7cc44 to your computer and use it in GitHub Desktop.
Save lavoiesl/a48e7d791c684e8b983bc7c8a3f7cc44 to your computer and use it in GitHub Desktop.
#!/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 user.email=test@test.com 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