content_adapter.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. # -*- coding: utf-8 -*-
  2. """
  3. Wraps a content object to provide some git information
  4. """
  5. import logging
  6. from pelican.utils import memoized
  7. from .git_wrapper import git_wrapper
  8. DEV_LOGGER = logging.getLogger(__name__)
  9. class GitContentAdapter(object):
  10. """
  11. Wraps a content object to provide some git information
  12. """
  13. def __init__(self, content):
  14. self.content = content
  15. self.git = git_wrapper('.')
  16. self.tz_name = content.settings.get('TIMEZONE', None)
  17. self.follow = content.settings['GIT_HISTORY_FOLLOWS_RENAME']
  18. @memoized
  19. def is_committed(self):
  20. '''
  21. Is committed
  22. '''
  23. return len(self.get_commits()) > 0
  24. @memoized
  25. def is_modified(self):
  26. '''
  27. Has content been modified since last commit
  28. '''
  29. return self.git.is_file_modified(self.content.source_path)
  30. @memoized
  31. def is_managed_by_git(self):
  32. '''
  33. Is content stored in a file managed by git
  34. '''
  35. return self.git.is_file_managed_by_git(self.content.source_path)
  36. @memoized
  37. def get_commits(self):
  38. '''
  39. Get all commits involving this filename
  40. :returns: List of commits newest to oldest
  41. '''
  42. if not self.is_managed_by_git():
  43. return []
  44. return self.git.get_commits(self.content.source_path, self.follow)
  45. @memoized
  46. def get_oldest_commit(self):
  47. '''
  48. Get oldest commit involving this file
  49. :returns: Oldest commit
  50. '''
  51. return self.git.get_commits(self.content.source_path, self.follow)[-1]
  52. @memoized
  53. def get_newest_commit(self):
  54. '''
  55. Get oldest commit involving this file
  56. :returns: Newest commit
  57. '''
  58. return self.git.get_commits(self.content.source_path, follow=False)[0]
  59. @memoized
  60. def get_oldest_filename(self):
  61. '''
  62. Get the original filename of this content. Implies follow
  63. '''
  64. commit_and_name_iter = self.git.get_commits_and_names_iter(
  65. self.content.source_path)
  66. _commit, name = next(commit_and_name_iter)
  67. return name
  68. @memoized
  69. def get_oldest_commit_date(self):
  70. '''
  71. Get datetime of oldest commit involving this file
  72. :returns: Datetime of oldest commit
  73. '''
  74. oldest_commit = self.get_oldest_commit()
  75. return self.git.get_commit_date(oldest_commit, self.tz_name)
  76. @memoized
  77. def get_newest_commit_date(self):
  78. '''
  79. Get datetime of newest commit involving this file
  80. :returns: Datetime of newest commit
  81. '''
  82. newest_commit = self.get_newest_commit()
  83. return self.git.get_commit_date(newest_commit, self.tz_name)