aboutsummaryrefslogtreecommitdiff
path: root/gitosis/repository.py
blob: 764c9808a537e25d4a9d0e4b1c459314e697687c (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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import os
import subprocess
 
from gitosis import util
 
class GitError(Exception):
    """git failed"""
 
    def __str__(self):
        return '%s%s' % (self.__doc__''.join(self.args))
 
class GitInitError(Exception):
    """git init failed"""
 
def init(
    path,
    template=None,
    _git=None,
    ):
    if _git is None:
        _git = 'git'
 
    util.mkdir(path0750)
    args = [_git, 'init']
    if template is not None:
        args.append('--template=%s' % template)
    returncode = subprocess.call(
        args=args,
        cwd=path,
        close_fds=True,
        env=dict(GIT_DIR='.'),
        )
    if returncode != 0:
        raise GitInitError('exit status %d' % returncode)
 
 
class GitFastImportError(GitError):
    """git fast-import failed"""
    pass
 
def fast_import(
    git_dir,
    commit_msg,
    committer,
    files,
    ):
    """
    Create an initial commit.
    """
    init(path=git_dir)
    child = subprocess.Popen(
        args=['git', 'fast-import', '--quiet', '--date-format=now'],
        cwd=git_dir,
        stdin=subprocess.PIPE,
        close_fds=True,
        env=dict(GIT_DIR=git_dir),
        )
    files = list(files)
    for index(pathcontent) in enumerate(files):
        child.stdin.write("""\
blob
mark :%(mark)d
data %(len)d
%(content)s
""" % dict(
            mark=index+1,
            len=len(content),
            content=content,
            ))
    child.stdin.write("""\
commit refs/heads/master
committer %(committer)s now
data %(commit_msg_len)d
%(commit_msg)s
""" % dict(
        committer=committer,
        commit_msg_len=len(commit_msg),
        commit_msg=commit_msg,
        ))
    for index(pathcontent) in enumerate(files):
        child.stdin.write('M 100644 :%d %s\n' % (index+1path))
    child.stdin.close()
    returncode = child.wait()
    if returncode != 0:
        raise GitFastImportError(
            'git fast-import failed''exit status %d' % returncode)
 
class GitExportError(GitError):
    """Export failed"""
    pass
 
class GitReadTreeError(GitExportError):
    """git read-tree failed"""
 
class GitCheckoutIndexError(GitExportError):
    """git checkout-index failed"""
 
def export(git_dir, path):
    # it's a literal prefix for git, a trailing slash is needed to 
    # extract to the subdirectory 
    path = os.path.join(path'')
    returncode = subprocess.call(
        args=['git', 'read-tree', 'HEAD'],
        close_fds=True,
        env=dict(GIT_DIR=git_dir),
        )
    if returncode != 0:
        raise GitReadTreeError('exit status %d' % returncode)
    returncode = subprocess.call(
        args=[ 
            'git', 
            'checkout-index', 
            '-a', 
            '-f', 
            '--prefix=%s' % path, 
            ],
        close_fds=True,
        env=dict(GIT_DIR=git_dir),
        )
    if returncode != 0:
        raise GitCheckoutIndexError('exit status %d' % returncode)