3

I have a relatively large feature branch, in which I've added a lot of printf style debugging (as well as a lot of other additions of course.)

I'd like a way to find which files I've added printf statements to.

At the moment I use something like

git diff master | grep printf | grep "^+" 

This gives an output like

+ printf("Debug %d\n", i); + printf("Another debug\n"); 

Then I can search for each of those lines in my entire project... sigh.

Or I could use git log master..HEAD -G printf but that shows me a lot of additional context that I just dont need.

I would love a way to get grep like output from git like this: (hypothetical only)

> git magic-grep master -e "printf" /some/path/foo.c:65: printf("Debug %d\n", i); /someother/path/bar.c:123: printf("Another debug\n"); 

then my editor would be happy and I would be much more efficient.

Is there a way to pass git a magical incantation to make it do something like this? Or are there additional scripts to make it work?

3 Answers 3

1

I created a bash script (at end of answer) to do what I wanted. I named it git-find-changes-matching made it executable and stuck it in my path.

Now I can do

git find-changes-matching printf master 

And get the result

/some/path/foo.c:65: printf("Debug %d\n", i); /someother/path/bar.c:123: printf("Another debug\n"); 

NOTE: It doesn't do much error handling, and its escaping is probably a little wonky.

NOTE 2: It will give wrong results if your working directory is dirty. Just make sure everything is commited.

#!/bin/bash usage () { echo "usage:" $@ exit 127 } die () { echo $@ exit 128 } if test $# -ne 2 then usage "$0 <regex> <other-branch>" fi KEY=$1 MERGEBASE=$(git merge-base HEAD $2) for x in $(git log ${MERGEBASE}..HEAD -G "${KEY}" --raw | grep "^:" | awk '{ print $6 }' | sort | uniq) ; do test -f $x || continue while read -r line; do if [[ "$line" = "" ]] then true else grep -H -n -F "$line" $x fi done <<< "$(git diff ${MERGEBASE} $x | grep "${KEY}" | grep "^+" | sed "s/^+//")" done 
Sign up to request clarification or add additional context in comments.

Comments

1

Regarding a git-based solution using git log, you can reduce the context with the -U option:

git log master..HEAD -U1 -G printf 

That is:

-U<n> --unified=<n> 

Generate diffs with <n> lines of context instead of the usual three. Implies -p.

2 Comments

git log master..HEAD -U1 -G printf will give you a reduced context for the entire hunk that is changed, not just the line... so in some cases I'd still need to hunt through several hundred lines of code to find the one printf statement.
@MichaelAnderson true, even if it had, the second pipe would not pick it up. Removing.
0

For anything like this I would suggest not automating it.

git add -p 

Will go though each of your changes asking if you want to stage them

This gets you to double check all changes.

Edit this should be done before its been committed. Now it has been committed just do

git grep printf 

Or

git grep printf -- '*.c' 

to search only the c files

1 Comment

It's a feature branch, not a single commit. And git grep printf will find all the printfs in all the files, not just those I've added on my branch. Since my branch changes about 100 out of 10,000 files, and many of those old files use printfs for various purposes... Thats a lot of extra cruft to wade through.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.