I want to add a hook to my repository. I can't work out where i need to put the hook file. The hgrc file is as follows:
[hooks]
precommit.test = precommithook
Location is C:\Code\RepoTest.hg\hgrc
Hook file is:
echo "hello world"
Location is C:\Code\RepoTest.hg\precommithook
When i run
hg commit -m"test"
from the command line i get
running hook precommit.test: precommithook
'precommithook' is not recognized as an internal or external command,
operable program or batch file.
abort: precommit.test hook exited with status 1
I've tried various paths but nothing works.
Most of the examples i googled with regards to Mercurial hooks are Unix based.
Is it possible to write hooks in a powershell?
According to Hooks doc, hook must be executable in given environment program, it haven't predefined location, but have to be found by OS.
Thus:
For pure Windows, hook must be named precommithook.bat
It must be placed into dir in $PATH or full path used in hook definition
As result, with
precommit.test = z:\precommit.bat in hgrc
#echo off
echo Hello World in z:\precommit.bat
I have on commit attempt
>hg commit -m "Edits"
Hello World
Related
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.
For example I have a hg versioned project in this path: C:\src\sample_project
Now, lets this project have subfolders, and lets say I'm editing a file inside this project C:\src\sample_project\docs\index.rst.
Having the path of this file C:\src\sample_project\docs\index.rst what is the easiest and most effective way to check if the file is versioned by hg, by either using Windows shell commands, hg.exe or tortoise (thg.exe)?
I'll post my doubt as answer.
Command to check if file is versioned: hg status <path> and then if the first character in stdout of this command is ? or a (from abort: no repository found in...) I should assume that file is not versioned.
What you stated is a way, but there is a cleaner one imo. You can use:
hg status -u which lists all unknown (read: not tracked) files in your repository.
How can I, on a Mercurial repository server, figure out the current repository URL or at least name (subpath) in a changegroup — or somewhat equivalent — hook? I'm running HgWeb on IIS.
$HG_URL returns the pushers URL, not the receiving repository's. $HG_SOURCE only returns serve.
Context: I'm trying to write a changegroup hook for Jenkins using /mercurial/notifyCommit?url=<url> that tells Jenkins to perform an SCM poll, and if I can't get this to work, I have to do about 50 cURL calls (once for every repository on the server) on every changegroup trigger, and then remember to maintain this list in hgweb.config for all eternity.
Your hook will be executed inside the root folder of that specific repository, you can use the following command in bash to get the current folder name:
${PWD##*/}
As per Ton's answer, this is what I ended up doing since I'm on Windows:
changegroup.jenkins.cmd:
#for /f "delims=\" %%a in ("%CD%") do #set TOPMOST=%%~nxa
#curl.exe "http://jenkins/mercurial/notifyCommit?url=http://hgweb/%TOPMOST%" -s -S
I've been using Zsh as a Bash replacement for a while now. One thing that doesn't work as well anymore is the completion for branch and uncommitted file names for mercurial.
If previously (bash) I had the following hg tracked folder:
repo/
.hg/
file1.txt
file2.txt <-- modified
Then doing this in Bash:
% hg commit -m "changed file2.txt" <TAB>
automagically completed file2.txt.
Same with branches — assuming I had default, dev and crazy branches, Bash knew how to complete branch names:
% hg update cr<TAB>
completed the branch name to crazy.
Basically what I'm asking is how to restore this functionality — which file/s take care of that and so on.
zsh uses its internal system for advanced completion, while bash uses a separate bash-completion software for that. Their configuration is incompatible so if you want some function to work you need to find a 3rd party zsh completion module for it or write it yourself. mercurial contains a sample zsh completion function, it is installed on my system as /usr/share/doc/mercurial/examples/zsh_completion.gz.
I'm looking for a way to set .hgrc configuration items without actually editing the text file. I'm trying to standardize the setup of the hgrc across multiple developers and I would like a command like
hg --config ui.username=foo
but which also saves that config change into the hgrc file.
It seems like this should be something that should be supported directly in the vanilla hg command, but I can't find it anywhere.
Someone -- either you or Mercurial -- will have to edit the configuration file if you want the config change to be saved :-)
And if you can call Mercurial with
hg --config ui.username=foo
then you should also be able to do
echo '[ui]' >> ~/.hgrc
echo 'username = foo' >> ~/.hgrc
which will save the config change, not matter how the ~/.hgrc file happens to look like (it is okay to have multiple [ui] sections).
Mercurial 3.0 and later has the hg config --edit command that opens an editor with the user config file. Still not quite what you're asking for, but at least this makes it easier to edit the file interactively.
This form:
hg --config ui.username=foo
Doesn't save anything. It sets the value for just the one run.
Also you can use /etc/mercurial/hgrc for system wide settings if that helps anything.
There is an extension that helps with this, https://bitbucket.org/alu/hgconfig/wiki/Home
After installing that hgext, you can do things like this.
% hg showconfig paths
paths.default=ssh://hg#bitbucket.org/alu/hgconfig
% hg config paths.upstream $(hg showconfig paths.default)
% hg config paths.default $(hg showconfig paths.default | sed 's|/alu/|/nassrat/|')
% hg showconfig paths
paths.default=ssh://hg#bitbucket.org/nassrat/hgconfig
paths.upstream=ssh://hg#bitbucket.org/alu/hgconfig
The only gotcha is this overrides the builtin config command, you can either tweak the code to change the command name, or live with it. Fortunately, it probably would not matter if your use case is simply to set and get specific configs.