forked from xgouchet/AutoMergeTool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgen_simplify.py
executable file
·96 lines (77 loc) · 2.95 KB
/
gen_simplify.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import argparse
from amtlcs import *
from amtutils import *
def parse_arguments():
"""Parses the arguments passed on invocation in a dict and return it"""
parser = argparse.ArgumentParser(description="A tool to resolve dummy conflicts")
parser.add_argument('-m', '--merged', required=True)
parser.add_argument(
'-r',
'--report',
choices=[REPORT_NONE, REPORT_SOLVED, REPORT_UNSOLVED, REPORT_FULL],
default=REPORT_NONE,
required=False)
parser.add_argument('-v', '--verbose', required=False, action='store_true')
return parser.parse_args()
def handle_conflict(conflict):
"""Handles a conflict which can be simplified"""
# TODO override comparator to ignore \s+
lines_local = conflict.local_lines()
lines_base = conflict.base_lines()
lines_remote = conflict.remote_lines()
# Prevent recursion limit
limit = sys.getrecursionlimit()
max_size = max(len(lines_base), len(lines_local), len(lines_remote))
# Arbitrary threshold
if max_size * 6 > limit:
return
# find common lines
analyser = LCSAnalyser(concatenate=lambda a, b: list(a) + list(b))
result = analyser.lcs(l=lines_local, b=lines_base, r=lines_remote)
if len(result) == 0:
return
# split conflicts
ib = il = ir = 0
resolution = ""
for sub in result:
if (sub.pos_b > ib) or (sub.pos_l > il) or (sub.pos_r > ir):
# write conflict before subsequence
resolution += conflict.marker_local
for line in lines_local[il:sub.pos_l]:
resolution += line
resolution += CONFLICT_BASE + "\n"
for line in lines_base[ib:sub.pos_b]:
resolution += line
resolution += CONFLICT_SEP + "\n"
for line in lines_remote[ir:sub.pos_r]:
resolution += line
resolution += conflict.marker_remote
# write subsequence
size = len(list(sub.content))
for line in list(sub.content):
resolution += line
# increment indices
ib = sub.pos_b + size
il = sub.pos_l + size
ir = sub.pos_r + size
if (ib < len(lines_base)) or (il < len(lines_local)) or (ir < len(lines_remote)):
resolution += conflict.marker_local
for line in lines_local[il:]:
resolution += line
resolution += CONFLICT_BASE + "\n"
for line in lines_base[ib:]:
resolution += line
resolution += CONFLICT_SEP + "\n"
for line in lines_remote[ir:]:
resolution += line
resolution += conflict.marker_remote
conflict.rewrite(resolution)
if __name__ == '__main__':
args = parse_arguments()
walker = ConflictsWalker(args.merged, 'simplify', args.report, args.verbose)
while walker.has_more_conflicts():
handle_conflict(walker.next_conflict())
walker.end()
sys.exit(walker.get_merge_status())