hg.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. # copyright 2003-2011 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
  2. # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
  3. #
  4. # This file is part of logilab-common.
  5. #
  6. # logilab-common is free software: you can redistribute it and/or modify it under
  7. # the terms of the GNU Lesser General Public License as published by the Free
  8. # Software Foundation, either version 2.1 of the License, or (at your option) any
  9. # later version.
  10. #
  11. # logilab-common is distributed in the hope that it will be useful, but WITHOUT
  12. # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  13. # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
  14. # details.
  15. #
  16. # You should have received a copy of the GNU Lesser General Public License along
  17. # with logilab-common. If not, see <http://www.gnu.org/licenses/>.
  18. """mercurial utilities (mercurial should be installed)"""
  19. __docformat__ = "restructuredtext en"
  20. import os
  21. import sys
  22. import os.path as osp
  23. try:
  24. from mercurial.error import RepoError
  25. from mercurial.__version__ import version as hg_version
  26. except ImportError:
  27. from mercurial.repo import RepoError
  28. from mercurial.version import get_version
  29. hg_version = get_version()
  30. from mercurial.hg import repository as Repository
  31. from mercurial.ui import ui as Ui
  32. from mercurial.node import short
  33. try:
  34. # mercurial >= 1.2 (?)
  35. from mercurial.cmdutil import walkchangerevs
  36. except ImportError, ex:
  37. from mercurial.commands import walkchangerevs
  38. try:
  39. # mercurial >= 1.1 (.1?)
  40. from mercurial.util import cachefunc
  41. except ImportError, ex:
  42. def cachefunc(func):
  43. return func
  44. try:
  45. # mercurial >= 1.3.1
  46. from mercurial import encoding
  47. _encoding = encoding.encoding
  48. except ImportError:
  49. try:
  50. from mercurial.util import _encoding
  51. except ImportError:
  52. import locale
  53. # stay compatible with mercurial 0.9.1 (etch debian release)
  54. # (borrowed from mercurial.util 1.1.2)
  55. try:
  56. _encoding = os.environ.get("HGENCODING")
  57. if sys.platform == 'darwin' and not _encoding:
  58. # On darwin, getpreferredencoding ignores the locale environment and
  59. # always returns mac-roman. We override this if the environment is
  60. # not C (has been customized by the user).
  61. locale.setlocale(locale.LC_CTYPE, '')
  62. _encoding = locale.getlocale()[1]
  63. if not _encoding:
  64. _encoding = locale.getpreferredencoding() or 'ascii'
  65. except locale.Error:
  66. _encoding = 'ascii'
  67. try:
  68. # demandimport causes problems when activated, ensure it isn't
  69. # XXX put this in apycot where the pb has been noticed?
  70. from mercurial import demandimport
  71. demandimport.disable()
  72. except:
  73. pass
  74. Ui.warn = lambda *args, **kwargs: 0 # make it quiet
  75. def find_repository(path):
  76. """returns <path>'s mercurial repository
  77. None if <path> is not under hg control
  78. """
  79. path = osp.realpath(osp.abspath(path))
  80. while not osp.isdir(osp.join(path, ".hg")):
  81. oldpath = path
  82. path = osp.dirname(path)
  83. if path == oldpath:
  84. return None
  85. return path
  86. def get_repository(path):
  87. """Simple function that open a hg repository"""
  88. repopath = find_repository(path)
  89. if repopath is None:
  90. raise RuntimeError('no repository found in %s' % osp.abspath(path))
  91. return Repository(Ui(), path=repopath)
  92. def incoming(wdrepo, masterrepo):
  93. try:
  94. return wdrepo.findincoming(masterrepo)
  95. except AttributeError:
  96. from mercurial import hg, discovery
  97. revs, checkout = hg.addbranchrevs(wdrepo, masterrepo, ('', []), None)
  98. common, incoming, rheads = discovery.findcommonincoming(
  99. wdrepo, masterrepo, heads=revs)
  100. if not masterrepo.local():
  101. from mercurial import bundlerepo, changegroup
  102. if revs is None and masterrepo.capable('changegroupsubset'):
  103. revs = rheads
  104. if revs is None:
  105. cg = masterrepo.changegroup(incoming, "incoming")
  106. else:
  107. cg = masterrepo.changegroupsubset(incoming, revs, 'incoming')
  108. fname = changegroup.writebundle(cg, None, "HG10UN")
  109. # use the created uncompressed bundlerepo
  110. masterrepo = bundlerepo.bundlerepository(wdrepo.ui, wdrepo.root, fname)
  111. return masterrepo.changelog.nodesbetween(incoming, revs)[0]
  112. def outgoing(wdrepo, masterrepo):
  113. try:
  114. return wdrepo.findoutgoing(masterrepo)
  115. except AttributeError:
  116. from mercurial import hg, discovery
  117. revs, checkout = hg.addbranchrevs(wdrepo, wdrepo, ('', []), None)
  118. o = discovery.findoutgoing(wdrepo, masterrepo)
  119. return wdrepo.changelog.nodesbetween(o, revs)[0]