From: Joe Van Dyk Date: 2005-07-31T17:31:35+09:00 Subject: Re: code analysis request On 7/30/05, Joe Van Dyk wrote: > highlighted version at http://rafb.net/paste/results/aU76cN45.html > > On 7/30/05, Joe Van Dyk wrote: > > I wrote the below code in maybe 10 minutes. It seems to do the job, > > but I'm sure it could be better. Any ideas? > > Updated version, thanks to cjs on irc: # Examines a subversion directory. # If a file has been added, add the file to the repository. # If a file has been deleted, delete the file from the repository. # And save all modified files. # And then do a 'svn up'. # Meant to do automated versioned backups of a directory for # non-technically inclined users. require 'test/unit/testcase' class SvnStatus attr_reader :filename, :status def initialize(status_line) (@status, @filename) = status_line.split(nil, 2) end def hidden /^\./.match(filename) end def new /^\?/.match(status) end def deleted /^\!/.match(status) end def add svn_execute "add #{filename}" end def delete svn_execute "delete '#{filename}'" end def svn_execute(command_line) puts(command_line) `svn #{command_line}` end end class TC_SvnStatus < Test::Unit::TestCase def test_parse file = SvnStatus.new("X this is a file") assert_equal("X", file.status) assert_equal("this is a file", file.filename) end def test_hidden file = SvnStatus.new("? .foo") assert(file.hidden, 'should be hidden') file = SvnStatus.new("? foo") assert(!file.hidden, 'should not be hidden') end def test_new file = SvnStatus.new("? .foo") assert(file.new, 'should be new') file = SvnStatus.new("R .foo") assert(!file.new, 'should not be new') end def test_deleted file = SvnStatus.new("! deleted_file") assert(file.deleted, 'should be deleted') file = SvnStatus.new("X deleted_file") assert(!file.deleted, 'should not be deleted') end end def get_subversion_status_list list = Array.new `svn status`.split("\n").each do |line| list << SvnStatus.new(line) end list end def subversion_commit `svn commit -m "autocommit"` end filelist = get_subversion_status_list filelist.each { |file| next if file.hidden file.add if file.new file.delete if file.deleted } subversion_commit