Eskil

Diff
Login

Differences From Artifact [a4428b36b2]:

To Artifact [e9ef1db7e4]:


8
9
10
11
12
13
14
15

16
17

18

19
20
21
22
23
24
25
8
9
10
11
12
13
14

15


16

17
18
19
20
21
22
23
24







-
+
-
-
+
-
+








package require vfs
package provide vcsvfs 0.1

namespace eval vcsvfs {
    variable DataRefChan
    variable mpoints {}
    namespace eval fossil {
    namespace eval fossil {}
    }
    namespace eval svn {
    namespace eval svn {}
    }
    namespace eval git {}
}

# Create a Virtual File System showing a revision of a fossil checkout
#
# dir: Directory in a fossil checkout
# rev: Revision to mount
#
43
44
45
46
47
48
49

50
51
52
53
54
55
56
57

58
59
60
61
62
63
64
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65







+








+







    # Getting files via ls
    set allfiles [exec fossil ls --age $rev .]
    foreach line [split $allfiles \n] {
        # Expected format in a line:
        # 2012-08-21 20:38:19  tests/rev.test
        regexp {(\S+ \S+)\s+(.+)} $line -> fDate fName
        dict set finfo $fName mtimestr $fDate
        dict set finfo $fName type file
        dict set finfo $fName isfile 1
        dict set finfo $fName isdir 0
        # Mark all known directory paths and build up file tree info
        set parentStr ""
        foreach dirPath [file split $fName] {
            dict set finfo $parentStr child $dirPath 1
            dict set finfo $parentStr isfile 0
            dict set finfo $parentStr isdir 1
            dict set finfo $parentStr type directory
            set parentStr [file join $parentStr $dirPath]
        }
    }
    # Getting files via artifact
    set artifact [exec fossil artifact $rev]
    foreach line [split $artifact \n] {
        # Expected format in a line:
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
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







+




+







+







        # Each line is one file/dir name
        set fName $line
        if {[string index $fName end] eq "/"} {
            # This is a directory, strip the /
            set fName [string range $fName 0 end-1]
            dict set finfo $fName isfile 0
            dict set finfo $fName isdir 1
            dict set finfo $fName type directory
        } else {
            # This is a file
            dict set finfo $fName isfile 1
            dict set finfo $fName isdir 0
            dict set finfo $fName type file
        }
        # Mark all known directory paths and build up file tree info
        set parentStr ""
        foreach dirPath [file split $fName] {
            dict set finfo $parentStr child $dirPath 1
            dict set finfo $parentStr isfile 0
            dict set finfo $parentStr isdir 1
            dict set finfo $parentStr type directory
            set parentStr [file join $parentStr $dirPath]
        }
    }

    set xml [exec svn ls -R -r $rev --xml]
    # TBD real xml parser
    foreach line [split $xml \n] {
186
187
188
189
190
191
192












































































193
194
195
196
197
198
199
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







+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+







    #puts [dict size $finfo]
    #puts [dict get $finfo [lindex $finfo 0]]
    return $result
}

proc vcsvfs::svn::unmount {dir} {
    variable ::vcsvfs::mpoints
    # TBD: Find the mountpoint
    #dict unset mpoints $mountpoint
    #vfs::filesystem unmount $mountpoint
}

