dwyl / elixir-pre-commit

✅ Pre-commit hooks for Elixir projects
GNU General Public License v2.0
46 stars 10 forks source link

"Not a git repository: '.git'" when using git dependent versioning #29

Open ecly opened 6 years ago

ecly commented 6 years ago

This is may not be directly related to the project, but I figure others could wind up in a similar pickle, and I haven't been able to find a solution to this yet, so I thought I'd reach out here.

For this umbrella-project using edeliver for deployment, I've had to implement auto revisioning inside each app's mix.exs. This is done as shown by the edeliver project here.

My pre_commit hook is configured to run test, format --check-formatted and credo, and this works fine when I call mix pre_commit, however when the pre_commit is triggered by actually using git commit, the following happens:

...
fatal: Not a git repository: '.git'
fatal: Not a git repository: '.git'
fatal: Not a git repository: '.git'
fatal: Not a git repository: '.git'
...

This basically causes the code:

  def auto_version() do
    {rev, _} = System.cmd("git", ["rev-parse", "--short", "HEAD"])
    "1.0.0+#{String.trim_trailing(rev)}"
  end

To cause an error, resulting in a bad formatting of the SemVer version, causing mix to throw the error: ** (Mix) Expected :version to be a SemVer version, got: "1.0.0+". and for the pre_commit to fail.

Now the bit which I cannot wrap my head around, is that when I add an IO.inspect(File.cwd!()) to the auto_version/0 function, it returns the folder of the individual mix.exs-files, all of which should be under the git directory, and from which I can open IEx and call the function without error myself.

Similarly, if i manually run the hook sh .git/hooks/pre-commit from the directory from which I invoke the git commit, it also works.

Any ideas for a solution to this?

ecly commented 6 years ago

For the time being I've resorted to this simple "fix", however I'm definitely not too stoked about it:

  def auto_version() do
    {rev, _} = System.cmd("git", ["rev-parse", "--short", "HEAD"], stderr_to_stdout: true)
    # HACK this is necessary for the command to result
    # in valid SemVer versions even during pre_commit hooks.
    if String.starts_with?(rev, "fatal") do
      "1.0.0"
    else
      "1.0.0+#{String.trim_trailing(rev)}"
    end
  end