<commit_before>class SCM::Git::Commit < SCM::Git
  CW = ENV['TM_SUPPORT_PATH'] + '/bin/CommitWindow.app/Contents/MacOS/CommitWindow'
  
  def initialize
    @paths = paths
    @base  = git_base
    chdir_base
  end

  def status(*args)
    SCM::Git::Status.new.status(*args)
  end
  
  def clean_directory?
    statuses.empty?
  end
  
  def run
    if File.exist?(File.join(git_base, ".git/MERGE_HEAD"))
      run_merge_commit
    else
      run_partial_commit
    end
  end
  
  def run_merge_commit
    f = Formatters::Commit.new
    f.layout do
      f.header "Committing merge result"
    
      status = Git::Status.new
      status.run([git_base])
      
      f.commit_merge_dialog(File.read(File.join(git_base, ".git/MERGE_MSG")))
    end
  end
  
  def run_partial_commit
    f = Formatters::Commit.new
    target_file_or_dir = paths.first
    f.layout do
      f.header "Committing Files in ‘#{htmlize(shorten(target_file_or_dir))}’"
      flush
    
      files, status = [], []
      statuses(target_file_or_dir).each do |e|
        files  << e_sh(shorten(e[:path], @base))
        status << e_sh(e[:status][:short])
      end

      res = %x{#{e_sh CW}                 \
        --diff-cmd   '#{git},diff'        \
        --status #{status.join ':'}       \
        #{files.join ' '} 2>/dev/console
      }

      if $? != 0
        puts "<strong>Cancel</strong>"
        abort
      end

      res   = Shellwords.shellwords(res)
      msg   = res[1]
      files = res[2..-1]

      puts "<h2>Commit Files:</h2><ul>"
      puts files.map { |e| "<li>#{htmlize(e)}</li>\n" }.join
      puts "</ul>"

      puts "<h2>Using Message:</h2>"
      puts "<pre>#{htmlize(msg)}</pre>"
      STDOUT.flush

      unless files.empty?
        puts "<h2>Result:</h2>"
        auto_add_rm(files)
        res = commit(msg, files)
        f.output_commit_result(res)
      end
    end
  end
  
  def auto_add_rm(files)
    chdir_base
    add_files = files.select{ |f| File.exists?(f) }
    remove_files = files.reject{ |f| File.exists?(f) }
    puts add_files.inspect
    res = add(add_files) unless add_files.empty?
    res = rm(remove_files) unless remove_files.empty?
  end
  
  def statuses(path = paths.first)
    @statuses ||= status(path)<commit_msg>Don't show commit dialog if conflicts still detected.  Show a friendly informative message, instead.<commit_after>class SCM::Git::Commit < SCM::Git
  CW = ENV['TM_SUPPORT_PATH'] + '/bin/CommitWindow.app/Contents/MacOS/CommitWindow'
  
  def initialize
    @paths = paths
    @base  = git_base
    chdir_base
  end

  def status(*args)
    SCM::Git::Status.new.status(*args)
  end
  
  def clean_directory?
    statuses.empty?
  end
  
  def run
    if File.exist?(File.join(git_base, ".git/MERGE_HEAD"))
      run_merge_commit
    else
      run_partial_commit
    end
  end
  
  def run_merge_commit
    f = Formatters::Commit.new
    f.layout do
      f.header "Resolve a merge conflict"
    
      status = Git::Status.new
      status.run([git_base])
      if statuses.any? {|status_options| status_options[:status][:short] == "C"}
        puts "<p>You still have outstanding merge conflicts.  Resolve them, and try to commit again.</p>"
        abort
      end
      f.commit_merge_dialog(File.read(File.join(git_base, ".git/MERGE_MSG")))
    end
  end
  
  def run_partial_commit
    f = Formatters::Commit.new
    target_file_or_dir = paths.first
    f.layout do
      f.header "Committing Files in ‘#{htmlize(shorten(target_file_or_dir))}’"
      flush
    
      files, status = [], []
      statuses(target_file_or_dir).each do |e|
        files  << e_sh(shorten(e[:path], @base))
        status << e_sh(e[:status][:short])
      end

      res = %x{#{e_sh CW}                 \
        --diff-cmd   '#{git},diff'        \
        --status #{status.join ':'}       \
        #{files.join ' '} 2>/dev/console
      }

      if $? != 0
        puts "<strong>Cancel</strong>"
        abort
      end

      res   = Shellwords.shellwords(res)
      msg   = res[1]
      files = res[2..-1]

      puts "<h2>Commit Files:</h2><ul>"
      puts files.map { |e| "<li>#{htmlize(e)}</li>\n" }.join
      puts "</ul>"

      puts "<h2>Using Message:</h2>"
      puts "<pre>#{htmlize(msg)}</pre>"
      STDOUT.flush

      unless files.empty?
        puts "<h2>Result:</h2>"
        auto_add_rm(files)
        res = commit(msg, files)
        f.output_commit_result(res)
      end
    end
  end
  
  def auto_add_rm(files)
    chdir_base
    add_files = files.select{ |f| File.exists?(f) }
    remove_files = files.reject{ |f| File.exists?(f) }
    res = add(add_files) unless add_files.empty?
    res = rm(remove_files) unless remove_files.empty?
  end
  
  def statuses(path = paths.first)
    @statuses ||= status(path)
{'fraction_non_alphanumeric': 0.13848732453383616, 'fraction_numerical': 0.004609260423213911, 'mean_word_length': 2.2856159669649, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 0, '>': 35, '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': '3284558', 'n_tokens_mistral': 1696, 'n_tokens_neox': 1706, 'n_words': 332}