I want to periodically backup a Mercurial repository to the bitbucket clone. One option is to schedule it with cron. But fail to see how to 'add' and then 'push' from the cron configuration file (how to execute 'hg' in the local directory?).
A line like this in the crontab
*/60 * * * * ~/path/to/repository/hg push https://user#bitbucket.org/user/repository
does not work.
You need to give the full qualified path in cron scripts, ~ is not expanded to the home directory.
However the way you quote looks funky. You can call hg and directly specify the path to the repository:
hg -R /full/path/to/repository push URL
Thus
*/60 * * * * hg -R /full/path/to/repository push URL
might do the trick for you.
Related
We have 500 plus Hg repositories and am looking for a quick and efficient want of backup. Is there a script or Tool that we can use to backup these repositories. We tried Hg bundle, hg clone and regular file system backup but they are not helping.
Is there a standard practice, or some documentation for Hg repositories backup policy?
A follow-up question, what will happen when a user is in a middle of pushing the changeset and we start the backup ?
We do use RhodeCode to publish the Hg repositories.
Thanks
There's no standard. A true FS-level snapshot taken during a push will be fine, but a non-instantaneous mirroring operation (recursive copy) could end up with a corrupt repo, though it would be repairable to the pre-push state.
In the past I've done something as simple as:
for repo in $(find /srv/repos -type d -name .hg | sed 's/\.hg$//') ; do
hg --cwd $repo --repository $repo push ssh://backupserver/$(basename $repo)
done
That pushes all repos to a remote ssh server, incrementally, with full updating-while-pushing integrity, creating them if necessary.
I have a somewhat similar solution of cloning/pulling from a backup server but I trigger the backup on an hg hook.
In the main server global hgrc, I have:
changegroup.backup = .../backup.sh
and in backup.sh something like:
REPO=`hg root`
ts sshpass -p '...' ssh hg#backupserver "~/bin/pull.exp $REPO" 2>> $LOG
Ts (task spooler) allows the operation to happen asynchronously. The expect script is handling the fact that the repository might be new (thus performing an hg clone --noupdate) or already existing (thus performing an hg pull) and also can give the ssh key a passphrase when requested.
Pushing on the second server is only allowed for the backup hook, so no issue of multiple heads or needed force can happen.
What I find interesting in this type of real time backup is that in case of crash of the main server, it should be much faster to switch to the backup one.
My .hg/hgrc file has the line:
default = http://some/remote/repository
Is there a quick command to print the tip revision of that repository (which may or may not be inside my local repository)?
You can use the identify command like this:
$ hg identify $(hg paths default)
This is one of the few commands that can operate on a remote repository. If you need more information about the remote repository, then I suggest you take a look at hg incoming.
The following returns the latest changeset number (tip) of a remote repository:
hg identify --id http://www.myrepo.com
hg id default
This is a shorter form of "hg identify $(hg paths default)".
I am working with Git repositories in the following way:
I have the master repository and several remotes on the different production machines.
I am pushing the production code to the remotes and restart the services for the changes to take effect.
I am about to switch from Git to Mercurial and I would like to know ahead how I can achieve something like that.
You add entries to the [paths] section of your local clone's .hg/hgrc file. Here's an example of a section that would go in the .hg/hgrc file:
[paths]
remote1 = http://path/to/remote1
remote2 = http://path/to/remote2
You can then use commands like hg push remote1 to send changesets to that repo. If you want that remote repo to update is working directory you'd need to put a changegroup hook in place at that remote location that does an update. That would look something like:
[hooks]
changegroup = hg update 2>&1 > /dev/null && path/to/script/restart-server.sh
Not everyone is a big fan of having remote repos automatically update their working directories on push, and it's certainly not the default.
if you want to add default path, you have to work with default in your ~project/.hg/hgrc file. As Follows:
[paths]
default = https://path/to/your/repo
Good Luck.
You could have a look at hg-git GitHub plugin:
adding the ability to push to and pull from a Git server repository from Mercurial.
This means you can collaborate on Git based projects from Mercurial, or use a Git server as a collaboration point for a team with developers using both Git and Mercurial.
Note: I haven't tested that tool with the latest versions of Mercurial.
If you're on Unix and you have Git installed, you can use this bash function to readily add a path to the remotes without a text editor:
add-hg-path() {
git config -f $(hg root)/.hg/hgrc --add paths.$1 $2
awk '{$1=$1}1' $(hg root)/.hg/hgrc > /tmp/hgrc.tmp
mv /tmp/hgrc.tmp $(hg root)/.hg/hgrc
}
Then invoke it with:
$ add-hg-path remote1 https://path.to/remote1
If someone would like to build a Powershell equivalent, I'd like to include that as well. Other potentials improvements include error checking on the parameters and factoring out the call to $(hg root).
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.
Being very familiar with the subversion workflow and that fact that 99.9% of the time my computer is connected to the internet, I don't like doing 'hg ci' and 'hg push' separately.
I remember bzr had a 'checkout' command that would bind subsequent 'commit' commands to automatically commit directly to the server ('push').
Does mercurial have something similar to this?
PS: Writing a shell script or alias that runs 'hg ci $* && hg push' would be the last thing I'd do.
You could add a hook to run push after a successful commit.
EDIT: I just tried it out and it seems to work fine. I added the following to the .hg/hgrc file of the repository I wanted to activate automatic pushing for:
[hooks]
commit.autopush = hg push
EDIT 2: Also, you don't have to worry about something like this:
You're in a repository that you don't want to automatically push.
You use hg -R ~/another-repo-that-autopushes commit to commit in a different repo that does automatically push.
Will the hg push hook end up pushing the changes in the current directory instead of the one you're committing in?
No, it won't. According to the page I linked:
An executable hook is always run with its current directory set to a repository's root directory.
It's an edge case, but Mercurial handles it correctly.