How to use mercurial built-in templates? - mercurial

Mercurial comes with some templates bundled. They may be stored in /usr/local/lib/python2.7/dist-packages/mercurial/templates/ or in /usr/share/mercurial/templates.
In this folder there are few files:
map-cmdline.bisect
map-cmdline.changelog
map-cmdline.compact
map-cmdline.default
map-cmdline.xml
They're styles to use in command line e.g. hg log -l 10 --style changelog.
Alse there are folders:
atom
coal
gitweb
monoblue
paper
raw
rss
spartan
Each of them contains files like:
bookmarkentry.tmpl
bookmarks.tmpl
changelogentry.tmpl
changelog.tmpl
error.tmpl
filelog.tmpl
header.tmpl
map
tagentry.tmpl
tags.tmpl
I'm guessing these are templates to use with hg serve. While trying run hgweb with -t option i get following error:
$ hg serve -t paper
listening at http://127.0.0.1:8000/ (bound to *:8000)
127.0.0.1 - - [14/May/2012 14:16:38] "GET / HTTP/1.1" 500 -
127.0.0.1 - - [14/May/2012 14:16:38] Exception happened during processing request '/':
Traceback (most recent call last):
[traceback here]
RuntimeError: No hgweb templates found in ['paper']
Does anyone know what are this folders/files and how to run hg serve with different templates?
ps.: I use Mercurial 2.1.1

You need to use the --style flag to specify a template style such as gitweb or paper.
hg serve --style paper
This will look in the shared templates folder and use the set of templates there. The -t option expects a path pointing to a set of templates.

Related

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.

How to write mercurial hooks on windows?

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

Detect is file is versioned

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.

Mercurial changegroup hook: repository URL

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

Is it possible to get the latest set of files from Mercurial without creating a local repository?

I am working on a system that performs continuous integration and I am looking for a method I can use to get the most recent changeset from a Mercurial repository without creating a repository locally.
I have considered using clone but this method will only work if you have set a working directory locally (since this will be occurring on a build server, I would prefer not to do this because of inclusion of the .hg file and the diffs - all I want is essentially an export of the files from the tip revision and nothing more.)
This request may not even be possible, and it's very likely that I just do not understand DVCS very well. However, if I cannot do what I want to do, is there a workaround?
It's possible using 'hg archive' depending how your remote repository is set up.
If it's available over HTTP using hgweb.cgi or hg serve you can hit the archive link programmatically to get the files you want. Example:
wget https://www.mercurial-scm.org/repo/hg/archive/tip.zip --output-document=- | unzip -
or it's available over ssh:
ssh you#there.com hg archive --type=zip - | unzip -
You can use:
$ hg clone http://your_repo
$ hg archive ../export/
$ rm -rf *
$ cd ..
$ cd export
From Mercurial's help files:
$ hg help archive
hg archive [OPTION]... DEST
create an unversioned archive of a
repository revision
You can use:
http://merc/raw-file
to retrieve a list of files in the repository or
http://merc/raw-file/filename
to get a specific file.