-
Notifications
You must be signed in to change notification settings - Fork 79
/
ghp_import.py
executable file
·306 lines (255 loc) · 9.02 KB
/
ghp_import.py
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#! /usr/bin/env python
import errno
import os
import subprocess as sp
import sys
import time
from dateutil import tz
from datetime import datetime
try:
from shlex import quote
except ImportError:
from pipes import quote
__all__ = ['ghp_import']
__version__ = "2.1.0"
class GhpError(Exception):
def __init__(self, message):
self.message = message
if sys.version_info[0] == 3:
def enc(text):
if isinstance(text, bytes):
return text
return text.encode()
def dec(text):
if isinstance(text, bytes):
return text.decode('utf-8')
return text
def write(pipe, data):
try:
pipe.stdin.write(data)
except IOError as e:
if e.errno != errno.EPIPE:
raise
else:
def enc(text):
if isinstance(text, unicode): # noqa F821
return text.encode('utf-8')
return text
def dec(text):
if isinstance(text, unicode): # noqa F821
return text
return text.decode('utf-8')
def write(pipe, data):
pipe.stdin.write(data)
class Git(object):
def __init__(self, use_shell=False):
self.use_shell = use_shell
self.cmd = None
self.pipe = None
self.stderr = None
self.stdout = None
def check_repo(self):
if self.call('rev-parse') != 0:
error = self.stderr
if not error:
error = "Unknown Git error"
error = dec(error)
if error.startswith("fatal: "):
error = error[len("fatal: "):]
raise GhpError(error)
def try_rebase(self, remote, branch, no_history=False):
rc = self.call('rev-list', '--max-count=1', '%s/%s' % (remote, branch))
if rc != 0:
return True
rev = dec(self.stdout.strip())
if no_history:
rc = self.call('update-ref', '-d', 'refs/heads/%s' % branch)
else:
rc = self.call('update-ref', 'refs/heads/%s' % branch, rev)
if rc != 0:
return False
return True
def get_config(self, key):
self.call('config', key)
return self.stdout.strip()
def get_prev_commit(self, branch):
rc = self.call('rev-list', '--max-count=1', branch, '--')
if rc != 0:
return None
return dec(self.stdout).strip()
def open(self, *args, **kwargs):
if self.use_shell:
self.cmd = 'git ' + ' '.join(map(quote, args))
else:
self.cmd = ['git'] + list(args)
if sys.version_info >= (3, 2, 0):
kwargs['universal_newlines'] = False
for k in 'stdin stdout stderr'.split():
kwargs.setdefault(k, sp.PIPE)
kwargs['shell'] = self.use_shell
self.pipe = sp.Popen(self.cmd, **kwargs)
return self.pipe
def call(self, *args, **kwargs):
self.open(*args, **kwargs)
(self.stdout, self.stderr) = self.pipe.communicate()
return self.pipe.wait()
def check_call(self, *args, **kwargs):
kwargs["shell"] = self.use_shell
sp.check_call(['git'] + list(args), **kwargs)
def mk_when(timestamp=None):
if timestamp is None:
timestamp = int(time.time())
currtz = datetime.now(tz.tzlocal()).strftime('%z')
return "%s %s" % (timestamp, currtz)
def start_commit(pipe, git, branch, message, prefix=None):
uname = os.getenv('GIT_COMMITTER_NAME', dec(git.get_config('user.name')))
email = os.getenv('GIT_COMMITTER_EMAIL', dec(git.get_config('user.email')))
when = os.getenv('GIT_COMMITTER_DATE', mk_when())
write(pipe, enc('commit refs/heads/%s\n' % branch))
write(pipe, enc('committer %s <%s> %s\n' % (uname, email, when)))
write(pipe, enc('data %d\n%s\n' % (len(enc(message)), message)))
head = git.get_prev_commit(branch)
if head:
write(pipe, enc('from %s\n' % head))
if prefix:
write(pipe, enc('D %s\n' % prefix))
else:
write(pipe, enc('deleteall\n'))
def add_file(pipe, srcpath, tgtpath):
with open(srcpath, "rb") as handle:
if os.access(srcpath, os.X_OK):
write(pipe, enc('M 100755 inline %s\n' % tgtpath))
else:
write(pipe, enc('M 100644 inline %s\n' % tgtpath))
data = handle.read()
write(pipe, enc('data %d\n' % len(data)))
write(pipe, enc(data))
write(pipe, enc('\n'))
def add_nojekyll(pipe, prefix=None):
if prefix:
fpath = os.path.join(prefix, '.nojekyll')
else:
fpath = '.nojekyll'
write(pipe, enc('M 100644 inline %s\n' % fpath))
write(pipe, enc('data 0\n'))
write(pipe, enc('\n'))
def add_cname(pipe, cname):
write(pipe, enc('M 100644 inline CNAME\n'))
write(pipe, enc('data %d\n%s\n' % (len(enc(cname)), cname)))
def gitpath(fname):
norm = os.path.normpath(fname)
return "/".join(norm.split(os.path.sep))
def run_import(git, srcdir, **opts):
srcdir = dec(srcdir)
pipe = git.open('fast-import', '--date-format=rfc2822', '--quiet',
stdin=sp.PIPE, stdout=None, stderr=None)
start_commit(pipe, git, opts['branch'], opts['mesg'], opts['prefix'])
for path, _, fnames in os.walk(srcdir, followlinks=opts['followlinks']):
for fn in fnames:
fpath = os.path.join(path, fn)
gpath = gitpath(os.path.relpath(fpath, start=srcdir))
if opts['prefix']:
gpath = os.path.join(opts['prefix'], gpath)
add_file(pipe, fpath, gpath)
if opts['nojekyll']:
add_nojekyll(pipe, opts['prefix'])
if opts['cname'] is not None:
add_cname(pipe, opts['cname'])
write(pipe, enc('\n'))
pipe.stdin.close()
if pipe.wait() != 0:
sys.stdout.write(enc("Failed to process commit.\n"))
def options():
return [
(('-n', '--no-jekyll'), dict(
dest='nojekyll',
default=False,
action="store_true",
help='Include a .nojekyll file in the branch.',
)),
(('-c', '--cname'), dict(
dest='cname',
default=None,
help='Write a CNAME file with the given CNAME.',
)),
(('-m', '--message'), dict(
dest='mesg',
default='Update documentation',
help='The commit message to use on the target branch.',
)),
(('-p', '--push'), dict(
dest='push',
default=False,
action='store_true',
help='Push the branch to origin/{branch} after committing.',
)),
(('-x', '--prefix'), dict(
dest='prefix',
default=None,
help='The prefix to add to each file that gets pushed to the '
'remote. Only files below this prefix will be cleared '
'out. [%(default)s]',
)),
(('-f', '--force'), dict(
dest='force',
default=False, action='store_true',
help='Force the push to the repository.',
)),
(('-o', '--no-history'), dict(
dest='no_history',
default=False,
action='store_true',
help='Force new commit without parent history.',
)),
(('-r', '--remote'), dict(
dest='remote',
default='origin',
help='The name of the remote to push to. [%(default)s]',
)),
(('-b', '--branch'), dict(
dest='branch',
default='gh-pages',
help='Name of the branch to write to. [%(default)s]',
)),
(('-s', '--shell'), dict(
dest='use_shell',
default=False,
action='store_true',
help='Use the shell when invoking Git. [%(default)s]',
)),
(('-l', '--follow-links'), dict(
dest='followlinks',
default=False,
action='store_true',
help='Follow symlinks when adding files. [%(default)s]',
))
]
def ghp_import(srcdir, **kwargs):
if not os.path.isdir(srcdir):
raise GhpError("Not a directory: %s" % srcdir)
opts = {kwargs["dest"]: kwargs["default"] for _, kwargs in options()}
opts.update(kwargs)
git = Git(use_shell=opts['use_shell'])
git.check_repo()
if not git.try_rebase(opts['remote'], opts['branch'], opts['no_history']):
raise GhpError("Failed to rebase %s branch." % opts['branch'])
run_import(git, srcdir, **opts)
if opts['push']:
if opts['force'] or opts['no_history']:
git.check_call('push', opts['remote'], opts['branch'], '--force')
else:
git.check_call('push', opts['remote'], opts['branch'])
def main():
from argparse import ArgumentParser
parser = ArgumentParser()
parser.add_argument("--version", action="version", version=__version__)
parser.add_argument("directory")
for args, kwargs in options():
parser.add_argument(*args, **kwargs)
args = parser.parse_args().__dict__
try:
ghp_import(args.pop("directory"), **args)
except GhpError as e:
parser.error(e.message)
if __name__ == '__main__':
main()