create a commit of changes between two branches - version-control

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

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.

Capturing (git) command output in github actions

I am trying to capture the output of this git command (or any for that matter).
git diff-tree --no-commit-id --patch-with-raw -r HEAD # HEAD or some commit SHA
However, unlike an echo, the following command does not log any output in the GitHub actions log. Nor does it streams the output to a variable. On my local, the same command logs the changes made in the last commit.
# result is empty
result=$(git diff-tree --no-commit-id --patch-with-raw -r HEAD)
What am I missing? How do I capture the output of the above git command?
Are you using the checkout action to checkout your code? git diff-tree probably doesn't output anything if you're not fetching the history. Try
- uses: actions/checkout#v2
with:
fetch-depth: 0
Probably something alike this... in every case with echo:
echo $(git diff-tree --no-commit-id --patch-with-raw -r HEAD)

Mercurial show diff against 2 parents or base during 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())'

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

How to correctly close a feature branch in Mercurial?

I've finished working on a feature branch feature-x. I want to merge results back to the default branch and close feature-x in order to get rid of it in the output of hg branches.
I came up with the following scenario, but it has some issues:
$ hg up default
$ hg merge feature-x
$ hg ci -m merge
$ hg up feature-x
$ hg ci -m 'Closed branch feature-x' --close-branch
So the feature-x branch (changests 40-41) is closed, but there is one new head, the closing branch changeset 44, that will be listed in hg heads every time:
$ hg log ...
o 44 Closed branch feature-x
|
| # 43 merge
|/|
| o 42 Changeset C
| |
o | 41 Changeset 2
| |
o | 40 Changeset 1
|/
o 39 Changeset B
|
o 38 Changeset A
|
Update: It appears that since version 1.5 Mercurial doesn't show heads of closed branches in the output of hg heads anymore.
Is it possible to close a merged branch without leaving one more head? Is there more correct way to close a feature branch?
Related questions:
Is there a downside to this Mercurial workflow: named branch "dead" head?
One way is to just leave merged feature branches open (and inactive):
$ hg up default
$ hg merge feature-x
$ hg ci -m merge
$ hg heads
(1 head)
$ hg branches
default 43:...
feature-x 41:...
(2 branches)
$ hg branches -a
default 43:...
(1 branch)
Another way is to close a feature branch before merging using an extra commit:
$ hg up feature-x
$ hg ci -m 'Closed branch feature-x' --close-branch
$ hg up default
$ hg merge feature-x
$ hg ci -m merge
$ hg heads
(1 head)
$ hg branches
default 43:...
(1 branch)
The first one is simpler, but it leaves an open branch. The second one leaves no open heads/branches, but it requires one more auxiliary commit. One may combine the last actual commit to the feature branch with this extra commit using --close-branch, but one should know in advance which commit will be the last one.
Update: Since Mercurial 1.5 you can close the branch at any time so it will not appear in both hg branches and hg heads anymore. The only thing that could possibly annoy you is that technically the revision graph will still have one more revision without childen.
Update 2: Since Mercurial 1.8 bookmarks have become a core feature of Mercurial. Bookmarks are more convenient for branching than named branches. See also this question:
Mercurial branching and bookmarks
imho there are two cases for branches that were forgot to close
Case 1:
branch was not merged into default
in this case I update to the branch and do another commit with --close-branch, unfortunatly this elects the branch to become the new tip and hence before pushing it to other clones I make sure that the real tip receives some more changes and others don't get confused about that strange tip.
hg up myBranch
hg commit --close-branch
Case 2:
branch was merged into default
This case is not that much different from case 1 and it can be solved by reproducing the steps for case 1 and two additional ones.
in this case I update to the branch changeset, do another commit with --close-branch and merge the new changeset that became the tip into default. the last operation creates a new tip that is in the default branch - HOORAY!
hg up myBranch
hg commit --close-branch
hg up default
hg merge myBranch
Hope this helps future readers.
EDIT ouch, too late... I know read your comment stating that you want to keep the feature-x changeset around, so the cloning approach here doesn't work.
I'll still let the answer here for it may help others.
If you want to completely get rid of "feature X", because, for example, it didn't work, you can clone. This is one of the method explained in the article and it does work, and it talks specifically about heads.
As far as I understand you have this and want to get rid of the "feature-x" head once and for all:
# changeset: 7:00a7f69c8335
|\ tag: tip
| | parent: 4:31b6f976956b
| | parent: 2:0a834fa43688
| | summary: merge
| |
| | o changeset: 5:013a3e954cfd
| |/ summary: Closed branch feature-x
| |
| o changeset: 4:31b6f976956b
| | summary: Changeset2
| |
| o changeset: 3:5cb34be9e777
| | parent: 1:1cc843e7f4b5
| | summary: Changeset 1
| |
o | changeset: 2:0a834fa43688
|/ summary: Changeset C
|
o changeset: 1:1cc843e7f4b5
| summary: Changeset B
|
o changeset: 0:a9afb25eaede
summary: Changeset A
So you do this:
hg clone . ../cleanedrepo --rev 7
And you'll have the following, and you'll see that feature-x is indeed gone:
# changeset: 5:00a7f69c8335
|\ tag: tip
| | parent: 4:31b6f976956b
| | parent: 2:0a834fa43688
| | summary: merge
| |
| o changeset: 4:31b6f976956b
| | summary: Changeset2
| |
| o changeset: 3:5cb34be9e777
| | parent: 1:1cc843e7f4b5
| | summary: Changeset 1
| |
o | changeset: 2:0a834fa43688
|/ summary: Changeset C
|
o changeset: 1:1cc843e7f4b5
| summary: Changeset B
|
o changeset: 0:a9afb25eaede
summary: Changeset A
I may have misunderstood what you wanted but please don't mod down, I took time reproducing your use case : )
It is strange, that no one yet has suggested the most robust way of closing a feature branches...
You can just combine merge commit with --close-branch flag (i.e. commit modified files and close the branch simultaneously):
hg up feature-x
hg merge default
hg ci -m "Merge feature-x and close branch" --close-branch
hg branch default -f
So, that is all. No one extra head on revgraph. No extra commit.