Skip to content

Instantly share code, notes, and snippets.

@yuya-takeyama
Last active September 25, 2015 22:28
Show Gist options
  • Save yuya-takeyama/995342 to your computer and use it in GitHub Desktop.
Save yuya-takeyama/995342 to your computer and use it in GitHub Desktop.
不正な .svn ファイルを持ったディレクトリを探すヤツ.

invalid-svn-detect.rb

How to use?

$ cd /path/to/svn/worktree
$ ruby /path/to/invalid-svn-detect.rb

ChangeLog

2015/03/26

  • svn info の実行は環境変数 LC_ALL をクリアした状態で行うよう修正
  • 読めないディレクトリはスキップするよう修正

2013/09/30

  • ディレクトリがシンボリックリンクであればスキップするように修正
  • Ruby のバージョンが 1.9 未満のときのみ $KCODE を使用するよう修正
  • チェック中の例外をキャッチするよう修正
  • 不正な .svn ディレクトリまたは例外が見つかった場合は Non-zero で exit するよう修正

2011-05-27

  • Initial release
#!/usr/bin/env ruby
# coding: utf-8
$KCODE = "u" if RUBY_VERSION < '1.9'
def dir_walk(dir, &block)
return unless File.readable?(dir)
Dir::entries(dir).each do |f|
unless [".svn", ".", ".."].include? f
f = dir + "/" + f
if FileTest::directory? f and not FileTest::symlink? f
yield f
dir_walk(f, &block)
end
end
end
end
# シェル引数のエスケープ.
# http://webos-goodies.jp/archives/51353401.html
def shellesc(str, opt = {})
str = str.dup
if opt[:erace]
opt[:erace] = [opt[:erace]] unless Array === opt[:erace]
opt[:erace].each do |i|
case i
when :ctrl then str.gsub!(/[\x00-\x08\x0a-\x1f\x7f]/, '')
when :hyphen then str.gsub!(/^-+/, '')
else str.gsub!(i, '')
end
end
end
str.gsub!(/[\!\"\$\&\'\(\)\*\,\:\;\<\=\>\?\[\\\]\^\`\{\|\}\t ]/, '\\\\\\&')
str
end
class String
def end_with?(str)
size = str.size
self[-size, size] == str
end
end
module Subversion
class Info
def initialize(params = {})
@info = params[:info] || nil
@absolute_path = params[:absolute_path] || nil
@svn = true
@svn = false if @info.size == 0
end
def svn?
@svn
end
def url
@info["URL"]
end
# ディレクトリのパスから Subversion::Info オブジェクトの生成.
def self.create_from_path(dir)
hash = {}
`LC_ALL="" svn info #{shellesc(dir)} 2> /dev/null`.split(/\r\n|\r|\n/).each do |line|
key, value = line.split ": ", 2
hash[key] = value
end
self.new :info => hash, :absolute_path => dir
end
def relative_path
@info["URL"].sub @info["Repository Root"], ""
end
def valid_path?
@info["URL"].split("/")[-1] == @info["Path"].split("/")[-1]
end
end
end
include Subversion
count = 0
exceptions = 0
dir_walk Dir::pwd do |dir|
begin
info = Info.create_from_path dir
if info.svn? and not info.valid_path?
count += 1
puts "#{dir} => #{info.url}"
end
rescue => e
exceptions += 1
puts "An exception is occured on checking for #{dir}"
puts e
end
end
puts
if count > 0
puts "Above directories have invalid .svn"
exit 1
elsif exceptions > 0
puts "Some exception is occured"
exit 1
else
puts "No invalid directory detected"
exit 0
end
@yuya-takeyama
Copy link
Author

ディレクトリ名にスペースがあると誤検知するらしいのでなおす.

@yuya-takeyama
Copy link
Author

リポジトリ上からは削除されていて, .svn も無いが, そのディレクトリ自体はあり, 親ディレクトリ上で update されてない場合にコケる?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment