author | Claus Gittinger <cg@exept.de> |
Mon, 15 Jul 2019 18:53:35 +0200 | |
changeset 18903 | b0d007a5e683 |
parent 18466 | 326bcf80428f |
permissions | -rw-r--r-- |
18466 | 1 |
"{ Encoding: utf8 }" |
2 |
||
9346 | 3 |
" |
4 |
COPYRIGHT (c) 2009 by eXept Software AG |
|
5 |
All Rights Reserved |
|
6 |
||
7 |
This software is furnished under a license and may be used |
|
8 |
only in accordance with the terms of that license and with the |
|
9 |
inclusion of the above copyright notice. This software may not |
|
10 |
be provided or otherwise made available to, or used by, any |
|
11 |
other person. No title to or ownership of the software is |
|
12 |
hereby transferred. |
|
13 |
" |
|
8933 | 14 |
"{ Package: 'stx:libtool' }" |
15 |
||
18025 | 16 |
"{ NameSpace: Smalltalk }" |
17 |
||
8933 | 18 |
Object subclass:#DiffListUtility |
19 |
instanceVariableNames:'' |
|
20 |
classVariableNames:'DiffCommandTemplate' |
|
21 |
poolDictionaries:'' |
|
22 |
category:'Views-Text' |
|
23 |
! |
|
24 |
||
25 |
!DiffListUtility class methodsFor:'documentation'! |
|
26 |
||
9346 | 27 |
copyright |
28 |
" |
|
29 |
COPYRIGHT (c) 2009 by eXept Software AG |
|
30 |
All Rights Reserved |
|
31 |
||
32 |
This software is furnished under a license and may be used |
|
33 |
only in accordance with the terms of that license and with the |
|
34 |
inclusion of the above copyright notice. This software may not |
|
35 |
be provided or otherwise made available to, or used by, any |
|
36 |
other person. No title to or ownership of the software is |
|
37 |
hereby transferred. |
|
38 |
" |
|
39 |
! |
|
40 |
||
8933 | 41 |
documentation |
42 |
" |
|
43 |
a utility to encapsulate access to the diff command |
|
9494 | 44 |
(may also be a facade to a smalltalk-diff algorithm, eventually) |
8933 | 45 |
|
46 |
[author:] |
|
47 |
cg (cg@CG-PC) |
|
48 |
||
49 |
[instance variables:] |
|
50 |
||
51 |
[class variables:] |
|
52 |
||
53 |
[see also:] |
|
54 |
||
55 |
" |
|
56 |
! ! |
|
57 |
||
58 |
!DiffListUtility class methodsFor:'defaults'! |
|
59 |
||
60 |
diffCommand |
|
12358 | 61 |
"return the diff-command (with argument placeHolders). |
62 |
By default, diff is used with windows, diff -b with Unix. |
|
63 |
You can change this via the diffCommandTemplate" |
|
8933 | 64 |
|
65 |
DiffCommandTemplate notNil ifTrue:[ ^ DiffCommandTemplate ]. |
|
12358 | 66 |
^ UserPreferences current externalDiffCommandTemplate |
8933 | 67 |
! |
68 |
||
69 |
diffCommandTemplate:aCommandTemplateString |
|
70 |
"set the diff-command template" |
|
71 |
||
72 |
DiffCommandTemplate := aCommandTemplateString |
|
73 |
! ! |
|
74 |
||
75 |
!DiffListUtility class methodsFor:'private'! |
|
76 |
||
18466 | 77 |
saveForDiff:text on:stream |
8933 | 78 |
[ |
79 |
text do:[:line | |
|
80 |
|lOut i| |
|
81 |
||
82 |
line notEmptyOrNil ifTrue:[ |
|
83 |
lOut := line. |
|
84 |
(line includes:Character return) ifTrue: [ |
|
85 |
(line endsWith:Character return) ifTrue:[ |
|
12717
dcde1443a6bb
Changed usage of deprecated #copyWithoutLast: to #copyButLast:
Stefan Vogel <sv@exept.de>
parents:
12384
diff
changeset
|
86 |
lOut := line copyButLast:1. |
12384 | 87 |
]. |
88 |
] ifFalse:[ |
|
89 |
(line endsWith:(String crlf)) ifTrue: [ |
|
12717
dcde1443a6bb
Changed usage of deprecated #copyWithoutLast: to #copyButLast:
Stefan Vogel <sv@exept.de>
parents:
12384
diff
changeset
|
90 |
lOut := line copyButLast:2. |
8933 | 91 |
] |
12384 | 92 |
"/ i := line indexOf:Character return. |
93 |
"/ (line at:i+1) == Character nl ifTrue:[ |
|
94 |
"/ "/ crnl endings |
|
95 |
"/ lOut := line copyReplaceString:(String crlf) withString:(String lf). |
|
96 |
"/ ] ifFalse:[ |
|
97 |
"/ "/ cr endings |
|
98 |
"/ lOut := line copyReplaceAll:(Character return) with:Character nl. |
|
99 |
"/ ]. |
|
100 |
"/ ] |
|
8933 | 101 |
]. |
12082 | 102 |
lOut isWideString ifTrue:[ |
8933 | 103 |
(lOut first = (Character value:16rFEFF)) ifTrue:[ |
12082 | 104 |
lOut := (lOut copyFrom:2) asSingleByteStringReplaceInvalidWith:(Character value:16rFF). |
8933 | 105 |
]. |
106 |
]. |
|
13340 | 107 |
lOut printOn:stream. |
8933 | 108 |
]. |
109 |
stream cr |
|
110 |
]. |
|
111 |
] ensure:[ |
|
112 |
stream close. |
|
113 |
]. |
|
114 |
||
115 |
"Modified: / 22-10-2008 / 17:52:52 / cg" |
|
116 |
! ! |
|
117 |
||
118 |
!DiffListUtility class methodsFor:'utilities'! |
|
119 |
||
120 |
diffListFor:text1 and:text2 |
|
121 |
"execute DiffCommand to get a list of diffs." |
|
122 |
||
123 |
" |
|
124 |
The returned list is in raw-diff output format, such as: |
|
125 |
1 : '1c1' |
|
126 |
2 : '< hello world' |
|
127 |
3 : '---' |
|
128 |
4 : '> Hello World' |
|
129 |
5 : '2a3' |
|
130 |
6 : '> line2' |
|
131 |
7 : '4d4' |
|
132 |
8 : '< line4' |
|
133 |
" |
|
134 |
||
18466 | 135 |
|stream line |
136 |
diffList diffTemplate diffCmd tmpStream1 tmpStream2| |
|
8933 | 137 |
|
138 |
diffTemplate := self diffCommand. |
|
14334 | 139 |
diffTemplate isEmptyOrNil ifTrue:[ |
8933 | 140 |
"/ self warn:'no diff command available'. |
141 |
^ nil |
|
142 |
]. |
|
143 |
||
144 |
text1 = text2 ifTrue:[ |
|
145 |
"no diff" |
|
146 |
^ #() |
|
147 |
]. |
|
148 |
||
149 |
" |
|
150 |
save them texts in two temporary files ... |
|
151 |
" |
|
152 |
[ |
|
18466 | 153 |
self saveForDiff:text1 on:(tmpStream1 := FileStream newTemporary). |
154 |
self saveForDiff:text2 on:(tmpStream2 := FileStream newTemporary). |
|
155 |
tmpStream1 close. |
|
156 |
tmpStream2 close. |
|
8933 | 157 |
|
158 |
" |
|
159 |
start diff on it ... |
|
160 |
" |
|
161 |
diffCmd := diffTemplate |
|
18466 | 162 |
bindWith:tmpStream1 pathName |
163 |
with:tmpStream2 pathName. |
|
8933 | 164 |
|
165 |
stream := PipeStream readingFrom:diffCmd. |
|
166 |
stream isNil ifTrue:[ |
|
14334 | 167 |
"this code is not reached in win32. |
168 |
PipeStream starts a cmd.exe, which is always successful |
|
169 |
at the first place and fails later" |
|
18466 | 170 |
stream := PipeStream readingFrom:('support' asFilename constructString:diffCmd). |
8933 | 171 |
stream isNil ifTrue:[ |
172 |
self error:'cannot execute diff' mayProceed:true. |
|
173 |
^ nil. |
|
174 |
] |
|
175 |
]. |
|
176 |
||
177 |
diffList := OrderedCollection new. |
|
178 |
(stream readWaitWithTimeout:10) ifTrue:[ |
|
179 |
"/ timeout |
|
18140 | 180 |
stream abortAndClose. |
181 |
self error:'cannot execute diff (timeout)' mayProceed:true. |
|
8933 | 182 |
^ nil. |
183 |
]. |
|
184 |
||
185 |
[stream atEnd] whileFalse:[ |
|
186 |
line := stream nextLine. |
|
187 |
line notNil ifTrue:[diffList add:line] |
|
188 |
]. |
|
189 |
stream close. |
|
190 |
] ensure:[ |
|
18466 | 191 |
tmpStream1 notNil ifTrue:[ tmpStream1 fileName remove ]. |
192 |
tmpStream2 notNil ifTrue:[ tmpStream2 fileName remove ]. |
|
8933 | 193 |
]. |
194 |
^ diffList |
|
195 |
||
196 |
" |
|
197 |
self |
|
198 |
diffListFor:#( |
|
199 |
'hello world' |
|
200 |
'line1' |
|
201 |
'line3' |
|
202 |
'line4' |
|
203 |
) |
|
204 |
and: #( |
|
205 |
'Hello World' |
|
206 |
'line1' |
|
207 |
'line2' |
|
208 |
'line3' |
|
209 |
) |
|
210 |
" |
|
18140 | 211 |
|
212 |
"Modified: / 22-05-2018 / 18:18:34 / Stefan Vogel" |
|
8933 | 213 |
! ! |
214 |
||
215 |
!DiffListUtility class methodsFor:'documentation'! |
|
216 |
||
12384 | 217 |
version |
18025 | 218 |
^ '$Header$' |
12384 | 219 |
! |
220 |
||
8933 | 221 |
version_CVS |
18025 | 222 |
^ '$Header$' |
8933 | 223 |
! ! |
12358 | 224 |