objectspec.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. # objectspec.py -- Object specification
  2. # Copyright (C) 2014 Jelmer Vernooij <jelmer@samba.org>
  3. #
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU General Public License
  6. # as published by the Free Software Foundation; version 2
  7. # of the License or (at your option) a later version of the License.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  17. # MA 02110-1301, USA.
  18. """Object specification."""
  19. def parse_object(repo, objectish):
  20. """Parse a string referring to an object.
  21. :param repo: A `Repo` object
  22. :param objectish: A string referring to an object
  23. :return: A git object
  24. :raise KeyError: If the object can not be found
  25. """
  26. if getattr(objectish, "encode", None) is not None:
  27. objectish = objectish.encode('ascii')
  28. return repo[objectish]
  29. def parse_commit_range(repo, committishs):
  30. """Parse a string referring to a range of commits.
  31. :param repo: A `Repo` object
  32. :param committishs: A string referring to a range of commits.
  33. :return: An iterator over `Commit` objects
  34. :raise KeyError: When the reference commits can not be found
  35. :raise ValueError: If the range can not be parsed
  36. """
  37. if getattr(committishs, "encode", None) is not None:
  38. committishs = committishs.encode('ascii')
  39. return iter([repo[committishs]])