# an option to make "git-diff Z A" prints Z's diff before A's

4 messages from 2006-10-25 to 2006-10-26. Participants: Jim Meyering, Linus Torvalds, Karl Hasselström, Junio C Hamano.
Thread: https://gitlist.dev/t/43535

## Jim Meyering, 2006-10-25 07:15

Subject: an option to make "git-diff Z A" prints Z's diff before A's
Message-ID: <871wowzx15.fsf@rho.meyering.net>
URL: https://gitlist.dev/e/871wowzx15.fsf%40rho.meyering.net

```
Hello,

In a recent patch set I prepared, I placed the names of the
more relevant files at the front of the list given to "git-diff".
So I was surprised to see diff output in which the blocks of output
are sorted by file name.  The parts I'd tried to put at the "top"
ended up being buried in the latter part of the patch.

Here's a small test case:

mkdir d && cd d && touch x y && git-init-db
git-add x y && git-commit -m. x y
echo a > x && echo a > y
git-diff y x|grep '^d'

I expected this git-diff output, with "y's" diff first:
diff --git a/y b/y
diff --git a/x b/x

but got this, where x's come first:
diff --git a/x b/x
diff --git a/y b/y

I know about the -O<orderfile> option, and it can make git-diff do
what I want, but only if I first create a separate file containing
the names that I'm already providing to git-diff in the very same order.

Is there an easier way?

If not, would you be amenable to a new option enabling this behavior
without requiring a temporary file?


```

## Junio C Hamano, 2006-10-25 19:16

Subject: Re: an option to make "git-diff Z A" prints Z's diff before A's
Message-ID: <7vd58g9pfs.fsf@assigned-by-dhcp.cox.net>
URL: https://gitlist.dev/e/7vd58g9pfs.fsf%40assigned-by-dhcp.cox.net
In-Reply-To: <871wowzx15.fsf@rho.meyering.net>

```
Jim Meyering <jim@meyering.net> writes:

> In a recent patch set I prepared, I placed the names of the
> more relevant files at the front of the list given to "git-diff".
>...
> I know about the -O<orderfile> option, and it can make git-diff do
> what I want, but only if I first create a separate file containing
> the names that I'm already providing to git-diff in the very same order.
>
> Is there an easier way?

No, not right now.

> If not, would you be amenable to a new option enabling this behavior
> without requiring a temporary file?

The thing is, "git diff -- Z A" does *not* mean:

	I know I have a file called Z and a file called A;
	please give diff for these files.

What it means is:

	Please give me the diff as usual, but I care about paths
	that match these patterns, Z or A.

So "git diff -- Documentation" names all changed files in that
directory; you could also spell it "Documentation/" for clarity.

git-diff traverses two tree-like things (either tree-vs-tree,
tree-vs-index, or tree-vs-working tree) in parallel in the
canonical order, but skips comparing paths that do not match the
list of patterns you gave on the command line.  While it does
so, we do not record which pattern caused the path to be
included in the output anywhere, so there currently is no way to
tell which ones matched an earlier pattern and which ones
matched a later one.

If somebody wants to do this, the place to modify would be the
following:

 - add a new parameter, "int match_number", to change_fn_t and
   add_remove_fn_t functions, and add a new member to struct
   diff_filepair to record it.

 - update all callers of diff_addremove, diff_change, and
   diff_unmerge to pass which pathspec the user gave on the
   command line matched the path to be included (in your example
   if both Z and A were directory, file Z/foo gets number 1 and
   file A/bar gets number 2).

 - update diff_addremove, diff_change and diff_unmerge to pass
   that match_number to diff_queue(), and make diff_queue()
   record the number in the new diff_filepair it creates.

 - in places where an existing filepair is split into two and
   two existing filepairs are merged into one (e.g. "break" and
   "rename"), make sure match_number is propagated sensibly from
   the original filepairs to the modified ones.

 - in diffcore_std(), if orderfile is not in use, use the
   match_number to sort the queued filepairs.


```

## Karl Hasselström, 2006-10-26 08:42

Subject: Re: an option to make "git-diff Z A" prints Z's diff before A's
Message-ID: <20061026084221.GB13780@diana.vm.bytemark.co.uk>
URL: https://gitlist.dev/e/20061026084221.GB13780%40diana.vm.bytemark.co.uk
In-Reply-To: <7vd58g9pfs.fsf@assigned-by-dhcp.cox.net>

```
On 2006-10-25 12:16:07 -0700, Junio C Hamano wrote:

> The thing is, "git diff -- Z A" does *not* mean:
>
>       I know I have a file called Z and a file called A; please give
>       diff for these files.
>
> What it means is:
>
>       Please give me the diff as usual, but I care about paths that
>       match these patterns, Z or A.

A related question: is there a way to limit the path to Z, but
excluding Z/B? That is, I'm interested in the changes in Z, but not
the changes in its subdirectory B.

-- 
Karl Hasselström, kha@treskal.com

```

## Linus Torvalds, 2006-10-26 15:16

Subject: Re: an option to make "git-diff Z A" prints Z's diff before A's
Message-ID: <Pine.LNX.4.64.0610260812120.3962@g5.osdl.org>
URL: https://gitlist.dev/e/Pine.LNX.4.64.0610260812120.3962%40g5.osdl.org
In-Reply-To: <20061026084221.GB13780@diana.vm.bytemark.co.uk>

```


On Thu, 26 Oct 2006, Karl Hasselström wrote:
> 
> A related question: is there a way to limit the path to Z, but
> excluding Z/B? That is, I'm interested in the changes in Z, but not
> the changes in its subdirectory B.

We never did that, no. It's certainly a relevant thing to do, and it would 
be sensible to have a kind of common logic with revision parsing (where a 
caret (^) at the beginning would mean "not"), but at the same time, it's 
not been common enough (read: "I have personally never missed it") to 
actually become an issue.

So I _think_ it would fit fairly well into the current code (just teach 
the stuff that uses "char **pathspec" about that new rule), and it might 
not be too bad. On the other hand, that particular code is pretty dense 
and part of a very core and performance critical subsystem, so unless you 
_really_ want this, you might be better off averting your eyes and just 
ignoring this issue ;)

			Linus
```
