سؤال

I have a file in my repository that is generated by a software program.

This program sometimes reorder the lines on that file, which is not really important because line order doesn't matter. The problem is that when performing git-diff it's very difficult to see whether anything has actually changed or not.

Is there anyway to perform a diff in which line order is not taken into account? Or, if not possible with git-diff, any shell command you might think of?

Thanks!

هل كانت مفيدة؟

المحلول

At the end of the day, I'm manually running this command before committing to know whether the file actually changed or it's just a line reorder. Maybe I'll set up a git hook for it.

diff -wB <(sort file.txt) <(git show HEAD:file.txt | sort -)

This command compares the file in the working directory to the file in the last commit of your branch without taking the line order into account.

In my case, I use -w and -B, to ignore blank spaces and lines which the program also adds. From man diff:

   -w, --ignore-all-space
          ignore all white space
   -B, --ignore-blank-lines
          ignore changes whose lines are all blank

نصائح أخرى

here's a pretty nasty hack using DIFF_EXTERNAL_TOOL:

#!/bin/bash

# this will be called with path old-file old-hex old-mode new-file new-hex new-mode
echo $*
diff -uw <(tr -s '[[:space:]]' < "$2" | sort -fbi) <(tr -s '[[:space:]]' < "$5" | sort -fbi) || true

this can be called with, assuming you throw the above script in the wdiff-sort program in your $PATH:

GIT_EXTERNAL_DIFF=wdiff-sort git diff

Here, for example, how I used this to compare a bunch of changes to bind zone files:

#!/bin/bash

#                path old-file old-hex old-mode new-file new-hex new-mode
#echo $*
atomize_zone="sed 's/^@//;/^\;/d;s/[[:space:]][[:space:]]*/ /g' | egrep -v '^.TTL (1D|86400)' | grep -v '; serial' | grep -v 'RR TTL' | grep -v '^.TTL 1H' | egrep -v 'IN SOA (ns1|bureau).koumbit.net' | sort -fbi" 

output=$(diff -uw <(sh -c "$atomize_zone" <"$2") <(sh -c "$atomize_zone" < "$5") || true)

if [ ! -z "$output" ]; then
        echo $1
        cat <<EOF
$output
EOF

This must look totally insane, but it actually works great and allows for cleaning up the files in various ways before diffing.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top