File: mktest-git.tcl

package info (click to toggle)
tkrev 9.6.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,972 kB
  • sloc: tcl: 30,582; sh: 2,296; makefile: 22; csh: 14
file content (585 lines) | stat: -rwxr-xr-x 13,607 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
#!/bin/sh
# the next line restarts using tclsh \
exec tclsh "$0" -- ${1+"$@"}


proc cleanup_old {root} {
  if {[ file isdirectory $root ]} {
    puts "Deleting $root"
    file delete -force $root
  }
  set oldirs [glob -nocomplain -- git_test**]
  foreach od $oldirs {
    puts "Deleting $od"
    file delete -force $od
  }
}

proc clone {Root Clone} {
  puts "==============================="
  puts "CLONING"
  set exec_cmd "git clone $Root $Clone"
  puts "$exec_cmd"
  set ret [catch {eval "exec $exec_cmd"} out]
  # For some reason catch returns 1 even though it succeeded
  puts $out
  if {! [file exists $Clone/.git] && ! [file exists $Clone/config]} {
    puts "COULD NOT CLONE REPOSITORY $Root to $Clone"
    exit 1
  }
}

proc worktree {Root Branch} {
  puts "==============================="
  puts "MAKING WORKTREE"
  cd $Root
  set exec_cmd "git worktree add --track -b branch$Branch ../git_test_branch$Branch"
  puts "$exec_cmd"
  set ret [catch {eval "exec $exec_cmd"} out]
  puts $out
  cd ..
}

proc repository {Root} {
  puts "==============================="
  puts "MAKING REPOSITORY $Root"

  # Create the repository
  #file mkdir $Root
  set exec_cmd "git init --bare $Root"
  puts "$exec_cmd"
  set ret [catch {eval "exec $exec_cmd"} out]
  puts $out
  if {$ret} {
    puts "COULD NOT CREATE REPOSITORY $Root"
    exit 1
  }
  puts "CREATED $Root"
}

proc populate {clone} {
  global WD

  mkfiles $clone
  # Git needs to know our email or else it won't commit
  cd $clone
  set exec_cmd "whoami"
  puts "$exec_cmd"
  set ret [catch {eval "exec $exec_cmd"} myname]
  if {$ret} {
    puts $myname
  }
  set exec_cmd "hostname"
  puts "$exec_cmd"
  set ret [catch {eval "exec $exec_cmd"} host]
  if {$ret} {
    puts $host
  }
  set mymail "$myname@$host"
  set exec_cmd "git config user.name $myname"
  puts "$exec_cmd"
  set ret [catch {eval "exec $exec_cmd"} out]
  if {$ret} {
    puts $out
  }
  set exec_cmd "git config user.email $mymail"
  puts "$exec_cmd"
  set ret [catch {eval "exec $exec_cmd"} out]
  if {$ret} {
    puts $out
  }
  puts "==============================="
  puts "IMPORTING FILETREE"
  set exec_cmd "git add --verbose ."
  puts "$exec_cmd"
  set ret [catch {eval "exec $exec_cmd"} out]
  puts $out
  # See what we did
  set exec_cmd "git status"
  puts "$exec_cmd"
  set ret [catch {eval "exec $exec_cmd"} out]
  puts $out
  puts "IMPORT FINISHED"
  cd $WD
}

proc newbranch {oldtag newtag} {
  global WD

  puts "==============================="
  puts "Creating new $newtag"
  puts "In [pwd]"
  cd git_test_$oldtag
  puts "In [pwd]"
  set exec_cmd "git branch --track $newtag"
  puts "$exec_cmd"
  set ret [catch {eval "exec $exec_cmd"} out]
  if {$ret} {
    puts $out
    exit
  }
  set exec_cmd "git checkout $newtag"
  puts "$exec_cmd"
  set ret [catch {eval "exec $exec_cmd"} out]
  puts $out

  cd $WD
  puts "\nIn [pwd]"
  puts "Cloning $oldtag to a new directory for $newtag"
  set exec_cmd "git clone git_test_$oldtag git_test_$newtag"
  puts "$exec_cmd"
  set ret [catch {eval "exec $exec_cmd"} out]
  puts $out

  puts "Restoring git_test_$oldtag to $oldtag"
  cd git_test_$oldtag
  puts "In [pwd]"
  set exec_cmd "git checkout $oldtag"
  puts "$exec_cmd"
  set ret [catch {eval "exec $exec_cmd"} out]
  puts $out

  cd $WD
}

proc merge {fromtag totag} {
  global WD

  cd git_test_$totag
  set exec_cmd "git merge --no-ff $fromtag"
  puts "$exec_cmd"
  set ret [catch {eval "exec $exec_cmd"} out]
  puts $out

  cd $WD
}

proc tag {tag msg} {
  if {$msg eq ""} {
    set exec_cmd "git tag $tag"
  } else {
    set exec_cmd "git tag -a $tag -m \"$msg\""
  }
  puts "$exec_cmd"
  set ret [catch {eval "exec $exec_cmd"} out]
  if {$ret} {
    puts $out
    exit 1
  }
}

proc writefile {filename string {encoding {}} } {
  puts " append \"$string\" to $filename"
  set fp [open "$filename" a]
  if {$encoding != ""} {
    chan configure $fp -encoding $encoding
  }
  puts $fp $string
  chan close $fp
}

proc addfile {filename branch} {
  puts "Add $filename on $branch"
  set exec_cmd "git add --verbose $filename"
  puts "$exec_cmd"
  set ret [catch {eval "exec $exec_cmd"} out]
  puts $out
}

proc stage {} {
  puts "Stage [pwd]"
  set exec_cmd "git add --verbose *"
  puts "$exec_cmd"
  set ret [catch {eval "exec $exec_cmd"} out]
  puts $out
}

proc delfile {filename branch} {
  puts "Delete $filename on $branch"
  file delete $filename
  set exec_cmd "git rm -r $filename"
  puts "$exec_cmd"
  set ret [catch {eval "exec $exec_cmd"} out]
  puts $out
}

proc movefile {oldname newname} {
  # rename a file
  puts "Rename $oldname to $newname"
  set exec_cmd "git mv $oldname $newname"
  puts "$exec_cmd"
  set ret [catch {eval "exec $exec_cmd"} out]
  puts $out
}

proc push {origin} {
  puts "==============================="
  set exec_cmd "git push $origin"
  puts "$exec_cmd"
  set ret [catch {eval "exec $exec_cmd"} out]
  puts $out
  if {$ret} {
    if {[string match {fatal*} $out]} {
      exit 1
    }
  }
}

proc fetch {{origin {}}} {
  puts "Fetching from $origin"
  set exec_cmd "git fetch "
  if {$origin != ""} {
    append exec_cmd " $origin"
  }
  puts "$exec_cmd"
  set ret [catch {eval "exec $exec_cmd"} out]
}

proc commit {comment} {
  # It seems to need the email all over again in a cloned directory
  set exec_cmd "whoami"
  puts "$exec_cmd"
  set ret [catch {eval "exec $exec_cmd"} myname]
  if {$ret} {
    puts $myname
  }
  set exec_cmd "hostname"
  puts "$exec_cmd"
  set ret [catch {eval "exec $exec_cmd"} host]
  if {$ret} {
    puts $host
  }
  set mymail "$myname@$host"
  set exec_cmd "git config user.name $myname"
  puts "$exec_cmd"
  set ret [catch {eval "exec $exec_cmd"} out]
  if {$ret} {
    puts $out
  }
  set exec_cmd "git config user.email $mymail"
  puts "$exec_cmd"
  set ret [catch {eval "exec $exec_cmd"} out]
  if {$ret} {
    puts $out
  }

  # Finally, do it
  set exec_cmd "git commit -m \"$comment\""
  # This is so the timestamp is different from the last commit
  after 1000
  puts "$exec_cmd"
  set ret [catch {eval "exec $exec_cmd"} out]
  puts $out
}

proc mkfiles {topdir} {
  global WD

  puts "MAKING FILETREE"
  # Make some files to put in the repository
  file mkdir "$topdir"
  cd $topdir

  # Make some files each containing a random word
  foreach n {1 2 3 4} {
    writefile "File$n.txt" "Initial"
  }
  writefile "File Spc.txt" "Initial"
  writefile "FTags.txt" "Initial"
  writefile "F-utf-8.txt" "\u20AC20" "utf-8"
  writefile "F-iso8859-1.txt" "Copyright \xA9 2025" "iso8859-1"
  foreach D {Dir1 "Dir 2"} {
    puts $D
    file mkdir $D
    foreach n {1 2 " 3" 4} {
      set subf [file join $D "F$n.txt"]
      writefile $subf "Initial"
    }
  }
  cd $WD
}

