Search code examples
windowstortoisegitcommit-message

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:

  1. Search for added or modified lines containing " TODO"
  2. Add these lines to the commit message (meant as a warning)
  3. Show commit dialog with the prepared commit message

How can I do this?


Solution

  • It can be done using TortoiseGit hooks (not to be confused with git hooks):

    1. Create a batch file with the following line (just copy and paste):

      git diff --color=always | findstr "[32m+[m[32m" | findstr /c:" TODO" >> %2
      
    2. In the TortoiseGit settings go to Hook Scripts.

    3. Press Add.
    4. Check Enabled.
    5. Choose path this hook should work for (* for all paths)
    6. Put the path to the batch script in the Command Line To Execute box.
    7. Press Ok and close settings.
    8. 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.