/[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.10 - (hide annotations)
Mon Jul 8 04:34:49 2002 UTC (11 years, 9 months ago) by kaz
Branch: MAIN
Changes since 1.9: +4 -37 lines
* grab.lisp (added-removed): Function removed.
(mcvs-grab): Use new intersection-difference function
instead of added-removed.
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.1
164     (chatter-terse "Analyzing ~a added files.~%" (length added-files))
165     (setf added-files (mapcar #'(lambda (name)
166     (list name (word-hash-file name)))
167     added-files))
168    
169     (chatter-terse "Analyzing ~a removed files.~%" (length removed-files))
170     (setf removed-files (mapcar #'(lambda (map-entry)
171     (destructuring-bind (f-file path)
172     map-entry
173     (list path
174     (word-hash-file f-file)
175     f-file)))
176     removed-files))
177 kaz 1.4 (let ((common-word-hash (make-hash-table :test #'equalp)))
178     (determine-common-words common-word-hash added-files)
179     (determine-common-words common-word-hash removed-files)
180 kaz 1.8 (let ((threshold (max 5 (* 1/5 (+ (length added-files)
181 kaz 1.4 (length removed-files))))))
182     (eliminate-common-words common-word-hash added-files threshold)
183     (eliminate-common-words common-word-hash removed-files threshold)))
184 kaz 1.1 (chatter-terse "Determining move candidates.~%")
185     (multiple-value-bind (moved-files added-files removed-files)
186     (move-candidates added-files removed-files)
187     (let ((mapping (mapping-read *mcvs-map*))
188     (f-hash (make-hash-table :test #'equal)))
189     (when moved-files
190     (dolist (pair moved-files)
191     (destructuring-bind ((target-name hash-2)
192     (source-name hash-1 f-file) confidence)
193     pair
194     (declare (ignore hash-1 hash-2))
195     (chatter-terse "moving ~a -> ~a (confidence ~a%)~%"
196     source-name target-name (round (* confidence 100)))
197     (setf (gethash f-file f-hash) target-name)))
198    
199     (setf mapping
200     (mapcar #'(lambda (entry)
201     (let ((replacement (gethash (first entry)
202     f-hash)))
203     (if replacement
204 kaz 1.9 (list (first entry)
205     (real-to-abstract-path replacement))
206 kaz 1.1 entry)))
207     mapping))
208     (mapping-write mapping *mcvs-map*))
209     (mapping-write mapping *mcvs-map-local*)
210     (dolist (entry mapping)
211     (when (gethash (first entry) f-hash)
212     (unlink (first entry))
213 kaz 1.9 (link (abstract-to-real-path (second entry)) (first entry))))
214 kaz 1.1 (dolist (entry stable-files)
215     (unlink (first entry))
216     (link (second entry) (first entry))))
217     (when removed-files
218 kaz 1.7 (mcvs-remove nil (mapcar #'first removed-files) :no-sync t))
219 kaz 1.1 (when added-files
220     (mcvs-add nil global-options nil (mapcar #'first added-files)))))))
221    
222     (defun mcvs-grab-wrapper (global-options command-options args)
223 kaz 1.9 (flet ((error ()
224     (error "mcvs-grab: specify module name, branch and optional subdirectory.")))
225     (when (< (length args) 2)
226     (error))
227     (destructuring-bind (module branch &optional subdir &rest superfluous) args
228     (when superfluous
229     (error))
230     (mcvs-grab global-options command-options module subdir branch))))

  ViewVC Help
Powered by ViewVC 1.1.5