git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Junio C Hamano <gitster@pobox.com>
To: Iago-lito <iago-lito@cleb.io>
Cc: git@vger.kernel.org
Subject: Re: `git apply -p` doesn't remove source prefix in 'rename' diff entries.
Date: Fri, 01 Nov 2024 00:05:54 -0700	[thread overview]
Message-ID: <xmqq4j4r4enh.fsf@gitster.g> (raw)
In-Reply-To: <3af246eb-3473-4964-9aed-ecff62b215b7@cleb.io> (iago-lito@cleb.io's message of "Fri, 1 Nov 2024 01:17:20 +0100 (GMT+01:00)")

Iago-lito <iago-lito@cleb.io> writes:

> Hello,
>
> I have troubles understanding the behaviour of `git apply` with respect to renames in a `--no-index` context.
>
> Let us craft a toy folder:
> ```sh
> $ mkdir x
> $ echo a > x/a # To be modified.
> $ echo b > x/b # To be renamed.
> ```
>
> Duplicate it twice to get three identical folders `x = y = z`.
>
> ```sh
> $ cp -r x y
> $ cp -r x z
> ```
>
> Modify `y`:
> ```sh
> $ echo newline >> y/a # Edit.
> $ mv y/b y/c # Rename.
> ```
>
> Now I would like to use git as a "better GNU patch".
> Calculate the diff from `x` to `y`:
> ```sh
> $ git diff --no-prefix x y | tee patch
> diff --git x/a y/a
> index 7898192..4030aa5 100644
> --- x/a
> +++ y/a
> @@ -1 +1,2 @@
> a
> +newline
> diff --git x/b y/c
> similarity index 100%
> rename from x/b
> rename to y/c
> ```

Interesting.  I think "git diff --no-index" is "wrong" here, but
"wrong" is not in the sense that it is _incorrect_, but in the sense
that it merely is incompatible with what you are trying to achieve.

If you had files a and b, modified a in place and renamed b to c,
and if you did this in a repository with tracked files, you would
have seen

   $ git diff --src-prefix=x --dst-prefix=y
   diff --git x/a y/a
   index ...
   --- x/a
   +++ y/a
   ...
   diff --git x/b y/c
   similarity index 100%
   rename from b
   rename to c

Notice how "rename from" and "rename to" information is different in
the "real" git-diff output, as opposed to "git diff --no-index"
output you have?

At least when comparing two directories, "git diff --no-index X Y"
could be interpreted as a request to treat as if X and Y were
checkouts from two Git commits X and Y, and by comparing them the
output should be identical to the result of comparing the contents
of commits X and Y.

But feeding "git apply" however may not be the only reason why
people are using "diff --no-index X Y", and there may be a
legitimate reason why peole want to see "from x/b to y/c" in the
output, so unconditionally changing the output may very likely break
workflows of other people.



  reply	other threads:[~2024-11-01  7:05 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-11-01  0:17 `git apply -p` doesn't remove source prefix in 'rename' diff entries Iago-lito
2024-11-01  7:05 ` Junio C Hamano [this message]
2024-11-04 20:35   ` Iago-lito
2024-11-05  0:36     ` Junio C Hamano

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=xmqq4j4r4enh.fsf@gitster.g \
    --to=gitster@pobox.com \
    --cc=git@vger.kernel.org \
    --cc=iago-lito@cleb.io \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).