mapmerge update + mats properly work

This commit is contained in:
Letter N
2021-03-05 15:38:35 +08:00
parent a0020c6fdf
commit 2542fe05e9
35 changed files with 552 additions and 765 deletions
+19 -7
View File
@@ -1,10 +1,12 @@
#!/usr/bin/env python3
import os
import sys
import pygit2
from . import dmm
from .mapmerge import merge_map
def main(repo):
def main(repo, *, use_workdir=False):
if repo.index.conflicts:
print("You need to resolve merge conflicts first.")
return 1
@@ -16,12 +18,21 @@ def main(repo):
except KeyError:
pass
target_statuses = pygit2.GIT_STATUS_INDEX_MODIFIED | pygit2.GIT_STATUS_INDEX_NEW
skip_to_file_statuses = pygit2.GIT_STATUS_WT_DELETED | pygit2.GIT_STATUS_WT_MODIFIED
if use_workdir:
target_statuses |= pygit2.GIT_STATUS_WT_MODIFIED | pygit2.GIT_STATUS_WT_NEW
skip_to_file_statuses &= ~pygit2.GIT_STATUS_WT_MODIFIED
changed = 0
for path, status in repo.status().items():
if path.endswith(".dmm") and (status & (pygit2.GIT_STATUS_INDEX_MODIFIED | pygit2.GIT_STATUS_INDEX_NEW)):
if path.endswith(".dmm") and (status & target_statuses):
# read the index
index_entry = repo.index[path]
index_map = dmm.DMM.from_bytes(repo[index_entry.id].read_raw())
if use_workdir:
index_map = dmm.DMM.from_file(os.path.join(repo.workdir, path))
else:
index_map = dmm.DMM.from_bytes(repo[index_entry.id].read_raw())
try:
head_blob = repo[repo[repo.head.target].tree[path].id]
@@ -32,7 +43,7 @@ def main(repo):
merged_map = index_map
else:
# Entry in HEAD, merge the index over it
print(f"Merging map: {path}", flush=True)
print(f"Converting map: {path}", flush=True)
assert not (status & pygit2.GIT_STATUS_INDEX_NEW)
head_map = dmm.DMM.from_bytes(head_blob.read_raw())
merged_map = merge_map(index_map, head_map)
@@ -43,15 +54,16 @@ def main(repo):
changed += 1
# write to the working directory if that's clean
if status & (pygit2.GIT_STATUS_WT_DELETED | pygit2.GIT_STATUS_WT_MODIFIED):
if status & skip_to_file_statuses:
print(f"Warning: {path} has unindexed changes, not overwriting them")
else:
merged_map.to_file(os.path.join(repo.workdir, path))
if changed:
repo.index.write()
print(f"Merged {changed} maps.")
return 0
if __name__ == '__main__':
exit(main(pygit2.Repository(pygit2.discover_repository(os.getcwd()))))
repo = pygit2.Repository(pygit2.discover_repository(os.getcwd()))
exit(main(repo, use_workdir='--use-workdir' in sys.argv))