proc modfiles {string} {
  global tcl_platform

  set tmpfile "list.tmp"
  file delete -force $tmpfile
  if {$tcl_platform(platform) eq "windows"} {
    puts "Must be a PC"
    set ret [catch {eval "exec [auto_execok dir] /b F*.txt /s > $tmpfile"} out]
  } else {
    set ret [catch {eval "exec find . -name F*.txt -a -type f > $tmpfile"} out]
  }
  if {$ret} {
    puts "Find failed"
    puts $out
    exit 1
  }
  set fl [open $tmpfile r]
  while { [gets $fl item] >= 0} {
    writefile $item $string
  }
  close $fl
  file delete -force $tmpfile
}

proc conflict {filename} {
  # Create a conflict. In Git, this is done with a temporary branch.
 
  # Check out a new branch
  set exec_cmd "git checkout -b clash"
  puts "$exec_cmd"
  set ret [catch {eval "exec $exec_cmd"} out]
  puts $out
  writefile $filename "Conflict A"
  set exec_cmd "git commit -m \"change on clash\" $filename"
  puts "$exec_cmd"
  set ret [catch {eval "exec $exec_cmd"} out]
  puts $out
  # Check out head, which now conflicts with our change
  set exec_cmd "git checkout master"
  puts "$exec_cmd"
  set ret [catch {eval "exec $exec_cmd"} out]
  puts $out
  # Make a different change to same line
  writefile $filename "Conflict B"
  set exec_cmd "git commit -m \"change on master\" $filename"
  puts "$exec_cmd"
  set ret [catch {eval "exec $exec_cmd"} out]
  puts $out
  set exec_cmd "git merge clash"
  puts "$exec_cmd"
  set ret [catch {eval "exec $exec_cmd"} out]
  puts $out
}

##############################################
set branching_desired 1
set leave_a_mess 1

for {set i 1} {$i < [llength $argv]} {incr i} {
  set arg [lindex $argv $i]

  switch -regexp $arg {
    {^--*nobranch} {
      set branching_desired 0
    }
    {^--*nomess} {
      set leave_a_mess 0
    }
  }
}

if [file exists .git] {
  puts "Please don't do that here.  There's already a .git directory."
  exit 1
}

set WD [pwd]
set Root [file join $WD "GIT_REPOSITORY.git"]
set Master "git_test_master"

cleanup_old $Root

# Create the bare "server" repo
repository $Root
# Clone it to one we can work in
clone $Root $Master
# Import some files
populate $Master
cd $Master
commit "Commit the imported files"
tag "init" "the starting point"
push ""
cd $WD

# Make some changes
puts "==============================="
puts "First revision on trunk"
cd $Master
writefile .gitignore ".DS_Store"
addfile .gitignnore trunk

modfiles "Main 1"
writefile Ftrunk.txt "Main 1"
addfile Ftrunk.txt master
stage
commit "First revision on trunk"
tag "tagA" ""
tag "tagC" ""
push ""
cd $WD

if {$branching_desired} {
  puts "==============================="
  puts "MAKING BRANCH A"
  newbranch master branchA
  cd $WD/git_test_branchA
  writefile FbranchA.txt "BranchA 1"
  addfile FbranchA.txt branchA
  stage
  commit "Add file FbranchA.txt on branch A"

  puts "==============================="
  puts "First revision on Branch A"
  modfiles "BranchA 1"
  stage
  commit "First revision on branch A"

  puts "==============================="
  puts "Second revision on Branch A"
  modfiles "BranchA 2"
  stage
  commit "Second revision on branch A"
  push ""
  cd $WD

  # Branch C
  puts "==============================="
  puts "MAKING BRANCH C FROM SAME ROOT"
  worktree git_test_master C
  cd $WD/git_test_branchC
  modfiles "BranchC 1"
  writefile FbranchC.txt "BranchC 1"
  addfile FbranchC.txt branchC
  stage
  commit "First changes on Branch C"
  # Make two identical branches (OK in Git)
  set exec_cmd "git branch -c branchC branchD"
  puts "$exec_cmd"
  set ret [catch {eval "exec $exec_cmd"} out]
  cd $WD

  puts "==============================="
  puts "Merging BranchA to trunk"
  merge branchA master
  cd $WD
}

