-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path.task.py
1394 lines (1046 loc) · 45.3 KB
/
.task.py
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
#!/usr/bin/env python3
import argparse
import subprocess
import sys
FORMAT_HASH = '%H'
FORMAT_SUMMARY = '%s'
class TaskException(Exception):
pass
class TaskCheckException(TaskException):
def __init__(self, message):
super().__init__("CHECK FAILED: %s" % message)
def branch_list():
"""Get list of branches."""
result = subprocess.run(['git', 'branch'], stdout=subprocess.PIPE, check=True)
return [branch.strip() for branch in result.stdout.decode("utf-8").strip().split('\n')]
def current_branch():
"""Get name of the current branch."""
result = subprocess.run(['git', 'branch', '--show-current'], stdout=subprocess.PIPE, check=True)
return result.stdout.decode("utf-8").strip()
def switch_branch(branch):
"""Switch current branch."""
subprocess.run(['git', 'checkout', branch], check=True)
def commit_log(branch_name, pretty_format=FORMAT_HASH):
"""Get commit log of commits in given branch (on top of the tasks branch)."""
result = subprocess.run(
['git', 'log', '--pretty=' + pretty_format, 'origin/tasks..' + branch_name],
stdout=subprocess.PIPE,
check=True
)
return result.stdout.decode("utf-8").strip().split('\n')
def commit_show(commit, pretty_format=FORMAT_HASH):
"""Get commit information in particular format."""
result = subprocess.run(
['git', 'show', '--pretty=' + pretty_format, '-s', commit], stdout=subprocess.PIPE, check=True
)
return result.stdout.decode("utf-8").strip()
def check_branches_identical(old_branch, new_branch):
"""Check all the commits in the two branches are the same."""
if commit_log(old_branch) != commit_log(new_branch):
raise TaskCheckException('The `%s` branch changed.' % new_branch)
def check_old_commits_unchanged(old_branch, new_branch):
"""Check all the commits in old branch are unchanged in the new branch
(have the same hexsha)."""
new_hexshas = commit_log(new_branch)
new_summaries = commit_log(new_branch, FORMAT_SUMMARY)
for hexsha in commit_log(old_branch):
if hexsha not in new_hexshas:
summary = commit_show(hexsha, FORMAT_SUMMARY)
if summary not in new_summaries:
raise TaskCheckException('A commit is missing: %s' % summary)
else:
raise TaskCheckException('A commit was unexpectedly modified: %s' % summary)
def check_commits_count(branch, expected_commits_count):
commits_count = len(commit_log(branch))
diff = abs(commits_count - expected_commits_count)
msg = (
'Unexpected number of commits in branch {branch} ({diff} {quantifier} than expected).'
)
if commits_count > expected_commits_count:
raise TaskCheckException(msg.format(branch=branch, diff=diff, quantifier='more'))
if commits_count < expected_commits_count:
raise TaskCheckException(msg.format(branch=branch, diff=diff, quantifier='less'))
def check_summaries(branch, expected, skip=0):
actual = commit_log(branch, FORMAT_SUMMARY)
if actual[skip:] != expected[skip:]:
raise TaskCheckException(
'Unexpected commits in the `%s` branch. Expected summaries: \n%s' % (branch, '\n'.join(expected))
)
def git_diff(*args):
result = subprocess.run(['git', 'diff', *args], stdout=subprocess.PIPE, check=True)
return result.stdout.decode("utf-8").strip()
class Task():
branch_names = []
def reset_branches(self):
"""Delete all branches for this task and checkout them again from origin."""
subprocess.run(['git', 'reset', '--hard'])
switch_branch('main')
for branch_name in self.branch_names:
subprocess.run(['git', 'branch', '-D', branch_name])
subprocess.run(
['git', 'checkout', '-b', branch_name, 'origin/' + branch_name], check=True
)
switch_branch('main')
class CherryPick(Task):
branch_names = ['cherry-pick-main', 'cherry-pick-feature']
def start(self):
self.reset_branches()
print("""
=================
Task: cherry-pick
=================
In the `cherry-pick-feature` branch, there are some commits that modified the `fame-is-a-bee.md` \
file (and then few other commits that modified the `after-great-pain.md` file). Cherry-pick into \
the `cherry-pick-main` branch only those that modified the `fame-is-a-bee.md`.
If the history looks like this:
A---B---C---D---E---F feature
/
G---H---I---J main
Then the result should look like this:
A---B---C---D---E---F feature
/
G---H---I---J---C`--D` main
(To show only task-related branches in gitk: gitk --branches=cherry-pick-*)
""")
def check(self):
# Check the cherry-pick-feature branch hasn't changed.
check_branches_identical('origin/cherry-pick-feature', 'cherry-pick-feature')
# Check all commits from the origin/cherry-pick-main are present in the cherry-pick-main.
check_old_commits_unchanged('origin/cherry-pick-main', 'cherry-pick-main')
# Check the commits count
check_commits_count('cherry-pick-main', 5)
# Check the commit order
expected_summaries = [
'Add the name of the author of the "Fame is a bee"',
'Finisth the poem "Fame is a bee"',
'Add the first line',
'Add a poem: I started Early',
'Add a place for poems',
]
check_summaries('cherry-pick-main', expected_summaries)
print("OK")
class ConflictCherryPick(Task):
branch_names = ['conflict-cherry-pick-main', 'conflict-cherry-pick-feature']
def start(self):
self.reset_branches()
print("""
==========================
Task: conflict-cherry-pick
==========================
In the `conflict-cherry-pick-feature` branch, there are some commits that added the "Fame is a \
bee" poem to the `poems.md` file (and then few other commits that added another poem). Cherry-pick \
into the `conflict-cherry-pick-main` branch only those that added the "Fame is a bee" poem.
If the history looks like this:
A---B---C---D---E---F feature
/
G---H---I---J main
Then the result should look like this:
A---B---C---D---E---F feature
/
G---H---I---J---C`--D` main
(To show only task-related branches in gitk: gitk --branches=conflict-cherry-pick-*)
""")
def check(self):
# Check the conflict-cherry-pick-feature branch hasn't changed.
check_branches_identical('origin/conflict-cherry-pick-feature', 'conflict-cherry-pick-feature')
# Check all commits from the origin/conflict-cherry-pick-main are present in the conflict-cherry-pick-main.
check_old_commits_unchanged('origin/conflict-cherry-pick-main', 'conflict-cherry-pick-main')
# Check the commits count
check_commits_count('conflict-cherry-pick-main', 3)
# Check the commit order
expected_summaries = [
'Add the "Fame is a bee" poem',
'Add a title and author of "Fame is a bee"',
'Create a file with poems',
]
check_summaries('conflict-cherry-pick-main', expected_summaries)
# Check the file contains the correct changes
subprocess.run(['git', 'switch', 'conflict-cherry-pick-main'], check=True)
with open('poems.md') as f:
lines = [line.strip() for line in f if line.strip()]
for line in lines:
if line[:7] in ["=======", "<<<<<<<", ">>>>>>>"]:
raise TaskCheckException(
'The conflict was not resolved, there are some conflict markings '
'left: %s' % line[:7]
)
expected_lines = [
'# Fame is a bee',
'*By Emily Dickinson*',
'Fame is a bee.',
'It has a song --',
'It has a sting --',
'Ah, too, it has a wing.',
]
if lines != expected_lines:
raise TaskCheckException(
'The content of poems.md is different than expected. '
'Expected lines (without empty lines):\n%s' % '\n'.join(expected_lines)
)
print("OK")
class Merge(Task):
branch_names = ['merge-main', 'merge-feature']
def start(self):
self.reset_branches()
print("""
===========
Task: merge
===========
Merge the `merge-feature` branch into the `merge-main` branch (and create a merge commit in the \
process).
If the history looks like this:
A---B feature
/
C---D---E---F main
Then the result should look like this:
A---B feature
/ \\
C---D---E---F---G main
The merge commit can contain a message describing the whole feature that was merged.
(To show only task-related branches in gitk: gitk --branches=merge-*)
""")
def check(self):
# Check the merge-feature branch hasn't changed.
check_branches_identical('origin/merge-feature', 'merge-feature')
# Check all commits from the origin/merge-main and origin/merge-feature branches are
# present in merge-main.
check_old_commits_unchanged('origin/merge-main', 'merge-main')
check_old_commits_unchanged('origin/merge-feature', 'merge-main')
# Check the commits count (old ones, plus one merge commit)
check_commits_count('merge-main', 7)
# Check last commit is the new merge commit
last_commit_hexsha = commit_show('merge-main')
last_commit_summary = commit_show('merge-main', FORMAT_SUMMARY)
old_hexshas = commit_log('origin/merge-main') + commit_log('origin/merge-feature')
old_summaries = commit_log('origin/merge-main', FORMAT_SUMMARY)
old_summaries += commit_log('origin/merge-feature', FORMAT_SUMMARY)
if last_commit_hexsha in old_hexshas:
raise TaskCheckException('The last commit is not new: %s' % last_commit_summary)
if last_commit_summary in old_summaries:
raise TaskCheckException(
'The last commit is probably not new: %s' % last_commit_summary
)
print("OK")
class Rebase(Task):
branch_names = ['rebase-main', 'rebase-feature']
def start(self):
self.reset_branches()
print("""
============
Task: rebase
============
Rebase the `rebase-feature` branch on top of the `rebase-main` branch.
If the history looks like this:
A---B feature
/
D---E---F---G main
Then the result should look like this:
A'--B' feature
/
D---E---F---G main
(To show only task-related branches in gitk: gitk --branches=rebase-*)
""")
def check(self):
# Check the rebase-main branch hasn't changed.
check_branches_identical('origin/rebase-main', 'rebase-main')
# Check all commits from the origin/rebase-main are present in the rebase-feature.
check_old_commits_unchanged('origin/rebase-main', 'rebase-feature')
# Check the commits count
check_commits_count('rebase-feature', 6)
# Check the commit order
expected_summaries = [
'Add a poem I started early',
'Update the poem to the newer version',
'Add "by" before the name of the author',
'Add a poem: Forever is composed of nows',
'Add the missing name of the author',
'Add the poem The Chariot',
]
check_summaries('rebase-feature', expected_summaries)
print("OK")
class ConflictRebase(Task):
branch_names = ['conflict-rebase-main', 'conflict-rebase-feature']
def start(self):
self.reset_branches()
print("""
=====================
Task: conflict-rebase
=====================
Rebase the `conflict-rebase-feature` branch on top of the `conflict-rebase-main` branch.
In this scenario, you will encounter a conflict and will need to resolve it.
If the history looks like this:
A---B feature
/
D---E---F---G main
Then the result should look like this:
A'--B' feature
/
D---E---F---G main
(To show only task-related branches in gitk: gitk --branches=conflict-rebase-*)
""")
def check(self):
# Check the conflict-rebase-main branch hasn't changed.
check_branches_identical('origin/conflict-rebase-main', 'conflict-rebase-main')
# Check all commits from the origin/conflict-rebase-main are present in the conflict-rebase-feature.
check_old_commits_unchanged('origin/conflict-rebase-main', 'conflict-rebase-feature')
# Check the commits count
check_commits_count('conflict-rebase-feature', 5)
# Check the commit order
expected_summaries = [
'Fix a typo in the word "forever" in "Forever is composed of nows"',
'Split the poem "Forever is composed by nows" correctly into lines',
'Add a poem "I started early"',
'Add a poem "The Chariot"',
'Add a poem "Forever is composed of nows"',
]
check_summaries('conflict-rebase-feature', expected_summaries)
# Check the forever-is-composed-of-nows.md file is correct
expected_start = [
"# Forever – is composed of Nows",
"*By Elimy Dickinson*",
"Forever – is composed of Nows –",
"‘Tis not a different time –",
]
if current_branch() != 'conflict-rebase-feature':
switch_branch('conflict-rebase-feature')
with open('forever-is-composed-of-nows.md') as f:
lines = [line.strip() for line in f if line.strip()]
if lines[:4] != expected_start:
raise TaskCheckException(
'The content of forever-is-composed-of-nows.md is different than expected. '
'It should start with (without empty lines):\n%s' % '\n'.join(expected_start)
)
print("OK")
class ResetHard(Task):
branch_names = ['simple']
def start(self):
self.reset_branches()
print("""
================
Task: reset-hard
================
Reset the `simple` branch to the point right before the last commit. Reset both the \
index and the working tree, i.e. completely discard all changes introduced by the commit.
If the history looks like this:
A---B---C---D---E---F main
Then the result of resetting to commit E should look like this:
A---B---C---D---E main
""")
def check(self):
# Check all commits from the origin/simple are present in simple.
new_hexshas = commit_log('simple')
new_summaries = commit_log('simple', FORMAT_SUMMARY)
skip_first = True
for hexsha in commit_log('origin/simple'):
if skip_first:
skip_first = False
continue
if hexsha not in new_hexshas:
summary = commit_show(hexsha, FORMAT_SUMMARY)
if summary not in new_summaries:
raise TaskCheckException('A commit is missing: %s' % summary)
else:
raise TaskCheckException(
'A commit was unexpectedly modified: %s' % summary
)
# Check the commits count
check_commits_count('simple', 7)
# Check the commit order
expected_summaries = [
'Fix the name of the poem: Because I could not stop for Death',
'Keep both versions of the poem after all',
'Update the poem to the newer version',
'Add "by" before the name of the author',
'Add a poem: Forever is composed of nows',
'Add the missing name of the author',
'Add the poem The Chariot',
]
check_summaries('simple', expected_summaries)
print("OK")
class ResetSoft(Task):
branch_names = ['simple']
def start(self):
self.reset_branches()
print("""
================
Task: reset-soft
================
Reset the `simple` branch to the point right before the last commit, but keep the index \
and the working tree.
""")
def check(self):
# Check all commits from the origin/reset-soft-main are present in reset-soft-main.
new_hexshas = commit_log('simple')
new_summaries = commit_log('simple', FORMAT_SUMMARY)
skip_first = True
for hexsha in commit_log('origin/simple'):
if skip_first:
skip_first = False
continue
if hexsha not in new_hexshas:
summary = commit_show(hexsha, FORMAT_SUMMARY)
if summary not in new_summaries:
raise TaskCheckException('A commit is missing: %s' % summary)
else:
raise TaskCheckException(
'A commit was unexpectedly modified: %s' % summary
)
# Check the commits count
check_commits_count('simple', 7)
# Check the commit order
expected_summaries = [
'Fix the name of the poem: Because I could not stop for Death',
'Keep both versions of the poem after all',
'Update the poem to the newer version',
'Add "by" before the name of the author',
'Add a poem: Forever is composed of nows',
'Add the missing name of the author',
'Add the poem The Chariot',
]
check_summaries('simple', expected_summaries)
diff_staged = git_diff('--staged')
diff_resetted_commit = git_diff('origin/simple^', 'origin/simple')
if diff_staged != diff_resetted_commit:
raise TaskCheckException('The index is not the same as the resetted commit.')
print("OK")
class Revert(Task):
branch_names = ['simple']
def start(self):
self.reset_branches()
print("""
============
Task: revert
============
In a branch `simple`, there is a commit with summary "Add a poem: Forever is composed of \
nows". Revert this commit.
Note that you don't want to change the history of the `simple` branch, but to create new \
commit that is opposite to the one you want to undo.
If the history looks like this:
A---B---C---D---E---F main
Then the result should look like this:
A---B---C---D---E---F---D' main
""")
def check(self):
# Check all commits from the origin/simple are present in simple.
check_old_commits_unchanged('origin/simple', 'simple')
# Check the commits count
check_commits_count('simple', 9)
# Check the commit order
expected_summaries = [
'<REVERT COMMIT>',
'Add a poem I started early',
'Fix the name of the poem: Because I could not stop for Death',
'Keep both versions of the poem after all',
'Update the poem to the newer version',
'Add "by" before the name of the author',
'Add a poem: Forever is composed of nows',
'Add the missing name of the author',
'Add the poem The Chariot',
]
check_summaries('simple', expected_summaries, skip=1)
# Check the last commit is the correct reverted commit by comparing diffs
commits = commit_log('simple')
expected_diff = git_diff(commits[7], commits[6])
revert_commit_diff = git_diff(commits[0], commits[1])
if revert_commit_diff != expected_diff:
raise TaskCheckException(
'The last commit is not the reverted commit.\n\n'
'Expected diff:\n\n%s\n\nActual diff:\n\n%s' % (expected_diff, revert_commit_diff))
print("OK")
class ChangeMessage(Task):
branch_names = ["change-message-tasks"]
def start(self):
self.reset_branches()
print("""
====================
Task: change-message
====================
In a branch `change-message-tasks`, there are several commits that make up a complete poem by \
Emily Dickinson. The very first commit of the branch has a wrong commit message saying \
`Add title and author`.
Use interactive rebase to replace the commit message, so that the new message is `Add 'After Great Pain' by Emily Dickinson.`
Make sure that the commit history remains unchanged, except for this one commit message.
""")
def check(self):
# Check the commits count
check_commits_count('change-message-tasks', 2)
# Check the commit order
expected_summaries = [
'Add text.',
"Add 'After Great Pain' by Emily Dickinson.",
]
main_summaries = commit_log('change-message-tasks', FORMAT_SUMMARY)
# Check that the commit message has been changed.
if main_summaries[1] != expected_summaries[1]:
raise TaskCheckException(
'The commit message seems not be changed correctly.\nCurrent message: '
'%s\nExpected message: %s' % (main_summaries[1], expected_summaries[1]))
# Check summaries
check_summaries('change-message-tasks', expected_summaries)
print("OK")
class SquashCommit(Task):
branch_names = ["squash-commits-tasks"]
def start(self):
self.reset_branches()
print("""
====================
Task: squash-commits
====================
In a branch `squash-commits-tasks`, there are several commits that make up a complete poem by \
Emily Dickinson. Before we merge the content of this branch into `main`, we would like to squash \
the commits so that the whole added content is only represented by \
the very first commit. All following commits should be squashed into the first one.
Use interactive rebase to squash the commits, so that there is only the very first commit left in \
the branch, while the content of the branch remains unchanged.
""")
def check(self):
# Check the commits count
check_commits_count('squash-commits-tasks', 1)
# Check the commit order.
expected_summaries = [
"Add 'Fame is a bee' by Emily Dickinson.",
]
main_summaries = commit_log('squash-commits-tasks', FORMAT_SUMMARY)
if main_summaries[0] != "Add 'Fame is a bee' by Emily Dickinson.":
raise TaskCheckException(
'The message of the first commit has changed, but it should be the same.\n'
'Expected commit message: %s\nCurrent commit message: %s' % (
expected_summaries[0], main_summaries[0]
)
)
# Check that there is no difference in content between the original and the squashed
# repository.
original = commit_show('origin/squash-commits-tasks')
new = commit_show('squash-commits-tasks')
diff = git_diff(original, new)
if diff:
raise TaskCheckException(
'The content of the squashed branch is different from the original branch.\n'
'See the diff: \n%s' % diff)
print("OK")
class ReorganizeCommits(Task):
branch_names = ["reorganize-commits-tasks"]
def start(self):
self.reset_branches()
print("""
========================
Task: reorganize-commits
========================
In a branch `reorganize-commits-tasks`, there are several commits that make up two complete poems \
by Emily Dickinson. Before we merge the content of this branch into `main`, we would like to \
squash the commits so that the whole added content is only represented by two commits, each one \
for a particular poem.
Use interactive rebase to reorganize, squash and reword the commits, so that there are only two \
commits left in the branch, while the content of the branch remains unchanged.
The final commits should be named `Poem 1: Add a poem.` and `Poem 2: Add a poem.`
""")
def check(self):
# Check the commits count
check_commits_count('reorganize-commits-tasks', 2)
# Check the commit order.
expected_summaries = [
"Poem 2: Add a poem.",
"Poem 1: Add a poem.",
]
main_summaries = commit_log('reorganize-commits-tasks', FORMAT_SUMMARY)
if main_summaries[0] != "Poem 2: Add a poem.":
raise TaskCheckException(
'The message of the second commit differs from what is expected.\n'
'Expected commit message: %s\nCurrent commit message: %s' % (
expected_summaries[0], main_summaries[0]
)
)
if main_summaries[1] != "Poem 1: Add a poem.":
raise TaskCheckException(
'The message of the first commit differs from what is expected.\n'
'Expected commit message: %s\nCurrent commit message: %s' % (
expected_summaries[1], main_summaries[1]
)
)
# Check that there is no difference in content between the original and the squashed
# repository.
original = commit_show('origin/reorganize-commits-tasks')
new = commit_show('reorganize-commits-tasks')
diff = git_diff(original, new)
if diff:
raise TaskCheckException(
'The content of the squashed branch is different from the original branch.\n'
'See the diff: \n%s' % diff)
print("OK")
class CommitAmend(Task):
branch_names = ["simple"]
def start(self):
self.reset_branches()
print("""
==================
Task: commit-amend
==================
In the branch `simple`, the last commit adds a poem by Emily Dickinson. \
Unfortunately, one of the writers made a typo and left this mistake in the name of \
the author which is `Elimy` but should be `Emily`. Before we merge the content of this branch \
into `main`, we would like to correct the mistake before we do so.
Since this is only a minor change, do not produce an extra commit, but add the change \
to the existing commit instead.
After the change, there should be the same number of commits in the branch with the same commit \
messages as before!
""")
def check(self):
# Check the commits count
check_commits_count('simple', 8)
# Check the summaries were not changed.
if commit_log('origin/simple', FORMAT_SUMMARY) != commit_log('simple', FORMAT_SUMMARY):
last_original = commit_log('origin/simple', FORMAT_SUMMARY)[0]
last_new = commit_log('simple', FORMAT_SUMMARY)[0]
if last_original != last_new:
raise TaskCheckException(
'The commit messages differ from what is expected.\nExpected commit '
'message: %s\nCurrent commit message: %s' % (last_original, last_new)
)
raise TaskCheckException('The commit messages on the branch `simple` changed.')
# Check that there is a difference in content between the original and the new commit.
original = commit_show('origin/simple')
new = commit_show('simple')
diff = git_diff(original, new)
if not diff:
raise TaskCheckException(
'The content of the branch seems not to be corrected! '
'The text is the same as it was before.\n')
else:
if "+*By Emily Dickinson*" not in diff:
raise TaskCheckException(
'The mistake was not corrected as expected.\n\n'
'See the diff:\n%s' % diff)
print("OK")
class Stash(Task):
branch_names = ["stash-tasks"]
def start(self):
self.reset_branches()
print("""
===========
Task: stash
===========
In the branch `stash-tasks`, there is one commit that makes up a skeleton for your own poem. You \
should change the skeleton file into a text of your liking. Change some lines and save the file.
Unfortunately, before you could commit and push the changes, you have learnt that the remote \
branch has been rebased and you need to reset your local branch to the remote branch, but you do \
not want to lose any changes you have already made in your local branch.
Use stash to protect your changes and reset local branch onto the remote one.
""")
def check(self):
# Check the apply-stash-tasks branch hasn't changed.
check_branches_identical('origin/stash-tasks', 'stash-tasks')
# Check that there is a difference in content between the original and the new commit.
original = commit_show('origin/stash-tasks')
new = commit_show('stash-tasks')
diff = git_diff(original, new)
if diff:
raise TaskCheckException(
'The content of the branch is different from the original branch.\n'
'See the diff: \n%s' % diff)
# Check that there is a stash saved.
result = subprocess.run(['git', 'stash', 'list'], stdout=subprocess.PIPE, check=True)
stash_list = result.stdout.decode("utf-8").strip()
if not stash_list:
raise TaskCheckException(
'Nothing has been put into stash. The content is not protected.\n\n'
'Expected was something like "stash@{0}: WIP on stash-tasks: ..."')
print("OK")
class ApplyStash(Task):
branch_names = ["apply-stash-tasks"]
def start(self):
self.reset_branches()
print("""
=================
Task: apply-stash
=================
In the branch `apply-stash-tasks`, there is one commit that makes up a skeleton for your own \
poem. You should change the skeleton file into a text of your liking. Change some lines and save \
the file.
Unfortunately, before you could commit and push the changes, you have learnt that the remote \
branch has been rebased and you need to reset your local branch to the remote branch, but you do \
not want to lose any changes you have already made in your local branch.
Use stash to protect your changes and reset local branch onto the remote one. Then apply the \
stashed content and delete it from the stash. Stage the new content and commit it. Make the \
commit message be 'Add my favourite poem.'
""")
def check(self):
# Check the commits count
check_commits_count('apply-stash-tasks', 2)
# Check the commit order.
expected_summaries = [
"Add my favourite poem.",
"Add a poem skeleton.",
]
main_summaries = commit_log('apply-stash-tasks', FORMAT_SUMMARY)
if main_summaries[0] != "Add my favourite poem.":
raise TaskCheckException(
'The message of the commit differs from what is expected.\n'
'Expected commit message: %s\nCurrent commit message: %s' % (
expected_summaries[0], main_summaries[0]
)
)
# Check summaries
check_summaries('apply-stash-tasks', expected_summaries)
# Check that there is a difference in content between the original and the new commit.
original = commit_show('origin/apply-stash-tasks')
new = commit_show('apply-stash-tasks')
diff = git_diff(original, new)
if not diff:
raise TaskCheckException(
'The content of the branch seems not to be correctly applied from stash!')
# Check that there is a stash saved.
result = subprocess.run(['git', 'stash', 'list'], stdout=subprocess.PIPE, check=True)
stash_list = result.stdout.decode("utf-8").strip()
if stash_list:
raise TaskCheckException(
'There is something in the stash, but the stash should be empty.\n\n'
'See stash:\n%s' % stash_list)
print("OK")
class ConflictRevert(Task):
branch_names = ['conflict-revert-main']
def start(self):
self.reset_branches()
print("""
=====================
Task: conflict-revert
=====================
In a branch `conflict-revert-main`, revert the commit with summary "Add another poem: Fame is a bee".
Note that you don't want to change the history of the `conflict-revert-main` branch, but to \
create new commit that is opposite to the one you want to undo.
In this scenario, you will encounter a conflict and will need to resolve it (and then "git add" \
all the changes and "git revert --continue"). It is also possible that you will need to combine \
changes from both sides of the conflict!
If the history looks like this:
A---B---C---D---E---F---G main
Then the result should look like this:
A---B---C---D---E---F---G---D' main
(To show only task-related branches in gitk: gitk --branches=conflict-revert-*)
""")
def check(self):
# Check all commits from the origin/conflict-revert-main are present in
# conflict-revert-main.
check_old_commits_unchanged('origin/conflict-revert-main', 'conflict-revert-main')
# Check the commits count
check_commits_count('conflict-revert-main', 4)
# Check the commit order
expected_summaries = [
'<REVERT COMMIT>',
'Fix typos in the name and author of the second poem',
'Add another poem: Fame is a bee',
'Add poems by Emily Dickinson',
]
check_summaries('conflict-revert-main', expected_summaries, skip=1)
# Check the file contains the correct changes
subprocess.run(['git', 'switch', 'conflict-revert-main'], check=True)
with open('poems.md') as f:
lines = [line.strip() for line in f if line.strip()]
for line in lines:
if line[:7] in ["=======", "<<<<<<<", ">>>>>>>"]:
raise TaskCheckException(
'The conflict was not resolved, there are some conflict markings '
'left: %s' % line[:7]