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
|
#!/usr/bin/python
"""
pymergechanges: Merge commits made with changes package into text
Copyright (C) 2018 Y. Cui
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
pymergechanges: Merge commits made with changes package into text
Requires Python version 3; Tested on Python 3.6.6
Supported commands: added, deleted, replaced, highlight
Usage: python pyMergeChanges.py [-arh] <Input File> <Output File>
<Output File> will be overwritten and must be different than <Input File>.
Options:
-a: accept all added, deleted and replaced
-r: reject all added, deleted and replaced
-h: remove all highlights
If no option is given, runs interactively.
Created on Wed Dec 5 20:28:40 2018
Revised on Tue Aug 27 17:51:58 2019
"""
import sys
import re
import codecs
def parse_param(parstr):
if all(p in parstr for p in ('a', 'r')):
print('You cannot accept and reject at the same time.')
sys.exit(1)
parout = ''
for par in parstr[1:]:
if par == 'a':
print('Accepting all added, deleted and replaced')
parout += 'a'
elif par == 'r':
print('Rejecting all added, deleted and replaced')
parout += 'r'
elif par == 'h':
print('Removing all highlights.')
parout += 'h'
else:
print('Unknown parameter: ' + par)
sys.exit(1)
if not parout:
parout = 'i'
return parout
def ask1(): # accept, reject, keep, break
if PARAMS == 'i':
while True:
ans = input('[a]ccept or [r]eject or [k]eep or [b]reak ? ').lower()
if ans == 'a':
print('Accepted.')
break
elif ans == 'r':
print('Rejected.')
break
elif ans == 'k':
print('Kept.')
break
elif ans == 'b':
print('Alright.')
break
elif any(p == 'a' for p in PARAMS):
print('Accepted.')
ans = 'a'
elif any(p == 'r' for p in PARAMS):
print('Rejected.')
ans = 'r'
else:
ans = 'k'
return ans
def ask2(): # remove, keep, break
if PARAMS == 'i':
while True:
ans = input('[r]emove or [k]eep or [b]reak ? ').lower()
if ans == 'r':
print('Removed.')
break
elif ans == 'k':
print('Kept.')
break
elif ans == 'b':
print('Alright.')
break
return ans
elif any(p == 'h' for p in PARAMS):
print('Removed.')
ans = 'r'
else:
print('Kept.')
ans = 'k'
return ans
def trim_space(line, pos): # trim two consecutive white spaces
if line[pos-1:pos+1] == ' ':
line = line[:pos] + line[pos+1:]
return line
# BEGIN OF SCRIPT
if len(sys.argv) not in [3, 4]:
print(__doc__)
sys.exit(1)
# parse input parameters
if len(sys.argv) == 3:
print('Running in interactive mode. ')
INPUTFILE, OUTPUTFILE = sys.argv[1:]
PARAMS = "i"
if len(sys.argv) == 4:
PARAMLIST, INPUTFILE, OUTPUTFILE = sys.argv[1:]
PARAMS = parse_param(PARAMLIST)
if INPUTFILE == OUTPUTFILE:
print('Input File and Output File must be different.')
sys.exit(1)
RE_ADDED = re.compile(r'(\\added)(\[[^\]]*\])?\{(([^\}]?(\{[^\}]*\})?)*)\}')
RE_DELETED = re.compile(r'(\\deleted)(\[[^\]]*\])?\{(([^\}]?(\{[^\}]*\})?)*)\}')
RE_REPLACED = re.compile(r'(\\replaced)(\[[^\]]*\])?\{(([^\}]?(\{[^\}]*\})?)*)\}\{(([^\}]?(\{[^\}]*\})?)*)\}')
RE_HIGHLIGHT = re.compile(r'(\\highlight)(\[[^\]]*\])?\{(([^\}]?(\{[^\}]*\})?)*)\}')
RE_COMMENT = re.compile(r'(\\comment)(\[[^\]]*\])?\{(([^\}]?(\{[^\}]*\})?)*)\}')
codecs.open(OUTPUTFILE, mode='w', encoding='utf8').close()
with codecs.open(INPUTFILE, mode='r', encoding='utf8') as fin, \
codecs.open(OUTPUTFILE, mode='a', encoding='utf8') as fout:
LINE_COUNT = 0
FLAG_FAST_BREAK = False # if you want to halt merging for some reason
for linein in fin:
if FLAG_FAST_BREAK:
fout.write(linein)
continue
LINE_COUNT += 1
lineout = linein
matchAdded = RE_ADDED.search(lineout)
matchDeleted = RE_DELETED.search(lineout)
matchReplaced = RE_REPLACED.search(lineout)
matchHighlight = RE_HIGHLIGHT.search(lineout)
matchComment = RE_COMMENT.search(lineout)
flagHasCommit = matchAdded or matchDeleted or matchReplaced or matchHighlight or matchComment
if flagHasCommit:
print('\n******** In Line %i :\n %s' % (LINE_COUNT, lineout))
next_pos = 0
while matchAdded:
if FLAG_FAST_BREAK:
break
print('\n** add commit ** \n' + matchAdded.group())
answer = ask1()
if answer == 'a':
lineout = (lineout[:matchAdded.start(0)]
+ matchAdded.group(3) + lineout[matchAdded.end(0):])
lineout = trim_space(lineout, matchAdded.start(0)
+ len(matchAdded.group(3)))
lineout = trim_space(lineout, matchAdded.start(0))
elif answer == 'r':
lineout = (lineout[:matchAdded.start(0)] + lineout[matchAdded.end(0):])
lineout = trim_space(lineout, matchAdded.start(0))
elif answer == 'k':
lineout = lineout
next_pos = matchAdded.end(0)
elif answer == 'b':
FLAG_FAST_BREAK = True
break
matchAdded = RE_ADDED.search(lineout, next_pos) # redo matching for updated text
next_pos = 0
matchDeleted = RE_DELETED.search(lineout, next_pos) # redo matching for (potentially) updated text
while matchDeleted:
if FLAG_FAST_BREAK:
break
print('\n** delete commit ** \n' + matchDeleted.group())
answer = ask1()
if answer == 'a':
lineout = (lineout[:matchDeleted.start(0)] + lineout[matchDeleted.end(0):])
lineout = trim_space(lineout, matchDeleted.start(0))
elif answer == 'r':
lineout = (lineout[:matchDeleted.start(0)] + matchDeleted.group(3)
+ lineout[matchDeleted.end(0):])
elif answer == 'k':
lineout = lineout
next_pos = matchDeleted.end(0)
elif answer == 'b':
FLAG_FAST_BREAK = True
break
matchDeleted = RE_DELETED.search(lineout, next_pos)
next_pos = 0
matchReplaced = RE_REPLACED.search(lineout, next_pos) # redo matching for (potentially) updated text
while matchReplaced:
if FLAG_FAST_BREAK:
break
print('\n** replace commit ** \n' + matchReplaced.group())
answer = ask1()
if answer == 'a':
lineout = (lineout[:matchReplaced.start(0)]
+ matchReplaced.group(3) + lineout[matchReplaced.end(0):])
lineout = trim_space(lineout, matchReplaced.start(0)
+ len(matchReplaced.group(3)))
lineout = trim_space(lineout, matchReplaced.start(0))
elif answer == 'r':
lineout = (lineout[:matchReplaced.start(0)]
+ matchReplaced.group(6) + lineout[matchReplaced.end(0):])
lineout = trim_space(lineout, matchReplaced.start(0)
+ len(matchReplaced.group(6)))
lineout = trim_space(lineout, matchReplaced.start(0))
elif answer == 'k':
lineout = lineout
next_pos = matchReplaced.end(0)
elif answer == 'b':
FLAG_FAST_BREAK = True
break
matchReplaced = RE_REPLACED.search(lineout, next_pos)
next_pos = 0
matchHighlight = RE_HIGHLIGHT.search(lineout, next_pos) # redo matching for (potentially) updated text
while matchHighlight:
if FLAG_FAST_BREAK:
break
print('\n** highlight commit ** \n' + matchHighlight.group())
answer = ask2()
if answer == 'r':
lineout = (lineout[:matchHighlight.start(0)]
+ matchHighlight.group(3) + lineout[matchHighlight.end(0):])
lineout = trim_space(lineout, matchHighlight.start(0)
+ len(matchHighlight.group(3)))
lineout = trim_space(lineout, matchHighlight.start(0))
elif answer == 'k':
lineout = lineout
next_pos = matchHighlight.end(0)
elif answer == 'b':
FLAG_FAST_BREAK = True
break
matchHighlight = RE_HIGHLIGHT.search(lineout, next_pos)
next_pos = 0
matchComment = RE_COMMENT.search(lineout, next_pos) # redo matching for (potentially) updated text
while matchComment:
if FLAG_FAST_BREAK:
break
print('\n** comment commit ** \n' + matchComment.group())
answer = ask2()
if answer == 'r':
lineout = (lineout[:matchComment.start(0)] + lineout[matchComment.end(0):])
lineout = trim_space(lineout, matchComment.start(0))
elif answer == 'k':
lineout = lineout
next_pos = matchComment.end(0)
elif answer == 'b':
FLAG_FAST_BREAK = True
break
matchComment = RE_COMMENT.search(lineout, next_pos)
if lineout.isspace():
print('\nResult is empty line and not stored.')
else:
print('\nResult: \n' + lineout + '\n')
fout.write(lineout)
else:
fout.write(linein)
#
# END OF SCRIPT
|