Search code examples
gitpre-commitpre-commit.com

How do I configure pre-commit config to leverage the git config credential.helper


I'm trying to setup pre-commit in a Codefresh pipeline and to do this I need to be able to push credentials into the environment.

Normal git stuff can leverage the credential.helper. Like this for example:

git config --global credential.helper '!f() { echo username=oauth2; echo "password=$GIT_TOKEN"; };f'

and you use:

git clone https://[email protected]/pre-commit/pre-commit-hooks.git

And it will inject the password from the environment variable.

I tried to leverage this process to configure my pre-commit config, however it doesn't seem to leverage the git credential.helper.

repos:
  - repo: https://[email protected]/pre-commit/pre-commit-hooks.git
    rev: v4.4.0
    hooks:
    - id: check-added-large-files
    - id: check-merge-conflict
    - id: check-executables-have-shebangs
    - id: check-shebang-scripts-are-executable
    - id: check-case-conflict

Then try to install pre-commit hooks:

pre-commit install --install-hooks

I was expecting it to use the git credential.helper to inject the password, but instead it is waiting for the password to be provided.

I was expecting that because it is trying to do git operations (clone to begin with) in the background it would leverage my git configuration including the credential helper...


Solution

  • Checked the code. Relating to issue: https://github.com/pre-commit/pre-commit/issues/300

    The environment is cleared of variables beginning with GIT_

    Modified my variable to be MY_GIT_TOKEN and it correctly picks this up and uses the credential helper.