Skip to contentSkip to conversation

git: how to produce context diffs?

PATCH Bruno Haible 5 participants 8 messages Aug 11, 2016

lore
Share & tools
lore
#2 lore raw
In reply to Jakub Narebski
Quoted 4 lines · On Mon, 27 Nov 2006 15:27:20 +0100 Jakub Narebski <[email protected]> wrote:
Quoted 2 lines · Bruno Haible

Is this a bug in git-diff? The git-diff-files.html says:

Quoted 8 lines
Quoted 5 lines

  " When the environment variable GIT_EXTERNAL_DIFF is not set ... For example, if you prefer context diff:
    GIT_DIFF_OPTS=-c git-diff-index -p HEAD  "

This doesn't work for me with git-1.4.4:

Yes, the bug in documentation, I think. There is an option '-c' to git-diff, but it means "combined diff" (for merges), not "context diff".

Indeed. That documentation predates built-in diff completely.

It appears the only valid options now are "-u XX" and "--unified=XX". These options are never passed to diff, but rather used to control the internal diff. Strangely, it appears that gitk is even passing incorrect parameters via GIT_DIFF_OPTS.

Unless i've really missed something, the above documentation should be reworked to remove mention of running diff altogether, and should mention that the GIT_DIFF_OPTS only has two valid settings.

#5 lore raw
In reply to Jakub Narebski
Quoted 3 lines · On Mon, 27 Nov 2006 17:05:35 +0100 Jakub Narebski <[email protected]> wrote:
Quoted 1 line

It appears the only valid options now are "-u XX" and "--unified=XX".

Which both mean the same.

Yes, I guess I should have said that. The point was really that you can't pass any-diff-option and expect it to work.

Quoted 2 lines

Which, in convoluted way is said in documentation (the fact that GIT_DIFF_OPTS affect internal diff).

The idea is to make it less convoluted. It directly says that -c should work _because_ it use to work when the options were passed to external diff. It's not just -c that won't work, none of the other myriad external diff options will work either.

Quoted 5 lines
Quoted 3 lines

Unless i've really missed something, the above documentation should be reworked to remove mention of running diff altogether, and should mention that the GIT_DIFF_OPTS only has two valid settings.

For now.

Well the documentation can be updated later if additional options are added.

#0 lore raw

Hi,

Is this a bug in git-diff? The git-diff-files.html says:

" When the environment variable GIT_EXTERNAL_DIFF is not set ...
For example, if you prefer context diff:
GIT_DIFF_OPTS=-c git-diff-index -p HEAD "

This doesn't work for me with git-1.4.4:

$ unset GIT_EXTERNAL_DIFF
$ export GIT_DIFF_OPTS=-c
$ git-diff-index -p HEAD
configure.ac +1 -1
index 74901dc..d222ded 100644
@@ -1,5 +1,5 @@
 dnl Process this file with autoconf to produce a configure script.
-AC_INIT(hello, 2.1.1, [email protected])
+AC_INIT(hello, 2.1.2, [email protected])
 AC_CONFIG_SRCDIR([src/hello.c])
 
 AC_PREREQ(2.52)

Expected output:

$ git-diff-index -p HEAD
index 74901dc..d222ded 100644
configure.ac +0 -0

*** a/configure.ac
--- b/configure.ac
***************
*** 1,5 ****
dnl Process this file with autoconf to produce a configure script.
! AC_INIT(hello, 2.1.1, [email protected])
AC_CONFIG_SRCDIR([src/hello.c])

AC_PREREQ(2.52)
--- 1,5 ----
dnl Process this file with autoconf to produce a configure script.
! AC_INIT(hello, 2.1.2, [email protected])
AC_CONFIG_SRCDIR([src/hello.c])

AC_PREREQ(2.52)

(Really, while I find -u diffs fine for tiny changes, I find them unreadable for rewrites of larger blocks, and cannot live without -c for these.)

When I look at diff.c around

       const char *diffopts = getenv("GIT_DIFF_OPTS");

it appears that only unified diffs are supported??

#4 lore raw
In reply to Sean
Quoted 11 lines · Sean
Quoted 8 lines · On Mon, 27 Nov 2006 15:27:20 +0100 Jakub Narebski <[email protected]> wrote:
Quoted 6 lines · Bruno Haible

Is this a bug in git-diff? The git-diff-files.html says:

  " When the environment variable GIT_EXTERNAL_DIFF is not set ... For example, if you prefer context diff:
    GIT_DIFF_OPTS=-c git-diff-index -p HEAD  "

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

