<commit_before>require 'fileutils'
require 'zlib'

class GitDB::Commands::ReceivePack

  def execute(args)
    repository = args.first
    raise ArgumentError, "repository required" unless repository

    database = GitDB.database(repository)

    needs_capabilities = true
    database.get_refs.each do |ref, sha|
      GitDB.log("ISAERF #{ref} #{sha}")
      write_ref(ref, sha, needs_capabilities)
      needs_capabilities = false
    end
    write_ref("capabilities^{}", GitDB.null_sha1) if needs_capabilities
    io.write_eof

    refs = []
    new_shas = []

    while (data = io.read_command)
      old_sha, new_sha, ref = data.split(' ')
      ref, report = ref.split(0.chr)

      refs << ref
      new_shas << new_sha

      if new_sha == GitDB.null_sha1
        database.delete_ref(ref)
      else
        database.write_ref(ref, new_sha)
      end
    end

    unless new_shas.reject { |sha| sha == GitDB.null_sha1 }.length.zero?
      while (entries = io.read_pack)
        database.write_objects(entries)
      end
    end

    io.write_command("unpack ok\n")
    refs.each do |ref|
      io.write_command("ok #{ref}\n")
    end
    io.write_eof
  end

private

  def capabilities
    " report-status delete-refs ofs-delta "
  end

  def io<commit_msg>write objects in pages to couch<commit_after>require 'fileutils'
require 'zlib'

class GitDB::Commands::ReceivePack

  def execute(args)
    repository = args.first
    raise ArgumentError, "repository required" unless repository

    database = GitDB.database(repository)

    needs_capabilities = true
    database.get_refs.each do |ref, sha|
      GitDB.log("ISAERF #{ref} #{sha}")
      write_ref(ref, sha, needs_capabilities)
      needs_capabilities = false
    end
    write_ref("capabilities^{}", GitDB.null_sha1) if needs_capabilities
    io.write_eof

    refs = []
    new_shas = []

    while (data = io.read_command)
      old_sha, new_sha, ref = data.split(' ')
      ref, report = ref.split(0.chr)

      refs << ref
      new_shas << new_sha

      if new_sha == GitDB.null_sha1
        database.delete_ref(ref)
      else
        database.write_ref(ref, new_sha)
      end
    end

    unless new_shas.reject { |sha| sha == GitDB.null_sha1 }.length.zero?
      while (entries = io.read_pack)

        # bulk in 50 at a time
        while (page = entries.shift(50)).length > 0
          database.write_objects(page)
        end
      end
    end

    io.write_command("unpack ok\n")
    refs.each do |ref|
      io.write_command("ok #{ref}\n")
    end
    io.write_eof
  end

private

  def capabilities
    " report-status delete-refs ofs-delta "
  end

  def io
{'fraction_non_alphanumeric': 0.10472589792060491, 'fraction_numerical': 0.004914933837429111, 'mean_word_length': 2.6, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 4, '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': '652229', 'n_tokens_mistral': 904, 'n_tokens_neox': 866, 'n_words': 203}