-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathactions.py
1058 lines (847 loc) · 29.1 KB
/
actions.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
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
__all__ = [
"Action"
, "Interrupt"
, "FSAction"
, "RemoveDirectory"
, "ProvideDirectory"
, "RemoveFile"
, "SetCommitter"
, "ResetCommitter"
, "SetAuthor"
, "ResetAuthor"
, "GitAction"
, "InitRepo"
, "RemoteAction"
, "AddRemote"
, "RemoveRemote"
, "FetchRemote"
, "CheckoutCloned"
, "CheckoutOrphan"
, "MergeCloned"
, "ContinueCommitting"
, "SubtreeMerge"
, "CherryPick"
, "CreateHead"
, "DeleteHead"
, "CreateTag"
, "DeleteTag"
, "CollectGarbage"
, "PatchFileAction"
, "ApplyPatchFile"
, "HEAD2PatchFile"
, "UpdateCache"
, "ApplyCache"
, "ApplyCacheOrInterrupt"
, "ActionContext"
, "GitContext"
, "LOG_STANDARD"
, "switch_context"
, "get_context"
]
from shutil import rmtree
from os import (
rename,
walk,
environ,
unlink,
listdir,
getcwd,
chdir,
makedirs
)
from os.path import (
basename,
isdir,
isfile,
join,
exists
)
from time import (
time,
timezone,
mktime,
strptime,
gmtime,
strftime
)
from traceback import print_exc
import sys
from common import (
sloted,
launch,
LaunchFailed
)
from six import (
b,
u
)
from re import compile
from itertools import (
chain,
count
)
current_context = None
def switch_context(ctx):
global current_context
if current_context is ctx:
raise RuntimeError("Same action context")
ret = current_context
current_context = ctx
return ret
def get_context():
return current_context
def csv_excape(cell):
if b";" in cell:
# Note that quotes (") inside quoted cell seems to be supported
return b'"' + cell + b'"'
else:
return cell
class CSVLogFormatter(sloted):
__slots__ = ["writer", "kind"]
def write(self, data):
timestamp = b(dt(time(), timezone))
# attribute lookup optimization
kind = self.kind
write = self.writer.write
for ll in data.split(b"\r"):
# Two different line separators one by one is one line separator.
stripped = ll.strip(b"\n")
liter = iter(stripped.split(b"\n"))
# This forward scan logic omits last empty line. I.e. if last byte
# of data is the line separator.
prev_line = csv_excape(next(liter))
for line in liter:
write(timestamp + b";" + kind + b";" + prev_line + b";\n")
prev_line = csv_excape(line)
else:
if prev_line:
write(timestamp + b";" + kind + b";" + prev_line + b";\n")
# Just not a `str`
LOG_STANDARD = object()
# Python 2 & 3 compatibility
raw_stdout = getattr(sys.stdout, 'buffer', sys.stdout)
raw_stderr = getattr(sys.stderr, 'buffer', sys.stderr)
class ActionContext(sloted):
__slots__ = ["_actions", "current_action", "interrupted", "_doing",
"_extra_actions", "_out_log", "_err_log", "_log_io"]
def __init__(self,
current_action = -1,
interrupted = False,
log = LOG_STANDARD,
** kw
):
super(ActionContext, self).__init__(
current_action = current_action,
interrupted = interrupted,
**kw
)
self._actions = []
self._extra_actions = []
self._doing = False
self._log_io = None
# properties is not compatible with slots
self.log = log
@property
def log(self):
if self._out_log is raw_stdout:
return LOG_STANDARD
else:
return self._log_io.name
@log.setter
def log(self, value):
if self._log_io:
self._log_io.close()
self._log_io = None
if value is LOG_STANDARD:
self._out_log, self._err_log = raw_stdout, raw_stderr
else:
self._log_io = writer = open(value, "ab+")
self._out_log = CSVLogFormatter(writer = writer, kind = b"stdout")
self._err_log = CSVLogFormatter(writer = writer, kind = b"stderr")
def interrupt(self):
self.interrupted = True
def do(self, limit = None):
""" Perform actions until all actions finished or limit is exceeded or
exception is raised.
limit
Set maximum number of actions to perform. None means unlimited.
"""
actions = self._actions
extra_actions = self._extra_actions
ca = self.current_action
if ca < 0: # start
if limit is None:
i = enumerate(actions)
else:
i = enumerate(actions[:limit])
elif ca >= len(actions): # all actions were done
print("Nothing to do")
return True
# continue the work
elif limit is None:
i = enumerate(actions[ca:], ca)
else:
i = enumerate(actions[ca:ca + limit], ca)
self.interrupted = False
self._doing = True
ret = True
while not self.interrupted:
# This construction allows to switch i from within loop body. While
# the same is wrong for constructions like `for idx, a in i:`.
try:
idx, a = next(i)
except StopIteration:
break
try:
a()
except:
print("Failed on %s" % a)
print_exc(file = sys.stdout)
ret = False
break
if extra_actions:
next_idx = idx + 1
while extra_actions:
a = extra_actions.pop()
actions.insert(next_idx, a)
if limit is None:
i = enumerate(actions[next_idx:], next_idx)
else:
rest = limit - (next_idx - ca)
i = enumerate(actions[next_idx:next_idx + rest], next_idx)
self._doing = False
self.current_action = idx + 1
return ret
@property
def finished(self):
return self.current_action >= len(self._actions)
def __dfs_children__(self):
return list(self._actions)
def __gen_code__(self, g):
self.gen_by_slots(g, log = self.log)
g.line("switch_context(" + g.nameof(self) + ")")
g.line()
g.write("actions = ")
g.pprint(self._actions)
g.line()
g.line()
g.line("for a in actions:")
g.line(" a.q()")
cache_file_re = compile("[A-Fa-f0-9]{40}.*")
class GitContext(ActionContext):
__slots__ = ["_sha2commit", "src_repo_path", "_origin2cloned",
"git_command", "_git_version", "cache_path", "_cache",
"from_cache"]
def __init__(self,
git_command = "git",
cache_path = None,
from_cache = False,
**kw
):
super(GitContext, self).__init__(
git_command = git_command,
cache_path = cache_path,
from_cache = from_cache,
**kw
)
self._sha2commit = {}
self._origin2cloned = {}
# get version of git
_stdout, _stderr = launch([self.git_command, "--version"],
epfx = "Cannot get version of git"
)
_, _, version = _stdout.split(b" ")[:3]
self._git_version = tuple(int(v) for v in version.split(b".")[:3])
# walk cache_path and fill _cahce
self._cache = cache = {}
# refer the cache by absolute path
cwd = getcwd()
cache_path = self.cache_path
if not cache_path.startswith(cwd):
cache_path = join(cwd, cache_path)
self.cache_path = cache_path
backup_dir = join(cache_path, "backup")
if cache_path:
for root, _, files in walk(cache_path):
if root.startswith(backup_dir):
continue
for f in files:
# First 40 characters of the a file name must be the SHA1
# of the corresponding commit.
if not cache_file_re.match(f):
continue
key = b(f[:40].lower())
cached_file = join(root, f)
if key in cache:
print("Multiple entries for %s found in the "
"cache:\n '%s'\n '%s'" % (
u(key), cached_file, cache[key]
)
)
continue
cache[key] = cached_file
print("cache = " + str(self._cache)
.replace(",", ",\n ")
.replace("{", "{\n ")
.replace("}", "\n}")
)
def __backup_cloned(self):
origin2cloned = {}
for sha, c in self._sha2commit.items():
cloned_sha = c.cloned_sha
if cloned_sha is None:
continue
origin2cloned[sha] = cloned_sha
self._origin2cloned = origin2cloned
def plan_set_commiter_by_env(self):
"Inserts SetCommitter action getting its parameters from environment."
committed_date, committer_tz_offset = gds2so(
environ["GIT_COMMITTER_DATE"]
)
SetCommitter(
committer_name = environ["GIT_COMMITTER_NAME"],
committer_email = environ["GIT_COMMITTER_EMAIL"],
committed_date = committed_date,
committer_tz_offset = committer_tz_offset
)
def restore_cloned(self):
sha2commit = self._sha2commit
for sha, cloned_sha in self._origin2cloned.items():
sha2commit[sha].cloned_sha = cloned_sha
def __gen_code__(self, g):
ActionContext.__gen_code__(self, g)
g.line()
self.__backup_cloned()
g.write(g.nameof(self) + "._origin2cloned = ")
g.pprint(self._origin2cloned)
g.line()
def dt(ts, off):
dt = gmtime(ts - off)
ret = strftime("%Y-%m-%d %H:%M:%S", dt)
if off <= 0:
ret = ret + ("+%02d%02d" % (off / -3600, (off / -60) % 60))
else:
ret = ret + ("-%02d%02d" % (off / 3600, (off / 60) % 60))
return ret
def gds2so(gds):
"Git Date String To Seconds since epoch and time zone Offset"
offset_str = gds[-5:]
hours = int(offset_str[1:3])
minutes = int(offset_str[3:])
# sign is reverted
sign = -1 if offset_str[0] == "+" else 1
offset = sign * (hours * 3600 + minutes * 60)
datetime_str = gds[:-5]
dt_val = strptime(datetime_str, "%Y-%m-%d %H:%M:%S")
ts = mktime(dt_val)
# assert gds == dt(ts, offset)
return (ts, offset)
class Action(sloted):
__slots__ = ["_ctx"]
def __init__(self, queue = True, ** kw):
"""
@queue: auto add the action to queue of the current action context
"""
super(Action, self).__init__(**kw)
self._ctx = None
if queue:
self.q()
def queue(self):
if self._ctx is not None:
raise RuntimeError("Already in the action context")
global current_context
if current_context is None:
raise RuntimeError("No action context set")
if current_context._doing:
current_context._extra_actions.append(self)
else:
current_context._actions.append(self)
self._ctx = current_context
q = queue
def _out(self, data):
self._ctx._out_log.write(data)
def _err(self, data):
self._ctx._err_log.write(data)
def __call__(self):
raise NotImplementedError()
def __str__(self):
return type(self).__name__
def __dfs_children__(self):
return []
def __gen_code__(self, g):
self.gen_by_slots(g, queue = False)
class Interrupt(Action):
__slots__ = ["reason"]
def __call__(self):
print(self.reason)
self._ctx.interrupt()
class FSAction(Action):
__slots__ = ["path"]
class RemoveDirectory(FSAction):
def __call__(self):
if exists(self.path):
rmtree(self.path)
class ProvideDirectory(FSAction):
def __call__(self):
makedirs(self.path)
class RemoveFile(FSAction):
def __call__(self):
if exists(self.path):
unlink(self.path)
class SetCommitter(Action):
__slots__ = ["committer_name", "committer_email", "committed_date",
"committer_tz_offset"]
def __call__(self):
environ["GIT_COMMITTER_NAME"] = self.committer_name
environ["GIT_COMMITTER_EMAIL"] = self.committer_email
environ["GIT_COMMITTER_DATE"] = dt(self.committed_date,
self.committer_tz_offset
)
class ResetCommitter(Action):
def __call__(self):
try:
del environ["GIT_COMMITTER_NAME"]
del environ["GIT_COMMITTER_EMAIL"]
del environ["GIT_COMMITTER_DATE"]
except KeyError:
# If process was interrupted the env. var. values are lost.
pass
class SetAuthor(Action):
__slots__ = ["author_name", "author_email", "authored_date",
"author_tz_offset"]
def __call__(self):
environ["GIT_AUTHOR_NAME"] = self.author_name
environ["GIT_AUTHOR_EMAIL"] = self.author_email
environ["GIT_AUTHOR_DATE"] = dt(self.authored_date,
self.author_tz_offset
)
class ResetAuthor(Action):
def __call__(self):
try:
del environ["GIT_AUTHOR_NAME"]
del environ["GIT_AUTHOR_EMAIL"]
del environ["GIT_AUTHOR_DATE"]
except KeyError:
pass
class GitAction(Action):
__slots__ = ["path", "_stdout", "_stderr"]
def launch(self, *cmd_args):
cwd = getcwd()
if cwd != self.path:
chdir(self.path)
out, err = launch(cmd_args)
if out:
self._out(out)
if err:
self._err(err)
def git(self, *cmd_args):
self.launch(*((self._ctx.git_command,) + cmd_args))
def git2(self, *cmd_args):
cwd = getcwd()
if cwd != self.path:
chdir(self.path)
self._stdout, self._stderr = launch(
(self._ctx.git_command,) + cmd_args
)
def get_conflicts(self):
self.git2("diff", "--name-only", "--diff-filter=U")
# get conflicts skipping empty lines
conflicts = [ n for n in self._stdout.strip().split(b"\n") if n ]
return conflicts
class InitRepo(GitAction):
def __call__(self):
self.git("init")
class RemoteAction(GitAction):
__slots__ = ["name"]
class AddRemote(RemoteAction):
__slots__ = ["address"]
def __call__(self):
self.git("remote", "add", self.name, self.address)
class RemoveRemote(RemoteAction):
def __call__(self):
self.git("remote", "remove", self.name)
class FetchRemote(RemoteAction):
__slots__ = [
"tags"
]
def __call__(self):
self.git("fetch", "--%stags" % ("" if self.tags else "no-"), self.name)
class CheckoutCloned(GitAction):
__slots__ = ["commit_sha"]
def __call__(self):
commit = self._ctx._sha2commit[self.commit_sha]
self.git("checkout", "-f", commit.cloned_sha)
class CheckoutOrphan(GitAction):
__slots__ = [ "name" ]
def __call__(self):
self.git("checkout", "--orphan", self.name)
self.git("reset")
# clear the git
for the_file in listdir(self.path):
if the_file == ".git":
continue
file_path = join(self.path, the_file)
if isfile(file_path):
unlink(file_path)
elif isdir(file_path):
rmtree(file_path)
MSG_MNG_CNFLCT_BY_SFL = """\
Try to manage it by self. Non-resolved conflicts will be taken from the \
original repository automatically after continuing. \
"""
class MergeCloned(GitAction):
__slots__ = ["commit_sha", "message", "extra_parents"]
def __call__(self):
ctx = self._ctx
sha2commit = self._ctx._sha2commit
commit = sha2commit[self.commit_sha]
message = self.message
extra_parents = [
sha2commit[p] for p in self.extra_parents
]
try:
self.git("merge",
"--no-ff",
"-m", message,
*[ p.cloned_sha for p in extra_parents ]
)
except LaunchFailed as e:
# conflicts?
conflicts = self.get_conflicts()
if not conflicts:
# there is something else...
raise e
confl_str = (
("is merge conflict with '%s'" % conflicts[0])
if len (conflicts) == 1
else "are merge conflicts"
)
reason = ("There %s. Interrupting... %s" % (
confl_str, MSG_MNG_CNFLCT_BY_SFL
))
if ctx.from_cache:
ApplyCacheOrInterrupt(
path = self.path,
commit_sha = commit.sha,
reason = reason
)
else:
# try to resolve conflicts using the cache
if ctx.cache_path:
ApplyCache(path = self.path, commit_sha = commit.sha)
# let user to resolve conflict by self
Interrupt(reason = reason)
# preserve original committer information
ctx.plan_set_commiter_by_env()
ContinueCommitting(
path = self.path,
commit_sha = self.commit_sha
)
ResetCommitter()
if ctx.cache_path:
UpdateCache(path = self.path, commit_sha = commit.sha)
return
self.git2("rev-parse", "HEAD")
commit.cloned_sha = self._stdout.split(b"\n")[0]
class ContinueCommitting(GitAction):
__slots__ = ["commit_sha"]
def __call__(self):
sha2commit = self._ctx._sha2commit
commit = sha2commit[self.commit_sha]
# The behavior differs for merge commit completion and existing commit
# overwriting.
# Determine kind of commit to continue by presence of MERGE_MSG file.
# Note that cherry picked commits with conflicts do store original
# message into this file too.
merge_msg_path = join(self.path, ".git", "MERGE_MSG")
merging = isfile(merge_msg_path)
if merging:
# handle conflicts
conflicts = self.get_conflicts()
# get changes for unresolved conflicts from original history
for c in conflicts:
self.git("checkout", commit.sha, c.decode("utf-8"))
self.git("commit", "--allow-empty", "--no-edit")
else:
# All changes had been added to index and committed before. Only
# ensure that committer name, e-mail and date are correct.
self.git("commit", "--allow-empty", "--no-edit", "--amend")
self.git2("rev-parse", "HEAD")
commit.cloned_sha = self._stdout.split(b"\n")[0]
class SubtreeMerge(GitAction):
__slots__ = ["commit_sha", "message", "parent_sha", "prefix"]
def __call__(self):
ctx = self._ctx
sha2commit = ctx._sha2commit
commit = sha2commit[self.commit_sha]
message = self.message
prefix = self.prefix
parent = sha2commit[self.parent_sha]
if ctx._git_version >= (2, 9, 0):
self.git("merge", "-s", "ours", "--no-commit",
"--allow-unrelated-histories", parent.cloned_sha
)
else:
self.git("merge", "-s", "ours", "--no-commit", parent.cloned_sha)
if exists(".gic"):
rmtree(".gic")
makedirs(".gic")
self.git("read-tree",
"--prefix", ".gic/",
"-u",
parent.cloned_sha
)
if exists(prefix):
rmtree(prefix)
makedirs(prefix)
for root, dirs, files in walk(".gic"):
for d in dirs:
target_dir = join(prefix + root[5:], d)
if not exists(target_dir):
makedirs(target_dir)
for f in files:
self.git("mv", "-f",
join(root, f),
join(prefix + root[5:], f)
)
rmtree(".gic")
self.git("commit", "-m", message)
self.git2("rev-parse", "HEAD")
commit.cloned_sha = self._stdout.split(b"\n")[0]
class CherryPick(GitAction):
__slots__ = ["commit_sha", "message"]
def __call__(self):
ctx = self._ctx
c = ctx._sha2commit[self.commit_sha]
try:
self.git("cherry-pick", c.sha)
except LaunchFailed as e:
if b"--allow-empty" in e._stderr:
self.git("commit", "--allow-empty", "-m", self.message)
else:
# conflicts?
conflicts = self.get_conflicts()
if not conflicts:
# there is something else...
raise e
confl_str = (
("is conflict with '%s'" % conflicts[0])
if len (conflicts) == 1
else "are conflicts"
)
reason = ("There %s in course of cherry picking. "
"Interrupting... %s" % (confl_str, MSG_MNG_CNFLCT_BY_SFL))
if ctx.from_cache:
ApplyCacheOrInterrupt(
path = self.path,
commit_sha = c.sha,
reason = reason
)
else:
# try to resolve conflicts using the cache
if ctx.cache_path:
ApplyCache(path = self.path, commit_sha = c.sha)
# let user to resolve conflict by self
Interrupt(reason = reason)
# preserve original committer information
ctx.plan_set_commiter_by_env()
ContinueCommitting(
path = self.path,
commit_sha = self.commit_sha
)
ResetCommitter()
if ctx.cache_path:
UpdateCache(path = self.path, commit_sha = c.sha)
return
self.git2("rev-parse", "HEAD")
c.cloned_sha = self._stdout.split(b"\n")[0]
class CreateHead(GitAction):
__slots__ = ["name"]
def __call__(self):
self.git("branch", "-f", self.name)
class DeleteHead(GitAction):
__slots__ = ["name"]
def __call__(self):
self.git("branch", "-f", "-d", self.name)
class CreateTag(GitAction):
__slots__ = ["name"]
# TODO: tag message
def __call__(self):
self.git("tag", "-f", self.name)
class DeleteTag(GitAction):
__slots__ = ["name"]
def __call__(self):
self.git("tag", "-d", self.name)
class CollectGarbage(GitAction):
def __call__(self):
self.git("gc", "--aggressive", "--prune=all")
class PatchFileAction(GitAction):
__slots__ = ["patch_name"]
class ApplyPatchFile(PatchFileAction):
def __call__(self):
patch_name = self.patch_name
try:
self.git("am", "--committer-date-is-author-date", patch_name)
except LaunchFailed:
self.git("am", "--abort")
Interrupt(
reason = "Failed to apply the patch form file '%s'" % patch_name
)
class HEAD2PatchFile(PatchFileAction):
def __call__(self):
patch_name = self.patch_name
self.git2("format-patch", "--stdout", "HEAD~1")
f = open(patch_name, "wb")
f.write(self._stdout)
f.close()
class UpdateCache(GitAction):
__slots__ = ["commit_sha"]
def __call__(self):
self.git2("format-patch",
"--stdout",
"-k", # --keep-subject, avoids [PATCH] prefix
"--binary", # TODO: test me
"HEAD~1"
)
patch = self._stdout
ctx = self._ctx
cache = ctx._cache
cache_path = ctx.cache_path
sha = self.commit_sha
cached = cache.get(sha, None)
if cached is None:
write_to = join(cache_path, sha + ".patch")
print("Caching user changes to " + write_to)
else:
f = open(cached, "rb")
cached_patch = f.read()
f.close()
# The first line contains SHA1 which is always different.
if cached_patch.split(b"\n")[1:] == patch.split(b"\n")[1:]:
return
# provide directory for current patch version backup
backup_dir = join(cache_path, "backup")
for i in count(0):
if exists(backup_dir):
if isdir(backup_dir):
break
else:
makedirs(backup_dir)
break
backup_dir = join(cache_path, "backup" + str(i))
# provide name for the backup file
backup_name = basename(cached)
backup_path = join(backup_dir, backup_name)
for i in count(0):
if not exists(backup_path):
break
backup_path = join(backup_dir, "%s-%d" % (backup_name, i))
print("Backing up current cache to " + backup_path)
rename(cached, backup_path)
write_to = cached
print("Updating user changes in " + write_to)
f = open(write_to, "wb")
f.write(patch)
f.close()
re_commit_message = compile(b"(Subject: *)(\[PATCH\] *)?(.*)")
class ApplyCache(GitAction):
__slots__ = ["commit_sha", "_changed_files", "_deleted_files",
"_created_files"]
def __call__(self):
ctx = self._ctx
cache = ctx._cache
sha = self.commit_sha
if sha not in cache:
return
patch_file_name = cache[sha]
print("Applying changes from " + patch_file_name)
# analyze the patch and prepare working directory to patching
p = open(patch_file_name, "rb")
liter = iter(list(p.readlines()))
p.close()
msg = b""
for l in liter:
minfo = re_commit_message.match(l)
if minfo:
msg += minfo.group(3)
break
else:
raise ValueError("Incorrect patch file: no commit message.")
msg_lines = []
for l in liter:
if l.startswith(b"diff --git ") or l.startswith(b"---"):
msg += b"".join(msg_lines[:-1])
break
msg_lines.append(l)
else:
raise ValueError(
"Incorrect patch file: commit message terminator is not found."
)
changed_files = set()
created_files = set()
deleted_files = set()
for l in liter:
if l.startswith(b"--- a/"):
file_name = l[6:].strip(b"\n\r")
l = next(liter)
if l.startswith(b"--- /dev/null"):
deleted_files.add(file_name)
else:
changed_files.add(file_name)
elif l.startswith(b"--- /dev/null"):
l = next(liter)
created_files.add(l[6:].strip(b"\n\r"))
# for n in ["changed_files", "created_files", "deleted_files"]:
# print(n + " = " + str(locals()[n])
# .replace("set([", "set([\n ")
# .replace("'])", "'\n])")
# .replace("', '", "',\n '")
# )