module Hoe::Git2

def define_git2_tasks # :nodoc:

:nodoc:
def define_git2_tasks # :nodoc:
  return unless __run_git("rev-parse", "--is-inside-work-tree") == "true"
  desc "Print the current changelog."
  task "git:changelog" do
    tag = ENV["FROM"] || git_tags.last
    range = [tag, "HEAD"].compact.join("..")
    now = Time.new.strftime("%Y-%m-%d")
    changes =
      __run_git("log", range, "--format=tformat:%B|||%aN|||%aE|||")
        .split("|||")
        .each_slice(3)
        .map do |msg, _author, _email|
        msg.split("\n").reject(&:empty?)
      end
    changes = changes.flatten
    next if changes.empty?
    $changes = Hash.new { |h, k| h[k] = [] } # standard:disable Style/GlobalVars
    codes = {
      "!" => :major,
      "+" => :minor,
      "*" => :minor,
      "-" => :bug,
      "?" => :unknown
    }
    codes_re = Regexp.escape codes.keys.join
    changes.each do |change|
      if change =~ /^\s*([#{codes_re}])\s*(.*)/
        code, line = codes[$1], $2
      else
        code, line = codes["?"], change.chomp
      end
      $changes[code] << line # standard:disable Style/GlobalVars
    end
    puts "=== #{ENV["VERSION"] || "NEXT"} / #{now}"
    puts
    changelog_section :major
    changelog_section :minor
    changelog_section :bug
    changelog_section :unknown
    puts
  end
  desc "Update the manifest with Git's file list. Use Hoe's excludes."
  task "git:manifest" do
    with_config do |config, _|
      files = __run_git("ls-files").split($/)
      files.reject! { |f| f =~ config["exclude"] }
      File.open "Manifest.txt", "w" do |f|
        f.puts files.sort.join("\n")
      end
    end
  end
  desc "Create and push a TAG (default #{git_release_tag_prefix}#{version})."
  task "git:tag" do
    tag = ENV["TAG"]
    ver = ENV["VERSION"] || version
    pre = ENV["PRERELEASE"] || ENV["PRE"]
    ver += ".#{pre}" if pre
    tag ||= "#{git_release_tag_prefix}#{ver}"
    git_tag_and_push tag
  end
  task "git:tags" do
    p git_tags
  end
  task :release_sanity do
    unless __run_git("status", "--porcelain").empty?
      abort "Won't release: Dirty index or untracked files present!"
    end
  end
  task release_to: "git:tag"
end