Search code examples
gittodo

Use git to list TODOs in code sorted by date introduced


I would like to display a list of all TODOs I have in my code, but sorted by using the history (Git) and showing the most recent first.

Kind of like the result displayed here: git – order commits introducing "TODO"s by date

But showing the lines of the TODOs, not the hashes.

Should look like this:

Fri May 22 11:22:27 2015 +0200 - // TODO: Refactor this code
Fri Apr 25 17:32:13 2014 +0200 - // TODO: We should remove this when tests pass
Sat Mar 29 23:11:39 2014 +0100 - // TODO: Rename to FooBar

I don't think git log can show that, but I'm not sure and I don't have the Git CLI mojo to figure this out myself. Any idea?


Solution

  • Here is an approximate solution; the formatting isn't quite what you've asked for, so you'd need to pipe it through awk or something similar to reorder the fields if that's important.

    git ls-tree -r -z --name-only HEAD -- . | xargs -0 -n1 git blame -c | grep TODO | sort -t\t -k3
    

    It works as follows:

    • git ls-tree -r -z --name-only HEAD -- . gets all the file names in the repository
    • xargs -0 -n1 git blame -c calls git blame for every file; the -c tells it to use a tab between each field in the output (used by the sorting later) -- these two parts are based on the top answer to this question
    • grep TODO filter out lines that don't contain the text TODO
    • sort -t\t -k3 using tabs as delimiters, sort by the third field (which should be the date)

    Note that this ignores the time zone completely (i.e. it just sorts on the raw date without taking account of the +0000 part).