summaryrefslogtreecommitdiff
path: root/examples/zeitgeist-git.py
blob: 92f72d87ec9e289e3c997e9723c59bb55410e90a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#! /usr/bin/python
# vim: set fileencoding=utf-8 :
#
# (C) 2010 Guido Guenther <agx@sigxcpu.org>
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
# Simple Zeitgeist Git data source

"""Post-commit hook to submit the commit to Zeitgeist (http://www.zeitgeist-project.com)

copy as post-commit to

    .git/hooks/post-commit

in existing repositories or to

    /usr/share/git-core/templates

so it get's used for new ones.
"""


import os
import subprocess
import sys
import time

CLIENT = None

try:
    from zeitgeist.client import ZeitgeistClient
    from zeitgeist.datamodel import Event, Subject, Interpretation, Manifestation
except ImportError:
    pass
else:
    try:
        CLIENT = ZeitgeistClient()
    except RuntimeError as e:
        print("Unable to connect to Zeitgeist, won't send events. Reason: '%s'" %e)


def get_repo():
    """Get uri of remote repository and its name"""
    repo = None
    uri = subprocess.Popen(['git', 'config', '--get', 'remote.origin.url'],
                             stdout=subprocess.PIPE).communicate()[0]

    if uri:
        uri = uri.strip().decode(sys.getfilesystemencoding())
        if '/' in uri:
            sep = '/'
        else:
            sep = ':'
        try:
            repo = str(uri.rsplit(sep, 1)[1])
        except IndexError: # no known separator
            repo = uri
        repo = repo.rsplit('.git', 1)[0]
    return repo, uri


def main(argv):
    interpretation = Interpretation.MODIFY_EVENT.uri

    # FIXME: I'd be great if zeitgeist would allow for more detail:
    #           * branch
    #           * log summary (git log -1 --format=%s HEAD)
    curdir = os.path.abspath(os.curdir).decode(sys.getfilesystemencoding())
    uri = "file://%s" % curdir

    repo, origin = get_repo()
    if not repo:
        repo = str(curdir.rsplit('/', 1)[1])
        origin = uri

    subject = Subject.new_for_values(
                uri = uri,
                interpretation = Interpretation.DOCUMENT.TEXT_DOCUMENT.PLAIN_TEXT_DOCUMENT.SOURCE_CODE.uri,
                manifestation = Manifestation.FILE_DATA_OBJECT.uri,
                text = repo,
                origin = origin)
    event = Event.new_for_values(
                timestamp = int(time.time() * 1000),
                interpretation = interpretation,
                manifestation = Manifestation.USER_ACTIVITY.uri,
                actor = "application://gitg.desktop",
                subjects = [subject])
    CLIENT.insert_event(event)

if __name__ == '__main__':
    main(sys.argv)