lefthook
lefthook copied to clipboard
Git submodules doesn't appear in changed files
post-checkout:
commands:
update-submodules:
run: printf '%s\n' {all_files}
I expected the submodules we're using to appear in the {all_files}
but it wasn't the case. I think it should display the submodules as well.
Perhaps the more reliable way to test this could be:
post-checkout:
commands:
update-submodules:
files: git submodule foreach --recursive --quiet 'echo $displaypath' # Assuming it runs on the root
run: printf '%s\n' {all_files}
Why? Those are separate Git repositories.
Do you mean the submodule directories themselves, or rather the contents of submodules? The latter should be out of scope as I mentioned in my previous reply.
@mrexox I think the changed submodule in the below example called _template
is an unstaged change, yet somehow Lefthook skips it as part of the files
list.
pre-build:
commands:
git_diff_check:
files: git diff --name-only --staged
run: git diff --check
git_staged:
# This check helps to avoid a situation where fixes were applied and thus Lefthook raises no fault, but those
# changes weren't committed, and so wouldn't be pushed to the remote.
fail_text: Staged changes exist in your Git working copy. Please commit or stash them before pushing.
files: git diff --name-only --staged
run: git diff --exit-code --name-status --staged
git_untracked:
# This check helps to avoid a situation where Lefthook linters succeed because of to an untracked configuration
# file, and then fail when the repository is cloned without this configuration file.
fail_text: Untracked changes exist in your Git working copy. Please remove or index them before pushing.
files: git ls-files --exclude-standard --others
run: exit 9
git_unstaged:
# This check helps to avoid a situation where fixes were applied and thus Lefthook raises no fault, but those
# changes weren't committed, and so wouldn't be pushed to the remote.
fail_text: Unstaged changes exist in your Git working copy. Please commit or stash them before pushing.
files: git diff --name-only
run: git diff --exit-code --name-status -- {files}
$ lefthook run pre-build --commands git_staged,git_untracked,git_unstaged -vvvvv
│ [lefthook] cmd: [git rev-parse --show-toplevel]
│ [lefthook] dir:
│ [lefthook] err: <nil>
│ [lefthook] out: /Users/bla/repo
│ [lefthook] cmd: [git rev-parse --git-path hooks]
│ [lefthook] dir:
│ [lefthook] err: <nil>
│ [lefthook] out: .git/hooks
│ [lefthook] cmd: [git rev-parse --git-path info]
│ [lefthook] dir:
│ [lefthook] err: <nil>
│ [lefthook] out: .git/info
│ [lefthook] cmd: [git rev-parse --git-dir]
│ [lefthook] dir:
│ [lefthook] err: <nil>
│ [lefthook] out: .git
╭─────────────────────────────────────╮
│ 🥊 lefthook v1.5.5 hook: pre-build │
╰─────────────────────────────────────╯
│ [lefthook] cmd: [sh -c git diff --name-only --staged]
│ [lefthook] cmd: [sh -c git ls-files --exclude-standard --others]
│ [lefthook] cmd: [sh -c git diff --name-only]
│ [lefthook] dir: /Users/bla/repo
│ [lefthook] err: <nil>
│ [lefthook] out:
│ git_untracked (skip) no files for inspection
│ [lefthook] dir: /Users/bla/repo
│ [lefthook] err: <nil>
│ [lefthook] out: pdm.lock
pyproject.toml
│ [lefthook] files before filters:
[pdm.lock pyproject.toml]
│ [lefthook] files after filters:
[pdm.lock pyproject.toml]
│ [lefthook] dir: /Users/bla/repo
│ [lefthook] err: <nil>
│ [lefthook] out: _template
│ git_unstaged (skip) no files for inspection
┃ git_staged ❯
M pdm.lock
M pyproject.toml
────────────────────────────────────
summary: (done in 0.01 seconds)
🥊 git_staged: Staged changes exist in your Git working copy. Please commit or stash them before pushing.
bla@device:/Users/bla/repo
git status
On branch main
Your branch is up to date with 'origin/main'.
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: pdm.lock
modified: pyproject.toml
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
(commit or discard the untracked or modified content in submodules)
modified: _template (modified content)
Do you mean the submodule directories themselves
I meant the former, as I wanted to track whether the submodules are updated in the commit and verify the submodule was pointing to a valid branch (based on internal release policy).
I also wanted to detect submodule changes and expected it to be listed in lefthook's {staged_files}
for example.
Do you also expect lefthook to call the hooks of a submodule? Like parsing the lefthook config of a submodule and executing its hooks?
Not in my case, I only need to be notified that the submodule is dirty.