zeitgeist-git.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #! /usr/bin/python
  2. # vim: set fileencoding=utf-8 :
  3. #
  4. # (C) 2010 Guido Guenther <agx@sigxcpu.org>
  5. # This program is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation; either version 2 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, please see
  17. # <http://www.gnu.org/licenses/>
  18. #
  19. # Simple Zeitgeist Git data source
  20. """Post-commit hook to submit the commit to Zeitgeist (http://www.zeitgeist-project.com)
  21. copy as post-commit to
  22. .git/hooks/post-commit
  23. in existing repositories or to
  24. /usr/share/git-core/templates
  25. so it get's used for new ones.
  26. """
  27. import os
  28. import subprocess
  29. import sys
  30. import time
  31. CLIENT = None
  32. try:
  33. from zeitgeist.client import ZeitgeistClient
  34. from zeitgeist.datamodel import Event, Subject, Interpretation, Manifestation
  35. except ImportError:
  36. pass
  37. else:
  38. try:
  39. CLIENT = ZeitgeistClient()
  40. except RuntimeError as e:
  41. print("Unable to connect to Zeitgeist, won't send events. Reason: '%s'" %e)
  42. def get_repo():
  43. """Get uri of remote repository and its name"""
  44. repo = None
  45. uri = subprocess.Popen(['git', 'config', '--get', 'remote.origin.url'],
  46. stdout=subprocess.PIPE).communicate()[0]
  47. if uri:
  48. uri = uri.strip().decode(sys.getfilesystemencoding())
  49. if '/' in uri:
  50. sep = '/'
  51. else:
  52. sep = ':'
  53. try:
  54. repo = unicode(uri.rsplit(sep, 1)[1])
  55. except IndexError: # no known separator
  56. repo = uri
  57. repo = repo.rsplit(u'.git', 1)[0]
  58. return repo, uri
  59. def main(argv):
  60. interpretation = Interpretation.MODIFY_EVENT.uri
  61. # FIXME: I'd be great if zeitgeist would allow for more detail:
  62. # * branch
  63. # * log summary (git log -1 --format=%s HEAD)
  64. curdir = os.path.abspath(os.curdir).decode(sys.getfilesystemencoding())
  65. uri = u"file://%s" % curdir
  66. repo, origin = get_repo()
  67. if not repo:
  68. repo = unicode(curdir.rsplit('/', 1)[1])
  69. origin = uri
  70. subject = Subject.new_for_values(
  71. uri = uri,
  72. interpretation = Interpretation.DOCUMENT.TEXT_DOCUMENT.PLAIN_TEXT_DOCUMENT.SOURCE_CODE.uri,
  73. manifestation = Manifestation.FILE_DATA_OBJECT.uri,
  74. text = repo,
  75. origin = origin)
  76. event = Event.new_for_values(
  77. timestamp = int(time.time() * 1000),
  78. interpretation = interpretation,
  79. manifestation = Manifestation.USER_ACTIVITY.uri,
  80. actor = "application://gitg.desktop",
  81. subjects = [subject])
  82. CLIENT.insert_event(event)
  83. if __name__ == '__main__':
  84. main(sys.argv)