Use git to list TODOs in code sorted by date introduced

▼魔方 西西 提交于 2021-02-07 14:39:42

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/43871473/use-git-to-list-todos-in-code-sorted-by-date-introduced

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!