author | Jan Vrany <jan.vrany@fit.cvut.cz> |
Sun, 29 Jan 2012 12:53:39 +0000 | |
branch | jv |
changeset 12123 | 4bde08cebd48 |
parent 9494 | bc6540ee3987 |
child 12128 | a7ff7d66ee85 |
permissions | -rw-r--r-- |
9346 | 1 |
" |
2 |
COPYRIGHT (c) 2009 by eXept Software AG |
|
3 |
All Rights Reserved |
|
4 |
||
5 |
This software is furnished under a license and may be used |
|
6 |
only in accordance with the terms of that license and with the |
|
7 |
inclusion of the above copyright notice. This software may not |
|
8 |
be provided or otherwise made available to, or used by, any |
|
9 |
other person. No title to or ownership of the software is |
|
10 |
hereby transferred. |
|
11 |
" |
|
8933 | 12 |
"{ Package: 'stx:libtool' }" |
13 |
||
14 |
Object subclass:#DiffListUtility |
|
15 |
instanceVariableNames:'' |
|
16 |
classVariableNames:'DiffCommandTemplate' |
|
17 |
poolDictionaries:'' |
|
18 |
category:'Views-Text' |
|
19 |
! |
|
20 |
||
21 |
!DiffListUtility class methodsFor:'documentation'! |
|
22 |
||
9346 | 23 |
copyright |
24 |
" |
|
25 |
COPYRIGHT (c) 2009 by eXept Software AG |
|
26 |
All Rights Reserved |
|
27 |
||
28 |
This software is furnished under a license and may be used |
|
29 |
only in accordance with the terms of that license and with the |
|
30 |
inclusion of the above copyright notice. This software may not |
|
31 |
be provided or otherwise made available to, or used by, any |
|
32 |
other person. No title to or ownership of the software is |
|
33 |
hereby transferred. |
|
34 |
" |
|
35 |
! |
|
36 |
||
8933 | 37 |
documentation |
38 |
" |
|
39 |
a utility to encapsulate access to the diff command |
|
9494 | 40 |
(may also be a facade to a smalltalk-diff algorithm, eventually) |
8933 | 41 |
|
42 |
[author:] |
|
43 |
cg (cg@CG-PC) |
|
44 |
||
45 |
[instance variables:] |
|
46 |
||
47 |
[class variables:] |
|
48 |
||
49 |
[see also:] |
|
50 |
||
51 |
" |
|
52 |
! ! |
|
53 |
||
54 |
!DiffListUtility class methodsFor:'defaults'! |
|
55 |
||
56 |
diffCommand |
|
57 |
"return the diff-command (with argument placeHolders)" |
|
58 |
||
59 |
DiffCommandTemplate notNil ifTrue:[ ^ DiffCommandTemplate ]. |
|
60 |
||
61 |
OperatingSystem isMSDOSlike ifTrue:[ |
|
62 |
(OperatingSystem canExecuteCommand:'diff') ifFalse:[ |
|
63 |
'DiffTextView [warning]: no diff command found (please download and unzip "UnxUtils.zip" from "unxutils.sourceforge.net")' infoPrintCR. |
|
64 |
^ nil |
|
65 |
]. |
|
66 |
^ 'diff %1 %2' |
|
67 |
]. |
|
68 |
^ 'diff -b %1 %2' |
|
69 |
||
70 |
"Modified: / 30.1.1998 / 12:12:49 / cg" |
|
71 |
! |
|
72 |
||
73 |
diffCommandTemplate:aCommandTemplateString |
|
74 |
"set the diff-command template" |
|
75 |
||
76 |
DiffCommandTemplate := aCommandTemplateString |
|
77 |
! ! |
|
78 |
||
79 |
!DiffListUtility class methodsFor:'private'! |
|
80 |
||
81 |
saveForDiff:text as:filename |
|
82 |
|stream| |
|
83 |
||
84 |
[ |
|
85 |
stream := filename writeStream. |
|
86 |
text do:[:line | |
|
87 |
|lOut i| |
|
88 |
||
89 |
line notEmptyOrNil ifTrue:[ |
|
90 |
lOut := line. |
|
91 |
(line includes:Character return) ifTrue: [ |
|
92 |
(line endsWith:Character return) ifTrue:[ |
|
93 |
lOut := line copyWithoutLast:1. |
|
94 |
] ifFalse:[ |
|
95 |
i := line indexOf:Character return. |
|
96 |
(line at:i+1) == Character nl ifTrue:[ |
|
97 |
"/ crnl endings |
|
98 |
lOut := line copyReplaceString:(String crlf) withString:(String lf). |
|
99 |
] ifFalse:[ |
|
100 |
"/ cr endings |
|
101 |
lOut := line copyReplaceAll:Character return with:Character nl. |
|
102 |
]. |
|
103 |
] |
|
104 |
]. |
|
105 |
lOut bitsPerCharacter > 8 ifTrue:[ |
|
106 |
(lOut first = (Character value:16rFEFF)) ifTrue:[ |
|
107 |
lOut := (lOut copyFrom:2) asSingleByteStringIfPossible. |
|
108 |
]. |
|
109 |
lOut bitsPerCharacter > 8 ifTrue:[ |
|
110 |
lOut := lOut collect:[:ch | ch bitsPerCharacter > 8 |
|
111 |
ifTrue:[ Character value:16rFF ] |
|
112 |
ifFalse:[ ch ]]. |
|
113 |
lOut := lOut asSingleByteStringIfPossible. |
|
114 |
]. |
|
115 |
]. |
|
116 |
stream nextPutAll:lOut. |
|
117 |
]. |
|
118 |
stream cr |
|
119 |
]. |
|
120 |
] ensure:[ |
|
121 |
stream close. |
|
122 |
]. |
|
123 |
||
124 |
"Modified: / 22-10-2008 / 17:52:52 / cg" |
|
125 |
! ! |
|
126 |
||
127 |
!DiffListUtility class methodsFor:'utilities'! |
|
128 |
||
129 |
diffListFor:text1 and:text2 |
|
130 |
"execute DiffCommand to get a list of diffs." |
|
131 |
||
132 |
" |
|
133 |
The returned list is in raw-diff output format, such as: |
|
134 |
1 : '1c1' |
|
135 |
2 : '< hello world' |
|
136 |
3 : '---' |
|
137 |
4 : '> Hello World' |
|
138 |
5 : '2a3' |
|
139 |
6 : '> line2' |
|
140 |
7 : '4d4' |
|
141 |
8 : '< line4' |
|
142 |
" |
|
143 |
||
144 |
|tmpFile1 tmpFile2 stream line |
|
145 |
diffList diffTemplate diffCmd| |
|
146 |
||
147 |
diffTemplate := self diffCommand. |
|
148 |
diffTemplate isNil ifTrue:[ |
|
149 |
"/ self warn:'no diff command available'. |
|
150 |
^ nil |
|
151 |
]. |
|
152 |
||
153 |
text1 = text2 ifTrue:[ |
|
154 |
"no diff" |
|
155 |
^ #() |
|
156 |
]. |
|
157 |
||
158 |
" |
|
159 |
save them texts in two temporary files ... |
|
160 |
" |
|
161 |
[ |
|
162 |
self saveForDiff:text1 as:(tmpFile1 := Filename newTemporary). |
|
163 |
self saveForDiff:text2 as:(tmpFile2 := Filename newTemporary). |
|
164 |
||
165 |
" |
|
166 |
start diff on it ... |
|
167 |
" |
|
168 |
diffCmd := diffTemplate |
|
169 |
bindWith:tmpFile1 asString |
|
170 |
with:tmpFile2 asString. |
|
171 |
||
172 |
stream := PipeStream readingFrom:diffCmd. |
|
173 |
stream isNil ifTrue:[ |
|
174 |
stream := PipeStream readingFrom:('support' , Filename separator , diffCmd). |
|
175 |
stream isNil ifTrue:[ |
|
176 |
self error:'cannot execute diff' mayProceed:true. |
|
177 |
^ nil. |
|
178 |
] |
|
179 |
]. |
|
180 |
||
181 |
diffList := OrderedCollection new. |
|
182 |
(stream readWaitWithTimeout:10) ifTrue:[ |
|
183 |
"/ timeout |
|
184 |
stream close. |
|
185 |
self error:'cannot execute diff (timeout)' mayProceed:true. |
|
186 |
^ nil. |
|
187 |
]. |
|
188 |
||
189 |
[stream atEnd] whileFalse:[ |
|
190 |
line := stream nextLine. |
|
191 |
line notNil ifTrue:[diffList add:line] |
|
192 |
]. |
|
193 |
stream close. |
|
194 |
] ensure:[ |
|
195 |
tmpFile1 notNil ifTrue:[ tmpFile1 delete ]. |
|
196 |
tmpFile2 notNil ifTrue:[ tmpFile2 delete ]. |
|
197 |
]. |
|
198 |
^ diffList |
|
199 |
||
200 |
" |
|
201 |
self |
|
202 |
diffListFor:#( |
|
203 |
'hello world' |
|
204 |
'line1' |
|
205 |
'line3' |
|
206 |
'line4' |
|
207 |
) |
|
208 |
and: #( |
|
209 |
'Hello World' |
|
210 |
'line1' |
|
211 |
'line2' |
|
212 |
'line3' |
|
213 |
) |
|
214 |
" |
|
215 |
! ! |
|
216 |
||
217 |
!DiffListUtility class methodsFor:'documentation'! |
|
218 |
||
219 |
version_CVS |
|
12123
4bde08cebd48
trunk branched into /branches/jv
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
9494
diff
changeset
|
220 |
^ '§Header: /cvs/stx/stx/libtool/DiffListUtility.st,v 1.3 2010/05/20 09:38:12 cg Exp §' |
4bde08cebd48
trunk branched into /branches/jv
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
9494
diff
changeset
|
221 |
! ! |