How do you set the username that Mercurial uses for commits? - mercurial

When I commit something in Mercurial like this:
hg commit -m "username question"
I see this output:
No username found, using 'WindowsVistaAdmin#ChunkyMonkey' instead
ChunkyMonkey is my Windows machine name and obviously WindowsVistaAdmin is the user that I am signed in as on this machine.
How can I set the username to something more respectable, or, at least, more concise?

In your ~/.hgrc (*nix) or mercurial.ini (Windows) file:
[ui]
username = First Last <email#address.com>
(mercurial.ini is in C:\Documents and Settings\[username]\ for XP and lower, C:\Users\[username]\ for Vista and higher. You can also run hgtk userconfig if you have TortoiseHg installed and do it that way.)

you can specify your username on the command line directly if you want to using --config. eg
hg --config ui.username=frymaster -m "comment here" commit
in fact, you can override anything in your .hgrc with this command. just look at your .hgrc and note the format:
[section]
key=val
that translates directly to
hg --config section.key=val

Information from here:
Setting up a username
When you try to run hg commit for the
first time, it is not guaranteed to
succeed. Mercurial records your name
and address with each change that you
commit, so that you and others will
later be able to tell who made each
change. Mercurial tries to
automatically figure out a sensible
username to commit the change with. It
will attempt each of the following
methods, in order:
If you specify a -u option to the hg commit command on the command
line, followed by a username, this is
always given the highest precedence.
If you have set the HGUSER environment variable, this is checked
next.
If you create a file in your home directory called .hgrc, with a
username entry, that will be used
next. To see what the contents of this
file should look like, refer to the
section called “Creating a Mercurial
configuration file” below.
If you have set the EMAIL environment variable, this will be
used next.
Mercurial will query your system to find out your local user name and
host name, and construct a username
from these components. Since this
often results in a username that is
not very useful, it will print a
warning if it has to do this.
If all of these mechanisms fail,
Mercurial will fail, printing an error
message. In this case, it will not let
you commit until you set up a
username.
You should think of the HGUSER
environment variable and the -u option
to the hg commit command as ways to
override Mercurial's default selection
of username. For normal use, the
simplest and most robust way to set a
username for yourself is by creating a
.hgrc file; see below for details.

Here is how my windows /users/xxx/mercurial.ini looks. I don't have to enter username or passwords for anything. Looks like it might be repo specific. I have tortoiseHG installed, not sure if that makes any difference.
[ui]
username=mbroekhuis
[auth]
repo.prefix=http://myrepo
repo.username=mbroekhuis
repo.password=secret

For anyone trying to use HG workbench
settings
user global settings
Edit File
Save

Related

How can I pass my remote username and password directly into hg outgoing?