# Create a Virtual File System showing a revision of a GIT checkout
#
# dir: Directory in an SVN checkout
# rev: Revision to mount
#
# Returns: path to the generated VFS
proc vcsvfs::git::mount {dir rev} {
    variable ::vcsvfs::mpoints
    set dir [file normalize $dir]

    # Command must be run within the dir, so temporarily change pwd
    set oldpwd [pwd]
    cd $dir

    # The mount point will be at the given dir
    set root $dir

    # Getting files via ls
    set allfiles [exec git ls-tree -r --long $rev .]
    foreach line [split $allfiles \n] {
        # Each line is:
        # <mode> SP <type> SP <object> SP <object size> TAB <file>
        regexp {(\S)+\s+(\S+)\s+(\S+)\s+(\S+)\t(.*)} $line -> \
                mode type sha size fName
        # TBD: check mode to see a link
        if {$type eq "tree"} {
            dict set finfo $fName isfile 0
            dict set finfo $fName isdir 1
            dict set finfo $fName "type" directory
        } else {
            # This is a file
            dict set finfo $fName isfile 1
            dict set finfo $fName isdir 0
            dict set finfo $fName "type" file
            dict set finfo $fName "sha" $sha
            dict set finfo $fName "size" $size
            set mtime [exec git log --pretty=format:%ct -n 1 $fName]
            dict set finfo $fName "mtime" $mtime
        }
        # Mark all known directory paths and build up file tree info
        set parentStr ""
        foreach dirPath [file split $fName] {
            dict set finfo $parentStr child $dirPath 1
            dict set finfo $parentStr isfile 0
            dict set finfo $parentStr isdir 1
            dict set finfo $parentStr "type" directory
            set parentStr [file join $parentStr $dirPath]
        }
    }

    cd $oldpwd

    # Generate a mount point.
    set tail [string range $dir [string length $root] end]
    set mountpoint "${root} ($rev)"

    dict set mpoints $mountpoint "finfo" $finfo
    dict set mpoints $mountpoint "origroot" $root
    dict set mpoints $mountpoint "rev" $rev
    dict set mpoints $mountpoint "vcstype" git
    vfs::filesystem mount $mountpoint [list vcsvfs::Vfs]

    set result $mountpoint$tail
    #puts $result
    #puts [dict size $finfo]
    #puts [dict get $finfo [lindex $finfo 0]]
    return $result
}

proc vcsvfs::git::unmount {dir} {
    variable ::vcsvfs::mpoints
    # TBD: Find the mountpoint
    #dict unset mpoints $mountpoint
    #vfs::filesystem unmount $mountpoint
}

# Handler for Reflected Channel
proc vcsvfs::DataRefChan {id cmd chId args} {
298
299
300
301
302
303
304




















305
306
307
308
309
310
311
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411







+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+







    #set data [read $chId]
    #close $chId
    cd $oldpwd

    #set chId [vcsvfs::CreateDataRefChan $data]
    return [list $chId [list vcsvfs::ReadAllBeforeClose $chId]]
}

# Some notes about git commands that can be good to have
proc vcsvfs::git::openFile {rootD relative} {
    set oldpwd [pwd]
    cd [dict get $rootD "origroot"]
    set rev [dict get $rootD rev]
    set sha [dict get $rootD finfo $relative sha]
    #git cat-file
    #git show <rev>^{tree}
    # example: git show HEAD^^^:apa

    # Read through a pipe to get a binary channel
    set chId [open [list |git cat-file blob $sha] rb]
    #set data [read $chId]
    #close $chId
    cd $oldpwd

    #set chId [vcsvfs::CreateDataRefChan $data]
    return [list $chId [list vcsvfs::ReadAllBeforeClose $chId]]
}

# Parse a time string from Fossil
proc vcsvfs::fossil::mTime {mtimestr} {
    # TBD parse to mtime correct?
    set mtime [clock scan $mtimestr -gmt 1]
    return $mtime
}
371
372
373
374
375
376
377

378

379
380
381
382
383
384
385
386
387
471
472
473
474
475
476
477
478

479


480
481
482
483
484
485
486







+
-
+
-
-







            }

            return [vcsvfs::${vcstype}::openFile $rootD $relative]
        }
        stat {
            set res [dict create dev 0 ino 0 "mode" 0 nlink 0 uid 0 gid 0 \
                             size 0 atime 0 mtime 0 ctime 0 type file]
            dict set res type [dict get $finfor type]
            if {[dict get $finfor isdir]} {
            if {[dict get $finfor isfile]} {
                dict set res type directory
            } else {
                if {![dict exists $finfor mtime]} {
                    set mtime [vcsvfs::${vcstype}::mTime \
                                       [dict get $finfor mtimestr]]
                    dict set finfor "mtime" $mtime
                    # Cache in main dictionary too
                    dict set mpoints $root "finfo" $relative "mtime" $mtime
                }