class Object

Constants

NullLog
NullLogSink

Public Instance Methods

find_git_repository(realpath, d) click to toggle source
# File lib/vcs-ann/main.rb, line 157
def find_git_repository(realpath, d)
  relpath = realpath.relative_path_from(d).to_s
  rev, status = Open3.capture2('git', '-C', d.to_s, 'log', '--pretty=format:%H', '-1', relpath.to_s)
  if !status.success?
    raise "git log failed"
  end
  return GITRepo.new(d), relpath, rev
end
find_svn_repository(arg) click to toggle source
# File lib/vcs-ann/main.rb, line 125
def find_svn_repository(arg)
  svn_info_xml = IO.popen(['svn', 'info', '--xml', arg]) {|io| io.read }

  # <url>http://svn.ruby-lang.org/repos/ruby/trunk/ChangeLog</url>
  # <root>http://svn.ruby-lang.org/repos/ruby</root>
  # <commit
  #    revision="44930">

  if %r{<url>(.*?)</url>} !~ svn_info_xml
    raise "unexpected 'svn info' result: no url element"
  end
  url = CGI.unescapeHTML($1)
  if %r{<root>(.*?)</root>} !~ svn_info_xml
    raise "unexpected 'svn info' result: no root element"
  end
  root = CGI.unescapeHTML($1)
  if %r{#{Regexp.escape root}} !~ url
    raise "unexpected 'svn info' result: url is not a prefix of root"
  end
  relpath = $'
  if !relpath.empty? && %r{\A/} !~ relpath
    raise "unexpected 'svn info' result: relpath doesn't start with a slash"
  end

  if %r{<commit\s+revision="(\d+)">} !~ svn_info_xml
    raise "unexpected 'svn info' result: no revision"
  end
  rev = $1.to_i

  return SVNRepo.new(root), relpath, rev
end
main(argv) click to toggle source
# File lib/vcs-ann/main.rb, line 189
def main(argv)
  filename = parse_arguments(argv)
  repo, relpath, rev = setup_repository filename
  server = Server.new(repo)
  run_browser server.annotate_url(relpath, rev)
  server.stop
end
parse_arguments(argv) click to toggle source
# File lib/vcs-ann/main.rb, line 166
def parse_arguments(argv)
  # process options
  filename = argv[0]
  filename
end
run_browser(url) click to toggle source
# File lib/vcs-ann/main.rb, line 185
def run_browser(url)
  system "w3m", url
end
scan_udiff(string) { |:filename1, line, $1| ... } click to toggle source
# File lib/vcs-ann/main.rb, line 20
def scan_udiff(string)
  ln_cur1 = 0
  ln_cur2 = 0
  ln_num1 = 0
  ln_num2 = 0
  string.each_line {|line|
    line.force_encoding('locale').scrub!
    case line
    when /\A---\s+(\S+)/
      yield :filename1, line, $1
    when /\A\+\+\+\s+(\S+)/
      yield :filename2, line, $1
    when /\A@@ -(\d+),(\d+) \+(\d+),(\d+) @@/
      ln_cur1 = $1.to_i
      ln_num1 = $2.to_i
      ln_cur2 = $3.to_i
      ln_num2 = $4.to_i
      yield :hunk_header, line, ln_cur1, ln_num1, ln_cur2, ln_num2
    else
      if /\A-/ =~ line && 0 < ln_num1
        content_line = $'
        yield :del, line, content_line, ln_cur1
        ln_cur1 += 1
        ln_num1 -= 1
      elsif /\A\+/ =~ line && 0 < ln_num2
        content_line = $'
        yield :add, line, content_line, ln_cur2
        ln_cur2 += 1
        ln_num2 -= 1
      elsif /\A / =~ line && 0 < ln_num1 && 0 < ln_num2
        content_line = $'
        yield :com, line, content_line, ln_cur1, ln_cur2
        ln_cur1 += 1
        ln_cur2 += 1
        ln_num1 -= 1
        ln_num2 -= 1
      else
        yield :other, line
      end
    end
  }
end
setup_repository(filename) click to toggle source
# File lib/vcs-ann/main.rb, line 172
def setup_repository(filename)
  realpath = Pathname(filename).realpath
  realpath.dirname.ascend {|d|
    if (d+".svn").exist?
      return find_svn_repository(filename)
    end
    if (d+".git").exist?
      return find_git_repository(realpath, d)
    end
  }
  raise "cannot find a repository"
end