/[meta-cvs]/meta-cvs/F-26D2A2DCE1CF180F943606C59DC63A51.lisp
ViewVC logotype

Contents of /meta-cvs/F-26D2A2DCE1CF180F943606C59DC63A51.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.11 - (hide annotations)
Sun Jul 14 21:43:17 2002 UTC (11 years, 9 months ago) by kaz
Branch: MAIN
CVS Tags: mcvs-0-20, mcvs-0-19
Changes since 1.10: +36 -27 lines
* code/grab.lisp (mcvs-grab): If there are only added files
or only removed files, don't bother scanning any files,
since there are no moves to compute.
1 kaz 1.3 ;;; This source file is part of the Meta-CVS program,
2     ;;; which is distributed under the GNU license.
3     ;;; Copyright 2002 Kaz Kylheku
4    
5 kaz 1.1 (require "dirwalk")
6     (require "system")
7 kaz 1.2 (require "split")
8     (require "seqfuncs")
9 kaz 1.1 (require "mapping")
10     (require "types")
11     (require "chatter")
12     (require "options")
13     (require "checkout")
14     (require "remove")
15     (require "add")
16     (provide "grab")
17    
18     (defun read-word-hash (&optional (byte-stream t))
19 kaz 1.4 (let ((word-hash (make-hash-table :test #'equalp))
20 kaz 1.1 token
21     (state :junk))
22     (labels ((new-token ()
23 kaz 1.2 (if token
24     (setf (fill-pointer token) 0)
25     (setf token (make-array '(8)
26     :element-type 'character
27     :adjustable t
28     :fill-pointer 0))))
29     (save-token ()
30     (unless (gethash token word-hash)
31     (setf (gethash token word-hash) token)
32     (setf token nil))))
33 kaz 1.1 (do ((byte (read-byte byte-stream nil)
34     (read-byte byte-stream nil)))
35 kaz 1.2 ((null byte) (if (eq state :word) (save-token)) word-hash)
36 kaz 1.1 (let ((ch (int-char byte)))
37     (ecase state
38     ((:junk)
39     (when (or (alpha-char-p ch) (digit-char-p ch)
40     (char= ch #\_))
41 kaz 1.2 (new-token)
42 kaz 1.1 (vector-push-extend ch token)
43     (setf state :word)))
44     ((:word)
45     (cond
46     ((or (alpha-char-p ch) (digit-char-p ch)
47 kaz 1.6 (char= ch #\_) (char= ch #\-))
48 kaz 1.1 (vector-push-extend ch token))
49     (t (setf state :junk)
50 kaz 1.2 (save-token))))))))))
51 kaz 1.1
52     (defun word-hash-file (name)
53     (with-open-file (s (parse-posix-namestring name)
54     :direction :input
55     :element-type 'unsigned-byte)
56     (read-word-hash s)))
57    
58 kaz 1.2 (defun correlate-word-hashes (hash-1 hash-2)
59 kaz 1.1 (let ((hc-1 (hash-table-count hash-1))
60     (hc-2 (hash-table-count hash-2)))
61     (when (> hc-1 hc-2)
62     (psetf hash-1 hash-2 hash-2 hash-1
63     hc-1 hc-2 hc-2 hc-1))
64     (let ((common-count 0))
65     (maphash #'(lambda (key element)
66     (declare (ignore key))
67     (when (gethash element hash-2)
68     (incf common-count)))
69     hash-1)
70     (let ((total-count (- (+ hc-1 hc-2) common-count)))
71     (if (zerop total-count)
72     0
73     (/ common-count total-count))))))
74    
75 kaz 1.2
76     (defun correlate-paths (path-1 path-2)
77     (let* ((split-1 (split-fields path-1 *path-sep*))
78     (split-2 (split-fields path-2 *path-sep*))
79     (longer (max (length split-1) (length split-2)))
80     (lcs-len (length (longest-common-subsequence split-1 split-2
81     :test #'string=))))
82     (case (- longer lcs-len)
83     ((0 1) 1)
84     ((2) 95/100)
85     ((3) 90/100)
86     ((4) 85/100)
87     (otherwise 80/100))))
88    
89 kaz 1.4 (defun determine-common-words (common-hash added-or-removed-files)
90     (dolist (file added-or-removed-files common-hash)
91     (maphash #'(lambda (key value)
92     (declare (ignore value))
93     (let ((existing (gethash key common-hash)))
94     (if existing
95     (setf (gethash key common-hash)
96     (1+ existing))
97     (setf (gethash key common-hash)
98     1))))
99     (second file))))
100    
101     (defun eliminate-common-words (common-hash files threshold)
102     (dolist (file files)
103     (maphash #'(lambda (key value)
104     (declare (ignore value))
105     (let ((count (gethash key common-hash)))
106     (if (and count (>= count threshold))
107     (remhash key (second file)))))
108     (second file))))
109    
110 kaz 1.1 (defun move-candidates (added-files removed-files)
111     (let (pairs candidates taken-added taken-removed)
112     (dolist (added added-files)
113     (dolist (removed removed-files)
114 kaz 1.2 (let ((correlation (correlate-word-hashes (second added)
115     (second removed))))
116 kaz 1.5 (when (>= correlation 30/100)
117 kaz 1.2 (push (list added removed
118     (* correlation (correlate-paths (first added)
119     (first removed))))
120     pairs)))))
121 kaz 1.1 (setf pairs (sort pairs #'> :key #'third))
122     (dolist (pair pairs)
123     (unless (or (member (first pair) taken-added :test #'eq)
124     (member (second pair) taken-removed :test #'eq))
125     (push (first pair) taken-added)
126     (push (second pair) taken-removed)
127     (push pair candidates)))
128     (values candidates
129     (set-difference added-files taken-added :test #'eq)
130     (set-difference removed-files taken-removed :test #'eq))))
131    
132 kaz 1.9 (defun mcvs-grab (global-options command-options module subdir branch)
133 kaz 1.1 (declare (ignore command-options))
134 kaz 1.9 (mcvs-checkout module subdir global-options
135 kaz 1.1 `(("d" ,*this-dir*) ("r" ,branch))
136     :no-generate t)
137     (in-sandbox-root-dir
138 kaz 1.9 (let ((old-paths (mapping-read *mcvs-map*))
139     invisible-old-paths new-paths added-files removed-files stable-files)
140 kaz 1.1 (chatter-info "Scanning directory structure.~%")
141 kaz 1.9 (multiple-value-setq (old-paths invisible-old-paths)
142     (separate-if #'real-path-exists old-paths
143     :key #'second))
144     (setf old-paths (mapcar #'(lambda (entry)
145     (destructuring-bind (object name &rest stuff)
146     entry
147     (list* object (abstract-to-real-path name)
148     stuff))) old-paths))
149    
150 kaz 1.1 (for-each-file-info (fi *this-dir*)
151 kaz 1.9 (let* ((path (canonicalize-path (file-name fi))))
152 kaz 1.1 (cond
153     ((regular-p fi)
154     (push path new-paths))
155     ((directory-p fi)
156     (when (path-equal path *mcvs-dir*)
157     (skip))))))
158    
159 kaz 1.10 (multiple-value-setq (stable-files removed-files added-files)
160     (intersection-difference old-paths new-paths
161     :key1 #'second
162     :test #'equal))
163 kaz 1.11 (cond
164     ((or (null added-files) (null removed-files))
165     (setf added-files (mapcar #'(lambda (name) (list name)) added-files))
166     (setf removed-files (mapcar #'(lambda (map-entry)
167     (destructuring-bind (f-file path)
168     map-entry
169     (list path nil f-file)))
170     removed-files)))
171     (t (chatter-terse "Analyzing ~a added files.~%" (length added-files))
172     (setf added-files (mapcar #'(lambda (name)
173     (list name (word-hash-file name)))
174     added-files))
175 kaz 1.1
176 kaz 1.11 (chatter-terse "Analyzing ~a removed files.~%" (length removed-files))
177     (setf removed-files (mapcar #'(lambda (map-entry)
178     (destructuring-bind (f-file path)
179     map-entry
180     (list path
181     (word-hash-file f-file)
182     f-file)))
183     removed-files))
184     (let ((common-word-hash (make-hash-table :test #'equalp)))
185     (determine-common-words common-word-hash added-files)
186     (determine-common-words common-word-hash removed-files)
187     (let ((threshold (max 5 (* 1/5 (+ (length added-files)
188     (length removed-files))))))
189     (eliminate-common-words common-word-hash added-files threshold)
190     (eliminate-common-words common-word-hash removed-files threshold)))
191     (chatter-terse "Determining move candidates.~%")))
192 kaz 1.1 (multiple-value-bind (moved-files added-files removed-files)
193 kaz 1.11 (if (or (null added-files) (null removed-files))
194     (values nil added-files removed-files)
195     (move-candidates added-files removed-files))
196 kaz 1.1 (let ((mapping (mapping-read *mcvs-map*))
197     (f-hash (make-hash-table :test #'equal)))
198     (when moved-files
199     (dolist (pair moved-files)
200     (destructuring-bind ((target-name hash-2)
201     (source-name hash-1 f-file) confidence)
202     pair
203     (declare (ignore hash-1 hash-2))
204     (chatter-terse "moving ~a -> ~a (confidence ~a%)~%"
205     source-name target-name (round (* confidence 100)))
206     (setf (gethash f-file f-hash) target-name)))
207    
208     (setf mapping
209     (mapcar #'(lambda (entry)
210     (let ((replacement (gethash (first entry)
211     f-hash)))
212     (if replacement
213 kaz 1.9 (list (first entry)
214     (real-to-abstract-path replacement))
215 kaz 1.1 entry)))
216     mapping))
217 kaz 1.11 (mapping-write mapping *mcvs-map*)
218     (dolist (entry mapping)
219     (when (gethash (first entry) f-hash)
220     (unlink (first entry))
221     (link (abstract-to-real-path (second entry)) (first entry)))))
222 kaz 1.1 (mapping-write mapping *mcvs-map-local*)
223     (dolist (entry stable-files)
224     (unlink (first entry))
225     (link (second entry) (first entry))))
226     (when removed-files
227 kaz 1.7 (mcvs-remove nil (mapcar #'first removed-files) :no-sync t))
228 kaz 1.1 (when added-files
229     (mcvs-add nil global-options nil (mapcar #'first added-files)))))))
230    
231     (defun mcvs-grab-wrapper (global-options command-options args)
232 kaz 1.9 (flet ((error ()
233     (error "mcvs-grab: specify module name, branch and optional subdirectory.")))
234     (when (< (length args) 2)
235     (error))
236     (destructuring-bind (module branch &optional subdir &rest superfluous) args
237     (when superfluous
238     (error))
239     (mcvs-grab global-options command-options module subdir branch))))

  ViewVC Help
Powered by ViewVC 1.1.5