Imagine you have a pre-commit hook that does this:
#!/bin/bash
echo "--- Dataset Pre-Commit Hook ---"
DATASET_REPO_PATH="$(pwd)"
python3 -m my_script_that_generates_or_modifies_a_file
git -C $DATASET_REPO_PATH add file
echo "--- Done ---"
exit
It will work IF AND ONLY IF, there was already something to be commited. It does not have to be the same file tho. If there was nothing to be commited, the git add git add file will work, but will not be commited.
Example that works:
- You changed a file, and added it (for example, the
gitignore)
$> git status
On branch master
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: .gitignore
- Run commit. First, the
pre-commit hook will run and create file. Then it will commit.
--- Dataset Pre-Commit Hook ---
--- Done ---
[master (root-commit) 2ba2f7c] First commit
3 files changed, 34 insertions(+)
create mode 100644 .gitignore
create mode 100644 file
- You can see the message says
file was commited. And git status now outputs:
On branch master
nothing to commit, working tree clean
Example that DOES NOT work
- You have nothing to add. You can of course run
git add but it will do nothing. git status gives:
On branch master
Your branch is up to date with 'origin/master'.
nothing to commit, working tree clean
- Run commit. The hook will create
file. But the commit message says he did nothing.
--- Dataset Pre-Commit Hook ---
--- Done ---
On branch master
Your branch is up to date with 'origin/master'.
nothing to commit, working tree clean
- Verify the status
git status
On branch master
Your branch is up to date with 'origin/master'.
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: file
- You can see that file was indeed modified, and that it was added, but yet not commited. You need to run
git commit again for it to work.
I tried adding that as a post-commit hook but got no luck. If someone knows a fix for this case, I would love to know.