Kiln » TortoiseHg » TortoiseHg
Clone URL:  
Pushed to one repository · View In Graph Contained in 1.1, 1.1.1, and 1.1.2

status: refactoring: extract new chunks.py

Changeset 454495510854

Parent 049019fdd430

by Adrian Buehlmann

Changes to 4 files · Browse files at 454495510854 Showing diff from parent 049019fdd430 Diff from another changeset...

Change 1 of 1 Show Entire File tortoisehg/​hgtk/​chunks.py Stacked
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
@@ -0,0 +1,329 @@
+# chunks.py - status/commit chunk handling for TortoiseHg +# +# Copyright 2007 Brad Schick, brad at gmail . com +# Copyright 2007 TK Soh <teekaysoh@gmail.com> +# Copyright 2008 Steve Borho <steve@borho.org> +# Copyright 2008 Emmanuel Rosa <goaway1000@gmail.com> +# +# This software may be used and distributed according to the terms of the +# GNU General Public License version 2, incorporated herein by reference. + +import gtk +import pango +import cStringIO + +from tortoisehg.util import hglib, hgshelve + +from tortoisehg.hgtk import gtklib + +# diffmodel row enumerations +DM_REJECTED = 0 +DM_DISP_TEXT = 1 +DM_IS_HEADER = 2 +DM_PATH = 3 +DM_CHUNK_ID = 4 +DM_FONT = 5 + + +def hunk_markup(text): + 'Format a diff hunk for display in a TreeView row with markup' + hunk = '' + # don't use splitlines, should split with only LF for the patch + lines = hglib.tounicode(text).split(u'\n') + for line in lines: + line = hglib.toutf(line[:512]) + '\n' + if line.startswith('---') or line.startswith('+++'): + hunk += gtklib.markup(line, color=gtklib.DBLUE) + elif line.startswith('-'): + hunk += gtklib.markup(line, color=gtklib.DRED) + elif line.startswith('+'): + hunk += gtklib.markup(line, color=gtklib.DGREEN) + elif line.startswith('@@'): + hunk = gtklib.markup(line, color=gtklib.DORANGE) + else: + hunk += gtklib.markup(line) + return hunk + + +def hunk_unmarkup(text): + 'Format a diff hunk for display in a TreeView row without markup' + hunk = '' + # don't use splitlines, should split with only LF for the patch + lines = hglib.tounicode(text).split(u'\n') + for line in lines: + hunk += gtklib.markup(hglib.toutf(line[:512])) + '\n' + return hunk + + +class chunks(object): + + def __init__(self, stat): + self.stat = stat + self.filechunks = {} + self.diffmodelfile = None + + + def get_difftree(self): + self.diffmodel = gtk.ListStore( + bool, # DM_REJECTED + str, # DM_DISP_TEXT + bool, # DM_IS_HEADER + str, # DM_PATH + int, # DM_CHUNK_ID + pango.FontDescription # DM_FONT + ) + + difftree = gtk.TreeView(self.diffmodel) + + difftree.get_selection().set_mode(gtk.SELECTION_MULTIPLE) + difftree.set_headers_visible(False) + difftree.set_enable_search(False) + if getattr(difftree, 'enable-grid-lines', None) is not None: + difftree.set_property('enable-grid-lines', True) + self.difftree = difftree + + cell = gtk.CellRendererText() + diffcol = gtk.TreeViewColumn('diff', cell) + diffcol.set_resizable(True) + diffcol.add_attribute(cell, 'markup', DM_DISP_TEXT) + + # differentiate header chunks + cell.set_property('cell-background', gtklib.STATUS_HEADER) + diffcol.add_attribute(cell, 'cell_background_set', DM_IS_HEADER) + self.headerfont = self.stat.difffont.copy() + self.headerfont.set_weight(pango.WEIGHT_HEAVY) + + # differentiate rejected hunks + self.rejfont = self.stat.difffont.copy() + self.rejfont.set_weight(pango.WEIGHT_LIGHT) + diffcol.add_attribute(cell, 'font-desc', DM_FONT) + cell.set_property('background', gtklib.STATUS_REJECT_BACKGROUND) + cell.set_property('foreground', gtklib.STATUS_REJECT_FOREGROUND) + diffcol.add_attribute(cell, 'background-set', DM_REJECTED) + diffcol.add_attribute(cell, 'foreground-set', DM_REJECTED) + difftree.append_column(diffcol) + + return difftree + + def __getitem__(self, wfile): + return self.filechunks[wfile] + + def __contains__(self, wfile): + return wfile in self.filechunks + + def clear_filechunks(self): + self.filechunks = {} + + def clear(self): + self.diffmodel.clear() + self.diffmodelfile = None + + def del_file(self, wfile): + if wfile in self.filechunks: + del self.filechunks[wfile] + + def update_chunk_state(self, wfile, selected): + if wfile not in self.filechunks: + return + chunks = self.filechunks[wfile] + for chunk in chunks: + chunk.active = selected + if wfile != self.diffmodelfile: + return + for n, chunk in enumerate(chunks): + if n == 0: + continue + self.diffmodel[n][DM_REJECTED] = not selected + self.update_diff_hunk(self.diffmodel[n]) + self.update_diff_header(self.diffmodel, wfile, selected) + + def update_diff_hunk(self, row): + 'Update the contents of a diff row based on its chunk state' + wfile = row[DM_PATH] + chunks = self.filechunks[wfile] + chunk = chunks[row[DM_CHUNK_ID]] + buf = cStringIO.StringIO() + chunk.pretty(buf) + buf.seek(0) + if chunk.active: + row[DM_REJECTED] = False + row[DM_FONT] = self.stat.difffont + row[DM_DISP_TEXT] = hunk_markup(buf.read()) + else: + row[DM_REJECTED] = True + row[DM_FONT] = self.rejfont + row[DM_DISP_TEXT] = hunk_unmarkup(buf.read()) + + def update_diff_header(self, dmodel, wfile, selected): + try: + chunks = self.filechunks[wfile] + except IndexError: + return + lasthunk = len(chunks)-1 + sel = lambda x: x >= lasthunk or not dmodel[x+1][DM_REJECTED] + newtext = chunks[0].selpretty(sel) + if not selected: + newtext = "<span foreground='" + gtklib.STATUS_REJECT_FOREGROUND + \ + "'>" + newtext + "</span>" + dmodel[0][DM_DISP_TEXT] = newtext + + def get_chunks(self, wfile): # new + if wfile in self.filechunks: + chunks = self.filechunks[wfile] + else: + chunks = self.stat.read_file_chunks(wfile) + for c in chunks: + c.active = True + self.filechunks[wfile] = chunks + return chunks + + def update_hunk_model(self, wfile, checked): + # Read this file's diffs into hunk selection model + self.diffmodel.clear() + self.diffmodelfile = wfile + if not self.stat.is_merge(): + self.append_diff_hunks(wfile, checked) + + def len(self): + return len(self.diffmodel) + + def append_diff_hunks(self, wfile, checked): + 'Append diff hunks of one file to the diffmodel' + chunks = self.stat.read_file_chunks(wfile) + if not chunks: + if wfile in self.filechunks: + del self.filechunks[wfile] + return 0 + + rows = [] + for n, chunk in enumerate(chunks): + if isinstance(chunk, hgshelve.header): + # header chunk is always active + chunk.active = True + rows.append([False, '', True, wfile, n, self.headerfont]) + if chunk.special(): + chunks = chunks[:1] + break + else: + # chunks take file's selection state by default + chunk.active = checked + rows.append([False, '', False, wfile, n, self.stat.difffont]) + + # recover old chunk selection/rejection states, match fromline + if wfile in self.filechunks: + ochunks = self.filechunks[wfile] + next = 1 + for oc in ochunks[1:]: + for n in xrange(next, len(chunks)): + nc = chunks[n] + if oc.fromline == nc.fromline: + nc.active = oc.active + next = n+1 + break + elif nc.fromline > oc.fromline: + break + + self.filechunks[wfile] = chunks + + # Set row status based on chunk state + rej, nonrej = False, False + for n, row in enumerate(rows): + if not row[DM_IS_HEADER]: + if chunks[n].active: + nonrej = True + else: + rej = True + row[DM_REJECTED] = not chunks[n].active + self.update_diff_hunk(row) + self.diffmodel.append(row) + + if len(rows) == 1: + newvalue = checked + else: + newvalue = nonrej + self.update_diff_header(self.diffmodel, wfile, newvalue) + + return len(rows) + + def diff_tree_row_act(self, dtree, path, checked): + 'Row in diff tree (hunk) activated/toggled' + dmodel = dtree.get_model() + row = dmodel[path] + wfile = row[DM_PATH] + try: + chunks = self.filechunks[wfile] + except IndexError: + pass + chunkrows = xrange(1, len(chunks)) + if row[DM_IS_HEADER]: + for n, chunk in enumerate(chunks[1:]): + chunk.active = not checked + self.update_diff_hunk(dmodel[n+1]) + newvalue = not checked + partial = False + else: + chunk = chunks[row[DM_CHUNK_ID]] + chunk.active = not chunk.active + self.update_diff_hunk(row) + rej = [ n for n in chunkrows if dmodel[n][DM_REJECTED] ] + nonrej = [ n for n in chunkrows if not dmodel[n][DM_REJECTED] ] + newvalue = nonrej and True or False + partial = rej and nonrej and True or False + self.update_diff_header(dmodel, wfile, newvalue) + return partial, newvalue + + def get_wfile(self, dtree, path): + dmodel = dtree.get_model() + row = dmodel[path] + wfile = row[DM_PATH] + return wfile + + def save(self, files, patchfilename): + buf = cStringIO.StringIO() + dmodel = self.diffmodel + for wfile in files: + if wfile in self.filechunks: + chunks = self.filechunks[wfile] + else: + chunks = self.stat.read_file_chunks(wfile) + for c in chunks: + c.active = True + for i, chunk in enumerate(chunks): + if i == 0: + chunk.write(buf) + elif chunk.active: + chunk.write(buf) + buf.seek(0) + try: + try: + fp = open(patchfilename, 'wb') + fp.write(buf.read()) + except OSError: + pass + finally: + fp.close() + + def copy_to_clipboard(self, treeview): + 'Write highlighted hunks to the clipboard' + if not treeview.is_focus(): + w = self.stat.get_focus() + w.emit('copy-clipboard') + return False + saves = {} + model, tpaths = treeview.get_selection().get_selected_rows() + for row, in tpaths: + wfile, cid = model[row][DM_PATH], model[row][DM_CHUNK_ID] + if wfile not in saves: + saves[wfile] = [cid] + else: + saves[wfile].append(cid) + fp = cStringIO.StringIO() + for wfile in saves.keys(): + chunks = self[wfile] + chunks[0].write(fp) + for cid in saves[wfile]: + if cid != 0: + chunks[cid].write(fp) + fp.seek(0) + self.stat.clipboard.set_text(fp.read()) +
 
936
937
938
939
940
 
 
941
942
943
 
1178
1179
1180
1181
 
1182
1183
1184
 
936
937
938
 
 
939
940
941
942
943
 
1178
1179
1180
 
1181
1182
1183
1184
@@ -936,8 +936,8 @@
  cf = util.pconvert(f)   if cf in self.subrepos: continue   if cf not in self.status[0]: continue - if f not in self.filechunks: continue - chunks = self.filechunks[f] + if f not in self.chunks: continue + chunks = self.chunks[f]   if len(chunks) < 2: continue     # unfiltered files do not go through backup-revert-patch cycle @@ -1178,7 +1178,7 @@
  # refresh overlay icons and commit dialog   self.closebranch = False   self.nextbranch = None - self.filechunks = {} # force re-read of chunks + self.chunks.clear_filechunks() # force re-read of chunks   buf = self.text.get_buffer()   if buf.get_modified():   self.update_recent_messages(self.opts['message'])
 
12
13
14
15
16
17
18
 
22
23
24
 
25
26
27
 
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
 
85
86
87
88
89
90
91
92
 
212
213
214
215
 
216
217
218
 
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
 
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
 
710
711
712
713
714
 
715
716
717
718
719
 
720
721
722
 
735
736
737
738
739
 
740
741
742
 
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
 
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
 
