code analysis request

J

Joe Van Dyk

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?

# 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.


# Utility functions
class String
def get_filename
self.split[1]
end
end

def get_status line
line.split[0]
end

def new_file? line
get_status(line) =3D=3D "?"
end

def removed_file? line
get_status(line) =3D=3D "!"
end

def add_file file_name
puts "adding file: <#{file_name}>"
system "svn add '#{file_name}'"
end

def remove_file file_name
puts "removing file: <#{file_name}>"
system "svn del '#{file_name}'"
end

def hidden_filename line
line.get_filename[0,1] =3D=3D "." if line.size > 0
end

# Get svn stuff and process it.
svn_output =3D `svn status`
svn_output.split("\n").each do |line|
# a line should look something like "M some/dir/file_name"
unless hidden_filename(line)
add_file line.get_filename if new_file? line
remove_file line.get_filename if removed_file? line
end
end
system "svn commit -m 'autocommited!'"
system "svn up"
 
J

Joe Van Dyk


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) =3D 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 =3D 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 =3D SvnStatus.new("? .foo")
assert(file.hidden, 'should be hidden')
file =3D SvnStatus.new("? foo")
assert(!file.hidden, 'should not be hidden')
end
def test_new
file =3D SvnStatus.new("? .foo")
assert(file.new, 'should be new')
file =3D SvnStatus.new("R .foo")
assert(!file.new, 'should not be new')
end
def test_deleted
file =3D SvnStatus.new("! deleted_file")
assert(file.deleted, 'should be deleted')
file =3D SvnStatus.new("X deleted_file")
assert(!file.deleted, 'should not be deleted')
end
end

def get_subversion_status_list
list =3D 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 =3D get_subversion_status_list
filelist.each { |file|
next if file.hidden
file.add if file.new
file.delete if file.deleted
}
subversion_commit
 
D

Daniel Brockman

Hi Joe,

I only glanced over your code, but I noticed this:
def hidden
/^\./.match(filename)
end
def new
/^\?/.match(status)
end
def deleted
/^\!/.match(status)
end

Maybe you should add question marks to those predicates?
 
T

Tobias Luetke

def get_subversion_status_list
list =3D Array.new
`svn status`.split("\n").each do |line|
list << SvnStatus.new(line)
end
list
end

=3D=3D

`svn status`.to_a.collect { |s| SvnStatus.new(s) }


=20
Updated version, thanks to cjs on irc:
=20
=20
# 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'.
=20
# Meant to do automated versioned backups of a directory for
# non-technically inclined users.
=20
require 'test/unit/testcase'
=20
class SvnStatus
attr_reader :filename, :status
def initialize(status_line)
(@status, @filename) =3D 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
=20
class TC_SvnStatus < Test::Unit::TestCase
def test_parse
file =3D 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 =3D SvnStatus.new("? .foo")
assert(file.hidden, 'should be hidden')
file =3D SvnStatus.new("? foo")
assert(!file.hidden, 'should not be hidden')
end
def test_new
file =3D SvnStatus.new("? .foo")
assert(file.new, 'should be new')
file =3D SvnStatus.new("R .foo")
assert(!file.new, 'should not be new')
end
def test_deleted
file =3D SvnStatus.new("! deleted_file")
assert(file.deleted, 'should be deleted')
file =3D SvnStatus.new("X deleted_file")
assert(!file.deleted, 'should not be deleted')
end
end
=20
def get_subversion_status_list
list =3D Array.new
`svn status`.split("\n").each do |line|
list << SvnStatus.new(line)
end
list
end
=20
def subversion_commit
`svn commit -m "autocommit"`
end
=20
filelist =3D get_subversion_status_list
filelist.each { |file|
next if file.hidden
file.add if file.new
file.delete if file.deleted
}
subversion_commit
=20
=20


--=20
Tobi
http://www.snowdevil.ca - Snowboards that don't suck
http://typo.leetsoft.com - Open source weblog engine
http://blog.leetsoft.com - Technical weblog
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,176
Messages
2,570,950
Members
47,500
Latest member
ArianneJsb

Latest Threads

Top