# Make more modifications on trunk
puts "==============================="
puts "Second revision on trunk"
cd $WD/$Master
fetch {--all}

modfiles "Main 2"
stage
commit "Second revision on trunk"
foreach t {one ten one_hundred one_thousand ten_thousand one_hundred_thousand} {
  tag "tag_$t" FTags.txt
}

puts "==============================="
puts "Third revision on trunk"
modfiles "Main 3"
stage
commit "Third revision on trunk"
tag "tagB" ""
tag "one" "Dir1/F1.txt"
push ". HEAD"
cd $WD

if {$branching_desired} {

  # Branch off of the branch
  puts "==============================="
  puts "MAKING BRANCH AA"
  cd $WD/git_test_branchA
  tag "tagAA" ""
  cd $WD
  worktree git_test_branchA AA
  cd $WD/git_test_branchAA
  modfiles "BranchAA 1"
  writefile FbranchAA.txt "BranchAA 1"
  addfile FbranchAA.txt branchAA
  delfile Ftrunk.txt branchAA
  stage
  commit "First changes on Branch AA"

  puts "==============================="
  puts "Revision on Branch AA"
  modfiles "BranchAA 2"
  stage
  commit "Second changes on Branch AA"
  cd $WD

  # Branch Y
  puts "==============================="
  puts "MAKING BRANCH Y"
  worktree git_test_branchAA Y
  cd $WD/git_test_branchY
  modfiles "BranchY 1"
  stage
  commit "First changes on Branch Y"
  cd $WD

  # Make another revision on branchA after
  # branchAA has branched off
  puts "==============================="
  puts "Third revision on Branch A"
  cd $WD/git_test_branchA
  modfiles "BranchA 3"
  stage
  commit "Third revision on Branch A"
  cd $WD

  # Branch B
  puts "==============================="
  puts "MAKING BRANCH B"
  worktree git_test_master B
  cd $WD/git_test_branchB
  modfiles "BranchB 1"
  writefile FbranchB.txt "BranchB 1"
  addfile FbranchB.txt branchB
  stage
  commit "First changes on Branch B"

  puts "==============================="
  puts "Revision on Branch B"
  modfiles "BranchB 2"
  stage
  commit "Second changes on Branch B"
  cd $WD

  # Update the clones
  #foreach branch {branchA branchAA branchB branchC master} {
    #cd $WD/git_test_$branch
    #push {--all}
    #fetch {--all}
    #cd $WD
  #}
}

if {$leave_a_mess} {
  # Leave the trunk with uncommitted changes
  puts "==============================="
  puts "Making Uncommitted changes on trunk"
  cd $WD/$Master
  # Local only
  writefile FileLocal.txt "Pending"
  # Conflict. Have to do this before the add and delete,
  # or the merge will fail before you get to the conflicted file
  #conflict Ftrunk.txt
  # Newly added
  writefile FileAdd.txt "Pending"
  addfile FileAdd.txt trunk
  # Rename a file. git "R " status
  # git status reports this wrong! as "R Dir1/F4.txt -> FileMoved.txt"
  #movefile ./File4.txt ./FileMoved.txt
  # Plain, empty directory
  file mkdir Dir3
  file mkdir "Dir1/New dir"
  # Missing
  file delete -- File3.txt trunk
  # Modify
  writefile File2.txt "Pending"
  writefile "F-utf-8.txt" "\xA53378" "utf-8"
  writefile "F-iso8859-1.txt" "\xA9 2022-2024" "iso8859-1"
  writefile "Dir1/F 3.txt" "Pending"
  delfile "Dir1/F2.txt" trunk
  writefile "Dir 2/F1.txt" "Pending"
  movefile "Dir1/F4.txt" "Dir1/FMoved.txt"
  # Change a file that's been moved. Only git detects this
  writefile "Dir1/FMoved.txt" "Post-move change"
  cd $WD
}