This doesn't work for me with git-1.4.4:

Yes, the bug in documentation, I think. There is an option '-c' to git-diff, but it means "combined diff" (for merges), not "context diff".

Indeed. That documentation predates built-in diff completely.

It appears the only valid options now are "-u XX" and "--unified=XX".

Which both mean the same.

Quoted 3 lines

These options are never passed to diff, but rather used to control the internal diff. Strangely, it appears that gitk is even passing incorrect parameters via GIT_DIFF_OPTS.

Which, in convoluted way is said in documentation (the fact that GIT_DIFF_OPTS affect internal diff).

Quoted 3 lines

Unless i've really missed something, the above documentation should be reworked to remove mention of running diff altogether, and should mention that the GIT_DIFF_OPTS only has two valid settings.

For now.

Signature · 1 line
Jakub Narebski
#3 lore raw
In reply to Jakub Narebski
Quoted 4 lines · On Mon, 27 Nov 2006 15:27:20 +0100 Jakub Narebski <[email protected]> wrote:
Quoted 2 lines · Bruno Haible

Is this a bug in git-diff? The git-diff-files.html says:

Quoted 8 lines
Quoted 5 lines

  " When the environment variable GIT_EXTERNAL_DIFF is not set ... For example, if you prefer context diff:
    GIT_DIFF_OPTS=-c git-diff-index -p HEAD  "

This doesn't work for me with git-1.4.4:

Yes, the bug in documentation, I think. There is an option '-c' to git-diff, but it means "combined diff" (for merges), not "context diff".

Indeed. That documentation predates built-in diff completely.

It appears the only valid options now are "-u XX" and "--unified=XX". These options are never passed to diff, but rather used to control the internal diff. Strangely, it appears that gitk is even passing incorrect parameters via GIT_DIFF_OPTS.

Unless i've really missed something, the above documentation should be reworked to remove mention of running diff altogether, and should mention that the GIT_DIFF_OPTS only has two valid settings.

#1 lore raw
In reply to Bruno Haible
Quoted 7 lines · Bruno Haible

Is this a bug in git-diff? The git-diff-files.html says:

  " When the environment variable GIT_EXTERNAL_DIFF is not set ... For example, if you prefer context diff:
    GIT_DIFF_OPTS=-c git-diff-index -p HEAD  "

This doesn't work for me with git-1.4.4:

Yes, the bug in documentation, I think. There is an option '-c' to git-diff, but it means "combined diff" (for merges), not "context diff".

Signature · 3 lines
Jakub Narebski
Warsaw, Poland
ShadeHawk on #git
#6 lore raw
In reply to Bruno Haible

Hello,

I'm using the following script 'external-diff.sh':

--

# path old-file old-hex old-mode new-file new-hex new-mode
# $1   $2       $3      $4       $5       $6      $7

old_hex=$3
new_hex=git-hash-object $5

if [ "$old_hex" = "$new_hex" ]
then
exit 0
fi

echo "diff --git a/$1 b/$1"
echo "index ${old_hex:0:7}..${new_hex:0:7} $4"

diff -L a/$1 -L b/$1 -pc $2 $5

exit 0
--

which can be called by:

export GIT_EXTERNAL_DIFF=external-diff.sh
git-diff-index master -p > my.patch

Bye,
Thomas Kolejka

Signature · 2 lines
"Ein Herz für Kinder" - Ihre Spende hilft! Aktion: www.deutschlandsegelt.de
Unser Dankeschön: Ihr Name auf dem Segel der 1. deutschen America's C
#7 lore raw
In reply to Bruno Haible

There are a few things to note:

* Obviously diff.c::builtin_diff() needs to be taught to generate the new format, both in the patch text and also in the patch and hunk header part.

* Corresponding changes to builtin-apply.c need to be made; most of the functions in the callchain that starts at apply_patch() down to apply_one_fragment() need to be taught about the copied context format. Also code to count diffstat in builtin-apply.c needs to be adjusted. This part should be done first before diff.c if we were to support copied context diff, because we should not be generating what we cannot apply.

* I do not think of a sane way to show a combined diff from multiple preimages in copied context format; combine-diff.c can probably be left as it is.

I too find larger hunks easier to read with copied context than with unified context, but what needs to be done listed above is a nontrivial amount of work. Since the beginning, git has only supported unified context format and not copied context format, and apparently people who lived with git for the last 18 months have survived without copied context format, so it is of very low priority even for me right now.

I am not opposed to adding the copied context format as long as somebody else does the work ;-).