简体   繁体   中英

How to diff just the source files?

I have two almost similar source code trees, but do not have access to the source code repository so I am stuck with release packages that contain also test reports, documentation, binaries etc.

the diff command only support --exclude , but I would like to do something like diff -wbur --include='*.c,*.h' tree1 tree2

I know that this question is somewhat related, but does not really address my issue.

Bonus points for ignoring change blocks that are completely in C comments :)

Little modification to a result from google helped, in tree1 did find . -name '*.[ch]' -exec diff -wibu {} ../tree2/{} \\; find . -name '*.[ch]' -exec diff -wibu {} ../tree2/{} \\;

Here's a little patch maker script:

#!/bin/bash

USAGE="USAGE: $0 <dist dir> <edited dir>"

[ '--help' == "$1" ] && { echo $USAGE; exit 0; }
[ 2 -eq $# ] || { echo $USAGE; exit 1; }

# trim starting './' and trailing /'/
original=$(echo $1 | sed 's-^\./--;s-/$--')
changed=$(echo $2 | sed 's-^\./--;s-/$--')

[ -d $original ] || { echo "ERROR: Directory $original does not exist" >&2 ; exit 2; }
[ -d $changed ] || { echo "ERROR: Directory $changed does not exist" >&2; exit 3; }

#command="ls -l"
command="diff -Naur"

find $original -name '*.[ch]' -o -name '*.cpp' | sed 's-^[^/]*/--' | { while read file; do $command  $original/$file $changed/$file; done; }

I would exclude everything that doesn't match .c or .h. So it means it will only include .c and .h files :

diff -x "*.[^ch]"

For me it is the best way to do because you are only using diff

You can write all files to exclude into a temporary file and give it to diff's -X argument.

find tree1 tree2 -type f -not -name '*.[ch]' >exludes
diff -wbur tree1 tree2 -X excludes

Or simplier (works in Bash):

diff -wbur tree1 tree2 -X <(find tree1 tree2 -type f -not -name '*.[ch]')

You can use multiple name arguments if you have longer filename extensions:

diff -wbur tree1 tree2 \
  -X <(find tree1 tree2 -type f -not -name '*.java' -and -not -name '*.sql')

I proposed an easier solution than find to diff maintainers, here is the thread: http://lists.gnu.org/archive/html/bug-diffutils/2014-10/msg00000.html

The idea is provide a new option which instructs diff to parse on files that match a regex, like: diff -Nurp --only "*.[hc]" source/ source-new/

Here is the instructions to patch diffutils

Clone the repo git clone git://git.savannah.gnu.org/diffutils.git

run bootstrap.sh inside diffutils directory and resolve the dependencies until it creates the ./cofigure script

Download the patch from link above

Apply it git apply <PATCHFILE>

Configure and compile ./configure make

This will create the patched diff in src/diff

Cheers

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM