<commit_before># encoding: UTF-8

require "rest_client"

# implements the get-source subcommand
class SourceGetter

  # downloads a project's source into a specified directory
  def self.get_source(address, pomfile, directory)
    $log.info("downloading: #{address} in #{directory}, pomfile: #{pomfile}")
    
    dummy, prefix, scm_address = address.split(/^([^:]+):(.*)$/)
    $log.info("prefix: #{prefix}, scm_address: #{scm_address}")
	
		get_source_from_scm(prefix, scm_address, pomfile, directory)
  end

  # checks code out from an scm
  def self.get_source_from_scm(prefix, scm_address, pomfile, directory)
    pom = Pom.new(pomfile)
    dir = File.join(directory, "#{pom.group_id}:#{pom.artifact_id}:#{pom.version}")
    Dir::mkdir(dir)
    
		if prefix == "git"
			get_source_from_git(scm_address, dir, pom.version)
    elsif prefix == "svn"
			get_source_from_svn(scm_address, dir, pom.version)
		end
  end

  # checks code out of git
	def self.get_source_from_git(scm_address, dir, version)
		`git clone #{scm_address} #{dir}`
		
		Dir.chdir(dir) do
			tags = `git tag`.split("\n")
		  
			if tags.any?
				best_tag = get_best_tag(tags, version)		 	
				$log.info("checking out tag: #{best_tag}")

				`git checkout #{best_tag}`
			end	
		end
	end

  # checks code out of svn
	def self.get_source_from_svn(scm_address, dir, version)
		`svn checkout #{scm_address} #{dir}`
		
		Dir.chdir(dir) do
			tags = `svn ls "^/tags"`.split("\n")
			<commit_msg>Bugfix: re-use existing directories (eg. git repos)<commit_after># encoding: UTF-8

require "rest_client"

# implements the get-source subcommand
class SourceGetter

  # downloads a project's source into a specified directory
  def self.get_source(address, pomfile, directory)
    $log.info("downloading: #{address} in #{directory}, pomfile: #{pomfile}")
    
    dummy, prefix, scm_address = address.split(/^([^:]+):(.*)$/)
    $log.info("prefix: #{prefix}, scm_address: #{scm_address}")
	
		get_source_from_scm(prefix, scm_address, pomfile, directory)
  end

  # checks code out from an scm
  def self.get_source_from_scm(prefix, scm_address, pomfile, directory)
    pom = Pom.new(pomfile)
    dir = File.join(directory, "#{pom.group_id}:#{pom.artifact_id}:#{pom.version}")
		begin
	    Dir::mkdir(dir)
		rescue Errno::EEXIST
			$log.warn("Source directory exists, leaving...")
		end
    
		if prefix == "git"
			get_source_from_git(scm_address, dir, pom.version)
    elsif prefix == "svn"
			get_source_from_svn(scm_address, dir, pom.version)
		end
  end

  # checks code out of git
	def self.get_source_from_git(scm_address, dir, version)
		`git clone #{scm_address} #{dir}`
		
		Dir.chdir(dir) do
			tags = `git tag`.split("\n")
		  
			if tags.any?
				best_tag = get_best_tag(tags, version)		 	
				$log.info("checking out tag: #{best_tag}")

				`git checkout #{best_tag}`
			end	
		end
	end

  # checks code out of svn
	def self.get_source_from_svn(scm_address, dir, version)
		`svn checkout #{scm_address} #{dir}`
		
		Dir.chdir(dir) do
			tags = `svn ls "^/tags"`.split("\n")
			
{'fraction_non_alphanumeric': 0.14187418086500656, 'fraction_numerical': 0.000655307994757536, 'mean_word_length': 3.8848, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4187927', 'n_tokens_mistral': 1225, 'n_tokens_neox': 1119, 'n_words': 264}