简体   繁体   中英

Use hg commands to operate git and hg simultaneously

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?

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM