Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6881857
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:10:55+00:00 2026-05-27T05:10:55+00:00

It seems that it would be extremely handy to be able to filter a

  • 0

It seems that it would be extremely handy to be able to filter a diff so that trivial changes are not displayed. I would like to write a regular expression which would be run on the line and then pass it another string that uses the captured arguments to generate a canonical form. If the lines before and after produce the same output, then they would be removed from the diff.

For example, I am working on a PHP code base where a significant number of array accesses are written as my_array[my_key] when they should be my_array["my_key"] to prevent issues if a my_key constant is defined. It would be useful to generate a diff where the only change on the line wasn’t adding some quotes.

I can’t change them all at once, as we don’t have the resources to test the entire code base, so am fixing this whenever I make a change to a function. How can I achieve this? Is there anything else similar to this that I can use to achieve a similar result. For example, a simpler method might be to skip the canonical form and just see if the input is transformed into the output. BTW, I am using Git

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T05:10:55+00:00Added an answer on May 27, 2026 at 5:10 am

    There does not seem to be any options to Git’s diff command to support what you want to do. However, you could use the GIT_EXTERNAL_DIFF environment variable and a custom script (or any executable created using your preferred scripting or programming language) to manipulate a patch.

    I’ll assume you are on Linux; if not, you could tweak this concept to suit your environment. Let’s say you have a Git repo where HEAD has a file file05 that contains:

    line 26662: $my_array[my_key]
    

    And a file file06 that contains:

    line 19768: $my_array[my_key]
    line 19769: $my_array[my_key]
    line 19770: $my_array[my_key]
    line 19771: $my_array[my_key]
    line 19772: $my_array[my_key]
    line 19773: $my_array[my_key]
    line 19775: $my_array[my_key]
    line 19776: $my_array[my_key]
    

    You change file05 to:

    line 26662: $my_array["my_key"]
    

    And you change file06 to:

    line 19768: $my_array[my_key]
    line 19769: $my_array["my_key"]
    line 19770: $my_array[my_key]
    line 19771: $my_array[my_key]
    line 19772: $my_array[my_key]
    line 19773: $my_array[my_key]
    line 19775: $my_array[my_key2]
    line 19776: $my_array[my_key]
    

    Using the following shell script, let’s call it mydiff.sh and place it somewhere that’s in our PATH:

    #!/bin/bash
    echo "$@"
    git diff-files --patch --word-diff=porcelain "${5}" | awk '
    /^-./ {rec = FNR; prev = substr($0, 2);}
    FNR == rec + 1 && /^+./ {
        ln = substr($0, 2);
        gsub("\\[\"", "[", ln);
        gsub("\"\\]", "]", ln);
        if (prev == ln) {
            print " " ln;
        } else {
            print "-" prev;
            print "+" ln;
        }
    }
    FNR != rec && FNR != rec + 1 {print;}
    '
    

    Executing the command:

    GIT_EXTERNAL_DIFF=mydiff.sh git --no-pager diff
    

    Will output:

    file05 /tmp/r2aBca_file05 d86525edcf5ec0157366ea6c41bc6e4965b3be1e 100644 file05 0000000000000000000000000000000000000000 100644
    index d86525e..c2180dc 100644
    --- a/file05
    +++ b/file05
    @@ -1 +1 @@
     line 26662: 
     $my_array[my_key]
    ~
    file06 /tmp/2lgz7J_file06 d84a44f9a9aac6fb82e6ffb94db0eec5c575787d 100644 file06 0000000000000000000000000000000000000000 100644
    index d84a44f..bc27446 100644
    --- a/file06
    +++ b/file06
    @@ -1,8 +1,8 @@
     line 19768: $my_array[my_key]
    ~
     line 19769: 
     $my_array[my_key]
    ~
     line 19770: $my_array[my_key]
    ~
     line 19771: $my_array[my_key]
    ~
     line 19772: $my_array[my_key]
    ~
     line 19773: $my_array[my_key]
    ~
     line 19775: 
    -$my_array[my_key]
    +$my_array[my_key2]
    ~
     line 19776: $my_array[my_key]
    ~
    

    This output does not show changes for the added quotes in file05 and file06. The external diff script basically uses the Git diff-files command to create the patch and filters the output through a GNU awk script to manipulate it. This sample script does not handle all the different combinations of old and new files mentioned for GIT_EXTERNAL_DIFF nor does it output a valid patch, but it should be enough to get you started.

    You could use Perl regular expressions, Python difflib or whatever you’re comfortable with to implement an external diff tool that suits your needs.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

It seems that unsigned integers would be useful for method parameters and class members
It seems that licensing terms would prevent us from using Google Maps API in
Intuitively, it would seems that a compiler for language Foo cannot itself be written
It seems to me that it would work perfectly well to do tail-recursion optimization
This seems to me to be the kind of issue that would crop up
I've been looking into CEDET, but it seems that most of its features would
I would like to be able to list the 10 most popular tags (subjects)
Hey all. I would like to get some insight on a question that I
It would seem that IE7 puts an extra 1px of spacing above and beneath
So, I'm working on some network programming in C, and it would seem that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.