<commit_before>
module Coral
module Project
class Git < Plugin::Project
 
  #-----------------------------------------------------------------------------
  # Project plugin interface
   
  def normalize
    super   
  end
  
  #-----------------------------------------------------------------------------
  # Git interface (local)
   
  def ensure_git(reset = false)
    if reset || ! get(:git_lib, false)
      delete(:git_lib)
      if directory.empty?
        logger.warn("Can not manage Git project at #{directory} as it does not exist")  
      else
        logger.debug("Ensuring Git instance to manage #{directory}")
        set(:git_lib, Util::Git.new(directory))
      end
    end
    return self
  end
  protected :ensure_git
       
  #-----------------------------------------------------------------------------
  # Checks
   
  def can_persist?
    ensure_git
    return true if get(:git_lib, false)
    return false
  end
 
  #---
          
  def top?(path)
    git_dir = File.join(path, '.git')
    if File.exist?(git_dir)
      return true if File.directory?(git_dir)
    elsif File.exist?(path) && (path =~ /\.git$/ && File.exist?(File.join(path, 'HEAD')))
      return true
    end
    return false
  end
    
  #---
          
  def subproject?(path)
    git_dir = File.join(path, '.git')
    if File.exist?(git_dir)
      unless File.directory?(git_dir)
        git_dir = Util::Disk.read(git_dir)        
        unless git_dir.nil?
          git_dir = git_dir.gsub(/^gitdir\:\s*/, '').strip
          return true if File.directory?(git_dir)
        end  
      end
    end
    return false
  end
  
  #---
      
  def project_directory?(path, require_top_level = false)
    path    = File.expand_path(path)
    git_dir = File.join(path, '.git')

    if File.exist?(git_dir)
      if File.directory?(git_dir)
        return true
      elsif ! require_top_level
        git_dir = Util::Disk.read(git_dir)
        unless git_dir.nil?
          git_dir = git_dir.gsub(/^gitdir\:\s*/, '').strip
          return true if File.directory?(git_dir)
        end  
      end
    elsif File.exist?(path) && (path =~ /\.git$/ && File.exist?(File.join(path, 'HEAD')))
      return true
    end
    return false
  end
  
  #---
  
  def new?(reset = false)
    if get(:new, nil).nil? || reset
      set(:new, git.native(:rev_parse, { :all => true }).empty?)  
    end
    get(:new, false)
  end
   
  #-----------------------------------------------------------------------------
  # Property accessors / modifiers
  
  def lib(default = nil)
    return get(:git_lib, default)
  end
 
  #---
  
  def git
    return lib.git if can_persist?
    return nil
  end
  protected :git
    
  #---
   
  def set_location(directory)
    super do
      ensure_git(true)
    end
    return self
  end
  
  #---
  
  def config(name, options = {})
    return super do |config|
      git.config(config.export, name)
    end
  end
  
  #---
  
  def set_config(name, value, options = {})
    return super do |config|
      git.config(config.export, name, value)
    end
  end
  
  #---
  
  def delete_config(name, options = {})
    return super do |config|
      git.config(config.import({ :remove_section => true }).export, name)
    end
  end
  
  #---
 
  def subproject_config(options = {})
    return super do |config|
      result = {}
      
      if new?
        logger.debug("Project has no sub project configuration yet (has not been committed to)")  <commit_msg>Updating the set_config method of the git project plugin provider.<commit_after>
module Coral
module Project
class Git < Plugin::Project
 
  #-----------------------------------------------------------------------------
  # Project plugin interface
   
  def normalize
    super   
  end
  
  #-----------------------------------------------------------------------------
  # Git interface (local)
   
  def ensure_git(reset = false)
    if reset || ! get(:git_lib, false)
      delete(:git_lib)
      if directory.empty?
        logger.warn("Can not manage Git project at #{directory} as it does not exist")  
      else
        logger.debug("Ensuring Git instance to manage #{directory}")
        set(:git_lib, Util::Git.new(directory))
      end
    end
    return self
  end
  protected :ensure_git
       
  #-----------------------------------------------------------------------------
  # Checks
   
  def can_persist?
    ensure_git
    return true if get(:git_lib, false)
    return false
  end
 
  #---
          
  def top?(path)
    git_dir = File.join(path, '.git')
    if File.exist?(git_dir)
      return true if File.directory?(git_dir)
    elsif File.exist?(path) && (path =~ /\.git$/ && File.exist?(File.join(path, 'HEAD')))
      return true
    end
    return false
  end
    
  #---
          
  def subproject?(path)
    git_dir = File.join(path, '.git')
    if File.exist?(git_dir)
      unless File.directory?(git_dir)
        git_dir = Util::Disk.read(git_dir)        
        unless git_dir.nil?
          git_dir = git_dir.gsub(/^gitdir\:\s*/, '').strip
          return true if File.directory?(git_dir)
        end  
      end
    end
    return false
  end
  
  #---
      
  def project_directory?(path, require_top_level = false)
    path    = File.expand_path(path)
    git_dir = File.join(path, '.git')

    if File.exist?(git_dir)
      if File.directory?(git_dir)
        return true
      elsif ! require_top_level
        git_dir = Util::Disk.read(git_dir)
        unless git_dir.nil?
          git_dir = git_dir.gsub(/^gitdir\:\s*/, '').strip
          return true if File.directory?(git_dir)
        end  
      end
    elsif File.exist?(path) && (path =~ /\.git$/ && File.exist?(File.join(path, 'HEAD')))
      return true
    end
    return false
  end
  
  #---
  
  def new?(reset = false)
    if get(:new, nil).nil? || reset
      set(:new, git.native(:rev_parse, { :all => true }).empty?)  
    end
    get(:new, false)
  end
   
  #-----------------------------------------------------------------------------
  # Property accessors / modifiers
  
  def lib(default = nil)
    return get(:git_lib, default)
  end
 
  #---
  
  def git
    return lib.git if can_persist?
    return nil
  end
  protected :git
    
  #---
   
  def set_location(directory)
    super do
      ensure_git(true)
    end
    return self
  end
  
  #---
  
  def config(name, options = {})
    return super do |config|
      git.config(config.export, name)
    end
  end
  
  #---
  
  def set_config(name, value, options = {})
    return super do |config, processed_value|
      git.config(config.export, name, processed_value)
    end
  end
  
  #---
  
  def delete_config(name, options = {})
    return super do |config|
      git.config(config.import({ :remove_section => true }).export, name)
    end
  end
  
  #---
 
  def subproject_config(options = {})
    return super do |config|
      result = {}
      
      if new?
        logger.debug("Project has no sub project configuration yet (has not been committed to)")  
{'fraction_non_alphanumeric': 0.19851894047279978, 'fraction_numerical': 0.0, 'mean_word_length': 1.5857879234167893, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, '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': '84792', 'n_tokens_mistral': 2153, 'n_tokens_neox': 2162, 'n_words': 538}