18

I have a feature branch that's grown quite large. There are quite a few lingering TODO comments in our code, but I'd like to find all TODOs added to the code (and not removed yet) on commits not yet merged into master. How might I go about that?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Ben Dilts
  • 10,535
  • 16
  • 54
  • 85

4 Answers4

15

You can simply diff your branch against master and grep for TODOs in the output

git diff master..branch | grep "^+.*TODO"

This works great for me assuming enough context in the TODO line.

Neil
  • 1,165
  • 1
  • 14
  • 24
7

You can use this as an Git alias:

git --no-pager diff -U0 master | \
grep '^+.*TODO' | \
sed 's/^+//' | \
git --no-pager grep -nFf - 2> /dev/null

It shows the added/modified TODO lines of the current branch (compared to master), but you have to git add before using it (because of git grep).

It can be even more usefull if you add it to the status alias, thus you would be sure that no TODOs remaining when you see the status.

[alias]
  s = "!sh -c ' \
    [ $GIT_PREFIX ] && cd $GIT_PREFIX; \
    git status --short --branch $*; \
    git --no-pager diff -U0 master | \
    grep \"^+.*TODO\" | \
    sed \"s/^+//\" | \
    git --no-pager grep -nFf - 2> /dev/null' -"

Example output:

$ git s
## my-branch...origin/my-branch [ahead 2]
 M README.adoc
README.adoc:12: // TODO: Add screencast
README.adoc:26: // TODO: Advertise Asciidoctor
bimlas
  • 2,359
  • 1
  • 21
  • 29
  • For this alias to work in git worktrees, add `export GIT_WORK_TREE=$(pwd); \ ` as the first line, before the `[ $GIT_PREFIX ] && cd $GIT_PREFIX` – Jan Jul 26 '18 at 08:55
2

git grep can perform a text search on a given tree. git branch --no-merged $commit gives you all branches not merged into the named commit. Combine the two and you're good to go (I know, you should not use git branch in scripts, but I couldn't quickly find a way to tell git for-each-ref to only consider not-merged refs. If anyone has a solution, feel free to comment/edit)

git branch --no-merged master | cut -c3- | while read branch; do
  git grep 'TODO' "$branch"
done
knittl
  • 246,190
  • 53
  • 318
  • 364
1

Combine above two answers, I write this code to show TODOs in the HEAD commit.

git --no-pager diff -U0 HEAD^..HEAD | grep -o 'TODO.*$' | xargs -I{} git --no-pager grep -wn -P '{}$' 2>/dev/null
zyf0330
  • 41
  • 4