I have a repository where I want to undo all local commits on my branch since the most recent commit pulled down. I do not want to save any work.
After doing some research I found the following on SuperUser:
hg --config "extensions.strip = " strip "roots(outgoing())"
Note that I have added --config "extensions.strip = " because I do not have the strip extension enabled.
After executing this command it then appears to already know my remote username (which is not in my mercurial.ini) and prompts for my remote password - this then appears to function exactly as I want it to.
My question is how does it know my username already if it is not stored in my mercurial.ini? And more importantly how can I pass both my username and password directly into my command so I could give a different username and have it also not prompt me for the password?
Thanks!
Can you check the repo's .hg/hgrc to confirm if it doesn't have any credential? If you have cloned using your username (eg: http://username:password#ip:port/reponame), it will stay in the repo's config.
You can create a new named path in .hg/hgrc and pass it to "roots(outgoing(newpath))". That will do what you are expecting.

Mercurial, define my own template/keyword: hostname

I need a mercurail template/keyword "hostname" to get the name (or IP) of the computer where the repo is located. as far as i read the wiki, namely "Chapter 11. Customizing the output of Mercurial", hg help templates, and the web, I think it should be similar to the date keyword, dynamicly expanded. How can i define my own template/keyword?
Thank you verry much
The following extension should do the trick:
from mercurial import templatekw
testedwith = "3.5"
_ipname = None
def showipname(repo, ctx, templ, **args):
""":ipname: String. The hostname of the machine that the repository
resides on."""
import socket
global _ipname
if not _ipname:
_ipname = socket.gethostname()
return _ipname
def uisetup(ui):
templatekw.keywords["ipname"] = showipname
Then use (say):
hg log -r . -T '{node|short} at {ipname}\n'
Note that the value of socket.gethostname() may depend on your internet connectivity. If you need the value in /etc/hostname or something else that identifies your machine, use that method instead.
To use an extension, put it in a file, say ipnametempl.py somewhere, then add the following lines to your .hgrc:
[extensions]
ipnametempl=/path/to/ipnametempl.py
This can be either your user/global hgrc or the .hg/hgrc in your repository (the latter if you only want to enable it for a specific repository). See hg help hgrc to find out where the user/global hgrc files are on your system.
Either the repository is local to your machine (then you can get the absolute path via hg root) and query the name via the normal system tools. You can embed those also in your templates:
$ hg log -r. --template="{branch}-{rev} from $(hostname) running debian $(cat /etc/debian_version)"
trunk-22248 from MYHOSTNAME running debian 8.2
Or you know already the URL (remote path) in order to operate with it as you need to specify it as argument to hg pull/clone/outgoing/incoming
If the remote URL is not explicitly specified on the command line it is specified in your .hgrc in the [path] section. When there is a remote repository at all, then usually a default = URL is defined there.
If you need the URL printed, then install hooks for clone, pull and push (and maybe outgoing and incoming) which prints the $URL as available in those hooks - or maybe just the changegroup hook. Check http://hgbook.red-bean.com/read/handling-repository-events-with-hooks.html#sec:hook:changegroup and hg help config.

In Mercurial, how to run original command if default arguments are present?

I have configured hg log in ~/.hgrc to only list commits from the current branch by default:
[defaults]
log = --branch .
However, occasionally I'd like to really see commits from all branches. Is there a way to tell hg log when invoked form the command line to not use the configured defaults but fall back to the built-in behavior? As a brute-force solution ignoring ~/.hgrc altogether for this particular invocation of hg log would be fine for me.
I'm aware that defaults are deprecated in favor of aliases, but aliases cannot be created with the same names as existing commands, which is what I want in order to not have to learn new command names, esp. when ~/.hgrc settings are to be shared by multiple developers.
Edit: Not being able to create aliases with the same names as existing commands was a regression that has been fixed.
You should be able to use --config to override the setting in your .hgrc:
hg --config defaults.log= log
From the man page:
--config set/override config option (use 'section.name=value')
I have gone through the bug reports on the Mercurial website and cannot find any workarounds for this, the response being a blanket "this is deprecated".
Personally, not learning the commands to me is not a valid reason for not migrating away from default command values. A possible alternative would be to move away from per-repository settings and have some settings at the user level, so you can set your own defaults / aliases.
Defaults are now deprecated, so you should likely remove this and specify the arguments each time. Confirmed in the documentation:
https://www.mercurial-scm.org/wiki/Defaults
(defaults are deprecated. Don't use them. Use aliases instead)
Try:
[alias]
blog = log --branch
Usage:
hg blog <branch name>
hg blog default
I know I'm resurrecting an old question here, but this statement
aliases cannot be created with the same names as existing commands
is incorrect.
In your .hgrc file in the [alias] section, you can, for example, have:
[alias]
add = add --dry-run
This will make the add command always do a dry-run, instead of actually recursively adding all unknown files in the repository.
It seems the best solution to my use-case indeed it to temporarily ignore the ~/.hgrc file altogether. This can be done by setting HGRCPATH to an empty string, which causes Mercurial to only read the .hg/hgrc file from the current repository:
HGRCPATH="" hg log

Having ssh not ask for a password every time with Mercurial

I'm a Mercurial newbie and I just started to use it.
I work in local repository and when I commit changes I use hg <command> ssh://user#host/usr/www/site.com/project for pushing, pulling and see the incoming/outgoing changes.
But every time ssh ask me the password. Is there a way for remember my ssh password for this purpose? Also, how can I don't write every time the full command (ssh://user etc etc)?
You have to setup your ssh with public keys. There are many tutorials on the web e.g. see Getting started with SSH
Once you have the keys in place you can either use ssh-agent to only enter your local private-key password once per session. There are also GUI tools that act as ssh-agent (e.g. SSHKeychain on a Mac)
Or if you have low security requirements you can also generate your key without password.
But please don't store cleartext passwords in config files.
There are two possibilities to avoid typing the url on each command:
From hg help urls
These URLs can all be stored in your hgrc with path aliases under the
[paths] section like so:
[paths]
alias1 = URL1
alias2 = URL2
...
The other possibility is using the default paths:
default:
When you create a repository with hg clone, the clone command saves the
location of the source repository as the new repository's 'default'
path. This is then used when you omit path from push- and pull-like
commands (including incoming and outgoing).
Thats what I often use, since usually you get your working directory bay cloning from somewhere and from then on I just don't specify the url and use the default.

create hgrc file to work on all paths on a machine, and for several repos

I want to create a hgrc file to set the username and password for all paths on some machine, e.g no matter in which directory I am in, hg clone some_path will always work without prompting for a username and a password (this is for an auto-deploy script). Also, it should work for several repos, not just one.
I followed the instructions and created a file: /etc/mercurial/hgrc.d/deploy.rc
it's contents:
[auth]
default.prefix= http://myrepo
default.username = myuname
default.password = pwd
But when I do
hg clone some_path I get abort: error: Connection refused.
What Am i doing wrong?
It should work. You can use hg showconfig to verify that it really is reading the config and that you don't just have a connection problem or something.
What version of hg are you using?
Also, it could be that your .hg/hgrc file is taking precedence over your global config.
Could you get the log of the server you try to connecgt to?
It should be listed there if at least the server address is correct.
And perhaps a hg clone -v something