-
Notifications
You must be signed in to change notification settings - Fork 0
/
base.py
348 lines (259 loc) · 9.52 KB
/
base.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
import itertools
import operator
import os
import string
from collections import deque, namedtuple
import data
import diff
def init():
data.init()
data.update_ref('HEAD', data.RefValue(symbolic=True, value='refs/heads/main'))
def write_tree(directory='.'):
# Index is flat, we need it as a tree of dicts
index_as_tree = {}
with data.get_index() as index:
for path, oid in index.items():
path = path.split('/')
dir_path, filename = path[:-1], path[-1]
current = index_as_tree
# Find the dict for the directory of this file
for dir_name in dir_path:
current = current.setdefault(dir_name, {})
current[filename] = oid
def write_tree_recursive(tree_dict):
entries = []
for name, value in tree_dict.items():
if type(value) is dict:
type_ = 'tree'
oid = write_tree_recursive(value)
else:
type_ = 'blob'
oid = value
entries.append((name, oid, type_))
tree = ''.join(f'{type_} {oid} {name}\n' for name, oid, type_ in sorted(entries))
return data.hash_object(tree.encode(), 'tree')
return write_tree_recursive(index_as_tree)
def _iter_tree_entries(oid):
if not oid:
return
tree = data.get_object(oid, 'tree')
for entry in tree.decode().splitlines():
type_, oid, name = entry.split(sep=' ', maxsplit=2)
yield type_, oid, name
def get_tree(oid, base_path=''):
result = {}
for type_, oid, name in _iter_tree_entries(oid):
assert '/' not in name
assert name not in ('..', '.')
path = base_path + name
if type_ == 'blob':
result[path] = oid
elif type_ == 'tree':
result.update(get_tree(oid, f'{path}/'))
else:
assert False, f'Unknown tree entry {type_}'
return result
def get_working_tree():
result = {}
for root, _, filenames in os.walk('.'):
for filename in filenames:
path = os.path.relpath(f'{root}/{filename}')
if is_ignored(path) or not os.path.isfile(path):
continue
with open(path, 'rb') as f:
result[path] = data.hash_object(f.read())
return result
def get_index_tree():
with data.get_index() as index:
return index
def _empty_current_directory():
for root, dirnames, filenames in os.walk('.', topdown=False):
for filename in filenames:
path = os.path.relpath(f'{root}/{filename}')
if is_ignored(path) or not os.path.isfile(path):
continue
os.remove(path)
for dirname in dirnames:
path = os.path.relpath(f'{root}/{dirname}')
if is_ignored(path):
continue
try:
os.rmdir(path)
except (FileNotFoundError, OSError):
# Deletion might fail if the directory contains ignored files, so it's ok
pass
def read_tree(tree_oid, update_working=False):
with data.get_index() as index:
index.clear()
index.update(get_tree(tree_oid))
if update_working:
_checkout_index(index)
def read_tree_merged(t_base, t_HEAD, t_other, update_working=False):
with data.get_index() as index:
index.clear()
index.update(diff.merge_trees(
get_tree(t_base),
get_tree(t_HEAD),
get_tree(t_other),
))
if update_working:
_checkout_index(index)
def _checkout_index(index):
_empty_current_directory()
for path, oid in index.items():
os.makedirs(os.path.dirname(f'./{path}'), exist_ok=True)
with open(path, 'wb') as f:
f.write(data.get_object(oid, 'blob'))
def commit(message):
commit_contents = f'tree {write_tree()}\n'
HEAD = data.get_ref('HEAD').value
if HEAD:
commit_contents += f'parent {HEAD}\n'
MERGE_HEAD = data.get_ref('MERGE_HEAD').value
if MERGE_HEAD:
commit_contents += f'parent {MERGE_HEAD}\n'
data.delete_ref('MERGE_HEAD', deref=False)
commit_contents += '\n'
commit_contents += f'{message}\n'
oid = data.hash_object(commit_contents.encode(), 'commit')
data.update_ref('HEAD', data.RefValue(symbolic=False, value=oid))
return oid
def checkout(name):
oid = get_oid(name)
commit_contents = get_commit(oid)
read_tree(commit_contents.tree, update_working=True)
if is_branch(name):
HEAD = data.RefValue(symbolic=True, value=f'refs/heads/{name}')
else:
HEAD = data.RefValue(symbolic=False, value=oid)
data.update_ref('HEAD', HEAD, deref=False)
def reset(oid):
data.update_ref('HEAD', data.RefValue(symbolic=False, value=oid))
def merge(other):
HEAD = data.get_ref('HEAD').value
assert HEAD
merge_base = get_merge_base(other, HEAD)
c_other = get_commit(other)
# Handle fast-forward merge
if merge_base == HEAD:
read_tree(c_other.tree, update_working=True)
data.update_ref('HEAD', data.RefValue(symbolic=False, value=other))
print('Fast-forward merge, no need to commit')
return
data.update_ref('MERGE_HEAD', data.RefValue(symbolic=False, value=other))
c_base = get_commit(merge_base)
c_HEAD = get_commit(HEAD)
read_tree_merged(c_base.tree, c_HEAD.tree, c_other.tree, update_working=True)
print('Merged in working tree\nPlease commit')
def get_merge_base(oid1, oid2):
parents1 = set(iter_commits_and_parents({oid1}))
for oid in iter_commits_and_parents({oid2}):
if oid in parents1:
return oid
def is_ancestor_of(commit_contents, maybe_ancestor):
return maybe_ancestor in iter_commits_and_parents({commit_contents})
def create_tag(name, oid):
data.update_ref(f'refs/tags/{name}', data.RefValue(symbolic=False, value=oid))
def create_branch(name, oid):
data.update_ref(f'refs/heads/{name}', data.RefValue(symbolic=False, value=oid))
def iter_branch_names():
for ref_name, _ in data.iter_refs(prefix='refs/heads/'):
yield os.path.relpath(ref_name, 'refs/heads/')
def is_branch(branch):
return data.get_ref(f'refs/heads/{branch}').value is not None
def get_branch_name():
HEAD = data.get_ref('HEAD', deref=False)
if not HEAD.symbolic:
return None
HEAD = HEAD.value
assert HEAD.startswith('refs/heads/')
return os.path.relpath(HEAD, 'refs/heads')
Commit = namedtuple('Commit', ['tree', 'parents', 'message'])
def get_commit(oid):
parents = []
commit_contents = data.get_object(oid, 'commit').decode()
lines = iter(commit_contents.splitlines())
for line in itertools.takewhile(operator.truth, lines):
key, value = line.split(' ', 1)
if key == 'tree':
tree = value
elif key == 'parent':
parents.append(value)
else:
assert False, f'Unknown field {key}'
message = '\n'.join(lines)
return Commit(tree=tree, parents=parents, message=message)
def iter_commits_and_parents(oids):
# N.B. Must yield the oid before accessing it (to allow caller to fetch if needed)
oids = deque(oids)
visited = set()
while oids:
oid = oids.popleft()
if (not oid) or (oid in visited):
continue
visited.add(oid)
yield oid
commit_contents = get_commit(oid)
# Return first parent next
oids.extendleft(commit.parents[:1])
# Return other parents later
oids.extend(commit.parents[1:])
def iter_objects_in_commits(oids):
# N.B. Must yield the oid before accessing it (to allow caller to fetch if needed)
visited = set()
def iter_objects_in_tree(oid):
visited.add(oid)
yield oid
for type_, oid, _ in _iter_tree_entries(oid):
if oid not in visited:
if type_ == 'tree':
yield from iter_objects_in_tree(oid)
else:
visited.add(oid)
yield oid
for oid in iter_commits_and_parents(oids):
yield oid
commit_contents = get_commit(oid)
if commit_contents.tree not in visited:
yield from iter_objects_in_tree(commit_contents.tree)
def get_oid(name):
if name == '@':
name = 'HEAD'
# Name is ref
refs_to_try = [
f'{name}',
f'refs/{name}',
f'refs/tags/{name}',
f'refs/heads/{name}',
]
for ref in refs_to_try:
if data.get_ref(ref, deref=False).value:
return data.get_ref(ref).value
# Name is SHA1
is_hex = all(c in string.hexdigits for c in name)
if len(name) == 40 and is_hex:
return name
assert False, f'Unknown name {name}'
def add(filenames):
def add_file(filename):
# Normalize path
filename = os.path.relpath(filename)
with open(filename, 'rb') as f:
oid = data.hash_object(f.read())
index[filename] = oid
def add_directory(dir_name):
for root, _, filenames in os.walk(dir_name):
for filename in filenames:
# Normalize path
path = os.path.relpath(f'{root}/{filename}')
if is_ignored(path) or not os.path.isfile(path):
continue
add_file(path)
with data.get_index() as index:
for name in filenames:
if os.path.isfile(name):
add_file(name)
elif os.path.isdir(name):
add_directory(name)
def is_ignored(path):
return '.ugit' in path.split('/')