qgadrian/elixir_git_hooks

Pre commit/push hooks fail in the latest version

svsool opened this issue ยท 14 comments

Hi ๐Ÿ‘‹ ,

Thanks for this project!

Pre-commit or push hooks fail in the latest version for me under macOS Monterey 12.3 (whether it matters), I wonder if conditional cd shown in the screenshot is the culprit. (this commit)

image

image

My config:

if Mix.env() == :dev do
  config :git_hooks,
    verbose: true,
    hooks: [
      pre_commit: [
        tasks: [
          {:cmd, "mix format --check-formatted"}
        ]
      ],
      pre_push: [
        tasks: [
          {:cmd, "mix quality"}
        ]
      ]
    ]
end

Seem to work fine with the version 0.6.5.

How to reproduce:

  1. Download git_hooks_bug.zip
  2. mix deps.get && mix compile
  3. touch test.txt && git commit -m whatever

Same here.

Same issue happened here. Tried to execute:

โžœ mix deps.get && mix deps.compile
โžœ git commit -m "test"

Results:

Unchecked dependencies for environment dev:
* blankable (Hex package)
  the dependency is not available, run "mix deps.get"
* ex_doc (Hex package)
  the dependency is not available, run "mix deps.get"
* recase (Hex package)
  the dependency is not available, run "mix deps.get"
* dialyxir (Hex package)
  the dependency is not available, run "mix deps.get"
* credo (Hex package)
  the dependency is not available, run "mix deps.get"
** (Mix) Can't continue due to errors on dependencies

As an alternative 0.6.5 is working fine.

Unfortunately :mix_task doesn't respect result values in 0.6.5, so if you downgrade be sure to use :cmd.

We ran into this, and the current fix we have is to run mix git_hooks.install whenever we get this missing dependencies error even after running deps.get.

I have limited time but actively looking at this when I have time to focus on it ๐Ÿ˜ž

So far, I cannot reproduce this in a reliable way. The example project provided by this issue gets fixed after running mix git_hooks.install, but this should never happen in the first case so something it's definitely not right.

I will keep investigating, but it will be great if I can get more details such as the folder structure of the repo as I think it's the root of the issue ๐Ÿ™

When the auto_install feature creates .git/hooks/pre-push it looks like this:

#!/bin/sh

[ "/Users/myuser/code/myapp/deps/git_hooks" != "" ] && cd "/Users/myuser/code/myapp/deps/
git_hooks"

mix git_hooks.run pre_push "$@"
[ $? -ne 0 ] && exit 1
exit 0

I notice that if I remove the first line there, the git hook seems to work properly!

It seems to mean that mix deps.get is ran in the deps/git_hooks dir and not in the project root as we may want instead.

What is the idea with that cd line? Thanks, @qgadrian !

When the auto_install feature creates .git/hooks/pre-push it looks like this:

#!/bin/sh

[ "/Users/myuser/code/myapp/deps/git_hooks" != "" ] && cd "/Users/myuser/code/myapp/deps/
git_hooks"

mix git_hooks.run pre_push "$@"
[ $? -ne 0 ] && exit 1
exit 0

I notice that if I remove the first line there, the git hook seems to work properly!

It seems to mean that mix deps.get is ran in the deps/git_hooks dir and not in the project root as we may want instead.

What is the idea with that cd line? Thanks, @qgadrian !

Hey @djthread

I am currently reviewing this issue, expect a preview version by this week ๐Ÿคž

Regarding the cd... things started going south with this commit trying to provide more flexibility on some custom project setups (elixir projects not in the root of the git repo, submodules...) :/

I created a PR and will remove the auto directory change of this library, it's giving more problems than solving anything.

Can anyone please test 0.8.0-pre0 version and confirm the issues are gone? I've tested it on my projects and they work fine, I'm having a hard time trying to reproduce this every time.

Hm. I checked out my app fresh and changed my git_hooks dependency to the fix-hooks-and-dependencies branch.

During compilation, I saw

โ†— Installing git hooks...

And the dev phoenix app started up ...

My pre-push hook which should have been created wasn't created at all.

I'm not certain of many details, so feel free to ask questions.

Screenshot_20221123_141150

It asks to install , whenever there is a mix command.

It asks to install , whenever there is a mix command.

@apoorv-2204 is this happening with 0.7.3 or 0.8.0-pre0 version?

FWIW, I get the same error with 0.8.0-pre0:

Unchecked dependencies for environment dev:
* blankable (Hex package)
  the dependency is not available, run "mix deps.get"
* ex_doc (Hex package)
  the dependency is not available, run "mix deps.get"
* recase (Hex package)
  the dependency is not available, run "mix deps.get"
* dialyxir (Hex package)
  the dependency is not available, run "mix deps.get"
* credo (Hex package)
  the dependency is not available, run "mix deps.get"
** (Mix) Can't continue due to errors on dependencies

We ran into this, and the current fix we have is to run mix git_hooks.install whenever we get this missing dependencies error even after running deps.get.

this works for me:

mix deps.compile && mix git_hooks.install

Update on this, I published in hex.pm a new v0.8.0-pre2 release that hopefully resolves this issue.

I will wait to get some feedback before publishing a production release.

See this comment for more technical details.