1017
1018
1019
1020
1021
 
1022
1023
1024
 
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
 
 
 
 
1102
1103
1104
 
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
 
 
 
 
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
 
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
 
1261
1262
1263
 
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
 
 
 
1288
1289
1290
 
12
13
14
 
15
16
17
 
21
22
23
24
25
26
27
 
31
32
33
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
35
36
 
50
51
52
 
 
53
54
55
 
175
176
177
 
178
179
180
181
 
354
355
356
 
 
 
 
 
 
 
 
 
357
358
359
360
361
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
362
363
364
 
553
554
555
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
556
557
558
 
615
616
617
 
 
618
619
620
621
 
 
622
623
624
625
 
638
639
640
 
 
641
642
643
644
 
740
741
742
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
743
744
745
746
 
867
868
869
 
 
 
 
 
 
 
870
871
872
873
874
875
876
877
878
879
 
944
945
946
 
947
948
 
 
 
 
 
 
 
949
950
951
952
953
954
955
 
987
988
989
 
 
 
 
 
 
 
 
990
991
992
993
994
995
996
 
 
 
997
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
998
999
1000
1001
1002
1003
1004
 
1005
 
 
1006
1007
1008
 
1020
1021
1022
1023
1024
1025
1026
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1027
1028
1029
1030
1031
1032
@@ -12,7 +12,6 @@
 import cStringIO  import gtk  import gobject -import pango  import threading    from mercurial import cmdutil, util, patch, mdiff, error, hg @@ -22,6 +21,7 @@
 from tortoisehg.util import hglib, paths, hgshelve    from tortoisehg.hgtk import dialog, gdialog, gtklib, guess, hgignore, statusbar, statusact +from tortoisehg.hgtk import chunks    # file model row enumerations  FM_CHECKED = 0 @@ -31,41 +31,6 @@
 FM_MERGE_STATUS = 4  FM_PARTIAL_SELECTED = 5   -# diffmodel row enumerations -DM_REJECTED = 0 -DM_DISP_TEXT = 1 -DM_IS_HEADER = 2 -DM_PATH = 3 -DM_CHUNK_ID = 4 -DM_FONT = 5 - -def hunk_markup(text): - 'Format a diff hunk for display in a TreeView row with markup' - hunk = '' - # don't use splitlines, should split with only LF for the patch - lines = hglib.tounicode(text).split(u'\n') - for line in lines: - line = hglib.toutf(line[:512]) + '\n' - if line.startswith('---') or line.startswith('+++'): - hunk += gtklib.markup(line, color=gtklib.DBLUE) - elif line.startswith('-'): - hunk += gtklib.markup(line, color=gtklib.DRED) - elif line.startswith('+'): - hunk += gtklib.markup(line, color=gtklib.DGREEN) - elif line.startswith('@@'): - hunk = gtklib.markup(line, color=gtklib.DORANGE) - else: - hunk += gtklib.markup(line) - return hunk - -def hunk_unmarkup(text): - 'Format a diff hunk for display in a TreeView row without markup' - hunk = '' - # don't use splitlines, should split with only LF for the patch - lines = hglib.tounicode(text).split(u'\n') - for line in lines: - hunk += gtklib.markup(hglib.toutf(line[:512])) + '\n' - return hunk    class GStatus(gdialog.GWindow):   """GTK+ based dialog for displaying repository status @@ -85,8 +50,6 @@
  gdialog.GWindow.init(self)   self.mode = 'status'   self.ready = False - self.filechunks = {} - self.diffmodelfile = None   self.status = (None,) * 7   self.status_error = None   self.preview_tab_name_label = None @@ -212,7 +175,7 @@
    # set CTRL-c accelerator for copy-clipboard   gtklib.add_accelerator(self.difftree, 'copy-clipboard', accelgroup, mod+'c') - self.difftree.connect('copy-clipboard', self.copy_to_clipboard) + self.difftree.connect('copy-clipboard', self.chunks.copy_to_clipboard)     def scroll_diff_notebook(widget, direction=gtk.SCROLL_PAGE_DOWN):   page_num = self.diff_notebook.get_current_page() @@ -391,45 +354,11 @@
  # use treeview to show selectable diff hunks   self.clipboard = gtk.Clipboard()   - self.diffmodel = gtk.ListStore( - bool, # DM_REJECTED - str, # DM_DISP_TEXT - bool, # DM_IS_HEADER - str, # DM_PATH - int, # DM_CHUNK_ID - pango.FontDescription) - - difftree = gtk.TreeView(self.diffmodel) + self.chunks = chunks.chunks(self) + difftree = self.chunks.get_difftree() + self.difftree = difftree + difftree.connect('row-activated', self.diff_tree_row_act)   - difftree.get_selection().set_mode(gtk.SELECTION_MULTIPLE) - difftree.set_headers_visible(False) - difftree.set_enable_search(False) - if getattr(difftree, 'enable-grid-lines', None) is not None: - difftree.set_property('enable-grid-lines', True) - difftree.connect('row-activated', self.diff_tree_row_act) - self.difftree = difftree - - cell = gtk.CellRendererText() - diffcol = gtk.TreeViewColumn('diff', cell) - diffcol.set_resizable(True) - diffcol.add_attribute(cell, 'markup', DM_DISP_TEXT) - - # differentiate header chunks - cell.set_property('cell-background', gtklib.STATUS_HEADER) - diffcol.add_attribute(cell, 'cell_background_set', DM_IS_HEADER) - self.headerfont = self.difffont.copy() - self.headerfont.set_weight(pango.WEIGHT_HEAVY) - - # differentiate rejected hunks - self.rejfont = self.difffont.copy() - self.rejfont.set_weight(pango.WEIGHT_LIGHT) - diffcol.add_attribute(cell, 'font-desc', DM_FONT) - cell.set_property('background', gtklib.STATUS_REJECT_BACKGROUND) - cell.set_property('foreground', gtklib.STATUS_REJECT_FOREGROUND) - diffcol.add_attribute(cell, 'background-set', DM_REJECTED) - diffcol.add_attribute(cell, 'foreground-set', DM_REJECTED) - difftree.append_column(diffcol) -   scroller = gtk.ScrolledWindow()   scroller.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)   scroller.add(difftree) @@ -624,30 +553,6 @@
  def thgrefresh(self, window):   self.reload_status()   - def copy_to_clipboard(self, treeview): - 'Write highlighted hunks to the clipboard' - if not treeview.is_focus(): - w = self.get_focus() - w.emit('copy-clipboard') - return False - saves = {} - model, tpaths = treeview.get_selection().get_selected_rows() - for row, in tpaths: - wfile, cid = model[row][DM_PATH], model[row][DM_CHUNK_ID] - if wfile not in saves: - saves[wfile] = [cid] - else: - saves[wfile].append(cid) - fp = cStringIO.StringIO() - for wfile in saves.keys(): - chunks = self.filechunks[wfile] - chunks[0].write(fp) - for cid in saves[wfile]: - if cid != 0: - chunks[cid].write(fp) - fp.seek(0) - self.clipboard.set_text(fp.read()) -   def refresh_file_tree(self):   """Clear out the existing ListStore model and reload it from the   repository status. Also recheck and reselect files that remain @@ -710,13 +615,11 @@
  if row[FM_PARTIAL_SELECTED]:   # force refresh of partially selected files   self.update_hunk_model(i, self.filetree) - self.diffmodel.clear() - self.diffmodelfile = None + self.chunks.clear()   else:   # demand refresh of full or non selection   wfile = row[FM_PATH] - if wfile in self.filechunks: - del self.filechunks[wfile] + self.chunks.del_file(wfile)     # recover selections   firstrow = None @@ -735,8 +638,7 @@
  self.diff_text.set_buffer(gtk.TextBuffer())   self.preview_text.set_buffer(gtk.TextBuffer())   if not is_merge: - self.diffmodel.clear() - self.diffmodelfile = None + self.chunks.clear()     self.filetree.show()   if self.mode == 'commit': @@ -838,49 +740,7 @@
  fileentry[FM_PARTIAL_SELECTED] = False   wfile = fileentry[FM_PATH]   selected = fileentry[FM_CHECKED] - if wfile not in self.filechunks: - return - chunks = self.filechunks[wfile] - for chunk in chunks: - chunk.active = selected - if wfile != self.diffmodelfile: - return - for n, chunk in enumerate(chunks): - if n == 0: - continue - self.diffmodel[n][DM_REJECTED] = not selected - self.update_diff_hunk(self.diffmodel[n]) - self.update_diff_header(self.diffmodel, wfile, selected) - - def update_diff_hunk(self, row): - 'Update the contents of a diff row based on its chunk state' - wfile = row[DM_PATH] - chunks = self.filechunks[wfile] - chunk = chunks[row[DM_CHUNK_ID]] - buf = cStringIO.StringIO() - chunk.pretty(buf) - buf.seek(0) - if chunk.active: - row[DM_REJECTED] = False - row[DM_FONT] = self.difffont - row[DM_DISP_TEXT] = hunk_markup(buf.read()) - else: - row[DM_REJECTED] = True - row[DM_FONT] = self.rejfont - row[DM_DISP_TEXT] = hunk_unmarkup(buf.read()) - - def update_diff_header(self, dmodel, wfile, selected): - try: - chunks = self.filechunks[wfile] - except IndexError: - return - lasthunk = len(chunks)-1 - sel = lambda x: x >= lasthunk or not dmodel[x+1][DM_REJECTED] - newtext = chunks[0].selpretty(sel) - if not selected: - newtext = "<span foreground='" + gtklib.STATUS_REJECT_FOREGROUND + \ - "'>" + newtext + "</span>" - dmodel[0][DM_DISP_TEXT] = newtext + self.chunks.update_chunk_state(wfile, selected)     def updated_codes(self):   types = [('modified', 'M'), @@ -1007,18 +867,13 @@
  if not row[FM_CHECKED]:   continue   wfile = row[FM_PATH] - if wfile in self.filechunks: - chunks = self.filechunks[wfile] - else: - chunks = self.read_file_chunks(wfile) - for c in chunks: - c.active = True - self.filechunks[wfile] = chunks + chunks = self.chunks.get_chunks(wfile)   for i, chunk in enumerate(chunks):   if i == 0:   chunk.write(buf)   elif chunk.active:   chunk.write(buf) +   difftext = buf.getvalue().splitlines(True)   self.preview_text.set_buffer(self.diff_highlight_buffer(difftext))   @@ -1089,16 +944,12 @@
  self.stbar.set_text(str(e))   return self.diff_highlight_buffer(difftext)   -   def update_hunk_model(self, path, tree):   # Read this file's diffs into hunk selection model - wfile = self.filemodel[path][FM_PATH] - self.diffmodel.clear() - self.diffmodelfile = wfile - if not self.is_merge(): - self.append_diff_hunks(wfile) - if len(self.diffmodel): - tree.scroll_to_cell(0, use_align=True, row_align=0.0) + row = self.filemodel[path] + self.chunks.update_hunk_model(row[FM_PATH], row[FM_CHECKED]) + if not self.is_merge() and self.chunks.len(): + tree.scroll_to_cell(0, use_align=True, row_align=0.0)     def check_max_diff(self, pfile):   lines = [] @@ -1136,114 +987,22 @@
  difftext.seek(0)   return hgshelve.parsepatch(difftext)   - def append_diff_hunks(self, wfile): - 'Append diff hunks of one file to the diffmodel' - chunks = self.read_file_chunks(wfile) - if not chunks: - if wfile in self.filechunks: - del self.filechunks[wfile] - return - + def diff_tree_row_act(self, dtree, path, column): + 'Row in diff tree (hunk) activated/toggled' + wfile = self.chunks.get_wfile(dtree, path) +   for fr in self.filemodel:   if fr[FM_PATH] == wfile:   break - else: - # should not be possible - return   - rows = [] - for n, chunk in enumerate(chunks): - if isinstance(chunk, hgshelve.header): - # header chunk is always active - chunk.active = True - rows.append([False, '', True, wfile, n, self.headerfont]) - if chunk.special(): - chunks = chunks[:1] - break - else: - # chunks take file's selection state by default - chunk.active = fr[FM_CHECKED] - rows.append([False, '', False, wfile, n, self.difffont]) - - - # recover old chunk selection/rejection states, match fromline - if wfile in self.filechunks: - ochunks = self.filechunks[wfile] - next = 1 - for oc in ochunks[1:]: - for n in xrange(next, len(chunks)): - nc = chunks[n] - if oc.fromline == nc.fromline: - nc.active = oc.active - next = n+1 - break - elif nc.fromline > oc.fromline: - break - - self.filechunks[wfile] = chunks - - # Set row status based on chunk state - rej, nonrej = False, False - for n, row in enumerate(rows): - if not row[DM_IS_HEADER]: - if chunks[n].active: - nonrej = True - else: - rej = True - row[DM_REJECTED] = not chunks[n].active - self.update_diff_hunk(row) - self.diffmodel.append(row) - - if len(rows) == 1: - newvalue = fr[FM_CHECKED] - else: - newvalue = nonrej - partial = rej and nonrej - if fr[FM_PARTIAL_SELECTED] != partial: - fr[FM_PARTIAL_SELECTED] = partial - if fr[FM_CHECKED] != newvalue: - fr[FM_CHECKED] = newvalue - self.update_check_count() - self.update_diff_header(self.diffmodel, wfile, newvalue) - - - def diff_tree_row_act(self, dtree, path, column): - 'Row in diff tree (hunk) activated/toggled' - dmodel = dtree.get_model() - row = dmodel[path] - wfile = row[DM_PATH] - try: - chunks = self.filechunks[wfile] - except IndexError: - pass - chunkrows = xrange(1, len(chunks)) - for fr in self.filemodel: - if fr[FM_PATH] == wfile: - break - if row[DM_IS_HEADER]: - for n, chunk in enumerate(chunks[1:]): - chunk.active = not fr[FM_CHECKED] - self.update_diff_hunk(dmodel[n+1]) - newvalue = not fr[FM_CHECKED] - partial = False - else: - chunk = chunks[row[DM_CHUNK_ID]] - chunk.active = not chunk.active - self.update_diff_hunk(row) - rej = [ n for n in chunkrows if dmodel[n][DM_REJECTED] ] - nonrej = [ n for n in chunkrows if not dmodel[n][DM_REJECTED] ] - newvalue = nonrej and True or False - partial = rej and nonrej and True or False + partial, newvalue = self.chunks.diff_tree_row_act(dtree, path, fr[FM_CHECKED])     # Update file's check status   if fr[FM_PARTIAL_SELECTED] != partial:   fr[FM_PARTIAL_SELECTED] = partial   if fr[FM_CHECKED] != newvalue:   fr[FM_CHECKED] = newvalue - chunks[0].active = newvalue   self.update_check_count() - self.update_diff_header(dmodel, wfile, newvalue) -     def refresh_clicked(self, toolbutton, data=None):   self.reload_status() @@ -1261,30 +1020,13 @@
    buf = cStringIO.StringIO()   dmodel = self.diffmodel + files = []   for row in self.filemodel:   if not row[FM_CHECKED]:   continue - wfile = row[FM_PATH] - if wfile in self.filechunks: - chunks = self.filechunks[wfile] - else: - chunks = self.read_file_chunks(wfile) - for c in chunks: - c.active = True - for i, chunk in enumerate(chunks): - if i == 0: - chunk.write(buf) - elif chunk.active: - chunk.write(buf) - buf.seek(0) - try: - try: - fp = open(result, 'wb') - fp.write(buf.read()) - except OSError: - pass - finally: - fp.close() + files.append(row[FM_PATH]) + + self.chunks.save(files, result)     def diff_clicked(self, toolbutton, data=None):   diff_list = self.relevant_checked_files('MAR!')
 
172
173
174
175
 
176
177
178
179
 
180
181
182
 
194
195
196
197
 
198
199
200
 
172
173
174
 
175
176
177
178
 
179
180
181
182
 
194
195
196
 
197
198
199
200
@@ -172,11 +172,11 @@
  if file not in wfiles:   # file was not selected for inclusion   continue - if file not in self.filechunks: + if file not in self.chunks:   # file was never filtered, accept all chunks   accepted.append(chunk)   continue - schunks = self.filechunks[file] + schunks = self.chunks[file]   for i, c in enumerate(schunks):   if chunk != c:   continue @@ -194,7 +194,7 @@
  # shelve them!   hgshelve.shelve(self.ui, self.repo, **opts)   self.ui.setconfig('ui', 'interactive', 'off') - self.filechunks = {} # do not keep chunks + self.chunks.clear_filechunks() # do not keep chunks   self.reload_status()     def unshelve(self):