Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use git to list TODOs in code sorted by date introduced

Tags:

git

todo

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?

like image 351
Gui13 Avatar asked May 09 '17 13:05

Gui13


People also ask

How do I get a list of files in git?

This command will list the files that are being tracked currently. If you want a list of files that ever existed use: git log --pretty=format: --name-only --diff-filter=A | sort - | sed '/^$/d'This command will list all the files including deleted files.

How do I see files committed in git?

Find what file changed in a commit To find out which files changed in a given commit, use the git log --raw command. It's the fastest and simplest way to get insight into which files a commit affects.

How do I view a .git file?

Use the terminal to display the . git directory with the command ls -a . The ls command lists the current directory contents and by default will not show hidden files. If you pass it the -a flag, it will display hidden files.

What is git ls command?

git ls-files can use a list of "exclude patterns" when traversing the directory tree and finding files to show when the flags --others or --ignored are specified. gitignore[5] specifies the format of exclude patterns.


1 Answers

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

like image 174
Matthew Strawbridge Avatar answered Sep 28 '22 05:09

Matthew Strawbridge