<commit_before>puts __FILE__ if defined?(DEBUG)

require 'json'
require 'rake'
require_relative('../apps/svn.rb')
require_relative('dir.rb')
require_relative('environment.rb')
require_relative('string.rb')

class Project < Hash
	attr_accessor :filename,:env

    def initialize value='',fullname=''
        @filename=''
        @env=Environment.new
        self[:url]=Project.get_url
        self[:fullname]=Project.get_fullname_from_url self[:url] if self[:url].length > 0
        self[:timeout]=60*5
        if value.is_a?(String)
            self[:url] = value if value.is_a?(String) && value.length > 0
            self[:fullname] = Project.get_fullname_from_url self[:url]
        elsif(value.is_a?(Hash))
            value.each{|k,v|self[k.to_sym]=v}
        else
            self[:fullname]=Project.get_fullname_from_url self[:url] if self[:url].length > 0
        end
        self[:fullname] = fullname if fullname.length > 0
    end

    def set_timeout value
        self[:timeout] = value if value.is_a? Numeric
        self[:timeout] = value.gsub('m','').strip.to_f * 60 if value.include?('m')
        self[:timeout] = value.gsub('s','').strip.to_f * 60 if value.include?('s')
    end

	def self.get_url directory=Rake.application.original_dir
	  url=''
	  Dir.chdir(directory) do#Rake.application.original_dir) do
	    url=`git config --get remote.origin.url`.strip if(File.exists?('.git'))
	    url= Svn.url.strip if(File.exists?('.svn'))
	  end
	  url
	end

	def self.get_fullname directory
	    directory.gsub(@env.wrk_dir,'')
	end

	def self.get_fullname_from_url url
		return url.gsub('http://','').gsub('https://','').gsub('.com/','/').gsub('.git','')
	end

    def url; self[:url]; end
    def fullname; self[:fullname]; end

    def name
    	parts=fullname.split('/')
    	parts[parts.length-1]
    end

	def wrk_dir; "#{@env.wrk_dir}/#{self.fullname}"; end
	def make_dir tag=''
		"#{@env.make_dir}/#{self.fullname}" if tag.length==0
    	"#{@env.make_dir}/#{self.fullname}-#{tag}"
    end

	def pull
		if(File.exists?(wrk_dir) && File.exists?("#{wrk_dir}/.git"))
			Dir.chdir(wrk_dir) do
				puts "git pull (#{wrk_dir})"
				puts `git pull`
			end
		end
	end

	def clone
		if(!File.exists?(wrk_dir) && self[:url].include?('.git'))
            cmd=Command.new({ :input => "git clone #{self[:url]} #{self.wrk_dir}", :quiet => true,:ignore_failure => true})
            cmd.execute
            @env.out cmd.summary
		end
	end

	def checkout
		if(!File.exists?(wrk_dir) && self[:url].include?('svn'))
			#puts "checkout #{self.url} to #{self.wrk_dir}"
			#puts `svn checkout #{self.url} #{self.wrk_dir}`
            cmd=Command.new({ :input => "svn checkout #{self.url} #{self.wrk_dir}", :quiet => true,:ignore_failure => true})
            cmd.execute
            @env.out cmd.summary
		end
	end

	def rake
		if(!File.exists?(self.wrk_dir))
			clone
			checkout
		end
		if(File.exists?(self.wrk_dir))
			Dir.chdir(self.wrk_dir) do
				rake = Command.new({ :input => 'rake', :timeout => 300, :ignore_failure => true })
				rake.execute
				@env.out rake.summary
			end
		end
	end

	

    def latest_tag update=false
		makedir="#{@env.make_dir}/#{self.fullname}"
    	FileUtils.mkdir_p(File.dirname(makedir)) if !File.exists?(File.dirname(makedir))
        if(File.exists?(makedir))
        	Dir.chdir(makedir) do
        	  Command.exit_code('git pull') if update
            end
        else
        	if(update)
        	   clone=Command.new("git clone #{self.url} #{makedir}")
			   clone[:quiet]=true
			   clone[:ignore_failure]=true
			   clone.execute
		    end
        end
        if(File.exists?(makedir))
        	Dir.chdir(makedir) do
        		begin
        		    return Git.latest_tag
        	    rescue
        	    end
        	end
        end
        ''
    end

    

    def log_filenames tags=nil
    	tags=Array.new if tags.nil?
    	filenames=Array.new
    	Dir.chdir(@env.log_dir) do
    		dotname=fullname.gsub('/','.')
    		Dir.glob("#{dotname}*.json").each{|f|
    			if(tags.length==0)
    				filenames << "#{@env.log_dir}/#{f}"
    			else
    				has_tags=true
    				tags.each{|tag|
    					has_tags=false if !f.include? tag
    				}
    				filenames << "#{@env.log_dir}/#{f}" if has_tags
    			end
    		}
    	end
    	filenames
    end

    def command_history tags=nil
    	commands=Array.new
    	log_filenames(tags).each{|logfile|
    		commands << Command.new(JSON.parse(IO.read(logfile)))
    	}
        commands
    end
   
    def work_up_to_date?
        logfile=get_logfile ['work','up2date']
        if File.exists? logfile
            last_work_time=File.mtime(logfile)
            last_file_changed=Dir.get_latest_mtime Rake.application.original_dir
            if last_work_time > last_file_changed
                CLEAN.include logfile
                return true
            else
                File.remove(logfile)
            end
            #if File.mtime(logfile) > Dir.get_latest_mtime Rake.application.original_dir
                #CLEAN.include(logfile)
            #    return true
           # end
        end
        false
    end

    def mark_work_up_to_date
        logfile=get_logfile ['work','up2date']
        File.open(logfile,'w'){|f|f.write(' ')}
    end

    def get_logfile tags
    	tagstring=''
    	tagstring=tags if tags.kind_of?(String)
    	tagstring=tags.join('.') if tags.kind_of?(Array)
    	name="#{self.fullname}.#{tagstring}.json".gsub('/','.')
    	"#{@env.log_dir}/#{name}"
    end

    def list
        history=command_history
        if(history.length==0)
            @env.out "?      #{fullname}"
        else
            status=0
            history.each{|c|
                status=c.exit_code if c.exit_code != 0
            }
            if(status==0)
                @env.out "       #{fullname}"
            else<commit_msg>'all'<commit_after>puts __FILE__ if defined?(DEBUG)

require 'json'
require 'rake'
require_relative('../apps/svn.rb')
require_relative('dir.rb')
require_relative('environment.rb')
require_relative('string.rb')

class Project < Hash
	attr_accessor :filename,:env

    def initialize value='',fullname=''
        @filename=''
        @env=Environment.new
        self[:url]=Project.get_url
        self[:fullname]=Project.get_fullname_from_url self[:url] if self[:url].length > 0
        self[:timeout]=60*5
        if value.is_a?(String)
            self[:url] = value if value.is_a?(String) && value.length > 0
            self[:fullname] = Project.get_fullname_from_url self[:url]
        elsif(value.is_a?(Hash))
            value.each{|k,v|self[k.to_sym]=v}
        else
            self[:fullname]=Project.get_fullname_from_url self[:url] if self[:url].length > 0
        end
        self[:fullname] = fullname if fullname.length > 0
    end

    def set_timeout value
        self[:timeout] = value if value.is_a? Numeric
        self[:timeout] = value.gsub('m','').strip.to_f * 60 if value.include?('m')
        self[:timeout] = value.gsub('s','').strip.to_f * 60 if value.include?('s')
    end

	def self.get_url directory=Rake.application.original_dir
	  url=''
	  Dir.chdir(directory) do#Rake.application.original_dir) do
	    url=`git config --get remote.origin.url`.strip if(File.exists?('.git'))
	    url= Svn.url.strip if(File.exists?('.svn'))
	  end
	  url
	end

	def self.get_fullname directory
	    directory.gsub(@env.wrk_dir,'')
	end

	def self.get_fullname_from_url url
		return url.gsub('http://','').gsub('https://','').gsub('.com/','/').gsub('.git','')
	end

    def url; self[:url]; end
    def fullname; self[:fullname]; end

    def name
    	parts=fullname.split('/')
    	parts[parts.length-1]
    end

	def wrk_dir; "#{@env.wrk_dir}/#{self.fullname}"; end
	def make_dir tag=''
		"#{@env.make_dir}/#{self.fullname}" if tag.length==0
    	"#{@env.make_dir}/#{self.fullname}-#{tag}"
    end

	def pull
		if(File.exists?(wrk_dir) && File.exists?("#{wrk_dir}/.git"))
			Dir.chdir(wrk_dir) do
				puts "git pull (#{wrk_dir})"
				puts `git pull`
			end
		end
	end

	def clone
		if(!File.exists?(wrk_dir) && self[:url].include?('.git'))
            cmd=Command.new({ :input => "git clone #{self[:url]} #{self.wrk_dir}", :quiet => true,:ignore_failure => true})
            cmd.execute
            @env.out cmd.summary
		end
	end

	def checkout
		if(!File.exists?(wrk_dir) && self[:url].include?('svn'))
			#puts "checkout #{self.url} to #{self.wrk_dir}"
			#puts `svn checkout #{self.url} #{self.wrk_dir}`
            cmd=Command.new({ :input => "svn checkout #{self.url} #{self.wrk_dir}", :quiet => true,:ignore_failure => true})
            cmd.execute
            @env.out cmd.summary
		end
	end

	def rake
		if(!File.exists?(self.wrk_dir))
			clone
			checkout
		end
		if(File.exists?(self.wrk_dir))
			Dir.chdir(self.wrk_dir) do
				rake = Command.new({ :input => 'rake', :timeout => 300, :ignore_failure => true })
				rake.execute
				@env.out rake.summary
			end
		end
	end

	

    def latest_tag update=false
		makedir="#{@env.make_dir}/#{self.fullname}"
    	FileUtils.mkdir_p(File.dirname(makedir)) if !File.exists?(File.dirname(makedir))
        if(File.exists?(makedir))
        	Dir.chdir(makedir) do
        	  Command.exit_code('git pull') if update
            end
        else
        	if(update)
        	   clone=Command.new("git clone #{self.url} #{makedir}")
			   clone[:quiet]=true
			   clone[:ignore_failure]=true
			   clone.execute
		    end
        end
        if(File.exists?(makedir))
        	Dir.chdir(makedir) do
        		begin
        		    return Git.latest_tag
        	    rescue
        	    end
        	end
        end
        ''
    end

    

    def log_filenames tags=nil
    	tags=Array.new if tags.nil?
    	filenames=Array.new
    	Dir.chdir(@env.log_dir) do
    		dotname=fullname.gsub('/','.')
    		Dir.glob("#{dotname}*.json").each{|f|
    			if(tags.length==0)
    				filenames << "#{@env.log_dir}/#{f}"
    			else
    				has_tags=true
    				tags.each{|tag|
    					has_tags=false if !f.include? tag
    				}
    				filenames << "#{@env.log_dir}/#{f}" if has_tags
    			end
    		}
    	end
    	filenames
    end

    def command_history tags=nil
    	commands=Array.new
    	log_filenames(tags).each{|logfile|
    		commands << Command.new(JSON.parse(IO.read(logfile)))
    	}
        commands
    end
   
    def work_up_to_date?
        if wrk_dir == Rake.application.original_dir
          logfile=get_logfile ['work','up2date']
          if File.exists? logfile
            last_work_time=File.mtime(logfile)
            last_file_changed=Dir.get_latest_mtime Rake.application.original_dir
            if last_work_time > last_file_changed
                CLEAN.include logfile
                return true
            else
                File.delete(logfile)
            end
            #if File.mtime(logfile) > Dir.get_latest_mtime Rake.application.original_dir
                #CLEAN.include(logfile)
            #    return true
           # end
          end
        end
        false
    end

    def mark_work_up_to_date
        logfile=get_logfile ['work','up2date']
        File.open(logfile,'w'){|f|f.write(' ')}
    end

    def get_logfile tags
    	tagstring=''
    	tagstring=tags if tags.kind_of?(String)
    	tagstring=tags.join('.') if tags.kind_of?(Array)
    	name="#{self.fullname}.#{tagstring}.json".gsub('/','.')
    	"#{@env.log_dir}/#{name}"
    end

    def list
        history=command_history
        if(history.length==0)
            @env.out "?      #{fullname}"
        else
            status=0
            history.each{|c|
                status=c.exit_code if c.exit_code != 0
            }
            if(status==0)
                @env.out "       #{fullname}"
            else
{'fraction_non_alphanumeric': 0.14850136239782016, 'fraction_numerical': 0.003916893732970027, 'mean_word_length': 2.288073908174692, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 33, 'https://': 2, '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': '69923', 'n_tokens_mistral': 4325, 'n_tokens_neox': 4132, 'n_words': 713}