captainhookphp / captainhook

CaptainHook is a very flexible git hook manager for software developers that makes sharing git hooks with your team a breeze.
http://captainhook.info
MIT License
1.01k stars 87 forks source link

New Condition: File Of Type Pushed #199

Closed sprankhub closed 1 year ago

sprankhub commented 1 year ago

Inside pre-commit, we can use \CaptainHook\App\Hook\Condition\FileStaged\OfType to only execute an action if a file of a specific type is staged. I would like to have similar functionality inside pre-push. I only want to execute an action in pre-push if a file of a specific type is changed in this push.

Do you think this is doable in a sane way? Do you think this is valuable also for others?

Thanks!

sebastianfeldmann commented 1 year ago

Uhh that sounds actually really useful. By running something like git diff --name-only head origin/main or git diff --name-only STATE_GETTING_PUSHED REMOTE/BRANCH. we could determine the list of files getting pushed.

It could get a bit tricky because if I understand it correctly you can push to multiple remotes at the "same" time. Given that should be a very rare edge case I still don't want to break anything. I have to run some tests. Let me get back to you.

sebastianfeldmann commented 1 year ago

With version 5.15.0 a new Condition was added that can be used in pre-push hooks like this. It can also be used in all post-change events like post-rewrite, post-checkout and post-merge

{
  "action": "phpstan analyse",
  "conditions": [
    {"exec": "\\CaptainHook\\App\\Hook\\Condition\\FileChanged\\OfType",
      "args": [
        "php"
    ]}
  ]
}
sprankhub commented 1 year ago

Thank you so much, @sebastianfeldmann!