{"thread":{"id":"62865","subject":"git diff -G: Regex pattern to exclude a word","startedAt":"2025-01-29T06:05:36Z","lastAt":"2025-01-29T11:01:26Z","messageCount":2,"participants":["Raghavendra N","Chris Torek"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"511383","messageId":"CA+U2-7GiHq9qhiyLPLgzOd1JK072gzkmZ+4vQ11V0QpKgPif=Q@mail.gmail.com","threadId":"62865","inReplyTo":null,"subject":"git diff -G: Regex pattern to exclude a word","fromName":"Raghavendra N","fromEmail":"nraghavendra89@gmail.com","sentAt":"2025-01-29T06:05:24Z","receivedAt":"2025-01-29T06:05:36Z","isPatch":false,"sender":{"key":"nraghavendra89@gmail.com","avatar":null},"body":"Dear Members,\n\nTL;DR: Question is - How to use the -G option in git diff to exclude a word?\n\nI'm trying to understand how the -G option in git diff works.\nSpecifically I'm trying to test a Regex which excludes a word from the\ngit diff.\n\nConsider the following setup:\n\n    > git diff\n    first-file.txt\n    @@ -1 +1 @@\n    -Lorem Ipsum is simply dummy text.\n    +Lorem Ipsum is simply dummy text. tree\n\n    second-file.txt\n    @@ -1 +1 @@\n    -Lorem Ipsum is simply dummy text.\n    +Lorem Ipsum is simply dummy text. ball\n\nWord \"tree\" is added in the first-file.txt and word \"ball\" is added in\nthe second-file.txt.\n\nIf I run this `git diff -G \"^.*tree.*\"`, it shows only the first-file.txt.\n\n    > git diff -G \"^.*tree.*\"\n    first-file.txt\n    @@ -1 +1 @@\n    -Lorem Ipsum is simply dummy text.\n    +Lorem Ipsum is simply dummy text. tree\n\nThis is correct because the Regex is checking if textual diff has an\nadded or a deleted line that includes a word \"tree\".\n\nNow if I try to exclude the word \"tree\" with this Regex\n\"^([^t]|(t[^r])|(tr[^e])|(tre[^e]))*($|(t($|(r($|e$)))))\", it should\nonly show the second-file.txt. But the diff lists both the files.\n\n    > git diff -G \"^([^t]|(t[^r])|(tr[^e])|(tre[^e]))*($|(t($|(r($|e$)))))\"\n    first-file.txt\n    @@ -1 +1 @@\n    -Lorem Ipsum is simply dummy text.\n    +Lorem Ipsum is simply dummy text. tree\n\n    second-file.txt\n    @@ -1 +1 @@\n    -Lorem Ipsum is simply dummy text.\n    +Lorem Ipsum is simply dummy text. ball\n\nSo the question is how does the -G option work in this case? And how\nto use the -G option to exclude a word?\n\nThanks,\nRaghavendra\n"},{"id":"511405","messageId":"CAPx1Gvf+80Yty1R3O766fwYiRUysQ3Ss1XbFq7hmrfcUcawBAA@mail.gmail.com","threadId":"62865","inReplyTo":"CA+U2-7GiHq9qhiyLPLgzOd1JK072gzkmZ+4vQ11V0QpKgPif=Q@mail.gmail.com","subject":"Re: git diff -G: Regex pattern to exclude a word","fromName":"Chris Torek","fromEmail":"chris.torek@gmail.com","sentAt":"2025-01-29T11:01:13Z","receivedAt":"2025-01-29T11:01:26Z","isPatch":false,"sender":{"key":"chris.torek@gmail.com","avatar":"https://avatars.githubusercontent.com/u/16826774?v=4"},"body":"On Tue, Jan 28, 2025 at 10:05 PM Raghavendra N <nraghavendra89@gmail.com> wrote:\n> I'm trying to understand how the -G option in git diff works.\n> Specifically I'm trying to test a Regex which excludes a word from the\n> git diff.\n\n\"Inverting\" a regular expression is technically always possible,\nbut is difficult to get right, and I think you made a slight error in\nyour attempt. See also https://www.vidarholen.net/contents/blog/?p=36\n(which has a link to a Haskell program to generate an inverted regex\nautomatically).\n\nWith `git grep` you can specify perl-style regular expressions,\nwhich allow negative lookahead, but `git diff -G` does not\nsupport that. See\nhttps://stackoverflow.com/questions/39789921/what-flavor-of-regex-does-git-use\nfor details.\n\nI think it would be nice to add some of `git grep`'s options to\n`git diff` here, but I leave that as an exercise. :-)\n\nChris\n"}]}