class GitBug < Formula
  desc "Distributed, offline-first bug tracker embedded in git, with bridges"
  homepage "https://github.com/git-bug/git-bug"
  url "https://github.com/git-bug/git-bug/archive/refs/tags/v0.10.1.tar.gz"
  sha256 "1b5cafa3d9918ce18c4674c93b83359e211def83e716d5841fa93c77b457e6c2"
  license "GPL-3.0-or-later"
  head "https://github.com/git-bug/git-bug.git", branch: "master"

  depends_on "go" => :build

  def install
    ENV["GOBIN"] = bin
    ldflags = %W[
      -s -w
      -X github.com/git-bug/git-bug/commands.GitCommit="v#{tap.user}"
      -X github.com/git-bug/git-bug/commands.GitLastTag="v#{version}"
      -X github.com/git-bug/git-bug/commands.GitExactTag="v#{version}"
    ]
    system "go", "generate"
    system "go", "build", *std_go_args(ldflags:)

    man1.install Dir["doc/man/*.1"]
    doc.install Dir["doc/md/*.md"]

    bash_completion.install "misc/completion/bash/git-bug"
    zsh_completion.install "misc/completion/zsh/git-bug" => "_git-bug"
    fish_completion.install "misc/completion/fish/git-bug" => "git-bug.fish"
  end

  test do
    # Version
    assert_match version.to_s, shell_output("#{bin}/git-bug version")
    # Version through git
    assert_match version.to_s, shell_output("git bug version")

    mkdir testpath/"git-repo" do
      system "git", "init", "--initial-branch=main"
      system "git", "config", "user.name", "homebrew"
      system "git", "config", "user.email", "a@a.com"
      system "yes 'a b http://www/www' | git bug user new"
      system "git", "bug", "bug", "new", "-t", "Issue 1", "-m", "Issue body"
      system "git", "bug", "bug", "new", "-t", "Issue 2", "-m", "Issue body"
      system "git", "bug", "bug", "new", "-t", "Issue 3", "-m", "Issue body"

      assert_match "Issue 2", shell_output("git bug bug 'Issue 2'")
    end
  end
end
