Skip to content

Instantly share code, notes, and snippets.

@campanalbero
Last active August 29, 2015 14:14
Show Gist options
  • Save campanalbero/baad3fabfd4cd12acc63 to your computer and use it in GitHub Desktop.
Save campanalbero/baad3fabfd4cd12acc63 to your computer and use it in GitHub Desktop.
git リポジトリにデカい(1MB以上)バイナリファイルが push されそうになったら阻止するスクリプト
#!/usr/bin/ruby
class Custom
def self.get_log(old_value, new_value)
if old_value == "0000000000000000000000000000000000000000"
git_log = `git log --stat #{new_value}`
else
git_log = `git log --stat #{old_value}..#{new_value}`
end
end
def self.get_bigs(log)
big_files = Array.new
log.each_line {|line|
if not line.start_with?(" ")
# this is not file
next
elsif line.start_with?(" ")
# this is comment text
next
elsif not line.include?("|")
# this is support sentence. e.g. "1 file changed, 1 insertion(+)"
next
elsif not line.split("|")[1].include?("Bin")
# this is just text file, then ignore
next
end
# to avoid confusing by space-included file name. last string must be "bytes"
splitted = line.split(" ")
byte = splitted[(splitted.length - 2)]
# TODO hard coding file size limitation
if byte.to_i > 1024 * 1024
big_files.push("* " + line)
end
}
big_files
end
def self.big_binary?(old_value, new_value)
git_log = get_log(old_value, new_value)
big_files = get_bigs(git_log)
if big_files.empty?
return false
else
puts ""
puts "********** ********** ********** ********** ********** **********"
puts "* DO NOT commit binary files directly, use git-media."
puts "* see https://info.hue.workslan/redmine/projects/hue-ci-infra/wiki/How-to-use-git-media"
puts "* "
puts "* BINARY FILES are..."
puts big_files
puts "********** ********** ********** ********** ********** **********"
puts ""
return true
end
end
end
old_value = ARGV[1]
new_value = ARGV[2]
if not Custom::big_binary?(old_value, new_value)
exit 0
else
exit 1
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment