<commit_before>require "svn/repos" # load SVN Ruby bindings
require "svn/client"
require "md5"
require "rubygems"    # debugging
require "ruby-debug"  # debugging
require File.join(File.dirname(__FILE__),'/repository') # load repository module

module Repository

# subversion specific module constants
if !defined? SVN_CONSTANTS # avoid constants already defined warnings
  SVN_CONSTANTS = {
    :author => Svn::Core::PROP_REVISION_AUTHOR, 
    :date => Svn::Core::PROP_REVISION_DATE,
    :mime_type => Svn::Core::PROP_MIME_TYPE
  }
end
if !defined? SVN_FS_TYPES
  SVN_FS_TYPES = {:fsfs => Svn::Fs::TYPE_FSFS, :bdb => Svn::Fs::TYPE_BDB}
end

class InvalidSubversionRepository < Repository::ConnectionError; end

# Implements AbstractRepository for Subversion repositories
# It implements the following paradigm:
#   1. Repositories are created by using SubversionRepository.create()
#   2. Existing repositories are opened by using either SubversionRepository.open()
#      or SubversionRepository.new()
class SubversionRepository < Repository::AbstractRepository

  if !defined? CLOSEABLE_VERSION
    CLOSEABLE_VERSION = "1.6.5"
  end

  # Constructor: Connects to an existing Subversion
  # repository, using Ruby bindings; Note: A repository has to be
  # created using SubversionRepository.create(), it it is not yet existent
  def initialize(connect_string)
    # Check if configuration is in order
    if Repository.conf[:IS_REPOSITORY_ADMIN].nil?
      raise ConfigurationError.new("Required config 'IS_REPOSITORY_ADMIN' not set")
    end
    if Repository.conf[:REPOSITORY_PERMISSION_FILE].nil?
      raise ConfigurationError.new("Required config 'REPOSITORY_PERMISSION_FILE' not set")
    end
    begin
      super(connect_string) # dummy call to super
    rescue NotImplementedError; end
    @repos_path = connect_string
    @closed = false
    @repos_auth_file = Repository.conf[:REPOSITORY_PERMISSION_FILE] || File.dirname(connect_string) + "/svn_authz"
    @repos_admin = Repository.conf[:IS_REPOSITORY_ADMIN]
    if (SubversionRepository.repository_exists?(@repos_path))
      @repos = Svn::Repos.open(@repos_path)
    else
      raise "Repository does not exist at path \"" + @repos_path + "\""
    end
  end

  # Static method: Creates a new Subversion repository at
  # location 'connect_string'
  def self.create(connect_string)
    if SubversionRepository.repository_exists?(connect_string)
      raise RepositoryCollision.new("There is already a repository at #{connect_string}")
    end
    if File.exists?(connect_string)
      raise IOError.new("Could not create a repository at #{connect_string}: some directory with same name exists already")
    end

    # create the repository using the ruby bindings
    fs_config = {Svn::Fs::CONFIG_FS_TYPE => Repository::SVN_FS_TYPES[:fsfs]}
    repository = Svn::Repos.create(connect_string, {}, fs_config) #raises exception if not successful

    if SubversionRepository.closeable?
      repository.close
    end

    return true
  end

  # Static method: Opens an existing Subversion repository
  # at location 'connect_string'
  def self.open(connect_string)
    return SubversionRepository.new(connect_string)
  end

  # Static method: Yields an existing Subversion repository and closes it afterwards
  def self.access(connect_string)
    repository = self.open(connect_string)
    yield repository
    repository.close
  end

  # Static method: Deletes an existing Subversion repository
  def self.delete(repo_path)
    Svn::Repos::delete(repo_path)
  end

  # method : Export an existing Subversion repository to a new folder
  def export(repo_dest_dir)
    # Modify the path of the repository
    # If libsvn-ruby raise a segfault, check the first argument of
    # Svn::Client::export which must be an URI (ex : file:///home/...)

    repo_path_dir = "file://" + @repos_path
    ctx = Svn::Client::Context.new

    # don't fail on non CA signed ssl server
    ctx.add_ssl_server_trust_file_provider
    setup_auth_baton(ctx.auth_baton)
    ctx.add_username_provider

    # username and password
    ctx.add_simple_prompt_provider(0) do |cred, realm, username, may_save|
      cred.username = "markus"
      cred.password = "<PASSWORD>"
      cred.may_save = false
    end

    # Raise an error if the destination repository already exists
    if (File.exists?(repo_dest_dir))
      raise(ExportRepositoryAlreadyExists, "Exported repository already exists")
    end

    begin
      result = ctx.export( repo_path_dir, repo_dest_dir, nil, nil)<commit_msg>Fix to make Repostiory export work properly on Windows.  <commit_after>require "svn/repos" # load SVN Ruby bindings
require "svn/client"
require "md5"
require "rubygems"    # debugging
require "ruby-debug"  # debugging
require File.join(File.dirname(__FILE__),'/repository') # load repository module

module Repository

# subversion specific module constants
if !defined? SVN_CONSTANTS # avoid constants already defined warnings
  SVN_CONSTANTS = {
    :author => Svn::Core::PROP_REVISION_AUTHOR, 
    :date => Svn::Core::PROP_REVISION_DATE,
    :mime_type => Svn::Core::PROP_MIME_TYPE
  }
end
if !defined? SVN_FS_TYPES
  SVN_FS_TYPES = {:fsfs => Svn::Fs::TYPE_FSFS, :bdb => Svn::Fs::TYPE_BDB}
end

class InvalidSubversionRepository < Repository::ConnectionError; end

# Implements AbstractRepository for Subversion repositories
# It implements the following paradigm:
#   1. Repositories are created by using SubversionRepository.create()
#   2. Existing repositories are opened by using either SubversionRepository.open()
#      or SubversionRepository.new()
class SubversionRepository < Repository::AbstractRepository

  if !defined? CLOSEABLE_VERSION
    CLOSEABLE_VERSION = "1.6.5"
  end

  # Constructor: Connects to an existing Subversion
  # repository, using Ruby bindings; Note: A repository has to be
  # created using SubversionRepository.create(), it it is not yet existent
  def initialize(connect_string)
    # Check if configuration is in order
    if Repository.conf[:IS_REPOSITORY_ADMIN].nil?
      raise ConfigurationError.new("Required config 'IS_REPOSITORY_ADMIN' not set")
    end
    if Repository.conf[:REPOSITORY_PERMISSION_FILE].nil?
      raise ConfigurationError.new("Required config 'REPOSITORY_PERMISSION_FILE' not set")
    end
    begin
      super(connect_string) # dummy call to super
    rescue NotImplementedError; end
    @repos_path = connect_string
    @closed = false
    @repos_auth_file = Repository.conf[:REPOSITORY_PERMISSION_FILE] || File.dirname(connect_string) + "/svn_authz"
    @repos_admin = Repository.conf[:IS_REPOSITORY_ADMIN]
    if (SubversionRepository.repository_exists?(@repos_path))
      @repos = Svn::Repos.open(@repos_path)
    else
      raise "Repository does not exist at path \"" + @repos_path + "\""
    end
  end

  # Static method: Creates a new Subversion repository at
  # location 'connect_string'
  def self.create(connect_string)
    if SubversionRepository.repository_exists?(connect_string)
      raise RepositoryCollision.new("There is already a repository at #{connect_string}")
    end
    if File.exists?(connect_string)
      raise IOError.new("Could not create a repository at #{connect_string}: some directory with same name exists already")
    end

    # create the repository using the ruby bindings
    fs_config = {Svn::Fs::CONFIG_FS_TYPE => Repository::SVN_FS_TYPES[:fsfs]}
    repository = Svn::Repos.create(connect_string, {}, fs_config) #raises exception if not successful

    if SubversionRepository.closeable?
      repository.close
    end

    return true
  end

  # Static method: Opens an existing Subversion repository
  # at location 'connect_string'
  def self.open(connect_string)
    return SubversionRepository.new(connect_string)
  end

  # Static method: Yields an existing Subversion repository and closes it afterwards
  def self.access(connect_string)
    repository = self.open(connect_string)
    yield repository
    repository.close
  end

  # Static method: Deletes an existing Subversion repository
  def self.delete(repo_path)
    Svn::Repos::delete(repo_path)
  end

  # method : Export an existing Subversion repository to a new folder
  def export(repo_dest_dir)
    # Modify the path of the repository
    # If libsvn-ruby raise a segfault, check the first argument of
    # Svn::Client::export which must be an URI (ex : file:///home/...)

    repo_path_dir = "file://" + expand_path(@repos_path)
    ctx = Svn::Client::Context.new

    # don't fail on non CA signed ssl server
    ctx.add_ssl_server_trust_file_provider
    setup_auth_baton(ctx.auth_baton)
    ctx.add_username_provider

    # username and password
    ctx.add_simple_prompt_provider(0) do |cred, realm, username, may_save|
      cred.username = "markus"
      cred.password = "<PASSWORD>"
      cred.may_save = false
    end

    # Raise an error if the destination repository already exists
    if (File.exists?(repo_dest_dir))
      raise(ExportRepositoryAlreadyExists, "Exported repository already exists")
    end

    begin
      result = ctx.export( repo_path_dir, repo_dest_dir, nil, nil)
{'fraction_non_alphanumeric': 0.0848052660449808, 'fraction_numerical': 0.0015359297860669226, 'mean_word_length': 4.215102974828375, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7568290', 'n_tokens_mistral': 2787, 'n_tokens_neox': 2685, 'n_words': 770}