Repository navigation
Expand file tree
/
Copy pathvalidate_commands.py
More file actions
2482 lines (2320 loc) · 76.4 KB
/
Copy pathvalidate_commands.py
File metadata and controls
2482 lines (2320 loc) · 76.4 KB
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
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""Validate every git-sim subcommand against git ground truth.
Usage: python scripts/validate_commands.py [image-output-dir]
Needs the dev environment (git-dummy, skia). Prints a PASS/FAIL table.
For each case: build a fixture repo (git-dummy plus targeted edits), construct
the scene in-process, capture what it drew (commits, where ref labels landed,
arrows between commits, zone column titles and file lists, the title), and
compare with expectations computed from the repo with GitPython. An image is
rendered per case for spot checks.
"""
import os
import shutil
import subprocess
import sys
import tempfile
import traceback
from pathlib import Path
from types import SimpleNamespace
for var in list(os.environ):
if var.lower().startswith("git_sim_"):
del os.environ[var]
import git # noqa: E402
import numpy as np # noqa: E402
from git_sim.settings import Settings, settings # noqa: E402
from git_sim.enums import ResetMode, StashSubCommand, RemoteSubCommand # noqa: E402
ROOT = Path(tempfile.mkdtemp(prefix="gitsim_validate_"))
OUT = Path(sys.argv[1]) if len(sys.argv) > 1 else ROOT / "images"
ONLY = set(sys.argv[2:]) # optional case_* names to run; empty runs everything
OUT.mkdir(exist_ok=True)
for old in OUT.glob("*.png"):
old.unlink()
PY = sys.executable
DEFAULTS = Settings().model_dump()
RESULTS = []
# ----------------------------------------------------------------------------
# fixtures
# ----------------------------------------------------------------------------
def sh(*args, cwd, check=True):
return subprocess.run(args, cwd=cwd, capture_output=True, text=True, check=check)
def g(repo, *args, check=True):
return sh("git", *args, cwd=repo, check=check).stdout
def base_repo(name="sample_repo"):
"""git-dummy repo: 10 commits, main + branch1..3 diverging at 2, branch1 merged."""
target = ROOT / name
if not target.exists():
sh(
PY,
"-m",
"git_dummy",
"--commits=10",
"--branches=4",
"--merge=1",
"--constant-sha",
f"--name={name}",
"--diverge-at=2",
cwd=ROOT,
)
return target
_counter = [0]
def fresh(builder=None, name="repo"):
"""A private copy of the base repo, optionally modified by builder(path)."""
_counter[0] += 1
dest = ROOT / f"{name}_{_counter[0]}"
shutil.copytree(base_repo(), dest)
if builder:
builder(dest)
return dest
def dirty(repo):
(repo / "untracked.txt").write_text("new\n")
(repo / "main.3").write_text("modified\n")
(repo / "main.4").write_text("staged\n")
g(repo, "add", "main.4")
def with_stash(repo):
(repo / "main.5").write_text("stashed change\n")
g(repo, "stash")
def with_remote(repo):
"""origin = bare clone of the repo; returns path of the bare remote."""
remote = repo.parent / (repo.name + "_remote.git")
sh("git", "clone", "-q", "--bare", str(repo), str(remote), cwd=repo.parent)
g(repo, "remote", "add", "origin", str(remote))
g(repo, "fetch", "-q", "origin")
g(repo, "branch", "-u", "origin/main", "main")
return remote
def local_ahead(repo):
with_remote(repo)
(repo / "local_new.txt").write_text("local\n")
g(repo, "add", ".")
g(repo, "commit", "-q", "-m", "local-only commit")
def remote_ahead(repo, n=2, conflict=False):
remote = with_remote(repo)
other = repo.parent / (repo.name + "_other")
sh("git", "clone", "-q", str(remote), str(other), cwd=repo.parent)
g(other, "config", "user.email", "o@e.com")
g(other, "config", "user.name", "Other")
for i in range(n):
(other / f"remote_new_{i}.txt").write_text(f"remote {i}\n")
if conflict:
(other / "main.1").write_text("remote edit\n")
g(other, "add", ".")
g(other, "commit", "-q", "-m", f"remote-only commit {i}")
g(other, "push", "-q")
if conflict:
(repo / "main.1").write_text("local edit\n")
g(repo, "commit", "-q", "-am", "local conflicting edit")
def conflict_branches(repo):
g(repo, "checkout", "-q", "-b", "hot")
(repo / "main.1").write_text("hot version\n")
g(repo, "commit", "-q", "-am", "hot edit")
g(repo, "checkout", "-q", "main")
(repo / "main.1").write_text("main version\n")
g(repo, "commit", "-q", "-am", "main edit")
def behind_branch(repo):
g(repo, "checkout", "-q", "-b", "behind", "HEAD~2")
# ----------------------------------------------------------------------------
# oracle helpers
# ----------------------------------------------------------------------------
def expected_commits(repo, start, n, first_parent_only):
"""The commits parse_commits visits: depth < n along parents."""
seen = set()
def walk(c, i):
if i >= n:
return
seen.add(c.hexsha)
parents = c.parents[:1] if first_parent_only else c.parents
for p in parents:
walk(p, i + 1)
walk(start, 0)
return seen
def repo_status(repo):
r = git.Repo(repo)
return {
"untracked": set(r.untracked_files),
"modified": {d.a_path for d in r.index.diff(None)},
"staged": {d.a_path for d in r.index.diff("HEAD")},
}
# ----------------------------------------------------------------------------
# scene capture
# ----------------------------------------------------------------------------
class Capture:
def __init__(self, scene):
self.scene = scene
self.cols = None
self.zone_files = {}
self.zone_arrows = {}
orig_sdz = scene.setup_and_draw_zones
orig_pz = scene.populate_zones
def sdz(
first_column_name="Untracked files",
second_column_name="Modified files",
third_column_name="Staged files",
reverse=False,
):
names = [first_column_name, second_column_name, third_column_name]
if reverse and first_column_name == "Untracked files":
names[0] = "Staging area"
if reverse and third_column_name == "Staged files":
names[2] = "Deleted changes"
self.cols = tuple(names)
return orig_sdz(
first_column_name=first_column_name,
second_column_name=second_column_name,
third_column_name=third_column_name,
reverse=reverse,
)
def pz(f1, f2, f3, a1=None, a2=None, a3=None):
a1 = {} if a1 is None else a1
a2 = {} if a2 is None else a2
a3 = {} if a3 is None else a3
orig_pz(f1, f2, f3, a1, a2, a3)
self.zone_files = {"first": set(f1), "second": set(f2), "third": set(f3)}
# arrows keyed by the column they leave: the legacy maps plus the
# scene's zone_arrows list of (name, from_column, to_column)
self.zone_arrows = {"first": set(a1), "second": set(a2), "third": set(a3)}
names = {1: "first", 2: "second", 3: "third"}
for move in getattr(scene, "zone_arrows", []):
self.zone_arrows[names[move[1]]].add(move[0])
scene.setup_and_draw_zones = sdz
scene.populate_zones = pz
# after construct -------------------------------------------------------
def commits(self):
return {k: v.get_center() for k, v in self.scene.drawnCommits.items()}
def nearest_commit(self, point, tol=1.9):
# Arrows are shortened by 0.75 (horizontal) or 1.5 (diagonal) per end.
best, dist = None, tol
for sha, c in self.commits().items():
d = float(np.linalg.norm(c[:2] - np.asarray(point)[:2]))
if d < dist:
best, dist = sha, d
return best
def refs(self):
"""ref name -> sha of the commit whose column it sits above."""
out = {}
for name, mob in self.scene.drawnRefs.items():
if mob not in self.scene.mobjects and not any(
mob in top.get_family() for top in self.scene.mobjects
):
continue # removed from the scene (e.g. tag -d)
center = mob.get_center()
best, dx = None, 0.6
for sha, c in self.commits().items():
if abs(c[0] - center[0]) < dx and center[1] > c[1]:
best, dx = sha, abs(c[0] - center[0])
out[name] = best
return out
def edges(self):
from git_sim.render.shapes import Line, CurvedArrow
found = set()
seen = set()
for top in self.scene.mobjects:
for mob in top.get_family():
if id(mob) in seen:
continue
seen.add(id(mob))
if isinstance(mob, CurvedArrow) or (
isinstance(mob, Line)
and mob.tip is not None
and mob.get_length() > 0
):
a = self.nearest_commit(mob.get_start())
b = self.nearest_commit(mob.get_end())
if a and b and a != b:
found.add((a, b))
return found
def texts(self):
out = []
for top in self.scene.mobjects:
for mob in top.get_family():
if hasattr(mob, "text") and getattr(mob, "layout", None) is not None:
out.append(mob)
return out
def text_values(self):
return [t.text for t in self.texts()]
def column_texts(self, which):
group = getattr(self.scene, f"{which}ColumnFiles", None)
if group is None:
return []
live = [
t
for t in group
if any(t in top.get_family() for top in self.scene.mobjects)
]
return [(t.text, t.strikethrough) for t in live]
def reset_settings(**overrides):
for k, v in DEFAULTS.items():
setattr(settings, k, v)
settings.auto_open = False
for k, v in overrides.items():
setattr(settings, k, v)
def _short(sha_):
return sha_[:6] if isinstance(sha_, str) else sha_
def _debug(cap):
refs = {k: _short(v) for k, v in cap.refs().items()}
raw_refs = {
k: (np.round(v.get_center()[:2], 2).tolist())
for k, v in cap.scene.drawnRefs.items()
}
edges = sorted((_short(a), _short(b)) for a, b in cap.edges())
frame = cap.scene.camera.frame
commits = {_short(s): np.round(c[:2], 2).tolist() for s, c in cap.commits().items()}
return (
f"refs={refs} ref_pos={raw_refs} commits={commits} edges={edges} zones={cap.zone_files} "
f"frame_center={np.round(frame.get_center()[:2], 2).tolist()} frame_w={frame.get_width():.2f}"
)
def run_case(name, repo, make_scene, checks, expect_exit=False, **setting_overrides):
"""Construct the scene in repo, capture, run checks (list of (label, fn) -> fn(cap) truthy).
expect_exit: the scene is supposed to refuse (print an error and exit)."""
import re
reset_settings(**setting_overrides)
cwd = os.getcwd()
os.chdir(repo)
status = "PASS"
notes = []
exited = False
try:
try:
scene = make_scene()
except SystemExit as e:
exited = True
notes.append(f"exited at init (code {e.code})")
raise
cap = Capture(scene)
scene.construct()
try:
safe = re.sub(r"[^\w.-]+", "_", name)
scene.render_image(str(OUT / f"{safe}.png"), fmt="png")
except Exception as e: # rendering problems are their own finding
notes.append(f"render failed: {e}")
failed = False
for label, fn in checks:
try:
ok = fn(cap)
except Exception as e:
ok = False
label += f" [check raised {type(e).__name__}: {e}]"
if not ok:
failed = True
notes.append(label)
if failed:
status = "FAIL"
notes.append(_debug(cap))
if expect_exit:
status = "FAIL"
notes.append("expected the scene to refuse, but it ran")
except SystemExit as e:
if not exited:
notes.append(f"exited during construct (code {e.code})")
status = "PASS" if expect_exit else "EXIT"
except Exception:
status = "CRASH"
notes.append(traceback.format_exc().strip().splitlines()[-1])
finally:
os.chdir(cwd)
RESULTS.append((name, status, "; ".join(notes)))
def head(repo):
return git.Repo(repo).head.commit
def sha(repo, rev):
return git.Repo(repo).commit(rev).hexsha
# ----------------------------------------------------------------------------
# cases
# ----------------------------------------------------------------------------
def case_log():
from git_sim.log import Log
repo = fresh()
ctx = SimpleNamespace(parent=SimpleNamespace(params={"n": 5, "all": False}))
r = git.Repo(repo)
exp = expected_commits(repo, r.head.commit, 5, False)
run_case(
"log",
repo,
lambda: Log(ctx=ctx, n=None, all=False),
[
(
"drawn commits == depth-5 ancestry incl. both merge parents",
lambda c: set(c.commits()) == exp,
),
(
"HEAD and main labels sit above HEAD commit",
lambda c: c.refs().get("HEAD") == r.head.commit.hexsha
and c.refs().get("main") == r.head.commit.hexsha,
),
(
"branch1 label above branch1 tip",
lambda c: c.refs().get("branch1") == r.heads["branch1"].commit.hexsha,
),
(
"every drawn parent link has an arrow child->parent",
lambda c: all(
(cm.hexsha, p.hexsha) in c.edges()
for cm in map(r.commit, c.commits())
for p in cm.parents
if p.hexsha in c.commits()
),
),
("title is 'git log'", lambda c: c.scene.cmd == "git log"),
],
)
ctx3 = SimpleNamespace(parent=SimpleNamespace(params={"n": 5, "all": False}))
exp3 = expected_commits(repo, r.head.commit, 3, False)
run_case(
"log -n 3",
repo,
lambda: Log(ctx=ctx3, n=3, all=False),
[
("drawn commits == depth-3 ancestry", lambda c: set(c.commits()) == exp3),
("title", lambda c: c.scene.cmd == "git log -n 3"),
],
)
ctxa = SimpleNamespace(parent=SimpleNamespace(params={"n": 5, "all": False}))
tips = {r.heads[b].commit.hexsha for b in ("branch2", "branch3")}
run_case(
"log --all",
repo,
lambda: Log(ctx=ctxa, n=None, all=True),
[
("branch2 and branch3 tips drawn", lambda c: tips <= set(c.commits())),
(
"their labels sit above their tips",
lambda c: c.refs().get("branch2") == r.heads["branch2"].commit.hexsha
and c.refs().get("branch3") == r.heads["branch3"].commit.hexsha,
),
],
)
def case_reset():
from git_sim.reset import Reset
for mode, flag, col in (
("mixed", {}, "second"),
("soft", {"soft": True}, "third"),
("hard", {"hard": True}, "first"),
):
repo = fresh(dirty)
r = git.Repo(repo)
target = r.commit("HEAD~2")
touched = set()
for cm in r.iter_commits("HEAD~2..HEAD"):
touched |= set(cm.stats.files)
st = repo_status(repo)
if mode == "hard":
expected = touched | st["modified"] | st["staged"]
elif mode == "mixed":
expected = touched | st["modified"] | st["staged"]
else:
expected = touched | st["staged"]
kwargs = dict(
commit="HEAD~2", mode=ResetMode.DEFAULT, soft=False, mixed=False, hard=False
)
kwargs.update(flag)
run_case(
f"reset HEAD~2 --{mode}",
repo,
lambda: Reset(**kwargs),
[
(
"HEAD and main labels moved above target",
lambda c: c.refs().get("HEAD") == target.hexsha
and c.refs().get("main") == target.hexsha,
),
(
f"{col} column lists files from undone commits (+ dirty)",
lambda c: c.zone_files.get(col) == expected,
),
(
"other columns follow git semantics",
lambda c: (
(
mode == "soft"
and c.zone_files["second"] == st["modified"]
and not c.zone_files["first"]
)
or (
mode == "mixed"
and not c.zone_files["first"]
and not c.zone_files["third"]
)
or (
mode == "hard"
and not c.zone_files["second"]
and not c.zone_files["third"]
)
),
),
(
"title names the mode",
lambda c: (
c.scene.cmd == f"git reset --{mode} HEAD~2"
if mode != "mixed"
else c.scene.cmd == "git reset HEAD~2"
),
),
],
)
def case_commit():
from git_sim.commit import Commit
repo = fresh(dirty)
r = git.Repo(repo)
h = r.head.commit.hexsha
st = repo_status(repo)
run_case(
"commit -m",
repo,
lambda: Commit(message="Add feature", amend=False),
[
(
"new commit abcdef drawn with arrow to old HEAD",
lambda c: "abcdef" in c.commits() and ("abcdef", h) in c.edges(),
),
(
"HEAD and main labels above abcdef",
lambda c: c.refs().get("HEAD") == "abcdef"
and c.refs().get("main") == "abcdef",
),
("message text drawn", lambda c: "Add feature" in c.text_values()),
(
"staged files flow staged -> new commit",
lambda c: c.zone_files["second"] == st["staged"]
and c.zone_files["third"] == st["staged"]
and c.zone_arrows["second"] == st["staged"],
),
(
"modified-only files stay in working directory column",
lambda c: c.zone_files["first"] == st["modified"],
),
(
"column titles",
lambda c: c.cols == ("Working directory", "Staged files", "New commit"),
),
],
)
repo2 = fresh()
r2 = git.Repo(repo2)
old_parents = {p.hexsha for p in r2.head.commit.parents}
run_case(
"commit --amend -m",
repo2,
lambda: Commit(message="Amended message", amend=True),
[
(
"old HEAD commit is no longer drawn",
lambda c: r2.head.commit.hexsha not in c.commits(),
),
(
"amended commit keeps HEAD's parents (real amend semantics)",
lambda c: any(
sha_ != "abcdef"
and {p.hexsha for p in r2.commit(sha_).parents} == old_parents
for sha_ in c.commits()
if sha_ not in {p for p in old_parents}
),
),
(
"HEAD and main labels above the amended commit",
lambda c: c.refs().get("HEAD") is not None
and c.refs().get("HEAD") == c.refs().get("main")
and c.refs()["HEAD"] not in old_parents,
),
("amended message drawn", lambda c: "Amended message" in c.text_values()),
],
)
def case_merge():
from git_sim.merge import Merge
repo = fresh()
r = git.Repo(repo)
h, b2 = r.head.commit.hexsha, r.heads["branch2"].commit.hexsha
run_case(
"merge branch2 (true merge)",
repo,
lambda: Merge(branch="branch2", no_ff=False, message="Merge commit"),
[
(
"merge commit abcdef with edges to both parents",
lambda c: {("abcdef", h), ("abcdef", b2)} <= c.edges(),
),
(
"HEAD and main above abcdef; branch2 stays",
lambda c: c.refs().get("HEAD") == "abcdef"
and c.refs().get("main") == "abcdef"
and c.refs().get("branch2") == b2,
),
],
)
repo_ff = fresh(behind_branch)
rf = git.Repo(repo_ff)
main_tip = rf.heads["main"].commit.hexsha
run_case(
"merge main (fast-forward)",
repo_ff,
lambda: Merge(branch="main", no_ff=False, message="Merge commit"),
[
("no new commit for a fast-forward", lambda c: "abcdef" not in c.commits()),
(
"HEAD and behind labels moved to main tip",
lambda c: c.refs().get("HEAD") == main_tip
and c.refs().get("behind") == main_tip,
),
],
)
repo_noff = fresh(behind_branch)
rn = git.Repo(repo_noff)
run_case(
"merge main --no-ff",
repo_noff,
lambda: Merge(branch="main", no_ff=True, message="Merge commit"),
[
("merge commit abcdef created", lambda c: "abcdef" in c.commits()),
(
"abcdef points at main tip and (curved) at old HEAD",
lambda c: ("abcdef", rn.heads["main"].commit.hexsha) in c.edges()
and ("abcdef", rn.head.commit.hexsha) in c.edges(),
),
(
"HEAD/behind above abcdef",
lambda c: c.refs().get("HEAD") == "abcdef"
and c.refs().get("behind") == "abcdef",
),
],
)
repo_c = fresh(conflict_branches)
run_case(
"merge hot (conflict)",
repo_c,
lambda: Merge(branch="hot", no_ff=False, message="Merge commit"),
[
(
"conflicted file listed in middle column",
lambda c: c.zone_files.get("second") == {"main.1"}
and c.cols[1] == "Conflicted files",
),
("no merge commit drawn", lambda c: "abcdef" not in c.commits()),
],
)
repo_e = fresh()
run_case(
"merge branch1 (already merged -> refuses)",
repo_e,
lambda: Merge(branch="branch1", no_ff=False, message="x"),
[],
expect_exit=True,
)
def case_rebase():
from git_sim.rebase import Rebase
repo = fresh()
r = git.Repo(repo)
real = set(r.git.rev_list("--all").split())
to_rebase = list(r.iter_commits("branch2..HEAD", first_parent=True))
n_copies = min(len(to_rebase), 5)
b2 = r.heads["branch2"].commit.hexsha
run_case(
"rebase branch2",
repo,
lambda: Rebase(branch="branch2"),
[
(
f"{n_copies} replayed copies, each with a dotted arrow from its original",
lambda c: (
len([s for s in c.commits() if s not in real]) == n_copies
and len([e for e in c.edges() if e[0] in real and e[1] not in real])
== n_copies
),
),
(
"first copy is a child of branch2 tip",
lambda c: any(e[1] == b2 and e[0] not in real for e in c.edges()),
),
(
"HEAD and main labels above a replayed copy (not an original)",
lambda c: c.refs().get("HEAD") not in real
and c.refs().get("HEAD") == c.refs().get("main"),
),
(
"branch2 label stays on branch2 tip",
lambda c: c.refs().get("branch2") == b2,
),
],
)
def case_cherry_pick():
from git_sim.cherrypick import CherryPick
repo = fresh()
r = git.Repo(repo)
h, b2 = r.head.commit.hexsha, r.heads["branch2"].commit.hexsha
msg = r.heads["branch2"].commit.message.strip()
run_case(
"cherry-pick branch2",
repo,
lambda: CherryPick(commit="branch2", edit=None),
[
(
"abcdef after HEAD with dotted arrow from picked commit",
lambda c: ("abcdef", h) in c.edges() and (b2, "abcdef") in c.edges(),
),
(
"HEAD/main above abcdef",
lambda c: c.refs().get("HEAD") == "abcdef"
and c.refs().get("main") == "abcdef",
),
(
"copied message drawn",
lambda c: any(
" ".join(msg[:20].split()) in " ".join(t.split())
for t in c.text_values()
),
),
],
)
run_case(
"cherry-pick branch2 -e",
fresh(),
lambda: CherryPick(commit="branch2", edit="Edited msg"),
[
("edited message drawn", lambda c: "Edited msg" in c.text_values()),
],
)
def case_revert():
from git_sim.revert import Revert
repo = fresh()
r = git.Repo(repo)
target = r.commit("HEAD~1")
run_case(
"revert HEAD~1",
repo,
lambda: Revert(commit="HEAD~1"),
[
(
"abcdef after HEAD",
lambda c: ("abcdef", r.head.commit.hexsha) in c.edges(),
),
(
"HEAD/main above abcdef",
lambda c: c.refs().get("HEAD") == "abcdef"
and c.refs().get("main") == "abcdef",
),
(
"message 'Revert <sha>'",
lambda c: any(
t.startswith("Revert " + target.hexsha[:6]) for t in c.text_values()
),
),
(
"reverted files listed",
lambda c: c.zone_files.get("second") == set(target.stats.files)
and c.cols[1] == "Changes reverted from",
),
],
)
def case_branch_tag():
from git_sim.branch import Branch
from git_sim.tag import Tag
repo = fresh()
r = git.Repo(repo)
h = r.head.commit.hexsha
run_case(
"branch new_branch",
repo,
lambda: Branch(name="new_branch"),
[
("new label above HEAD commit", lambda c: c.refs().get("new_branch") == h),
("title", lambda c: c.scene.cmd == "git branch new_branch"),
],
)
run_case(
"tag v1",
fresh(),
lambda: Tag(name="v1", commit=None, d=False),
[
("tag label above HEAD", lambda c: c.refs().get("v1") == h),
("title", lambda c: c.scene.cmd == "git tag v1"),
],
)
target = sha(repo, "HEAD~2")
run_case(
"tag v1 HEAD~2",
fresh(),
lambda: Tag(name="v1", commit="HEAD~2", d=False),
[
("tag label above HEAD~2", lambda c: c.refs().get("v1") == target),
(
"title shows the commit argument",
lambda c: c.scene.cmd == "git tag v1 HEAD~2",
),
],
)
def tagged(p):
g(p, "tag", "v9", "HEAD~1")
run_case(
"tag -d v9",
fresh(tagged),
lambda: Tag(name="v9", commit=None, d=True),
[
(
"deleted tag no longer drawn",
lambda c: "v9" not in c.refs() or c.refs().get("v9") is None,
),
("title", lambda c: c.scene.cmd == "git tag -d v9"),
],
)
def case_checkout_switch():
from git_sim.checkout import Checkout
from git_sim.switch import Switch
repo = fresh()
r = git.Repo(repo)
h, b2 = r.head.commit.hexsha, r.heads["branch2"].commit.hexsha
run_case(
"checkout branch2",
repo,
lambda: Checkout(branch="branch2", b=False),
[
(
"HEAD label moves above branch2 tip",
lambda c: c.refs().get("HEAD") == b2,
),
("main label stays above old HEAD", lambda c: c.refs().get("main") == h),
],
)
run_case(
"checkout -b feature",
fresh(),
lambda: Checkout(branch="feature", b=True),
[
(
"new branch label above HEAD",
lambda c: c.refs().get("feature") == h and c.refs().get("HEAD") == h,
),
],
)
behind = fresh(behind_branch)
rb = git.Repo(behind)
run_case(
"checkout main (from behind branch, ancestor case)",
behind,
lambda: Checkout(branch="main", b=False),
[
(
"HEAD label above main tip",
lambda c: c.refs().get("HEAD") == rb.heads["main"].commit.hexsha,
),
(
"behind label stays on its commit",
lambda c: c.refs().get("behind") == rb.head.commit.hexsha,
),
],
)
run_case(
"switch branch2",
fresh(),
lambda: Switch(branch="branch2", c=False, detach=False),
[
(
"HEAD above branch2 tip; main stays",
lambda c: c.refs().get("HEAD") == b2 and c.refs().get("main") == h,
),
],
)
run_case(
"switch -c feature",
fresh(),
lambda: Switch(branch="feature", c=True, detach=False),
[
("new label above HEAD", lambda c: c.refs().get("feature") == h),
],
)
target = sha(repo, "HEAD~2")
run_case(
"switch --detach HEAD~2",
fresh(),
lambda: Switch(branch="HEAD~2", c=False, detach=True),
[
("HEAD label above HEAD~2", lambda c: c.refs().get("HEAD") == target),
# The range HEAD~2..HEAD counts the merged-in lineage too, so the scene
# takes its "target not reached" path and draws only the target's
# ancestry; old HEAD and main are out of frame by design.
(
"frame is finite (no NaN geometry)",
lambda c: np.isfinite(c.scene.camera.frame.get_center()).all(),
),
],
)
def case_zones():
from git_sim.add import Add
from git_sim.status import Status
from git_sim.restore import Restore
from git_sim.rm import Rm
from git_sim.mv import Mv
from git_sim.clean import Clean
from git_sim.stash import Stash
repo = fresh(dirty)
st = repo_status(repo)
run_case(
"status",
repo,
lambda: Status(),
[
(
"columns = untracked / modified / staged",
lambda c: c.zone_files
== {
"first": st["untracked"],
"second": st["modified"],
"third": st["staged"],
},
),
(
"titles",
lambda c: c.cols
== ("Untracked files", "Modified files", "Staged files"),
),
],
allow_no_commits=True,
)
run_case(
"add (no files)",
fresh(dirty),
lambda: Add(files=[]),
[
(
"nothing moves without arguments",
lambda c: c.zone_files["third"] == st["staged"]
and not c.zone_arrows["first"]
and not c.zone_arrows["second"],
),
],
)
run_case(
"add untracked.txt main.3",
fresh(dirty),
lambda: Add(files=["untracked.txt", "main.3"]),
[
(
"both files arrive in staged column",
lambda c: {"untracked.txt", "main.3"} <= c.zone_files["third"],
),
(
"arrows from untracked and modified columns",
lambda c: c.zone_arrows["first"] == {"untracked.txt"}
and c.zone_arrows["second"] == {"main.3"},
),
("title", lambda c: c.scene.cmd == "git add untracked.txt main.3"),
],
)
run_case(
"add nope.txt (refuses)",
fresh(dirty),
lambda: Add(files=["nope.txt"]),
[],
expect_exit=True,
)
run_case(