aboutsummaryrefslogtreecommitdiff
blob: 4ebbbfdbe3060aea44b669088a74c1ffaff44b16 (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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import os
import re
import subprocess
import sys

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(path, 0750)
    args = [_git, 'init']
    if template is not None:
        args.append('--template=%s' % template)
    returncode = subprocess.call(
        args=args,
        cwd=path,
        stdout=sys.stderr,
        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, (path, content) 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, (path, content) in enumerate(files):
        child.stdin.write('M 100644 :%d %s\n' % (index+1, path))
    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)

class GitHasInitialCommitError(GitError):
    """Check for initial commit failed"""

class GitRevParseError(GitError):
    """rev-parse failed"""

def has_initial_commit(git_dir):
    child = subprocess.Popen(
        args=['git', 'rev-parse', 'HEAD'],
        cwd=git_dir,
        stdout=subprocess.PIPE,
        close_fds=True,
        env=dict(GIT_DIR='.'),
        )
    got = child.stdout.read()
    returncode = child.wait()
    if returncode != 0:
        raise GitRevParseError('exit status %d' % returncode)
    if got == 'HEAD\n':
        return False
    elif re.match('^[0-9a-f]{40}\n$', got):
        return True
    else:
        raise GitHasInitialCommitError('Unknown git HEAD: %r' % got)