Seasons.NET

ちょっとした技術ブログです

カレントディレクトリエントリーを全てMercurialリポジトリにするスクリプト

$KCODE = "s"

IGNORE_ITEMS = ".mercurial_ignore_items"

class String 
  def file?
    File.file? self
  end
  def directory?
    File.directory? self
  end
end

def createHgIgnore(path)
  path += File::Separator + ".hgignore"
  open( path , "w" ) do |f|
    f.print <<EOF
# use glob syntax.
syntax: glob
EOF
    open(IGNORE_ITEMS,"r").readlines().each{|r| f.print r } if File.exists? IGNORE_ITEMS
  end
end

def hgReposRegist(path)
  %x{hg init "#{path}"}
  %x{hg add -R "#{path}"}
  %x{hg commit -m "regist" -R "#{path}"}
end

def hasHgEntries(path)
  Dir.entries(path).each do |subitem|
    return true if subitem.downcase==".hg"
  end
  return false
end

if $0 == __FILE__
  Dir.glob("*") do |item|
    path = Dir.pwd + File::Separator + item
    if item.directory? and !hasHgEntries(path)
      print "HG:#{path}\n"
      createHgIgnore(path)
      hgReposRegist(path)
    end
  end
end

このスクリプトを適当な名前で保存して、

ruby スクリプト名.rb

で実行すると実行時のカレントディレクトリに存在するディレクトリエントリーをMercurialリポジトリ
初期化->ファイル追加->初回登録まで一気に行います。
(空の.hgignoreも作成します。)

また、.mercurial_ignore_items
というファイル名を作成し、改行区切りで、

*.obj
*.pdb

というワイルドカードを登録すると、.hgignoreに無視リストとして追加して作成してくれます。