1

I have a mercurial repository and a git repository. Both are using the same code and same change-sets.

When I make any changes to my hg repository, commit it and push it to hg repository, the same changes should get pushed to the git repository also. Is this possible? How?

Jeroen
  • 60,696
  • 40
  • 206
  • 339
Nevin Raj Victor
  • 2,924
  • 3
  • 23
  • 37

2 Answers2

1

Enable the extension hg-git on the mercurial repository and make use of a post-commit and post-changegroup hook. Within these hooks push to the git repository.

EDIT: simply place the non-standard extension of hg-git (see http://hg-git.github.io/ ) somewhere and edit your .hgrc:

[extensions]
hggit = /path/to/hg-git/hggit
[hooks]
changegroup.git = hg push git+https://path/to/git-repo.git
post-commit.git = hg push git+https://path/to/git-repo.git

Also read hg help on hgrc / hooks.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
planetmaker
  • 5,884
  • 3
  • 28
  • 37
  • Seems to be a promising one. Can you kindly share some more details on that. – Nevin Raj Victor Jun 23 '15 at 11:10
  • I placed everything as per your instruction in the .hgrc file. Now when I committed and push the code to hg repository,it gets pushed only to the hg repository. It's not getting into git. No error is logged. What might be the issue? – Nevin Raj Victor Jun 23 '15 at 14:31
  • Sorry, but 1) I can;t find post-commit type of hooks 2) changegroup hook for **local** repository in wrong idea - it activated "after a group of changesets has been brought into the repository from elsewhere", i.e on **commit|unbundle** into local repo, not on push to remote hg – Lazy Badger Jun 24 '15 at 00:19
  • @Lazy Badger: using both hooks, the post-commit and changegroup should cover both cases, changesets committed locally (post-commit) as well as those coming from another repo (changegroup). In essence one of those might not be necessary if you always only pull or only expect to make local commits. The post-commit hook is not explicitly given in hgrc help, but instead summarized under "post-" – planetmaker Jun 24 '15 at 08:07
  • OP wants **only** to have *manual* (sic!) pushes duplicated, you got: all changes in local repo are published. Another approach and **Bad Idea (tm)** in common – Lazy Badger Jun 25 '15 at 07:16
1

Follow-up to planetmaker's answer

  1. Install hg-git extension
  2. (for simplicity, not mandatory) Add remote git-repo to [paths] section of local repository
  3. Push to Git-repo by hand, get successful push results
  4. Create outgoing hook in [hooks] section of repository hg push GIT, where GIT - name of your git-repository from [paths]
  5. Have fun

Final notes and samples

Beware: outgoing hook fired in repo in two cases: on push and on bundle. additional push to GIT in case of using bundle will do nothing dangerous in repote Git (more precisely - do nothing, because haven't anything for push), but if you want to be perfect, you can (in simple if) check value of an environment variable named HG_SOURCE and push only if it equal to "push"

My real two-remotes repo

[paths]
github = git+ssh://git@github.com/lazybadger/Fiver-l10n.git
default = ssh://bigbadger@hg.code.sf.net/u/bigbadger/code
...
[hooks]
outgoing = hg push github

and this way I'll have push to GitHub's repo after push to SF's repo - and push can be done from any Mercurial client: command line, TortoiseHG, SmartGit.

(I don't use hook name, because it's single hook for now, I have to run Pageant with my key for accessing ssh-type of Git-repo before pushing to sf)

For pure CLI use-case you can also use the lazy way: create alias, which will combine two pushes in new command and push from command line with this command only, not "classic" hg push. For my repo in sample it can be something like (dirty fast sample without error-checking)

[alias]
pushc = !hg push && hg push github

and only hg pushc for pushes

Lazy Badger
  • 94,711
  • 9
  • 78
  • 110
  • I tried the last option. When I use `hg pushc` it gives me an error on git as `abort: error: nodename nor servname provided, or not known` – Nevin Raj Victor Jun 24 '15 at 06:08
  • I was using two repositories of bitbucket;one git and the other mercurial.Now when I replcaed the git repo url with a new repo created in GitHub it worked for me. Not sure what's wrong with git repo in bitbucket. – Nevin Raj Victor Jun 25 '15 at 07:44