How-to diff across a renaming?

Sietse Brouwer sbbrouwer at gmail.com
Sat Feb 4 12:35:01 UTC 2023


Hi Marcos,

I usually use `hg cat` to materialize the old and new version as normal
files, and then compare them using `git diff`.
(I can't use `hg diff` to compare them because it won't compare two
untracked files, or two files in the same revision.)

First an ad-hoc example; then an alias that wraps this.

Ad-hoc example:

```
# get copies of OLDFILE and NEWFILE from their revisions
hg cat -r R0 OLDFILE > OLDFILE.tmp
hg cat -r R2 NEWFILE > NEWFILE.tmp

# Compare them using another tool
git diff OLDFILE.tmp NEWFILE.tmp

You could put that in an alias in your ~/.hgrc:
```

```
[alias]
# crossdiff: compare two files with different names and revisions
# Usage: hg crossdiff rev1 file1 rev2 file2
crossdiff = !
    # compute filenames
    from_tmp="/tmp/from.$2.at.$1.tmp"
    to_tmp="/tmp/to.$4.at.$3.tmp"
    # materialize files
    hg cat -r "$1" "$2" > "$from_tmp"
    hg cat -r "$3" "$4" > "$to_tmp"
    # diff using git
    git diff "$from_tmp" "$to_tmp"
```

Short, self-contained example of using this `hg crossdiff` alias:
```
# init repo
hg init /tmp/crossdiff
cd /tmp/crossdiff/

# Create under eggfile name
echo abc > eggfile
hg add eggfile
hg commit -m "eggfile"

# rename
hg mv eggfile chickenfile
hg commit -m "rename"

# Alter renamed file
echo abcx > chickenfile
hg commit -m "chickenfile"

# Crossdiff
hg crossdiff 0 eggfile 2 chickenfile
diff --git a/tmp/from.eggfile.at.0.tmp b/tmp/to.chickenfile.at.2.tmp
index 8baef1b..d8c09a4 100644
--- a/tmp/from.eggfile.at.0.tmp
+++ b/tmp/to.chickenfile.at.2.tmp
@@ -1 +1 @@
-abc
+abcx
```

I hope this helps. It might not work if you're on Windows, or if you have a
particular purpose beyond "look at the diff". In that case, please let me
know where this solution still falls short, and I'll see what I can do.

Kind regards,

Sietse


On Sun, 8 Jan 2023 at 10:15, Marcos Cruz <mercurial-list at programandala.net>
wrote:

>
> Is it possible to compare two versions of a file which has been renamed,
> across the revision in which it was renamed, and with any diff tool?
>
> Say I have these revisions:
>
> - R0: OLDFILE exists
> - R1: hg mv OLDFILE NEWFILE # and then also modify NEWFILE
> - R2: NEWFILE exists
>
> In order to compare the changes across the renaming I tried the
> following (with `diff` and other tools):
>
>   hg diff --from R0 --to R2 NEWFILE # displays only NEWFILE, as added
>   hg diff --from R0 --to R2 OLDFILE # displays only OLDFILE, as deleted
>   hg diff --from R2 --to R0 NEWFILE # displays only NEWFILE, as deleted
>   hg diff --from R2 --to R0 OLDFILE # displays only OLDFILE, as added
>
> Then I noticed this example in `hp help diff`, which reads a cryptic
> "with rename info":
>
> ________
> - compare two historical versions of a directory, with rename info:
>
>     hg diff --git --from 1.0 --to 1.2 lib/
> ________
>
> And tried this, which works fine (it displays the changes from OLDFILE
> to NEWFILE):
>
>   hg diff --git --from R0 --to R2 NEWFILE
>
> But it seems there's no way to do it with other diff tools, right? `hg
> help diffs` and `hg help.config.diff` say nothing about this.
>
> Thanks.
>
> --
> Marcos Cruz
> http://programandala.net
> _______________________________________________
> Mercurial mailing list
> Mercurial at lists.mercurial-scm.org
> https://lists.mercurial-scm.org/mailman/listinfo/mercurial
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mercurial-scm.org/pipermail/mercurial/attachments/20230204/436de11e/attachment.html>


More information about the Mercurial mailing list