Mercurial show diff against 2 parents or base during merge - merge

Our teem recently faced with merge that removes one leaf of merge and we "lost" changes (as if you perform hg merge --tool internal:local).
This happen because we don't experienced with hg merge command.
hg diff shown only one difference, but not other.
BASE --- HEAD1 --- MERGE
\---- HEAD2 --/
Suppose in HEAD1 I merge HEAD2 but has not yet commit changes.
HEAD2 diff against MERGE I see by hg diff. It is -r BASE:HEAD2 patch.
How can I see diff between current local merge state with HEAD1 as if we merge from HEAD2
How can I see diff between current local merge state with BASE?

Thanks #Vince for suggestion. I reread hg help diff and hg help revset and get that I want.
Assume that you at MERGE before commit and perform merge from HEAD1.
To compare diff against HEAD1 use one of:
hg diff
hg diff -r .
hg diff -r HEAD1
Check:
hg log -r .
To compare diff against HEAD1 use one of:
hg diff -r HEAD2
If you have only 2 heads in current branch last expression can be written without HEAD2 name:
hg diff -r 'branch(.) & head() - .'
Check:
hg log -r 'branch(.) & head() - .'
To compare against BASE:
hg log -r 'ancestor(HEAD1, HEAD2)'
If you have only 2 heads in current branch last expression can be written without HEAD1/HEAD2 names::
hg diff -r 'ancestor(branch(.) & head())'
Check:
hg log -r 'ancestor(branch(.) & head())'
I wander if there are any shortcut for second parent of current merge. For first - just dot sign...
UPDATE Hm... p1() and p2() are awesome! I rewrote my examples in way that they have no concrete names HEAD1/HEAD2:
hg diff -r 'p1()'
hg diff -r 'p2()'
hg diff -r 'ancestor(p1(), p2())'

Related

Why is the file I expect to be present missing after the final merge commit in Mercurial?

I ran into a situation in Mercurial at work where I would expect a file to exist, but it doesn't and I'd like to better understand why. I've put together a repro of the issue. In this repro I would expect the file foo.txt to exist on default after the final merge, since one parent of the merge does not have the file present because it was removed earlier, and the other parent is adding it back because of a commit that happened after the commit that removed the file. Instead, the file remains deleted, why?
Here's an image of the sequence of commits:
And here's the actual commands to go from an empty directory, to having a Mercurial repo in this state.
hg init
echo foo > foo.txt
echo bar > bar.txt
hg add foo.txt bar.txt
hg commit -m "Add foo.txt and bar.txt"
hg branch feature
hg remove foo.txt
hg commit -m "Remove foo.txt"
echo barbar > bar.txt
hg commit -m "Modify bar.txt"
hg update default
echo baz > baz.txt
hg add baz.txt
hg commit -m "Add baz.txt"
hg update feature
hg merge default
hg commit -m "Merge default"
echo foo > foo.txt
hg add foo.txt
hg commit -m "Restore foo.txt"
hg update default
echo bazbaz > baz.txt
hg commit -m "Modify baz.txt"
hg update 0
hg merge 2
hg commit -m "Merge feature"
hg merge
hg commit -m "Merge"
hg merge feature
hg commit -m "Merge feature"
State of the working directory after the final merge:
> ls
bar.txt baz.txt
EDIT:
This appears to affect hg versions 5.9.3 and 6.3.2 but not 5.0.2
EDIT:
Based on discussion on the libera.chat #mercurial channel, modifying the sequence of commands in the repro to include an edit to the file after its creation does not change the outcome. foo.txt is still not present. https://gist.github.com/OneGeek/6fa5dcd4c2b3db6649310de1167449f9
I just pasted the steps from the repro into a terminal here and tried it (twice), and it doesn't reproduce the problem as described: foo.txt exists.
The end result is a working directory that has:
C:\Users\abcde\source\test>dir
...
Directory of C:\Users\abcde\source\test
01/29/2023 04:08 PM <DIR> .
01/29/2023 04:07 PM <DIR> ..
01/29/2023 04:08 PM <DIR> .hg
01/29/2023 04:07 PM 0 .hgignore
01/29/2023 04:08 PM 9 bar.txt
01/29/2023 04:08 PM 9 baz.txt
01/29/2023 04:08 PM 6 foo.txt
4 File(s) 24 bytes
The graph in THG looks the same as in the question.
I happen to be using HG 5.0.2 on Windows.

create a commit of changes between two branches

I created branch br1 from default a few months back. I have been committing and pushing changes to br1 since then. Every now and then pulling in changes from default as follows:
hg up br1
hg merge default
// create a commit, push
I want to create a commit message of all the changes I made to br1 over the last few months. branch br1 and default are clean in my workspace, aka no uncommitted changes, no un-pushed commit. At this point, br1 is out of sync with default by 1 week. I did the following steps:
hg up default
hg merge br1
// At this point, "hg stat" shows files that I did not modify in br1. :(
// So, if I created a commit message at this point, it would be no good.
// I am not sure why these addition file modifications showed up.
I figured the issue might be because br1 is out of sync from default by a week. I performed the following set of steps in a clean workspace:
hg up br1
hg merge default
// created a commit -**ch1**, but did **NOT push**
hg up default
hg merge br1
// At this point, "hg stat" shows the same additional files as my pervious
// attempt. :(
Question:
- Does "hg merge" disregards commit that are not pushed?
- Do I need to push ch1 for these additional files to not to show up? Is this the reason for the additional files showing up when do a "hg merge br1"?
- Is there a way I can tell hg to take the ch1 into account when doing the merge from br1.
Thank you,
Ahmed.
well, since you didn't provide a working example, I'll have to build one from scratch, so forgive me for the bunch of command lines below
explanation at the end, because only makes sense if you follow the example steps
preparing the stage
# create a new dir an initialize the repository
mkdir hgtrial
cd hgtrial
hg init
# create an empty file and make first commit on default branch
echo . > dummy.txt
hg add dummy.txt
hg commit -m "1st commit"
# add info and make a second commmit also on default branch
echo abc >> dummy.txt
hg commit -m "2nd commit"
# create a new branch and make an empty commit on it
hg branch br1
hg commit -m "my new branch"
# work on br1
echo def >> dummy.txt
hg commit -m "def on br1"
# work on default (make br1 out of sync)
hg up default
echo ghi >> dummy.txt
hg commit -m "ghi on default"
echo jkl >> dummy.txt
hg commit -m "jkl on default"
# sync br1 by pulling default
hg up br1
hg merge default
# solve merge conflicts (abc def ghi jkl)
hg commit -m "br1 <- default"
# continue working on br1 (without touching dummy.txt)
echo 123 > another.txt
hg add another.txt
hg commit -m "another file"
echo 456 >> another.txt
hg commit -m "456"
# work on default (make br1 out of sync)
hg up default
echo yes-yes > one-more.txt
hg add one-more.txt
hg commit -m "yes-yes, on default"
echo no-no >> one-more.txt
hg commit -m "no-no, on default"
now the issue (notice we are standing on default branch)
# now the problem (scenario 1)
hg merge br1
hg stat
# dummy.txt is modified, trying to bring "def" from br1 into default
# abort
# checkout clean br1 (drop merge in progress)
hg up -C br1
hg merge default
hg commit -m "br1 <- default"
# the problem again (scenario 2)
hg up default
hg merge br1
hg stat
# dummy.txt is modified, trying to bring "def" from br1 into default
# SAME THING!
why?
because at some point "def" change was made in br1 and default never knew about it, so even when you haven't touch dummy.txt in a long while, you have synced default into br1, but no the other way around, therefore default has a lot to catch up with
EDIT: added screenshot with this scenario in TortoiseHg

Why doesn't git diff work between two dates?

I checked out a project from an internal GitLab server using Eclipse, then I pulled all the changes. When I view the history from Eclipse. (Team > show in history), it displays the full history of the project.
Now I go to the relevant project from the terminal.
/home/workspace/ProjectX/
I am trying to get the differences between 2 dates with the following command:
git diff --name-only master#{2015-10-10}..master#{2015-11-10} > /home/results/ProjectX/Changes.txt
It wont display any result for that. It displays:
warning: Log for 'master' only goes back to Tue, 10 Nov 2015.
How can I get all the differences in that date range?
In addition to that, how does Eclipse request its history from the remote server. If we can run the same command from the terminal, that should work.
Git parses dates like master#{2015-10-10} using your reflog, which doesn't appear to go back as far as you're searching. But, you can find commits for that date range anyway with rev-list:
git rev-list --since='2015-10-10' --until='2015-11-10' master
You want the files changes between the most recent and the oldest commit in that list, which we can get using head and tail. I'd like to use -n1 and --reverse, but --reverse applies after -n, so we can't.
first=$(git rev-list --since='2015-10-10' --until='2015-11-10' master | tail -1)
last=$(git rev-list --since='2015-10-10' --until='2015-11-10' master | head -1)
git diff --name-only $first..$last
Setting variables and duplicating the rev-list feels clumsy, but the pipe-y version I can come up with is sort of worse. It picks the first and last commits, converts the newline to a space using tr, replaces the new space with .. using sed, then passes the pair off to git diff.
git rev-list --since='2015-10-10' --until='2015-11-10' master | \
(head -n1 && tail -n1) | \
tr '\n' ' ' | \
sed 's/ /../' | \
xargs git diff --name-only

Mercurial - how to see the history for a specific line of code

I have a CSS file with thousands of lines of code. I want to see when a specific line/chunk of code was changed, without going back and reviewing each revision that changed this file (that will take a looooong time!)
Is there a way, using either TortoiseHg, Eclipse with a Mercurial plugin, or command-line, to view the history of a specific piece of code?
The correct answer is hg grep (Mercurial grep page).
More deep:
hg grep --all "PATTERN" FILENAME
Sample output:
>hg grep --all "textdomain" functions.php
functions.php:2:-:load_theme_textdomain('fiver', get_template_directory() . '/translation');
functions.php:2:+:load_theme_textdomain('fiver', get_template_directory() . '/languages');
functions.php:1:+:load_theme_textdomain('fiver', get_template_directory() . '/translation');
(in order - filename, revision, action, string in this revision)
You can use:
hg annotate <file>
to find out in which revision line was changed and then use same command with -r <revision> at the end to go backwards through revisions.
I don't think there is an option to view a specific part of a file. But to see the differences of the total file over several revisions you can use hg diff:
hg diff -r firstrevisionnumber:otherrevnumber filename
For example, hg diff -r 0:8 screen.css
Or the command hg log screen.css.
Use hg histgrep --all PATTERN FILENAME (used to be hg grep in the older versions, and that doesn't work anymore)

What does hg copy do?

We recently did a hg copy of a directory in our repository. We thought it
does something like cp -a and hg add and maybe flag somehow that
this file has been copied from another file inside the repo (so hg
annotate shows the original committer). But it now seems that hg
copy does more or different stuff than that. I couldn't really find
much on how exactly copy works. So:
What exactly does hg copy do and what special treatment does this
cause in the future?
If it turns out to do "the wrong thing(tm)" for our case, how do I
unflag the file as beeing a copy of another file?
(This question was asked on the Mercurial mailinglist, you may want to follow the original thread too.)
What exactly does hg copy do and what special treatment does this
cause in the future?
It adds new files and marks them as copies of the old files. Because they are copies, a change made in the original file will be merged into copy. Time flows from left to right:
(init) --- (edit a.txt) ---- (a.txt edit is copied to b.txt)
\ /
(hg copy a.txt b.txt)
If it turns out to do 'the wrong thing(tm)' for our case, how do I
unflag the file as beeing a copy of another file?
This mechanism only kicks in when you merge. If b.txt is not present in the
common ancestor revision (init in the above graph), then Mercurial will
do a search backwards to see if b.txt is copied from somewhere else.
Let us continue the above graph in abbreviated form:
(i) -- (edit a) -- (a edit copied to b) -- (edit a) -- (merge)
\ / /
(copy a b) --/------- (edit b) ------------------/
The question is how the final merge is done. The common ancestor point
is now the copy a b node and here both a and b are present. This means
that there wont be any search for copies! So the second edit to a wont
be merged into b.
To double-check, I tried it out:
$ hg init
$ echo a > a
$ hg add a
$ hg commit -m init
$ hg copy a b
$ hg commit -m "copy a b"
This was the copy, b now contains a only.
$ hg update 0
0 files updated, 0 files merged, 1 files removed, 0 files unresolved
$ echo aa >> a
$ hg commit -m "edit a"
created a new head
$ hg merge
merging a and b to b
0 files updated, 1 files merged, 0 files removed, 0 files unresolved
(branch merge, don't forget to commit)
$ hg commit -m "a edit copied to b"
This was the first merge and the edit to a has been copied into b:
$ cat b
a
aa
We now make changes in parallel:
$ echo aaa >> a
$ hg commit -m "edit a again"
$ hg update 3
1 files updated, 0 files merged, 0 files removed, 0 files unresolved
$ echo bbb >> b
$ hg commit -m "edit b"
created new head
$ hg merge
1 files updated, 0 files merged, 0 files removed, 0 files unresolved
(branch merge, don't forget to commit)
There are no further copying done:
$ cat a
a
aa
aaa
$ cat b
a
aa
bbb
As for disabling this... you can't really explicitly disable the copy
detection. But as I hope to have illustrated above, it wont "bother" you
again after the first merge.
If the first merge is a problem, then you can use hg resolve --tool
internal:local to reset the files back to their state before you
started the merge. So with
$ hg resolve --tool internal:local b
we could have brought b back to just containing one line with a.
How do I unflag the file as being a copy of another file?
If you revert a hg copy, the copied-to file remains in your working directory afterwards, untracked. You just have to add it normally.
The copied-from file isn't affected at all.
% hg copy file new-file
% hg status -C
A new-file
__file
% hg revert new-file
% hg add new-file
% hg status -C
A new-file
Reference: Mercurial: The definitive guide