6

Let's say I added two lines to the file hello.rb.

# this is a comment
puts "hello world"

If I do git diff, it will show that I added two lines.

I don't want git to show any line which is a Ruby comment. I tried using git diff -G <regular expression>, but it didn't work for me. How do I do git diff so that it won't show any Ruby comments?

pillravi
  • 4,035
  • 5
  • 19
  • 33
r3b00t
  • 6,953
  • 6
  • 27
  • 37
  • 4
    Probably not ideas, but for you could do `git diff | grep -v "^+\s*#"` depending on your use case. – Dogbert Sep 11 '13 at 06:41
  • 2
    There's nothing built-in to Git that will do this. It's somewhat complex, as comments are different in every language, and determining whether or not a line is really a comment or just looks like one is even more difficult without parsing files. This question has been asked on Stack Overflow a few different times, but I don't want to link to any of them because I don't see any satisfactory answers. – Jim Stewart Sep 11 '13 at 06:44
  • 1
    @JimStewart although tweaking http://stackoverflow.com/a/8291327/6309 would be a good start. – VonC Sep 11 '13 at 06:50
  • `git -G` matches regexp to show a whole commit, try a trick: `git diff | grep "^[ +-][^#]" | less` – Малъ Скрылевъ Nov 27 '13 at 08:52

2 Answers2

5

One possibility would be (ab)using git's textconv filters which are applied before the diff is created, so you can even transform binary formats to get a human-readable diff.

You can use any script that reads from a file and writes to stdout, such as this one which strips all lines starting with #:

#!/bin/sh
grep -v "^\s*#" "$1" || test $? = 1

(test $? = 1 corrects the exit code of grep, see https://stackoverflow.com/a/49627999/4085967 for details.)

For C, there is a sed script to remove comments, which I will use in the following example. Download the script:

cd ~/install
wget http://sed.sourceforge.net/grabbag/scripts/remccoms3.sed
chmod +x remccoms3.sed

Add it to your ~/.gitconfig:

[diff "strip-comments"]
         textconv=~/install/remccoms3.sed

Add it to the repository's .gitattributes to enable it for certain filetypes:

*.cpp diff=strip-comments
*.c diff=strip-comments
*.h diff=strip-comments

The main downside is that this will be always enabled by default, you can disable it with --no-textconv.

mgmax
  • 109
  • 1
  • 2
3

Git cares a lot about the data that you give to it, and tries really hard not to lose any information. For Git, it doesn't make sense to treat some lines as if they haven't changed if they had.

I think that the only reasonable way to do this is to post-process Git output, as Dogbert had already shown in his comment.

Andrew Grimm
  • 78,473
  • 57
  • 200
  • 338
Jan Warchoł
  • 1,063
  • 1
  • 9
  • 22