Skip to content

Instantly share code, notes, and snippets.

@oh-sky
Last active December 12, 2015 00:08
Show Gist options
  • Save oh-sky/4681764 to your computer and use it in GitHub Desktop.
Save oh-sky/4681764 to your computer and use it in GitHub Desktop.
カレントディレクトリ以下のファイル、ディレクトリ(.gitを除く)のパーミッションをchmodall.shというファイルに記録しておく。git clone 後にsh chmodall.sh すれば、各ファイルのパーミッションをコピーできる。
localhost$ mkchmodall.rb
localhost$ git add chmodall.sh
localhost$ git commit -m 'add chmodall.sh'
localhost$ git push origin master
remotehost$ git clone http://example.com/foo.bar.git
remotehost$ sh chmodall.sh
#!/usr/bin/ruby
# -*- coding: utf-8 -*-
class FileList
def initialize(filelist)
@filelist = filelist
@disposedlist = nil
end
def disposeList()
#skipがnilでない場合、空行まで読み飛ばす
skip = nil
#ディレクトリ名
dir = '.'
permission = Permission.new()
@filelist.each_line{|tmpline|
if /^$/ =~ tmpline
#空行の場合はskipにnilをセット
skip = nil
elsif skip
#skipがnilでない場合は読み飛ばす(何もしない)
elsif /\/.git/ =~ tmpline
# .git というディレクトリは除外(次の空行までskip)
skip = 1
elsif /^total/ =~ tmpline
#要らない行
elsif /^(.+):$/ =~ tmpline
#ディレクトリ名
dir = $1
elsif /([-a-z]{10}).+?\d{1,2}:\d{1,2} +(.+)/ =~ tmpline
if $2 != '.git' && $2!='.' && $2!='..'
permission.setString($1)
@disposedlist = @disposedlist.to_s << permission.toOctet().to_s << ' ' << dir << '/' << $2 << "\n"
end
end
}
return self
end
def makeChmodAll(filename)
fp = open(filename,'w')
if fp != nil
@disposedlist.each_line{|tmpline|
fp.puts 'chmod ' << tmpline
}
return 0
else
return nil
end
end
end
class Permission
def initialize()
@permission_string = nil
@permission_octet = nil
end
def setString(permission_string)
if /[-dlrwx]{10}/ =~ permission_string
@permission_string = permission_string
@permission_octet = self.toOctet()
else
self.init()
end
return @permission_string
end
def toOctet()
if /[-dlrwx]{10}/ =~ @permission_string
u = 0;g = 0;o = 0;
u += (@permission_string[1]!=45 ? 4 : 0)
u += (@permission_string[2]!=45 ? 2 : 0)
u += (@permission_string[3]!=45 ? 1 : 0)
g += (@permission_string[4]!=45 ? 4 : 0)
g += (@permission_string[5]!=45 ? 2 : 0)
g += (@permission_string[6]!=45 ? 1 : 0)
o += (@permission_string[7]!=45 ? 4 : 0)
o += (@permission_string[8]!=45 ? 2 : 0)
o += (@permission_string[9]!=45 ? 1 : 0)
@permission_octet = (u.to_s << g.to_s << o.to_s).to_i
end
return @permission_octet
end
end
#main routine
FileList.new(`ls -Rla `).disposeList().makeChmodAll('chmodall.sh')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment