Mercurial command to combine hg pull; hg up - mercurial

How can I combine the Mercurial commands hg pull and hg update into one?

Obviously, hg pull -u is the answer here.
However, there's a caveat that deserves mentioning: hg pull -u is not exactly equivalent to hg pull && hg update. This is briefly mentioned in the documentation, but it can be surprising if you first run into it; if there's nothing to pull (e.g. no new changesets came in), then hg pull -u doesn't update. This can be slightly confusing if you weren't on a head before issuing the command.

type hg help pull and you will see the -u switch

You can use:
hg pull -u
Read the documentation for more options.

The hg fetch extension will do the same once enabled. However, it has fallen out of favor and is noted as an "unloved feature" by the Mercurial team.

Related

Unable to fix Hg case-folding collision error with Mercurial's guide

Is there a problem with Mercurial's guide for fixing case-folding collisions or is there a problem with the way I am implementing the solution.
The solution as provided on the Mercurial wiki is as follows:
hg clone -U repo repair
cd repair
hg debugsetparents <bad revision>
hg debugrebuildstate
At this point, Mercurial will think you have the bad revision checked
out and all the files are missing (status '!'). To fix the repo, we
simply have to do:
hg rm -A <file causing the collision>
Now hg st should show the troublesome file in state 'R' and all other
files in state '!'. Now we can check in our fix:
hg ci -m"fix case collision"
To get all our files back, we just check out again:
hg co tip
The problem files are: SomeFile.bash and Somefile.bash. I originally had Somefile.bash and I would like it to now be SomeFile.bash. Also to note, version 157 is happy, no collision, but version 158 is where I have introduced the collision. The head of the repository is currently at revision 160.
I have implemented this solution as follows:
hg clone -U my-repo-url repair
cd repair
hg debugsetparent 160
hg debugrebuildstate
hg status (reveals that everything is 'missing' (!))
hg rm -A Somefile.bash (responds that SomeFile.bash has been removed, notice case change)
hg ci -m "Fixed the collision... I hope."
hg co tip
hg update -C tip
According to the guide, this should have removed the case-folding collision and brought the rest of the missing files back, yet another hg status reveals that everything is still missing (!).
Edit: By appending that last command (the update) to the existing commands, I was able to recover the missing files which solved the remainder of the problem.
Note: I had to use the most recent 'problem' version for <bad revision> to fix this problem (that was 160 in my case).
Try
hg update -C tip
That should bring the files back. If not, try reverting everything:
hg revert -r tip -a

HG: find where a deleted line was removed

I am looking for the mercurial equivalent of the solution to this question:
How do I "git blame" a deleted line?
In short, I am looking at a mercurial commit where a line was added, and in the current revision this line is no longer present, and I want to find when and why it was removed.
hg grep will let you search a change log for a pattern, such as a deleted string. If you are looking for more than just the first occurrence, be sure to include the --all flag. For me it looked something like this:
hg grep --all -r 9876:tip "pattern" path/to/file
Thanks to Anton for the helpful comments and krtek for his related answer.
hg log -p fileName > fileName.log
Then open the fileName.log file
In there you will find commit the details of each commit along with the diff of that commit. The details of the commit includes the username.

How to view diff between head of local repository and head of remote repository?

Before I push to a remote repository, I want to see a consolidated diff between the head of my local repository and the head of the repository I'm pushing too. The best way I know of doing this is to hg clone the remote repository, get the revision of the head, then do a diff between my head and that revision. But this is time-consuming. Is there a quick way?
In addition to
$ hg outgoing -p
which I normally use, I'll like to point you to revision sets. That is a query language that you can use with hg diff (and all other commands that lets you specify changesets). So you can implement hg outgoing -p by
$ hg log -r "outgoing()" -p
and you can get a diff between the parent of the first outgoing changeset and the last outgoing changeset with
$ hg diff -r "p1(first(outgoing()))" -r "last(outgoing())"
Finally, the remotebranch extension can maintain local information about the remote branches so that you don't need to use the network to lookup this information. It lets you use
$ hg log -r "not pushed()"
to find the outgoing changesets, but it's much faster since there's no network round trips involved.
If you're looking for a way of getting all the changes you've made that aren't in the remote repository.
$ hg outgoing -p
The -p is optional and reports in the form of a patch, otherwise it reports in the same way a hg log. This is just your changes regardless of whether anybody else has pushed anything to the remote repository.
If you're looking for changes in the remote repository that you don't have then you use
$ hg incoming
Again there's a -p form if you want it.
Neither of these are exactly what you asked for, but I suspect you don't actually want that.
If you really want the difference between your changes and the new head in the remote repo created by someone else, then you'll need to pull their changes over.
hg pull
hg heads # find revision number of new head
hg diff -r 124992 # or whatever the revision number is.

How to detect that commits are pushable

In Git it is easy, because remote/branch is pointing to a different commit than branch. How to do it with Mercurial?
If you mean seeing what's different between your local repo and the one you're pushing to, try
hg outgoing
Since Mercurial 2.1, there is also a purely local solution: phases. The draft phase is probably what you are looking for. For details, refer to:
https://www.mercurial-scm.org/wiki/Phases
You may find hg phase <rev> and hg log -r "draft()" interesting.
There is an remotebranch extension that will give you a Git-like setup. It tracks the remote heads for the repositories listed in the [paths] and exposes them as tags named <path>/<branch>. This lets you run
$ hg diff -r foo/default
to see what has changed since the default branch in the foo repository. There is also new revset keywords that let you do things like
$ hg log -r "not pushed()"
to get what
$ hg outgoing
would do, but without any network traffic.
What's the command line call to show all revisions in the draft phase?
hg log --style phases
That will display the log + the phase, since Mercurial 2.7 (2013-08-01).
I'd just use hg outgoing as others are suggesting, but hg summary will tell you too. It may require the --remote option to have it check the remote default server.
If you need to select the changesets for further processing, then you can use the outgoing revset predicate. This lets you re-implement hg outgoing as
hg log -r "outgoing()"
but the real benefit is that you can use this in other contexts, such as
hg strip "outgoing()"

Accidentally Working on the Wrong Named Branch in Mercurial

I have been making some changes to my working directory, and noticed that I have accidentally been working on the wrong branch. I have not committed anything yet, and I would like my next commit to go against another branch. What is the best way to do this?
The Shelve extension can give you grief, and this can be done entirely with Mercurial commands. Krtek almost had it but he used export instead of diff. Try this:
hg diff --git > ~/saved-work.patch
hg update --clean desiredbranch
hg import --no-commit ~/saved-work.patch
You should be able to just hg up otherbranch. It is important that you do not use the --clean option to hg up, either directly or via an alias as that will discard your uncommitted changes.
Another option is to use one of the extensions that provides hg shelve. The process would then be:
$ hg shelve --all
$ hg up otherbranch
$ hg unshelve
That will create a patch of your changes within the .hg directory, returning your working directory to a clean state, switch to the 'otherbranch', and then apply the saved patch.
I don't know if it is the best solution, but you can follow these steps :
1° hg diff --git > modifications.patch
2° hg update -C the_right_branch
3° hg patch modifications.patch
Maybe it's better to copy modifications.patch somewhere safe, just in case.
edit: update with diff instead of export. Thanks to the commenters.