Previous Next

SVN Commit Script · 846 days ago

After using Windows for the past couple years I got used to Tortoise SVN. But now I have no GUI for SVN since I’m back on a Mac. The CLI for SVN feels slow (adding and deleting files by hand) and the Mac OS X GUI’s aren’t cutting it for me (yet).

So I found these scripts and modified the ruby version a little.

Here is the new script:
#!/usr/bin/env ruby

dir = ARGV[0].nil? ? '.' : ARGV.shift

if !File.exists? dir
  puts "Not a valid folder.\a" #beep
  exit
end

status = `svn status #{dir}`
  
if status.strip.empty?
  folder = dir == '.' ? "working copy" : dir
  puts "No changes to #{folder}."
  exit
end

to_add = []
to_remove = []

status.each_line do |line|
  next if line.strip.empty?
  action = line[0,1].empty? ? line[1,1] : line[0,1]
  path = line.split(' ').last.strip
  case action
    when '?'
      to_add << path
    when '!'
      to_remove << path
  end
end

if to_add.size > 0
  puts "\nUnversioned files:"
  to_add.each { |i| puts "\t"+i }
  puts "\nAdd these files? [y/n]"
  if $stdin.gets.strip =~ /y/i
    puts `svn add #{to_add.join(' ')}`
  end
end

if to_remove.size > 0
  puts "\nMissing files:"
  to_remove.each { |i| puts "\t"+i }
  puts "\nRemove these files? [y/n]"
  if $stdin.gets.strip =~ /y/i
    `svn remove #{to_remove.join(' ')}`
  end
end

to_checkin = []
checkin_list = []

`svn status #{dir}`.each_line do |line|
  next if line.strip.empty?
  action = line[0,1].empty? ? line[1,1] : line[0,1]
  path = line.split(' ').last.strip
  case action
    when 'M', 'A', 'D'
    to_checkin << path
    checkin_list << line.strip
  end
end

if to_checkin.size > 0
  puts "\nChanged files:"
  checkin_list.each { |i| puts "\t"+i }
  puts "\nCommit these changes? [y/n]"
  if $stdin.gets.strip =~ /y/i
    puts "\nEnter message:"
    message = $stdin.gets.strip
    puts "Connecting to repository ..."
    message = message.empty? ? "** no message **" : message.gsub('"', '\"')
    puts `svn commit #{dir} -m "#{message}"`
  end
end

I like it but if you try to run this on a folder outside of the working copy it prints both svn's error (svn: 'folder_name' is not a working copy) and this script's error (No changes to folder_name). I don't know if the script can capture the svn error because status var is an empty string and the svn error seems to be printed automagically.

Update:
Wrote a new one, kinda related: SVN remote log

Comments