-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcore.py
484 lines (400 loc) · 14.9 KB
/
core.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
__all__ = [
"GICCommitDesc"
, "plan"
, "load_context"
]
from common import CommitDesc
from actions import *
from os.path import abspath
import sys
if sys.version_info[0] != 2:
def execfile(filename, globals = None, locals = None):
f = open(filename, "rb")
content = f.read()
f.close()
obj = compile(content, filename, "exec")
exec(content, globals, locals)
class GICCommitDesc(CommitDesc):
__slots__ = [
# A commit is used if at least one of its descendants is copied.
"used",
"skipped",
"processed",
"cloned_sha"
]
def __init__(self, *args):
super(GICCommitDesc, self).__init__(*args)
self.cloned_sha = None
self.processed = False
self.skipped = False
self.used = False
def is_subtree(c, acceptable = 4):
""" Heuristically detect a subtree merge.
@acceptable:
if the parent of @c have N files with same content but different names,
the Git can be confused about name correspondence:
E.g., let sys/time.h == time.h, then
sys/time.h -> prefix/time.h
time.h -> prefix/sys/time.h
is a correct Diff record (for version control system). While the assumption
of the heuristic is violated.
@acceptable is a threshold to handle such cases.
"""
p1 = c.parents[1]
d = p1.diff(c)
# suggest a prefix
for probe in d:
if probe.renamed:
break
else:
return None
if probe.b_path.endswith(probe.a_path):
prefix = probe.b_path[:-len(probe.a_path)]
else:
return None
# Were all parent files renamed using same prefix?
for po in p1.tree.traverse():
if po.type != "blob":
continue
new_path = prefix + po.path
for check in d:
if check.renamed:
if check.rename_from == po.path \
and check.rename_to == new_path:
break
elif check.new_file:
if check.b_path == new_path:
break
else:
# no such difference
if not acceptable:
return None
else:
acceptable -= 1
# print(po.path + " ACC")
continue
# print(po.path + " OK")
return prefix
def orphan(n):
return "__orphan__%d" % n
def plan_heads(c, dst_repo_path):
for h in c.heads:
if h.path.startswith("refs/heads/"):
CreateHead(
path = dst_repo_path,
name = h.name
)
elif h.path.startswith("refs/tags/"):
CreateTag(
path = dst_repo_path,
name = h.name
)
def get_actual_parents(orig_parent, sha2commit):
""" A parent of a merge commit could be skipped. But a replacement have to
be provided. This function looks it up. As a merge commit could be skipped too,
one parent could be replaced with several parents.
"""
if not sha2commit[orig_parent.hexsha].skipped:
return [orig_parent]
ret = []
# Parent order is reversed to preserve main stream (zero index) commit
# priority in course of depth-first graph traversal.
stack = list(reversed(orig_parent.parents))
while stack:
p = stack.pop()
if sha2commit[p.hexsha].skipped:
stack.extend(reversed(p.parents))
else:
ret.append(p)
return ret
CLONED_REPO_NAME = "__cloned__"
def plan(repo, sha2commit, dstRepoPath,
main_stream_bits = 0,
breaks = None,
skips = None,
insertions = None
):
"""
insertions:
List of commits to insert. Each insertion is described by a tuple of
an existing commit SHA1 and inserted commit content:
("...SHA1...", content)
Supported content:
- name of the patch file in `git am` compatible format.
The commit is inserted _before_ the commit identified by SHA1.
Multiple insertions for one SHA1 are supported. The order is preserved.
A commit can be inserted before skipped one.
If a main stream is set then insertions before non-main stream commits are
ignored.
XXX: insertion before a merge commit is buggy, except for that merge is
skipped.
"""
breaks = set() if breaks is None else set(breaks)
skips = set() if skips is None else set(skips)
# Group insertions by SHA1 for fastest search. Order of insertions for one
# SHA1 must be preserved.
insertion_table = {}
if insertions:
for sha1, insertion in insertions:
insertion_table.setdefault(sha1, []).append(insertion)
srcRepoPath = repo.working_dir
queue = sorted(sha2commit.values(), key = lambda c : c.num)
RemoveDirectory(path = dstRepoPath)
ProvideDirectory(path = dstRepoPath)
InitRepo(path = dstRepoPath)
AddRemote(
path = dstRepoPath,
name = CLONED_REPO_NAME,
address = srcRepoPath
)
FetchRemote(
path = dstRepoPath,
name = CLONED_REPO_NAME,
tags = True
)
iqueue = iter(queue)
orphan_counter = 0
prev_c = None
for c in iqueue:
c.processed = True
c_sha = c.sha # attribute getting optimization
if main_stream_bits and not (c.roots & main_stream_bits):
# this commit will be used as is
c.cloned_sha = c_sha
# TODO: heads and tags of such commits
continue
m = repo.commit(c_sha)
if prev_c is not None:
if not c.parents:
CheckoutOrphan(
name = orphan(orphan_counter),
path = dstRepoPath
)
orphan_counter += 1
at_least_one_in_trunk = False
else:
# get real parents order
main_stream = m.parents[0]
main_stream_sha = main_stream.hexsha
if main_stream_sha != prev_c.sha:
# main stream parent of the commit could be skipped...
aps = get_actual_parents(main_stream, sha2commit)
actual_main_stream_parent_sha = aps[0].hexsha
if actual_main_stream_parent_sha != main_stream_sha:
print("Main stream parent %s of %s is not available. "
"Its ancestor %s will become a new trunk "
"instead." % (
main_stream_sha, c_sha,
actual_main_stream_parent_sha
)
)
# jump to main stream commit
CheckoutCloned(
path = dstRepoPath,
commit_sha = actual_main_stream_parent_sha
)
at_least_one_in_trunk = False
# `pop` is used to detect unused insert positions
insertions = insertion_table.pop(c_sha, [])
for i in insertions:
abs_i = abspath(i)
ApplyPatchFile(
path = dstRepoPath,
patch_name = abs_i
)
if c_sha in skips:
skipping = True
skips.remove(c_sha) # Detection of unused skips
else:
skipping = False
if not skipping and len(c.parents) > 1:
# Handle merge commit parent skipping.
extra_parents = []
for p in m.parents[1:]:
aps = get_actual_parents(p, sha2commit)
if aps:
if aps[0] != p:
print("Parent %s of %s is skipped and will be "
"substituted with %s" % (
p.hexsha, c_sha,
", ".join(pp.hexsha for pp in aps)
)
)
else:
print("Parent %s of %s is skipped and cannot be "
"replaced" % (p.hexsha, c_sha)
)
extra_parents.extend(aps)
if not extra_parents:
print("Merge commit %s is skipping because its parents are "
"skipped leaving it with only one parent" % c_sha
)
skipping = True
if skipping:
c.skipped = True
for h in c.heads:
if h.path.startswith("refs/heads/"):
if at_least_one_in_trunk:
# Skipping a commits moves its head on first
# non-skipped ancestor.
CreateHead(
path = dstRepoPath,
name = h.name
)
else:
print("Head '%s' will be skipped because no commits "
"of this trunk are copied." % h.name
)
elif h.path.startswith("refs/tags/"):
print("Tag '%s' will be skipped with its commit!" % h.name)
else:
c.used = True
at_least_one_in_trunk = True
if len(c.parents) > 1:
subtree_prefix = None if len(c.parents) != 2 else is_subtree(m)
SetAuthor(
author_name = m.author.name,
author_email = m.author.email,
authored_date = m.authored_date,
author_tz_offset = m.author_tz_offset
)
SetCommitter(
committer_name = m.committer.name,
committer_email = m.committer.email,
committed_date = m.committed_date,
committer_tz_offset = m.committer_tz_offset
)
if subtree_prefix is None:
MergeCloned(
path = dstRepoPath,
commit_sha = c_sha,
message = m.message,
# original parents order is significant
extra_parents = [
p.hexsha for p in extra_parents
]
)
else:
SubtreeMerge(
path = dstRepoPath,
commit_sha = c_sha,
message = m.message,
parent_sha = extra_parents[0].hexsha,
prefix = subtree_prefix
)
ResetAuthor()
ResetCommitter()
else:
# Note that author is set by cherry-pick
SetCommitter(
committer_name = m.committer.name,
committer_email = m.committer.email,
committed_date = m.committed_date,
committer_tz_offset = m.committer_tz_offset
)
CherryPick(
path = dstRepoPath,
commit_sha = c_sha,
message = m.message
)
ResetCommitter()
plan_heads(c, dstRepoPath)
ctx = get_context()
if c_sha in breaks:
breaks.remove(c_sha) # Detection of unused breaks
if at_least_one_in_trunk:
# Note that SHA1 of the cloned commit is unknown now.
# Hence, use its message to identify it for a user.
try:
msg = m.message.split("\n")[0]
except IndexError:
msg = " after empty message commit %s" % c_sha
else:
msg = " after '%s'" % msg
reason = "Interrupting%s as requested..." % msg
if ctx.from_cache:
ApplyCacheOrInterrupt(
path = dstRepoPath,
commit_sha = c_sha,
reason = reason
)
else:
if ctx.cache_path:
ApplyCache(
path = dstRepoPath,
commit_sha = c_sha,
)
Interrupt(reason = reason)
# Update committer name, e-mail and date after user actions.
SetCommitter(
committer_name = m.committer.name,
committer_email = m.committer.email,
committed_date = m.committed_date,
committer_tz_offset = m.committer_tz_offset
)
ContinueCommitting(
path = dstRepoPath,
commit_sha = c_sha
)
ResetCommitter()
if ctx.cache_path:
UpdateCache(path = dstRepoPath, commit_sha = c_sha)
else:
print("Cannot interrupt on '%s' because no commits "
"of this trunk are copied." % c_sha
)
prev_c = c
# delete temporary branch names
for o in range(0, orphan_counter):
DeleteHead(path = dstRepoPath, name = orphan(o))
# propagate `used` flag
for c in queue:
if c.skipped:
continue
if not c.used:
continue
stack = list(c.parents)
while stack:
# a - ancestor
a = stack.pop()
if a.skipped:
continue # `skipped` commit do not propagate `used` flag
if a.used:
continue # `used` flag was already propagated
a.used = True
stack.append(reversed(a.parents))
# delete tags of non-cloned commits
for tag in repo.references:
if not tag.path.startswith("refs/tags/"):
continue
# Note that, no commit descriptors could be created for a trunk.
c = sha2commit.get(tag.commit.hexsha, None)
if c is None or not c.used:
DeleteTag(path = dstRepoPath, name = tag.name)
CheckoutCloned(
path = dstRepoPath,
commit_sha = repo.head.commit.hexsha
)
RemoveRemote(path = dstRepoPath, name = CLONED_REPO_NAME)
CollectGarbage(path = dstRepoPath)
for c in sha2commit.values():
if not c.processed:
print("Commit %s was not cloned!" % str(c.sha))
if breaks:
raise RuntimeError("Unused break(s): " + ", ".join(breaks))
if skips:
raise RuntimeError("Unused skip(s): " + ", ".join(skips))
if insertion_table:
raise RuntimeError("Unused insertion(s): " + ", ".join(
("'%s'" % val + " before " + sha1)
for (sha1, vals) in insertion_table.items()
for val in vals
))
def load_context(file_name):
loaded = {}
execfile(file_name, globals(), loaded)
for ctx in loaded.values():
if isinstance(ctx, GitContext):
return ctx
# no saved context found among loaded objects
raise RuntimeError("No context found in file '%s'" % file_name)