'Search for TODO in added lines and show them as default commit message in TortoiseGit on Windows
I use TortoiseGit on Windows. Everytime I start a new commit, the following shall happen:
- Search for added or modified lines containing " TODO"
- Add these lines to the commit message (meant as a warning)
- Show commit dialog with the prepared commit message
How can I do this?
Solution 1:[1]
It can be done using TortoiseGit hooks (not to be confused with git hooks):
Create a batch file with the following line (just copy and paste):
git diff --color=always | findstr "[32m+[m[32m" | findstr /c:" TODO" >> %2In the TortoiseGit settings go to
Hook Scripts.- Press
Add. - Check
Enabled. - Choose path this hook should work for (
*for all paths) - Put the path to the batch script in the
Command Line To Executebox. - Press
Okand close settings. - Close and reopen all other TortoiseGit windows to ensure the hook is setup.
What does the batch file do?git diff --color=always shows all changes with nice colors.
We use those colors in addition to the + to identify added lines with the first findstr command.
The second findstr command looks for " TODO".
The /c: parameter tells findstr to interpret the space as part of the search pattern.
Solution 2:[2]
I followed @Tim Pohlmann's answer above and ended up modifying the batch script to be:
git diff | findstr "^+// TODO:" > %2
I made this change because the original command was printing the color specifiers in the TortoiseGit commit dialog.
Additionally, I had to specify the Hook Type as "Start Commit Hook" and check the box labelled "Wait for the script to finish" in the Configure Hook Scripts dialog.
For further clarity, the %2 pipes the output of the command to TortoiseGit's commit dialog.
I looked into adding line numbers, but the git diff command does not support adding line numbers per this post. A simple enough solution is to be more descriptive in my TODO's :)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Benji Justice |
