/
__init__.py
1220 lines (1037 loc) · 41.5 KB
/
__init__.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
import contextlib
import fcntl
import io
import logging
import os
import shutil
import subprocess
import tempfile
import traceback
import transaction
import weakref
log = logging.getLogger(__name__)
class AcidFS(object):
"""
An instance of `AcidFS` exposes a transactional filesystem view of a `Git`
repository. Instances of `AcidFS` are not threadsafe and should not be
shared across threads, greenlets, etc.
**Paths**
Many methods take a `path` as an argument. All paths use forward slash `/`
as a separator, regardless of the path separator of the
underlying operating system. The path `/` represents the root folder of
the repository. Paths may be relative or absolute: paths beginning with a
`/` are absolute with respect to the repository root, paths not beginning
with a `/` are relative to the current working directory. The current
working directory always starts at the root of the repository. The current
working directory can be changed using the :meth:`chdir` and
:meth:`cd` methods.
**Constructor Arguments**
``repo``
The path to the repository in the real, local filesystem.
``head``
The name of a branch to use as the head for this transaction. Changes
made using this instance will be merged to the given head. The default,
if omitted, is to use the repository's current head.
``create``
If there is not a Git repository in the indicated directory, should one
be created? The default is `True`.
``bare``
If the Git repository is to be created, create it as a bare repository.
If the repository is already created or `create` is False, this argument
has no effect.
``user_name``
If the Git repository is to be created, set the user name for the
repository to this value. This is the same as creating the repository
and running `git config user.name "<user_name>"`.
``user_email``
If the Git repository is to be created, set the user email for the
repository to this value. This is the same as creating the repository
and running `git config user.email "<user_email>"`.
``name``
Name to be used as a sort key when ordering the various databases
(datamanagers in the parlance of the transaction package) during a
commit. It is exceedingly rare that you would need to use anything other
than the default, here.
``path_encoding``
Encode paths with this encoding. The default is `ascii`.
"""
session = None
_cwd = ()
def __init__(
self,
repo,
head="HEAD",
create=True,
bare=False,
user_name=None,
user_email=None,
name="AcidFS",
path_encoding="ascii",
):
wdpath = repo
dbpath = os.path.join(repo, ".git")
if not os.path.exists(dbpath):
wdpath = None
dbpath = repo
if create:
args = ["git", "init", repo]
if bare:
args.append("--bare")
else:
wdpath = repo
dbpath = os.path.join(repo, ".git")
_check_output(args)
if user_name:
args = ["git", "config", "user.name", user_name]
_check_output(args, cwd=dbpath)
if user_email:
args = ["git", "config", "user.email", user_email]
_check_output(args, cwd=dbpath)
args = ["git", "config", "core.quotepath", "false"]
_check_output(args, cwd=dbpath)
else:
raise ValueError("No database found in %s" % dbpath)
self.wd = wdpath
self.db = dbpath
self.head = head
self.name = name
self.path_encoding = path_encoding
def _session(self):
"""
Make sure we're in a session.
"""
if not self.session or self.session.closed:
self.session = _Session(
self.wd, self.db, self.head, self.name, self.path_encoding
)
return self.session
def _mkpath(self, path):
if path == ".":
parsed = []
else:
parsed = list(filter(None, path.split("/")))
if not path.startswith("/"):
parsed = list(self._cwd) + parsed
return parsed
def get_base(self):
"""
Returns the id of the commit that is the current base for the
transaction.
"""
session = self._session()
return session.prev_commit
def set_base(self, commit):
"""
Sets the base commit for the current transaction. The `commit`
argument may be the SHA1 of a commit or the name of a reference (eg.
branch or tag). The current transaction must be clean. If any changes
have been made in the transaction, a ConflictError will be raised.
"""
session = self._session()
session.set_base(commit)
def cwd(self):
"""
Returns the path to the current working directory in the repository.
"""
return "/" + "/".join(self._cwd)
def chdir(self, path):
"""
Change the current working directory in repository.
"""
session = self._session()
parsed = self._mkpath(path)
obj = session.find(parsed)
if not obj:
raise _NoSuchFileOrDirectory(path)
if not isinstance(obj, _TreeNode):
raise _NotADirectory(path)
self._cwd = parsed
@contextlib.contextmanager
def cd(self, path):
"""
A context manager that changes the current working directory only in
the scope of the 'with' context. Eg::
import acidfs
fs = acidfs.AcidFS('myrepo')
with fs.cd('some/folder'):
fs.open('a/file') # relative to /some/folder
fs.open('another/file') # relative to /
"""
prev = self._cwd
self.chdir(path)
yield
self._cwd = prev
def open(
self, path, mode="r", buffering=-1, encoding=None, errors=None, newline=None
):
"""
Open a file for reading or writing.
Implements the semantics of the `open` function in Python's `io module
<http://docs.python.org/library/io.html#io.open>`_, which is the
default implementation `in Python 3
<http://docs.python.org/py3k/library/functions.html#open>`_. Opening a
file in text mode will return a file-like object which reads or writes
unicode strings, while opening a file in binary mode will return a
file-like object which reads or writes raw bytes.
Because the underlying implementation uses a pipe to a `Git` plumbing
command, opening for update (read and write) is not supported, nor is
seeking.
"""
session = self._session()
parsed = self._mkpath(path)
if "b" in mode:
text = False
if "t" in mode:
raise ValueError("can't have text and binary mode at once")
else:
if not buffering:
raise ValueError("can't have unbuffered text I/O")
text = True
if "+" in mode:
raise ValueError("Read/write mode is not supported")
mode = mode.replace("b", "")
mode = mode.replace("t", "")
if mode == "a":
mode = "w"
append = True
else:
append = False
if mode == "x":
mode = "w"
exclusive = True
else:
exclusive = False
if buffering < 0:
buffer_size = io.DEFAULT_BUFFER_SIZE
line_buffering = False
elif buffering == 1:
buffer_size = io.DEFAULT_BUFFER_SIZE
line_buffering = True
else:
buffer_size = buffering
line_buffering = False
if mode == "r":
obj = session.find(parsed)
if not obj:
raise _NoSuchFileOrDirectory(path)
if isinstance(obj, _TreeNode):
raise _IsADirectory(path)
stream = obj.open()
if buffering:
stream = io.BufferedReader(stream, buffer_size)
if text:
stream = io.TextIOWrapper(
stream, encoding, errors, newline, line_buffering
)
return stream
elif mode == "w":
if not parsed:
raise _IsADirectory(path)
name = parsed[-1]
dirpath = parsed[:-1]
obj = session.find(dirpath)
if not obj:
raise _NoSuchFileOrDirectory(path)
if not isinstance(obj, _TreeNode):
raise _NotADirectory(path)
prev = obj.get(name)
if isinstance(prev, _TreeNode):
raise _IsADirectory(path)
if prev and exclusive:
raise _FileExists(path)
blob = obj.new_blob(name, prev)
if append and prev:
shutil.copyfileobj(prev.open(), blob)
if buffering:
blob = io.BufferedWriter(blob, buffer_size)
if text:
blob = io.TextIOWrapper(blob, encoding, errors, newline, line_buffering)
return blob
raise ValueError("Bad mode: %s" % mode)
def hash(self, path=""):
"""
Returns the sha1 hash of the object referred to by `path`. If `path` is
omitted the current working directory is used.
"""
session = self._session()
obj = session.find(self._mkpath(path))
if not obj:
raise _NoSuchFileOrDirectory(path)
return obj.hash()
def listdir(self, path=""):
"""
Return list of files in indicated directory. If `path` is omitted, the
current working directory is used.
"""
session = self._session()
obj = session.find(self._mkpath(path))
if not obj:
raise _NoSuchFileOrDirectory(path)
if not isinstance(obj, _TreeNode):
raise _NotADirectory(path)
return list(obj.contents.keys())
def mkdir(self, path):
"""
Create a new directory. The parent of the new directory must already
exist.
"""
session = self._session()
parsed = self._mkpath(path)
name = parsed[-1]
parent = session.find(parsed[:-1])
if not parent:
raise _NoSuchFileOrDirectory(path)
if not isinstance(parent, _TreeNode):
raise _NotADirectory(path)
if name in parent.contents:
raise _FileExists(path)
parent.new_tree(name)
def mkdirs(self, path):
"""
Create a new directory, including any ancestors which need to be created
in order to create the directory with the given `path`.
"""
session = self._session()
parsed = self._mkpath(path)
node = session.tree
for name in parsed:
next_node = node.get(name)
if not next_node:
next_node = node.new_tree(name)
elif not isinstance(next_node, _TreeNode):
raise _NotADirectory(path)
node = next_node
def rm(self, path):
"""
Remove a single file.
"""
session = self._session()
parsed = self._mkpath(path)
obj = session.find(parsed)
if not obj:
raise _NoSuchFileOrDirectory(path)
if isinstance(obj, _TreeNode):
raise _IsADirectory(path)
obj.parent.remove(obj.name)
def rmdir(self, path):
"""
Remove a single directory. The directory must be empty.
"""
session = self._session()
parsed = self._mkpath(path)
if not parsed:
raise ValueError("Can't remove root directory.")
obj = session.find(parsed)
if not obj:
raise _NoSuchFileOrDirectory(path)
if not isinstance(obj, _TreeNode):
raise _NotADirectory(path)
if not obj.empty():
raise _DirectoryNotEmpty(path)
obj.parent.remove(obj.name)
def rmtree(self, path):
"""
Remove a directory and any of its contents.
"""
session = self._session()
parsed = self._mkpath(path)
if not parsed:
raise ValueError("Can't remove root directory.")
obj = session.find(parsed)
if not obj:
raise _NoSuchFileOrDirectory(path)
if not isinstance(obj, _TreeNode):
raise _NotADirectory(path)
obj.parent.remove(obj.name)
def mv(self, src, dst):
"""
Move a file or directory from `src` path to `dst` path.
"""
session = self._session()
spath = self._mkpath(src)
if not spath:
raise _NoSuchFileOrDirectory(src)
sname = spath[-1]
sfolder = session.find(spath[:-1])
if not sfolder or sname not in sfolder:
raise _NoSuchFileOrDirectory(src)
dpath = self._mkpath(dst)
dobj = session.find(dpath)
if not dobj:
dname = dpath[-1]
dfolder = session.find(dpath[:-1])
if dfolder:
dfolder.set(dname, sfolder.remove(sname))
return
raise _NoSuchFileOrDirectory(dst)
if isinstance(dobj, _TreeNode):
dobj.set(sname, sfolder.remove(sname))
else:
dobj.parent.set(dobj.name, sfolder.remove(sname))
def exists(self, path):
"""
Returns boolean indicating whether a file or directory exists at the
given `path`.
"""
session = self._session()
return bool(session.find(self._mkpath(path)))
def isdir(self, path):
"""
Returns boolean indicating whether the given `path` is a directory.
"""
session = self._session()
return isinstance(session.find(self._mkpath(path)), _TreeNode)
def empty(self, path):
"""
Returns boolean indicating whether the directory indicated by `path` is
empty.
"""
session = self._session()
obj = session.find(self._mkpath(path))
if not obj:
raise _NoSuchFileOrDirectory(path)
if not isinstance(obj, _TreeNode):
raise _NotADirectory(path)
return obj.empty()
class ConflictError(Exception):
def __init__(self, msg="Unable to merge changes to repository."):
super(ConflictError, self).__init__(msg)
class _Session(object):
closed = False
lockfd = None
def __init__(self, wd, db, head, name, path_encoding):
self.wd = wd
self.db = db
self.name = name
self.path_encoding = path_encoding
self.lock_file = os.path.join(db, "acidfs.lock")
transaction.get().join(self)
curhead = open(os.path.join(db, "HEAD")).read().strip()[16:]
if head == curhead:
head = "HEAD"
if head == "HEAD":
self.headref = os.path.join(db, "refs", "heads", curhead)
else:
self.headref = os.path.join(db, "refs", "heads", head)
self.head = head
if os.path.exists(self.headref):
# Existing head, get head revision
self.prev_commit = _check_output(
["git", "rev-list", "--max-count=1", head], cwd=db
).strip()
tree = _check_output(
[
"git",
"rev-parse",
f"{self.prev_commit.decode('ascii')}^{{tree}}",
],
cwd=self.db,
).strip()
self.tree = _TreeNode.read(db, tree, path_encoding)
else:
# New head, no commits yet
self.tree = _TreeNode(db, path_encoding) # empty tree
self.prev_commit = None
def set_base(self, ref):
if self.tree.dirty:
raise ConflictError(
"Cannot set base when changes already made in transaction."
)
self.prev_commit = _check_output(
["git", "rev-list", "--max-count=1", ref], cwd=self.db
).strip()
self.tree = _TreeNode.read(self.db, self.prev_commit, self.path_encoding)
def find(self, path):
assert isinstance(path, (list, tuple))
return self.tree.find(path)
def abort(self, tx):
"""
Part of datamanager API.
"""
self.close()
def tpc_begin(self, tx):
"""
Part of datamanager API.
"""
def commit(self, tx):
"""
Part of datamanager API.
"""
def tpc_vote(self, tx):
"""
Part of datamanager API.
"""
if not self.tree.dirty:
# Nothing to do
return
# Write tree to db
tree_oid = self.tree.save()
if self.tree.committed_oid == tree_oid:
# Nothing actually changed
self.tree.dirty = False
return
if self.prev_commit:
parents = [self.prev_commit]
else:
parents = []
commit_oid = self.mkcommit(tx, tree_oid, parents)
# Acquire an exclusive (aka write) lock for merge.
self.acquire_lock()
# If this is initial commit, there's not really anything to merge
if not self.prev_commit:
# Make sure there haven't been other commits
if os.path.exists(self.headref):
# This was to be the initial commit, but somebody got to it
# first No idea how to try to resolve that one. Luckily it
# will be very rare.
raise ConflictError()
# New commit is new head
self.next_commit = commit_oid
return
# Find the merge base
current = _check_output(
["git", "rev-list", "--max-count=1", "HEAD"], cwd=self.db
).strip()
merge_base = _check_output(
["git", "merge-base", current, commit_oid], cwd=self.db
).strip()
# If the merge base is the current commit, it means there have been no
# intervening changes and we can just fast forward to the new commit.
# This is the most common case.
if merge_base == current:
self.next_commit = commit_oid
return
# Darn it, now we have to actually try to merge
self.merge(merge_base, current, tree_oid)
self.next_commit = self.mkcommit(
tx, self.tree.save(), [current, commit_oid], "Merge"
)
def tpc_finish(self, tx):
"""
Part of datamanager API.
"""
if not self.tree.dirty:
# Nothing to do
return
# Make our commit the new head
if self.head == "HEAD":
# Use git reset to update current head
args = ["git", "reset", self.next_commit]
if self.wd:
args.append("--hard")
cwd = self.wd
else:
args.append("--soft")
cwd = self.db
_check_output(args, cwd=cwd)
else:
# If not updating current head, just write the commit to the ref
# file directly.
reffile = os.path.join(self.db, "refs", "heads", self.head)
with open(reffile, "wb") as f:
f.write(self.next_commit)
f.write(b"\n")
self.close()
def tpc_abort(self, tx):
"""
Part of datamanager API.
"""
self.close()
def sortKey(self):
return self.name
def close(self):
self.closed = True
self.release_lock()
def acquire_lock(self):
assert not self.lockfd
self.lockfd = fd = os.open(self.lock_file, os.O_WRONLY | os.O_CREAT)
fcntl.lockf(fd, fcntl.LOCK_EX)
def release_lock(self):
fd = self.lockfd
if fd is not None:
fcntl.lockf(fd, fcntl.LOCK_UN)
os.close(fd)
self.lockfd = None
def mkcommit(self, tx, tree_oid, parents, message=None):
# Prepare metadata for commit
if not message:
message = tx.description
if not message:
message = "AcidFS transaction"
gitenv = os.environ.copy()
extension = tx._extension # "Official" API despite underscore
user = extension.get("acidfs_user")
if not user:
user = extension.get("user")
if not user:
user = tx.user
if user:
if user.startswith(" "):
user = user[1:]
else:
# strip Zope's "path"
user = user.split(None, 1)
if len(user) == 2:
user = user[1]
else:
user = user[0]
if user:
gitenv["GIT_AUTHOR_NAME"] = gitenv["GIT_COMMITER_NAME"] = user
email = extension.get("acidfs_email")
if not email:
email = extension.get("email")
if email:
gitenv["GIT_AUTHOR_EMAIL"] = gitenv["GIT_COMMITTER_EMAIL"] = gitenv[
"EMAIL"
] = email
# Write commit to db
args = ["git", "commit-tree", tree_oid, "-m", message]
for parent in parents:
args.append("-p")
args.append(parent)
return _check_output(args, cwd=self.db, env=gitenv).strip()
def merge(self, base_oid, current, tree_oid):
"""
This attempts to interpret the output of 'git merge-tree', given the
current head, the tree we're currently working on, and the nearest
common ancestor commit (base_oid).
I haven't found any documentation on the format of the output of
'git merge-tree' so this is basically reverse engineered from studying
its output in different situations. I try to be as conservative as
possible here and bail as soon as I hit anything I'm not 100% sure
about. It is far preferable to raise a ConflictError than incorrectly
merge. As such, the code below is peppered with assertions using the
'expect' function, which will raise a ConflictError if any of our
expectations aren't met. I also attempt to log as much useful debug
information as possible in the case of an unmet expectation, so I can go
back and take into account more cases as they are encountered.
The basic algorithm here is a finite state machine operating on the
output of 'git merge-tree' one line at a time. This should be fairly
memory efficient for even large changesets, with the caveat there may
have been added a large binary file which contains few or no line break
characters, which could cause a buffer to get large while scanning
through the merge data.
One might ask, why not use the porcelain 'git merge' command? One
reason is, in the context of the two phase commit protocol, we'd rather
do pretty much everything we possibly can in the voting stage, leaving
ourselves with nothing to do in the finish phase except updating the
head to the commit we just created, and possibly updating the working
directory--operations that are guaranteed to work. Since 'git merge'
will update the head, we'd prefer to do it during the final phase of the
commit, but we can't guarantee it will work. There is not a convenient
way to do a merge dry run during the voting phase. Although I can
conceive of ways to do the merge during the voting phase and roll back
to the previous head if we need to, that feels a little riskier. Doing
the merge ourselves, here, also frees us from having to work with a
working directory, required by the porcelain 'git merge' command. This
means we can use bare repositories and/or have transactions that use
a head other than the repositories 'current' head.
In general, tranactions will be short and will not have much a of a
chance to get very far behind the head, so merges will tend not to be
terribly complicated. We should be able to handle the vast majority of
cases here, even if there are some rare corner cases the porcelain
command might be able to handle that we can't. I think that's a
reasonable trade off for the flexibility this approach provides.
Some dead/unreachable branches are left in here, just in case we haven't
entirely characterized the behavior of 'git merge-tree'. These are marked with
'pragma NO COVER' and are easily recognized.
"""
with _popen(
["git", "merge-tree", base_oid, tree_oid, current],
cwd=self.db,
stdout=subprocess.PIPE,
) as proc:
# Messy finite state machine
state = None
extra_state = None
stream = proc.stdout
line = stream.readline()
def expect(expectation, *msg):
if not expectation: # pragma no cover
log.debug("Unmet expectation during merge.")
log.debug("".join(traceback.format_stack()))
if msg:
log.debug(msg[0], *msg[1:])
if extra_state:
log.debug("Extra state: %s", extra_state)
raise ConflictError()
while line:
if state is None: # default, scanning for start of a change
if line[0:1].isalpha():
# If first column is a letter, then we have the first
# line of a change, which describes the change.
line = line.strip()
if line in (
b"added in local",
b"removed in local",
b"removed in both",
): # pragma NO COVER
# This doesn't seem to come up in practice
# We don't care about changes to our current tree.
# We already know about those.
pass
elif line == b"added in remote":
# The head got a new file, we should grab it
state = _MERGE_ADDED_IN_REMOTE
extra_state = []
elif line == b"removed in remote":
# File got deleted from head, remove it
state = _MERGE_REMOVED_IN_REMOTE
extra_state = []
elif line == b"changed in both":
# File was edited in both branches, see if we can
# patch
state = _MERGE_CHANGED_IN_BOTH
extra_state = []
elif line == b"added in both":
state = _MERGE_ADDED_IN_BOTH
extra_state = []
else: # pragma NO COVER
log.debug("Don't know how to merge: %s", line)
raise ConflictError()
elif state is _MERGE_ADDED_IN_REMOTE:
if line[0:1].isalpha() or line.startswith(b"@"):
# Done collecting tree lines, only expecting one
expect(len(extra_state) == 1, "Wrong number of lines")
whose, mode, oid, path = _parsetree(extra_state[0])
expect(whose == b"their", "Unexpected whose: %s", whose)
expect(mode == b"100644", "Unexpected mode: %s", mode)
parsed = path.decode("ascii").split("/")
folder = self.find(parsed[:-1])
expect(isinstance(folder, _TreeNode), "Not a folder: %s", path)
folder.set(parsed[-1], (b"blob", oid, None))
state = extra_state = None
continue
else:
extra_state.append(line)
elif state is _MERGE_REMOVED_IN_REMOTE:
if line[0:1].isalpha() or line.startswith(b"@"):
# Done collecting tree lines, expect two, one for base,
# one for our copy, whose sha1s should match
expect(len(extra_state) == 2, "Wrong number of lines")
whose, mode, oid, path = _parsetree(extra_state[0])
expect(
whose in (b"our", b"base"), "Unexpected whose: %s", whose
)
expect(mode == b"100644", "Unexpected mode: %s", mode)
whose, mode, oid2, path2 = _parsetree(extra_state[1])
expect(
whose in (b"our", b"base"), "Unexpected whose: %s", whose
)
expect(mode == b"100644", "Unexpected mode: %s", mode)
expect(oid == oid2, "SHA1s don't match")
expect(path == path2, "Paths don't match")
path = path.decode("ascii").split("/")
folder = self.find(path[:-1])
expect(isinstance(folder, _TreeNode), "Not a folder")
folder.remove(path[-1])
state = extra_state = None
continue
else:
extra_state.append(line)
elif state is _MERGE_CHANGED_IN_BOTH:
if line.startswith(b"@"):
# Done collecting tree lines, expect three, one for base
# and one for each copy
expect(len(extra_state) == 3, "Wrong number of lines")
whose, mode, oid, path = _parsetree(extra_state[0])
expect(
whose in (b"base", b"our", b"their"),
"Unexpected whose: %s",
whose,
)
expect(mode == b"100644", "Unexpected mode: %s", mode)
for extra_line in extra_state[1:]:
whose, mode, oid2, path2 = _parsetree(extra_line)
expect(
whose in (b"base", b"our", b"their"),
"Unexpected whose: %s",
whose,
)
expect(mode == b"100644", "Unexpected mode: %s", mode)
expect(path == path2, "Paths don't match")
parsed = path.decode("ascii").split("/")
folder = self.find(parsed[:-1])
expect(isinstance(folder, _TreeNode), "Not a folder")
name = parsed[-1]
blob = folder.get(name)
expect(isinstance(blob, _Blob), "Not a blob")
with _tempfile() as tmp:
shutil.copyfileobj(blob.open(), open(tmp, "wb"))
with _popen(
["patch", "-s", tmp, "-"],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
) as p:
f = p.stdin
while line and not line[0:1].isalpha():
if line[1:9] == b"<<<<<<< ":
raise ConflictError()
f.write(line)
line = stream.readline()
newblob = folder.new_blob(name, blob)
shutil.copyfileobj(open(tmp, "rb"), newblob)
state = extra_state = None
continue
else:
extra_state.append(line)
elif state is _MERGE_ADDED_IN_BOTH: # pragma NO BRANCH
# NO BRANCH pragma added to workaround what seems to be a bug in
# coverage. This if..elif structure handles every case that's thrown
# at it, but coverage seems concerned that there isn't a case that
# doesn't get handled.
if line[0:1].isalpha() or line.startswith(b"@"):
# Done collecting tree lines, expect two, one for base,
# one for our copy, whose sha1s should match
expect(len(extra_state) == 2, "Wrong number of lines")
whose, mode, oid, path = _parsetree(extra_state[0])
expect(
whose in (b"our", b"their"), "Unexpected whose: %s", whose
)
expect(mode == b"100644", "Unexpected mode: %s", mode)
whose, mode, oid2, path2 = _parsetree(extra_state[1])
expect(
whose in (b"our", b"their"), "Unexpected whose: %s", whose
)
expect(mode == b"100644", "Unexpected mode: %s", mode)
expect(path == path2, "Paths don't match")
# Either it's the same file or a different file.
if oid != oid2:
# Different files, can't merge
raise ConflictError()
else: # pragma NO COVER
# Seems to not come up. Probably merge-tree detects this and
# doesn't bother us about it.
# Same file, nothing to do
state = extra_state = None
continue
else:
extra_state.append(line)
line = stream.readline()
class _TreeNode(object):
parent = None
name = None
dirty = False
oid = None
committed_oid = None
@classmethod
def read(cls, db, oid, path_encoding):
node = cls(db, path_encoding)
node.committed_oid = node.oid = oid
contents = node.contents
with _popen(["git", "ls-tree", oid], stdout=subprocess.PIPE, cwd=db) as lstree:
for line in lstree.stdout.readlines():
mode, type, oid, name = _parsetree(line)
name = name.decode(path_encoding)
contents[name] = (type, oid, None)
return node
def __init__(self, db, path_encoding):
self.db = db
self.path_encoding = path_encoding
self.contents = {}
def get(self, name):
contents = self.contents
obj = contents.get(name)
if not obj:
return None
type, oid, obj = obj
assert type in (b"tree", b"blob")
if not obj:
if type == b"tree":
obj = _TreeNode.read(self.db, oid, self.path_encoding)
else:
obj = _Blob(self.db, oid)
obj.parent = self
obj.name = name
contents[name] = (type, oid, obj)
return obj
def find(self, path):
if not path:
return self
obj = self.get(path[0])
if obj:
return obj.find(path[1:])
def new_blob(self, name, prev):
obj = _NewBlob(self.db, prev)
obj.parent = self
obj.name = name
self.contents[name] = (b"blob", None, weakref.proxy(obj))
self.set_dirty()
return obj
def new_tree(self, name):
node = _TreeNode(self.db, self.path_encoding)
node.parent = self
node.name = name
self.contents[name] = (b"tree", None, node)
self.set_dirty()
return node