summaryrefslogtreecommitdiff
path: root/morphlib/git.py
blob: 69f8752eacf0522aa4c68aaae18a6b12a9a2b900 (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
# Copyright (C) 2011  Codethink Limited
# 
# 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; version 2 of the License.
# 
# 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.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.


import gzip
import logging
import urlparse

import morphlib


class NoMorphs(Exception):

    def __init__(self, repo, ref):
        Exception.__init__(self, 
                            'Cannot find any morpologies at %s:%s' %
                                (repo, ref))


class TooManyMorphs(Exception):

    def __init__(self, repo, ref, morphs):
        Exception.__init__(self, 
                            'Too many morphologies at %s:%s: %s' %
                                (repo, ref, ', '.join(morphs)))


def export_sources(repo, ref, tar_filename):
    '''Export the contents of a specific commit into a compressed tarball.'''
    ex = morphlib.execute.Execute('.', msg=logging.debug)
    tar = ex.runv(['git', 'archive', '--remote', repo, ref])
    f = gzip.open(tar_filename, 'wb')
    f.write(tar)
    f.close()


def get_commit_id(repo, ref):
    '''Return the full SHA-1 commit id for a repo+ref.'''
    # FIXME: This assumes repo is a file:/// URL.

    scheme, netlock, path, params, query, frag = urlparse.urlparse(repo)
    assert scheme == 'file'
    ex = morphlib.execute.Execute(path, msg=logging.debug)
    out = ex.runv(['git', 'rev-list', '-n1', ref])
    return out.strip()


def get_morph_text(repo, ref):
    '''Return a morphology from a git repository.'''
    # FIXME: This implementation assumes a local repo.

    scheme, netlock, path, params, query, frag = urlparse.urlparse(repo)
    assert scheme == 'file'

    ex = morphlib.execute.Execute(path, msg=logging.debug)
    out = ex.runv(['git', 'ls-tree', '--name-only', '-z', ref])
    names = [x for x in out.split('\0') if x]
    morphs = [x for x in names if x.endswith('.morph')]
    if len(morphs) == 0:
        raise NoMorphs(repo, ref)
    if len(morphs) > 1:
        raise TooManyMorphs(repo, ref, morphs)
    out = ex.runv(['git', 'cat-file', 'blob', '%s:%s' % (ref, morphs[0])])
    
    return morphs[0], out