svi/lib/svi/application.rb

106 lines
2.9 KiB
Ruby

require "open3"
module Svi
class Application
def initialize
@svn_info = {}
@config = Config.new(self)
end
def wc_info
svn_info(".")
end
def checkout(url, options = {})
wc_path =
if options[:wc_path]
options[:wc_path]
elsif url =~ %r{/([^/]+)/trunk$}
$1
else
url.split("/").last
end
last_checkout_message = ""
checked_out_paths = []
clear_message = lambda do
if last_checkout_message.size > 0
clear = ""
lines = (last_checkout_message.size + C.screen_width - 1) / C.screen_width
if lines > 1
clear += Ansi.cursor_up(lines - 1)
end
clear += Ansi.cursor_back(999)
clear += Ansi.erase_cursor_to_eos
$stdout.write(clear)
last_checkout_message = ""
end
end
start_time = Time.new
SvnRunner.run_svn("checkout", [url, wc_path], allow_interactive: true) do |line|
if line =~ /^A.{4}(.*)$/
path = $1
checked_out_paths << path
clear_message[]
elapsed_time = Time.new - start_time
elapsed_time_formatted = Util.format_time(elapsed_time)
last_checkout_message = "Checking out #{path} [#{elapsed_time_formatted}]..."
$stdout.write(last_checkout_message)
$stdout.flush
elsif line =~ /^\sU\s{3}/
# Ignore the 'U'pdate line of the checkout directory itself.
elsif line =~ /^Checked out revision (\d+)/
revision = $1
clear_message[]
n_files = 0
n_directories = 0
checked_out_paths.uniq.each do |path|
if File.directory?(path)
n_directories += 1
else
n_files += 1
end
end
elapsed_time = Time.new - start_time
elapsed_time_formatted = Util.format_time(elapsed_time)
$stdout.puts "Checked out revision #{revision}: #{n_files} file#{n_files == 1 ? '' : 's'}, #{n_directories} director#{n_directories == 1 ? 'y' : 'ies'} [#{elapsed_time_formatted}]"
else
clear_message[]
$stdout.puts line
end
end
0
end
def status
SvnRunner.run_svn("status", []) do |line|
if line =~ %r{^([ACDIMRX\?!~ ])[CM ][L ][\+ ][SX ][KOTB ]..(.+)$}
action, path = $1, $2
if action == "?" and Util.is_path_ignored?(path, @config)
# Path is ignored
else
puts line
end
end
end
0
end
private
def svn_info(path)
@svn_info[path] ||= begin
info = {}
stdout, stderr, status = Open3.capture3("svn", "info", path)
stdout.lines.each do |line|
if line =~ /^(.*?):\s(.*)$/
info[$1] = $2
end
end
info
end
end
end
end