8

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?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Gui13
  • 12,993
  • 17
  • 57
  • 104

2 Answers2

8

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).

Community
  • 1
  • 1
Matthew Strawbridge
  • 19,940
  • 10
  • 72
  • 93
  • Wow! Nearly there. It doesn't handle my name ('Name FamilyName') so the sort function fails at this point, but it's not far. – Gui13 May 09 '17 at 15:22
  • That's strange; there are spaces in the repo I tested it on and it worked fine for me there. Actually, it seems to work for me without the `-t` flag at all. I'm using bash on Windows so it might be different on your OS. – Matthew Strawbridge May 09 '17 at 15:33
  • how could i include the file path in the output? – Richard Zilahi Dec 04 '21 at 22:41
  • 1
    @RichardZilahi I expect this is what you get with the `--full-tree` option, but I haven't tried it. See https://git-scm.com/docs/git-ls-tree – Matthew Strawbridge Dec 05 '21 at 00:32
0

Faced this issue a while ago and implemented the CLI tool: https://pypi.org/project/whatshouldido/