Created
May 28, 2013 10:12
-
-
Save alpaca-tc/5661789 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 | |
# coding: utf-8 | |
require 'grit' | |
require 'pry' | |
# ---------------------------------------- | |
# config | |
git_path = '/Volumes/disk1_pt1/git' | |
output_path = '~/git/backup' | |
output_filename = 'update.txt' | |
# ---------------------------------------- | |
# -- start -- | |
raise "#{git_path}が接続されていません" unless File.exists? git_path | |
class GitHash < Hash# {{{ | |
def get_repo( path, match = '/*.git' ) | |
git_repo = {} | |
path = File.expand_path path | |
Dir::glob("#{path}#{match}") do |git_dir| | |
grit = Grit::Repo.new(git_dir) | |
dir_name = grit.path.match(/[^\/]+$/).to_s | |
git_repo[dir_name] = grit | |
end | |
update git_repo | |
end | |
end# }}} | |
def how_update(git_repo, local_repo)# {{{ | |
return :empty if git_repo.commit_count.zero? | |
return :new unless local_repo | |
if local_repo.commit_count.zero? || git_repo.commit_count != local_repo.commit_count then | |
return :update | |
else | |
return :updated | |
end | |
end# }}} | |
hd_repo = GitHash.new.get_repo git_path | |
local_repo = GitHash.new.get_repo output_path | |
report = Hash.new{|h,k| h[k]= Array.new } | |
# 恐らくRubyのバグだけど、この書き方はだめ | |
# report.default = [] | |
# こうしないとだめ。。 | |
# report[hoge] = [] if report[hoge].nil? | |
# 非同期や複数スレッドは、HDDが遅くなるので行わない。 | |
hd_repo.each do |name, hd_grit|# {{{ | |
do_action = how_update(hd_grit, local_repo[name]) | |
case do_action | |
when :new | |
system("git clone --bare #{hd_grit.path} #{output_path}/#{name}") | |
when :update | |
# update | |
local_grit = local_repo[name] | |
system(" | |
cd #{local_grit.path}; | |
git fetch origin | |
git reset --soft FETCH_HEAD | |
") | |
when :empty | |
when :updated | |
# | |
end | |
puts "#{name} -- #{do_action}" | |
report[do_action] << name | |
end# }}} | |
# ファイルの生成 | |
output_file = File.expand_path "#{output_path}/#{output_filename}" | |
File.open(output_file, "w").close() unless File.exists? output_file | |
File.open output_file, "a" do |file|# {{{ | |
def file.output str | |
self.puts str | |
p str | |
end | |
file.output "" | |
file.output "----------------------------------------" | |
file.output "last update is #{Time.new}" | |
report.keys.each do |k| | |
file.output "" | |
file.output "- #{k}" | |
report[k].each do |message| | |
file.output message | |
end | |
end | |
end# }}} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment