How to list all my TODO messages in the current gi

2019-01-31 09:58发布

问题:

I want to see all TODO comments that only I wrote and that exist in the current code base that is git managed.

What I've got so far is printing all TODO comments that I've ever created or modified during the complete git history: git log -p --author="My name" -S TODO | grep "\+.*TODO"

But this tool chain lists all TODO comments ever written, even those that I've already resolved and thus removed again from code.

Is there a tool that can search the current code base line-by-line, check if it contains "TODO" and if this line was authored by me and then print those lines?

回答1:

You can combine git blame with grep.

Like this (not the best one, but should work)

git grep -l TODO | xargs -n1 git blame | grep 'Your name' | grep TODO

Improved versions might combine line numbers found by first grep with git blame's ability to show only given lines.



回答2:

I want do add on aragaer's and Kyle's solution:

  • use grep config to get your name
  • displaying the file name and the line number of the TODO comment
  • removing the commit SHA, the author's name and the commit timestamp
git grep -l TODO | xargs -n1 git blame -f -n -w | grep "$(git config user.name)" | grep TODO | sed "s/.\{9\}//" | sed "s/(.*)[[:space:]]*//"

This prints:

Cpp/CoolClass.cpp 123 //TODO: Do we really need this?
Cpp/AnotherClass.cpp 42 //TODO: Do we miss something?
Java/MyListener.java 23 //TODO: Optimize


回答3:

I found that the user name can have spaces, so it's easier to filter by email address. Also, the @todo (lowercase) is my way of adding notes in docblocks, so I added the "ignore lower/upper case" flag. This is my solution:

git grep -il TODO | xargs -n1 git blame -M  -f -e | grep -i TODO | grep $(git config user.email)


回答4:

Complete with using git config to get your name:

git grep -l TODO | xargs -n1 git blame | grep "$(git config user.name)" | grep TODO


回答5:

Small suggestion here that I just ran across, larger repo and some binary files. The xargs -ni git blame section was causing an unexpected term with signal 13. I got around this by adding a -I switch on the initial git grep.

FWIW here's full error (go go google)

xargs: git: terminated by signal 13

Here's an example I used (also removed grep for my mine as wanted to see all)

git grep -I -l TODO | xargs -n1 git blame -f -n -w | grep TODO | sed "s/.\{9\}//" | sed "s/(.*)[[:space:]]*//"