threads / discuss / 43535

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

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

## tl;dr

4 messages between Oct 25, 2006 and Oct 26, 2006.

replies: 3people: 4as markdown or json

Jim Meyering· Oct 25, 2006, 07:15 UTC · lore
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· Oct 25, 2006, 19:16 UTC · re: Jim Meyering · lore

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

Jim Meyering <jim@meyering.net> writes:
Show 8 quoted lines
> 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· Oct 26, 2006, 08:42 UTC · re: Junio C Hamano · lore

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

On 2006-10-25 12:16:07 -0700, Junio C Hamano wrote:
Show 9 quoted lines
> 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· Oct 26, 2006, 15:16 UTC · re: Karl Hasselström · lore

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

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

← back to recent threads