Fossil SCM

fossil-scm / test / roundtrip.test
Blame History Raw 314 lines
1
#
2
# Copyright (c) 2026 D. Richard Hipp
3
#
4
# This program is free software; you can redistribute it and/or
5
# modify it under the terms of the Simplified BSD License (also
6
# known as the "2-Clause License" or "FreeBSD License".)
7
#
8
# This program is distributed in the hope that it will be useful,
9
# but without any warranty; without even the implied warranty of
10
# merchantability or fitness for a particular purpose.
11
#
12
# Author contact information:
13
# [email protected]
14
# http://www.hwaci.com/drh/
15
#
16
############################################################################
17
#
18
# Round trips: "fossil export --git" into "fossil import --git",
19
# "fossil bundle export" into "fossil bundle import", "fossil patch
20
# create" into "fossil patch apply", and the files in "fossil zip" and
21
# "fossil tarball" compared with the files of the check-in.
22
#
23
24
set path [file dirname [info script]]
25
26
require_no_open_checkout
27
28
# Returns the F-cards of the manifest of CHECKIN in REPO, sorted, as a
29
# list of {NAME HASH} pairs.
30
#
31
proc roundtrip_files {repo checkin} {
32
fossil artifact $checkin -R $repo
33
set files [list]
34
foreach line [split $::RESULT \n] {
35
if {[regexp {^F (\S+) (\S+)} $line -> name hash]} {
36
lappend files [list $name $hash]
37
}
38
}
39
return [lsort $files]
40
}
41
42
# Returns the check-ins of REPO as a dictionary from comment to
43
# {BRANCH HASH}. The comments of the check-ins in these tests are
44
# distinct.
45
#
46
proc roundtrip_checkins {repo} {
47
fossil timeline -R $repo -t ci -n 0 -F {%H|%b|%c}
48
set result [dict create]
49
foreach line [split $::RESULT \n] {
50
if {[regexp {^([0-9a-f]{40,})\|([^|]*)\|(.*)$} $line -> hash branch c]} {
51
dict set result $c [list $branch $hash]
52
}
53
}
54
return $result
55
}
56
57
# Returns the files of an archive as a dictionary from name to content,
58
# without the top-level directory PREFIX and without directories.
59
# FORMAT is "zip" or "tarball".
60
#
61
proc roundtrip_archive_files {fileName format prefix} {
62
set fd [open $fileName rb]
63
set data [read $fd]
64
close $fd
65
set result [dict create]
66
if {$format eq "tarball"} {
67
set data [zlib gunzip $data]
68
set pos 0
69
while {$pos + 512 <= [string length $data]} {
70
set header [string range $data $pos [expr {$pos + 511}]]
71
binary scan $header a100a24a12a12a8a1a100a6a2a32a32a8a8a155 \
72
name x sizeText x x type x magic x x x x x tarPrefix
73
set name [string trimright $name \0]
74
if {$name eq ""} break
75
set tarPrefix [string trimright $tarPrefix \0]
76
if {$tarPrefix ne ""} {set name $tarPrefix/$name}
77
set size [scan [string trim $sizeText "\0 "] %o]
78
set start [expr {$pos + 512}]
79
if {$type eq "0" || $type eq "\0"} {
80
dict set result $name \
81
[string range $data $start [expr {$start + $size - 1}]]
82
}
83
set pos [expr {$start + (($size + 511) / 512) * 512}]
84
}
85
} else {
86
set end [string last "PK\x05\x06" $data]
87
binary scan $data @[expr {$end + 10}]su count
88
binary scan $data @[expr {$end + 16}]iu pos
89
for {set i 0} {$i < $count} {incr i} {
90
binary scan $data @${pos}a4x6sux4x4iuiusususu \
91
sig method csize usize nameLen extraLen commentLen
92
binary scan $data @[expr {$pos + 42}]iu localPos
93
set name [string range $data [expr {$pos + 46}] \
94
[expr {$pos + 45 + $nameLen}]]
95
set pos [expr {$pos + 46 + $nameLen + $extraLen + $commentLen}]
96
if {[string index $name end] eq "/"} continue
97
binary scan $data @[expr {$localPos + 26}]susu localNameLen localExtraLen
98
set start [expr {$localPos + 30 + $localNameLen + $localExtraLen}]
99
set content [string range $data $start [expr {$start + $csize - 1}]]
100
if {$method == 8} {
101
set content [zlib inflate $content]
102
}
103
dict set result $name $content
104
}
105
}
106
set files [dict create]
107
dict for {name content} $result {
108
if {[string first $prefix/ $name] == 0} {
109
dict set files [string range $name [string length $prefix/] end] \
110
$content
111
} else {
112
dict set files $name $content
113
}
114
}
115
return $files
116
}
117
118
# Returns the files of CHECKIN in the open repository as a dictionary
119
# from name to content.
120
#
121
proc roundtrip_checkin_files {checkin} {
122
set files [dict create]
123
fossil ls -r $checkin
124
foreach name [split [string trim $::RESULT] \n] {
125
set fd [open "|[list $::fossilexe cat -r $checkin $name]" rb]
126
dict set files $name [read $fd]
127
close $fd
128
}
129
return $files
130
}
131
132
# Returns a dictionary from the name of each file in the current
133
# directory and below to its content. Leaves out the files in SKIP,
134
# files whose names start with ".", and the Windows-specific checkout
135
# database name _FOSSIL_. Tricky note: on Windows "glob *" will match names
136
# starting with ".", but not on Unix/Mac.
137
#
138
proc roundtrip_dir_files {{dir .} {skip {}}} {
139
set files [dict create]
140
foreach f [lsort [glob -nocomplain -directory $dir -tails *]] {
141
set p [expr {$dir eq "." ? $f : "$dir/$f"}]
142
if {$p in $skip} continue
143
if {[string index $f 0] eq "." || $f eq "_FOSSIL_"} continue
144
if {[file isdirectory $p]} {
145
set files [dict merge $files [roundtrip_dir_files $p $skip]]
146
} else {
147
dict set files $p [read_file $p]
148
}
149
}
150
return $files
151
}
152
153
###############################################################################
154
#
155
# A history with a branch, a rename, a deletion, a merge and a file
156
# with no newline at its end.
157
#
158
test_setup
159
set repo [file join [pwd] .rep.fossil]
160
161
write_file a.txt "one\ntwo\nthree\n"
162
write_file b.txt "b\n"
163
file mkdir sub
164
write_file sub/c.txt "c\n"
165
write_file noeol.txt "no newline"
166
fossil add a.txt b.txt sub noeol.txt
167
fossil commit -m "rt-c1"
168
169
write_file a.txt "one\nTWO\nthree\n"
170
commit_file d.txt "d\n" "rt-c2"
171
172
fossil branch new rt-feature current
173
fossil update rt-feature
174
write_file a.txt "one\nTWO\nthree\nfour\n"
175
fossil commit -m "rt-f1"
176
write_file b.txt "b2\n"
177
fossil commit -m "rt-f2"
178
179
fossil update trunk
180
fossil rm --hard d.txt
181
fossil mv --hard sub/c.txt sub/c2.txt
182
fossil commit -m "rt-c3"
183
fossil merge rt-feature
184
fossil commit -m "rt-c4"
185
186
set original [roundtrip_checkins $repo]
187
test_eq roundtrip-setup-checkins [lsort [dict keys $original]] \
188
[lsort {rt-c1 rt-c2 rt-c3 rt-c4 rt-f1 rt-f2 {initial empty check-in}
189
{Create new branch named "rt-feature"}}]
190
191
###############################################################################
192
#
193
# "fossil export --git" into "fossil import --git": each check-in keeps
194
# its comment, its branch and its files with their content.
195
#
196
exec $::fossilexe export --git > export.git
197
fossil import --git imported.fossil export.git
198
set imported [roundtrip_checkins imported.fossil]
199
200
test_eq roundtrip-git-checkins [lsort [dict keys $imported]] \
201
[lsort [dict keys $original]]
202
203
dict for {comment info} $original {
204
if {![dict exists $imported $comment]} continue
205
lassign $info branch ciHash
206
lassign [dict get $imported $comment] importedBranch importedHash
207
set label [string trim [regsub -all {[^A-Za-z0-9]+} $comment -] -]
208
test_eq roundtrip-git-branch-$label $importedBranch $branch
209
test_eq roundtrip-git-files-$label \
210
[roundtrip_files imported.fossil $importedHash] \
211
[roundtrip_files $repo $ciHash]
212
}
213
214
# The merge keeps both parents: its P-card lists two check-ins.
215
#
216
fossil artifact [lindex [dict get $imported rt-c4] 1] -R imported.fossil
217
regexp -line {^P (.*)$} $RESULT -> parents
218
test_eq roundtrip-git-merge-parents [llength $parents] 2
219
220
file delete export.git imported.fossil
221
222
###############################################################################
223
#
224
# "fossil bundle export" of a branch, and "fossil bundle import
225
# --publish" into a clone made before the branch: the clone then has
226
# each check-in of the branch under the same hash, with the same files.
227
#
228
fossil clone $repo clone.fossil
229
230
fossil update trunk
231
fossil branch new rt-bundled current
232
fossil update rt-bundled
233
write_file a.txt "bundled\n"
234
fossil commit -m "rt-b1"
235
commit_file e.txt "e\n" "rt-b2"
236
fossil update trunk
237
238
fossil bundle export rt.bundle --branch rt-bundled
239
fossil bundle import rt.bundle --publish -R clone.fossil
240
241
set afterBundle [roundtrip_checkins clone.fossil]
242
foreach comment {rt-b1 rt-b2} {
243
set ciHash [lindex [dict get [roundtrip_checkins $repo] $comment] 1]
244
test_eq "roundtrip-bundle-hash-$comment" \
245
[lindex [dict get $afterBundle $comment] 1] $ciHash
246
test_eq "roundtrip-bundle-files-$comment" \
247
[roundtrip_files clone.fossil $ciHash] [roundtrip_files $repo $ciHash]
248
}
249
fossil cat e.txt -r rt-bundled -R clone.fossil
250
test_eq roundtrip-bundle-content $RESULT "e"
251
252
file delete rt.bundle clone.fossil
253
254
###############################################################################
255
#
256
# "fossil patch create" of uncommitted changes, then "fossil revert" and
257
# "fossil patch apply": the check-out has the same changes and files.
258
#
259
fossil update trunk
260
write_file a.txt "patched\n"
261
write_file new.txt "new\n"
262
fossil add new.txt
263
fossil rm --hard b.txt
264
fossil mv --hard sub/c2.txt sub/c3.txt
265
write_file noeol.txt "still no newline"
266
267
fossil changes
268
set changesBefore [normalize_result]
269
set filesBefore [roundtrip_dir_files . {rt.patch}]
270
271
fossil patch create rt.patch
272
273
# The patch data contains the name of the computer it was made on. Tcl
274
# "info hostname" can give multiple results for the same computer (eg with
275
# a domain added) so we're not using it.
276
#
277
fossil sqlite3 --no-repository rt.patch \
278
"SELECT value FROM cfg WHERE key='hostname'"
279
test_match roundtrip-patch-hostname [normalize_result] {^\S+$}
280
281
fossil revert
282
file delete new.txt
283
fossil changes
284
test_eq roundtrip-patch-reverted [normalize_result] ""
285
286
fossil patch apply rt.patch
287
fossil changes
288
test_eq roundtrip-patch-changes [normalize_result] $changesBefore
289
test_eq roundtrip-patch-files [roundtrip_dir_files . {rt.patch}] $filesBefore
290
291
fossil revert
292
file delete new.txt rt.patch
293
294
###############################################################################
295
#
296
# The files in "fossil zip" and "fossil tarball" of a check-in are the
297
# files of the check-in, with the same content.
298
#
299
set mergeHash [lindex [dict get $original rt-c4] 1]
300
set expected [roundtrip_checkin_files $mergeHash]
301
test_eq roundtrip-archive-names [lsort [dict keys $expected]] \
302
{a.txt b.txt noeol.txt sub/c2.txt}
303
fossil zip $mergeHash rt.zip --name rt
304
test_eq roundtrip-zip-files \
305
[roundtrip_archive_files rt.zip zip rt] $expected
306
fossil tarball $mergeHash rt.tar.gz --name rt
307
test_eq roundtrip-tarball-files \
308
[roundtrip_archive_files rt.tar.gz tarball rt] $expected
309
file delete rt.zip rt.tar.gz
310
311
###############################################################################
312
313
test_cleanup
314

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button