1

What command can I run in my Windows Git Bash that will show me the file names, line preview (context), and line numbers of all of the places there is "TODO" written in my code, limited to new files and modified files?

Inadequate Approach 1 (from here)

This is clunky and doesn't print line number:

function __greptodo {
    QUERY="TODO"
    for FILE in `git diff --name-only`; do
        grep "$QUERY" $FILE 2>&1 >/dev/null
        if [ $? -eq 0 ]; then
            echo '———————————————'
            echo $FILE 'contains' $QUERY
            grep "$QUERY" $FILE 2>&1
        fi
    done
}
alias greptodo=__greptodo

Inadequate Approach 2 (from here)

This is much better (shows context and includes new files and modified files) but still doesn't print line numbers:

grep -s "TODO" $(git ls-files -m)

Ryan
  • 22,332
  • 31
  • 176
  • 357

1 Answers1

2

The -n flag tells grep to show the line number, so you were close. Try:

grep -sn "TODO" $(git ls-files -m)

To include untracked files, use the --others (-o) flag, and the --exclude-standard flag to exclude the files usually ignored by Git:

grep -sn "TODO" $(git ls-files -mo --exclude-standard)
GregWW
  • 117
  • 1
  • 6
  • Thanks. This is getting closer. However, this seems to work for files that are "Changes to be committed" or "Changes not staged for commit" but not "Untracked files". And if I first run `git add *`, your command then just hangs. – Ryan Oct 03 '18 at 13:17
  • Gotcha. I missed that you needed new files, too. See my edit above, but you need the `--others` and `--exclude-standard` flags for `git ls-files`. – GregWW Oct 04 '18 at 14:23
  • Thank you! And I see that adding `-i` makes it case insensitive: https://stackoverflow.com/a/48492465/470749 – Ryan Oct 04 '18 at 15:41
  • I just noticed an odd, rare case where this wasn't working: I had popped a stash, which had a conflict in one file, and then I resolved the conflict (in Netbeans and marked it as resolved), and then when I ran this, none of my TODOs appeared. So then I saved a stash again, and this time instead of popping it, I used `apply`. Then this code worked to find my TODOs. – Ryan Dec 21 '19 at 15:37