hg.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. # Copyright (C) 2015 Mozilla Contributors
  2. #
  3. # This program is free software; you can redistribute it and/or
  4. # modify it under the terms of the GNU General Public License
  5. # as published by the Free Software Foundation; either version 2
  6. # of the License, or (at your option) any later version.
  7. #
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with this program; if not, write to the Free Software
  15. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  16. #
  17. # As a special exception, the copyright holders of this code give you
  18. # permission to combine this code with the software known as 'mozbuild',
  19. # and to distribute those combinations without any restriction
  20. # coming from the use of this file. (The General Public License
  21. # restrictions do apply in other respects; for example, they cover
  22. # modification of the file, and distribution when not combined with
  23. # mozbuild.)
  24. #
  25. # If you modify this code, you may extend this exception to your
  26. # version of the code, but you are not obliged to do so. If you
  27. # do not wish to do so, delete this exception statement from your
  28. # version.
  29. from __future__ import absolute_import
  30. import mercurial.error as error
  31. import mercurial.hg as hg
  32. import mercurial.ui as hgui
  33. from .files import (
  34. BaseFinder,
  35. MercurialFile,
  36. )
  37. import mozpack.path as mozpath
  38. # This isn't a complete implementation of BaseFile. But it is complete
  39. # enough for moz.build reading.
  40. class MercurialNativeFile(MercurialFile):
  41. def __init__(self, data):
  42. self.data = data
  43. def read(self):
  44. return self.data
  45. class MercurialNativeRevisionFinder(BaseFinder):
  46. def __init__(self, repo, rev='.', recognize_repo_paths=False):
  47. """Create a finder attached to a specific changeset.
  48. Accepts a Mercurial localrepo and changectx instance.
  49. """
  50. if isinstance(repo, (str, unicode)):
  51. path = repo
  52. repo = hg.repository(hgui.ui(), repo)
  53. else:
  54. path = repo.root
  55. super(MercurialNativeRevisionFinder, self).__init__(base=repo.root)
  56. self._repo = repo
  57. self._rev = rev
  58. self._root = mozpath.normpath(path)
  59. self._recognize_repo_paths = recognize_repo_paths
  60. def _find(self, pattern):
  61. if self._recognize_repo_paths:
  62. raise NotImplementedError('cannot use find with recognize_repo_path')
  63. return self._find_helper(pattern, self._repo[self._rev], self._get)
  64. def get(self, path):
  65. if self._recognize_repo_paths:
  66. if not path.startswith(self._root):
  67. raise ValueError('lookups in recognize_repo_paths mode must be '
  68. 'prefixed with repo path: %s' % path)
  69. path = path[len(self._root) + 1:]
  70. return self._get(path)
  71. def _get(self, path):
  72. if isinstance(path, unicode):
  73. path = path.encode('utf-8', 'replace')
  74. try:
  75. fctx = self._repo.filectx(path, self._rev)
  76. return MercurialNativeFile(fctx.data())
  77. except error.LookupError:
  78. return None