zf

zenflows testing
git clone https://s.sonu.ch/~srfsh/zf.git
Log | Files | Refs | Submodules | README | LICENSE

build_info.ex (2372B)


      1 defmodule Credo.BuildInfo do
      2   @moduledoc false
      3 
      4   # This is called at compile-time and should not be used at runtime!
      5 
      6   @doc false
      7   def version(mix_version) do
      8     version(mix_version, git_info())
      9   end
     10 
     11   defp version(mix_version, nil) do
     12     mix_version
     13   end
     14 
     15   defp version(mix_version, git_info) do
     16     version =
     17       if git_info.tag == "v#{mix_version}" do
     18         mix_version
     19       else
     20         branch = git_info.branch || "nobranch"
     21         commit = git_info.commit || "nocommit"
     22 
     23         "#{mix_version}-ref.#{branch}.#{commit}"
     24       end
     25 
     26     if git_info.dirty? do
     27       "#{version}+uncommittedchanges"
     28     else
     29       version
     30     end
     31   end
     32 
     33   defp git_info do
     34     if in_git_work_tree?() do
     35       %{
     36         commit: git_commit(),
     37         branch: git_branch(),
     38         tag: git_tag(),
     39         dirty?: git_dirty?()
     40       }
     41     end
     42   end
     43 
     44   defp in_git_work_tree? do
     45     {_, exit_status} =
     46       System.cmd("git", ["rev-parse", "--is-inside-work-tree"], stderr_to_stdout: true)
     47 
     48     exit_status == 0
     49   rescue
     50     _ -> false
     51   end
     52 
     53   defp git_branch do
     54     case System.cmd("git", ["branch"]) do
     55       {output, 0} -> git_branch(output)
     56       {_, _} -> nil
     57     end
     58   end
     59 
     60   defp git_branch(output) do
     61     line_with_active_branch =
     62       output
     63       |> String.split("\n")
     64       |> Enum.find(fn
     65         "* " <> _ -> true
     66         _ -> false
     67       end)
     68 
     69     case line_with_active_branch do
     70       "* (HEAD detached at origin/" <> remote_branch_name ->
     71         String.replace(remote_branch_name, ~r/\)$/, "")
     72 
     73       "* (HEAD detached at " <> branch_name ->
     74         String.replace(branch_name, ~r/\)$/, "")
     75 
     76       "* " <> branch_name ->
     77         branch_name
     78 
     79       _ ->
     80         nil
     81     end
     82   end
     83 
     84   defp git_commit do
     85     case System.cmd("git", ["rev-parse", "--short", "HEAD"]) do
     86       {output, 0} -> String.trim(output)
     87       {_, _} -> nil
     88     end
     89   end
     90 
     91   defp git_tag do
     92     case System.cmd("git", ["tag", "--points-at", "HEAD"]) do
     93       {output, 0} -> String.trim(output)
     94       {_, _} -> nil
     95     end
     96   end
     97 
     98   defp git_dirty? do
     99     case System.cmd("git", ["status", "--short"]) do
    100       {output, 0} -> output |> String.trim() |> git_dirty?()
    101       {_, _} -> nil
    102     end
    103   end
    104 
    105   defp git_dirty?(""), do: false
    106   # Hex puts a `.fetch` file in the working dir when downloading deps via git
    107   defp git_dirty?("?? .fetch"), do: false
    108   defp git_dirty?(_), do: true
    109 end