forked from reanahub/reana
-
Notifications
You must be signed in to change notification settings - Fork 0
/
git.py
1047 lines (936 loc) · 40.1 KB
/
git.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
# -*- coding: utf-8 -*-
#
# This file is part of REANA.
# Copyright (C) 2020 CERN.
#
# REANA is free software; you can redistribute it and/or modify it
# under the terms of the MIT License; see LICENSE file for more details.
"""`reana-dev`'s git commands."""
import datetime
import os
import subprocess
import sys
import click
from reana.config import (
COMPONENTS_USING_SHARED_MODULE_COMMONS,
COMPONENTS_USING_SHARED_MODULE_DB,
REPO_LIST_ALL,
REPO_LIST_SHARED,
)
from reana.reana_dev.utils import (
bump_component_version,
display_message,
fetch_latest_pypi_version,
get_current_component_version_from_source_files,
get_srcdir,
is_feature_branch,
run_command,
select_components,
update_module_in_cluster_components,
)
def get_all_branches(srcdir):
"""Return all local and remote Git branch names in the given directory.
:param srcdir: source code directory
:type srcdir: str
:return: checkout out branch in the component source code directory
:rtype: str
"""
os.chdir(srcdir)
return (
subprocess.check_output("git branch -a 2>/dev/null", shell=True)
.decode()
.split()
)
def get_current_branch(srcdir):
"""Return current Git branch name checked out in the given directory.
:param srcdir: source code directory
:type srcdir: str
:return: checkout out branch in the component source code directory
:rtype: str
"""
os.chdir(srcdir)
return (
subprocess.check_output(
'git branch 2>/dev/null | grep "^*" | colrm 1 2', shell=True
)
.decode()
.rstrip("\r\n")
)
def get_current_commit(srcdir):
"""Return information about git commit checked out in the given directory.
:param srcdir: source code directory
:type srcdir: str
:return: commit information composed of brief SHA1 and subject
:rtype: str
"""
os.chdir(srcdir)
return (
subprocess.check_output('git log --pretty=format:"%h %s" -n 1', shell=True)
.decode()
.rstrip("\r\n")
)
def git_is_current_version_tagged(component):
"""Determine whether the current version in source code is present as a git tag."""
current_version = get_current_component_version_from_source_files(component)
is_version_tagged = int(
run_command(
f"git tag --list {current_version} | wc -l",
component,
display=False,
return_output=True,
)
)
return bool(is_version_tagged)
def git_create_release_commit(component, next_version=None):
"""Create a release commit for the given component."""
if "release:" in get_current_commit(get_srcdir(component)):
display_message("Nothing to do, last commit is a release commit.", component)
return False
current_version = get_current_component_version_from_source_files(component)
if not current_version and not next_version:
display_message(
f"Version cannot be autodiscovered from source files.", component
)
sys.exit(1)
elif not git_is_current_version_tagged(component) and not next_version:
display_message(
f"Current version ({current_version}) "
"not present as a git tag, please release it and add a tag.",
component,
)
sys.exit(1)
next_version, modified_files = bump_component_version(
component, current_version, next_version=next_version
)
if (
run_command(
"git branch --show-current", component, display=False, return_output=True,
)
== "master"
):
run_command(f"git checkout -b release-{next_version}", component)
if modified_files:
run_command(f"git add {' '.join(modified_files)}", component)
run_command(
f"git commit -m 'release: {next_version}' {'--allow-empty' if not modified_files else ''}",
component,
)
return True
def compare_branches(branch_to_compare, current_branch):
"""Compare two branches with ``git rev-list``."""
cmd = "git rev-list --left-right --count {0}...{1}".format(
branch_to_compare, current_branch
)
behind, ahead = [
int(x) for x in run_command(cmd, display=False, return_output=True).split()
]
return behind, ahead
def is_component_behind_branch(
component, branch_to_compare, current_branch=None,
):
"""Report to stdout the differences between two branches."""
current_branch = current_branch or get_current_branch(get_srcdir(component))
behind, _ = compare_branches(branch_to_compare, current_branch)
return bool(behind)
def print_branch_difference_report(
component, branch_to_compare, current_branch=None, commit=None, short=False,
):
"""Report to stdout the differences between two branches."""
# detect how far it is ahead/behind from pr/origin/upstream
current_branch = current_branch or get_current_branch(get_srcdir(component))
commit = commit or get_current_commit(get_srcdir(component))
report = ""
behind, ahead = compare_branches(branch_to_compare, current_branch)
if ahead or behind:
report += "("
if ahead:
report += "{0} AHEAD ".format(ahead)
if behind:
report += "{0} BEHIND ".format(behind)
report += branch_to_compare + ")"
# detect rebase needs for local branches and PRs
if branch_to_compare != "upstream/master":
branch_to_compare = "upstream/master"
behind, ahead = compare_branches(branch_to_compare, current_branch)
if behind:
report += "(STEMS FROM "
if behind:
report += "{0} BEHIND ".format(behind)
report += branch_to_compare + ")"
click.secho("{0}".format(component), nl=False, bold=True)
click.secho(" @ ", nl=False, dim=True)
if current_branch == "master":
click.secho("{0}".format(current_branch), nl=False)
else:
click.secho("{0}".format(current_branch), nl=False, fg="green")
if report:
click.secho(" {0}".format(report), nl=False, fg="red")
click.secho(" @ ", nl=False, dim=True)
click.secho("{0}".format(commit))
# optionally, display also short status
if short:
cmd = "git status --short"
run_command(cmd, component, display=False)
def is_last_commit_release_commit(package):
"""Check whether the last commit is a release commit."""
current_commit = get_current_commit(get_srcdir(package))
return current_commit.split()[1] == "release:"
def git_push_to_origin(components):
"""Push current branch to origin."""
for component in components:
branch = run_command("git branch --show-current", component, return_output=True)
run_command(
f"git push --force origin {branch}", component,
)
@click.group()
def git_commands():
"""Git commands group."""
@click.option(
"--component",
"-c",
multiple=True,
default=["CLUSTER"],
help="Which components? [shortname|name|.|CLUSTER|ALL]",
)
@click.option(
"--browser", "-b", default="firefox", help="Which browser to use? [firefox]"
)
@git_commands.command(name="git-fork")
def git_fork(component, browser): # noqa: D301
"""Display commands to fork REANA source code repositories on GitHub.
\b
:param components: The option ``component`` can be repeated. The value may
consist of:
* (1) standard component name such as
'reana-workflow-controller';
* (2) short component name such as 'r-w-controller';
* (3) special value '.' indicating component of the
current working directory;
* (4) special value 'CLUSTER' that will expand to
cover all REANA cluster components [default];
* (5) special value 'CLIENT' that will expand to
cover all REANA client components;
* (6) special value 'DEMO' that will expand
to include several runable REANA demo examples;
* (7) special value 'ALL' that will expand to include
all REANA repositories.
:param browser: The web browser to use. [default=firefox]
:type component: str
:type browser: str
"""
components = select_components(component)
if components:
click.echo("# Fork REANA repositories on GitHub using your browser.")
click.echo(
"# Run the following eval and then complete the fork"
" process in your browser."
)
click.echo("#")
click.echo(
'# eval "$(reana-dev git-fork -b {0} {1})"'.format(
browser, "".join([" -c {0}".format(c) for c in component])
)
)
for component in components:
cmd = "{0} https://github.com/reanahub/{1}/fork;".format(browser, component)
click.echo(cmd)
click.echo(
'echo "Please continue the fork process in the opened' ' browser windows."'
)
@click.option("--user", "-u", default="anonymous", help="GitHub user name [anonymous]")
@click.option(
"--component",
"-c",
multiple=True,
default=["CLUSTER"],
help="Which components? [shortname|name|.|CLUSTER|ALL]",
)
@git_commands.command(name="git-clone")
def git_clone(user, component): # noqa: D301
"""Clone REANA source repositories from GitHub.
If the ``user`` argument is provided, the ``origin`` will be cloned from
the user repository on GitHub and the ``upstream`` will be set to
``reanahub`` organisation. Useful for setting up personal REANA development
environment,
If the ``user`` argument is not provided, the cloning will be done in
anonymous manner from ``reanahub`` organisation. Also, the clone will be
shallow to save disk space and CPU time. Useful for CI purposes.
\b
:param components: The option ``component`` can be repeated. The value may
consist of:
* (1) standard component name such as
'reana-workflow-controller';
* (2) short component name such as 'r-w-controller';
* (3) special value '.' indicating component of the
current working directory;
* (4) special value 'CLUSTER' that will expand to
cover all REANA cluster components [default];
* (5) special value 'CLIENT' that will expand to
cover all REANA client components;
* (6) special value 'DEMO' that will expand
to include several runable REANA demo examples;
* (7) special value 'ALL' that will expand to include
all REANA repositories.
:param user: The GitHub user name. [default=anonymous]
:type component: str
:type user: str
"""
components = select_components(component)
for component in components:
os.chdir(get_srcdir())
if os.path.exists("{0}/.git/config".format(component)):
msg = "Component seems already cloned. Skipping."
display_message(msg, component)
elif user == "anonymous":
cmd = "git clone https://github.com/reanahub/{0} --depth 1".format(
component
)
run_command(cmd)
else:
cmd = "git clone git@github.com:{0}/{1}".format(user, component)
run_command(cmd)
for cmd in [
"git remote add upstream"
' "git@github.com:reanahub/{0}"'.format(component),
"git config --add remote.upstream.fetch"
' "+refs/pull/*/head:refs/remotes/upstream/pr/*"',
]:
run_command(cmd, component)
@click.option(
"--component",
"-c",
multiple=True,
default=["CLUSTER"],
help="Which components? [shortname|name|.|CLUSTER|ALL]",
)
@click.option(
"--short",
"-s",
is_flag=True,
default=False,
help="Show git status short format details?",
)
@git_commands.command(name="git-status")
def git_status(component, short): # noqa: D301
"""Report status of REANA source repositories.
\b
:param components: The option ``component`` can be repeated. The value may
consist of:
* (1) standard component name such as
'reana-workflow-controller';
* (2) short component name such as 'r-w-controller';
* (3) special value '.' indicating component of the
current working directory;
* (4) special value 'CLUSTER' that will expand to
cover all REANA cluster components [default];
* (5) special value 'CLIENT' that will expand to
cover all REANA client components;
* (6) special value 'DEMO' that will expand
to include several runable REANA demo examples;
* (7) special value 'ALL' that will expand to include
all REANA repositories.
:param verbose: Show git status details? [default=False]
:type component: str
"""
components = select_components(component)
for component in components:
current_branch = get_current_branch(get_srcdir(component))
# detect all local and remote branches
all_branches = get_all_branches(get_srcdir(component))
# detect branch to compare against
if current_branch == "master": # master
branch_to_compare = "upstream/master"
elif current_branch.startswith("pr-"): # other people's PR
branch_to_compare = "upstream/" + current_branch.replace("pr-", "pr/")
else:
branch_to_compare = "origin/" + current_branch # my PR
if "remotes/" + branch_to_compare not in all_branches:
branch_to_compare = "origin/master" # local unpushed branch
print_branch_difference_report(component, branch_to_compare, short=short)
@click.option(
"--component",
"-c",
multiple=True,
default=["CLUSTER"],
help="Which components? [shortname|name|.|CLUSTER|ALL]",
)
@git_commands.command(name="git-clean")
def git_clean(component): # noqa: D301
"""Clean REANA source repository code tree.
Removes all non-source-controlled files in the component source code
repository. Useful to run before building container images.
\b
:param components: The option ``component`` can be repeated. The value may
consist of:
* (1) standard component name such as
'reana-workflow-controller';
* (2) short component name such as 'r-w-controller';
* (3) special value '.' indicating component of the
current working directory;
* (4) special value 'CLUSTER' that will expand to
cover all REANA cluster components [default];
* (5) special value 'CLIENT' that will expand to
cover all REANA client components;
* (6) special value 'DEMO' that will expand
to include several runable REANA demo examples;
* (7) special value 'ALL' that will expand to include
all REANA repositories.
:type component: str
"""
components = select_components(component)
for component in components:
for cmd in [
"git clean -d -ff -x",
]:
run_command(cmd, component)
@click.option(
"--update", is_flag=True, help="Update shared modules everywhere?", default=False
)
@click.option(
"--status",
is_flag=True,
help="Show status of shared modules everywhere.",
default=False,
)
@click.option(
"--delete", is_flag=True, help="Delete shared modules everywhere?", default=False
)
@git_commands.command(name="git-submodule")
def git_submodule(update=False, status=False, delete=False): # noqa: D301
"""Sync REANA shared modules across all the repositories.
Take currently checked-out reana-commons and reana-db modules and sync them
across REANA components. Useful for building container images with
not-yet-released shared modules.
The option ``--update`` propagates the shared modules across the code base
as necessary. Useful before running local docker image building.
The option ``--status`` shows the information about shared modules.
The option ``--delete`` removes the shared modules from everywhere. Useful
for clean up after testing.
"""
if update:
for component in COMPONENTS_USING_SHARED_MODULE_COMMONS:
for cmd in [
"rsync -az ../reana-commons modules",
]:
run_command(cmd, component)
for component in COMPONENTS_USING_SHARED_MODULE_DB:
for cmd in [
"rsync -az ../reana-db modules",
]:
run_command(cmd, component)
elif delete:
for component in set(
COMPONENTS_USING_SHARED_MODULE_COMMONS + COMPONENTS_USING_SHARED_MODULE_DB
):
for cmd in [
"rm -rf ./modules/",
]:
run_command(cmd, component)
elif status:
for component in COMPONENTS_USING_SHARED_MODULE_COMMONS:
for cmd in [
"git status -s",
]:
run_command(cmd, component)
for component in COMPONENTS_USING_SHARED_MODULE_DB:
for cmd in [
"git status -s",
]:
run_command(cmd, component)
else:
click.echo(
"Unknown action. Please specify `--update`, `--status` "
" or `--delete`. Exiting."
)
sys.exit(1)
@click.option(
"--component",
"-c",
multiple=True,
default=["CLUSTER"],
help="Which components? [shortname|name|.|CLUSTER|ALL]",
)
@git_commands.command(name="git-branch")
def git_branch(component): # noqa: D301
"""Display information about locally checked-out branches.
\b
:param components: The option ``component`` can be repeated. The value may
consist of:
* (1) standard component name such as
'reana-workflow-controller';
* (2) short component name such as 'r-w-controller';
* (3) special value '.' indicating component of the
current working directory;
* (4) special value 'CLUSTER' that will expand to
cover all REANA cluster components [default];
* (5) special value 'CLIENT' that will expand to
cover all REANA client components;
* (6) special value 'DEMO' that will expand
to include several runable REANA demo examples;
* (7) special value 'ALL' that will expand to include
all REANA repositories.
:type component: str
"""
for component in select_components(component):
cmd = "git branch -vv"
run_command(cmd, component)
@click.option(
"--branch", "-b", nargs=2, multiple=True, help="Which PR? [component PR#]"
)
@click.option("--fetch", is_flag=True, default=False)
@git_commands.command(name="git-checkout")
def git_checkout(branch, fetch): # noqa: D301
"""Check out local branch corresponding to a component pull request.
The ``-b`` option can be repetitive to check out several pull requests in
several repositories at the same time.
\b
:param branch: The option ``branch`` can be repeated. The value consist of
two strings specifying the component name and the pull
request number. For example, ``-b reana-workflow-controler
72`` will create a local branch called ``pr-72`` in the
reana-workflow-controller source code directory.
:param fetch: Should we fetch latest upstream first? [default=False]
:type branch: list
:type fetch: bool
"""
for cpr in branch:
component, pull_request = cpr
component = select_components([component,])[0]
if component in REPO_LIST_ALL:
if fetch:
cmd = "git fetch upstream"
run_command(cmd, component)
cmd = "git checkout -b pr-{0} upstream/pr/{0}".format(pull_request)
run_command(cmd, component)
else:
msg = "Ignoring unknown component."
display_message(msg, component)
@click.option(
"--branch", "-b", nargs=2, multiple=True, help="Which PR? [component PR#]"
)
@click.option(
"--push", is_flag=True, default=False, help="Should we push to origin and upstream?"
)
@git_commands.command(name="git-merge")
def git_merge(branch, push): # noqa: D301
"""Merge a component pull request to local master.
The ``-b`` option can be repetitive to merge several pull requests in
several repositories at the same time.
\b
:param branch: The option ``branch`` can be repeated. The value consist of
two strings specifying the component name and the pull
request number. For example, ``-b reana-workflow-controler
72`` will merge a local branch called ``pr-72`` from the
reana-workflow-controller to master branch.
:param push: Should we push to origin and upstream? [default=False]
:type branch: list
:type push: bool
"""
for cpr in branch:
component, pull_request = cpr
component = select_components([component,])[0]
if component in REPO_LIST_ALL:
for cmd in [
"git fetch upstream",
"git diff pr-{0}..upstream/pr/{0} --exit-code".format(pull_request),
"git checkout master",
"git merge --ff-only upstream/master",
"git merge --ff-only upstream/pr/{0}".format(pull_request),
"git branch -d pr-{0}".format(pull_request),
]:
run_command(cmd, component)
if push:
for cmd in [
"git push origin master",
"git push upstream master",
]:
run_command(cmd, component)
else:
msg = "Ignoring unknown component."
display_message(msg, component)
@click.option(
"--component",
"-c",
multiple=True,
default=["CLUSTER"],
help="Which components? [shortname|name|.|CLUSTER|ALL]",
)
@git_commands.command(name="git-fetch")
def git_fetch(component): # noqa: D301
"""Fetch REANA upstream source code repositories without upgrade.
\b
:param components: The option ``component`` can be repeated. The value may
consist of:
* (1) standard component name such as
'reana-workflow-controller';
* (2) short component name such as 'r-w-controller';
* (3) special value '.' indicating component of the
current working directory;
* (4) special value 'CLUSTER' that will expand to
cover all REANA cluster components [default];
* (5) special value 'CLIENT' that will expand to
cover all REANA client components;
* (6) special value 'DEMO' that will expand
to include several runable REANA demo examples;
* (7) special value 'ALL' that will expand to include
all REANA repositories.
:type component: str
"""
for component in select_components(component):
cmd = "git fetch upstream"
run_command(cmd, component)
@click.option(
"--component",
"-c",
multiple=True,
default=["CLUSTER"],
help="Which components? [shortname|name|.|CLUSTER|ALL]",
)
@git_commands.command(name="git-upgrade")
def git_upgrade(component): # noqa: D301
"""Upgrade REANA local source code repositories and push to GitHub origin.
\b
:param components: The option ``component`` can be repeated. The value may
consist of:
* (1) standard component name such as
'reana-workflow-controller';
* (2) short component name such as 'r-w-controller';
* (3) special value '.' indicating component of the
current working directory;
* (4) special value 'CLUSTER' that will expand to
cover all REANA cluster components [default];
* (5) special value 'CLIENT' that will expand to
cover all REANA client components;
* (6) special value 'DEMO' that will expand
to include several runable REANA demo examples;
* (7) special value 'ALL' that will expand to include
all REANA repositories.
:type component: str
"""
for component in select_components(component):
for cmd in [
"git fetch upstream",
"git checkout master",
"git merge --ff-only upstream/master",
"git push origin master",
"git checkout -",
]:
run_command(cmd, component)
@click.option(
"--component",
"-c",
multiple=True,
default=["CLUSTER"],
help="Which components? [shortname|name|.|CLUSTER|ALL]",
)
@click.option("--number", "-n", default=6, help="Number of commits to output [6]")
@click.option("--all", is_flag=True, default=False, help="Show all references?")
@git_commands.command(name="git-log")
def git_log(component, number, all): # noqa: D301
"""Show commit logs in given component repositories.
\b
:param components: The option ``component`` can be repeated. The value may
consist of:
* (1) standard component name such as
'reana-workflow-controller';
* (2) short component name such as 'r-w-controller';
* (3) special value '.' indicating component of the
current working directory;
* (4) special value 'CLUSTER' that will expand to
cover all REANA cluster components [default];
* (5) special value 'CLIENT' that will expand to
cover all REANA client components;
* (6) special value 'DEMO' that will expand
to include several runable REANA demo examples;
* (7) special value 'ALL' that will expand to include
all REANA repositories.
:param number: The number of commits to output. [6]
:param all: Show all references? [6]
:type component: str
:type number: int
:type all: bool
"""
for component in select_components(component):
cmd = (
"git log -n {0} --graph --decorate"
' --pretty=format:"%C(blue)%d%Creset'
" %C(yellow)%h%Creset %s, %C(bold green)%an%Creset,"
' %C(green)%cd%Creset" --date=relative'.format(number)
)
if all:
cmd += " --all"
msg = cmd[0 : cmd.find("--pretty")] + "..."
display_message(msg, component)
run_command(cmd, component, display=False)
@click.option(
"--component",
"-c",
multiple=True,
default=["CLUSTER"],
help="Which components? [shortname|name|.|CLUSTER|ALL]",
)
@git_commands.command(name="git-diff")
def git_diff(component): # noqa: D301
"""Diff checked-out REANA local source code repositories.
\b
:param components: The option ``component`` can be repeated. The value may
consist of:
* (1) standard component name such as
'reana-workflow-controller';
* (2) short component name such as 'r-w-controller';
* (3) special value '.' indicating component of the
current working directory;
* (4) special value 'CLUSTER' that will expand to
cover all REANA cluster components [default];
* (5) special value 'CLIENT' that will expand to
cover all REANA client components;
* (6) special value 'DEMO' that will expand
to include several runable REANA demo examples;
* (7) special value 'ALL' that will expand to include
all REANA repositories.
:type component: str
"""
for component in select_components(component):
for cmd in [
"git diff master",
]:
run_command(cmd, component)
@click.option(
"--component",
"-c",
multiple=True,
default=["CLUSTER"],
help="Which components? [name|CLUSTER]",
)
@git_commands.command(name="git-push")
def git_push(component): # noqa: D301
"""Push REANA local repositories to GitHub origin.
\b
:param components: The option ``component`` can be repeated. The value may
consist of:
* (1) standard component name such as
'reana-workflow-controller';
* (2) short component name such as 'r-w-controller';
* (3) special value '.' indicating component of the
current working directory;
* (4) special value 'CLUSTER' that will expand to
cover all REANA cluster components [default];
* (5) special value 'CLIENT' that will expand to
cover all REANA client components;
* (6) special value 'DEMO' that will expand
to include several runable REANA demo examples;
* (7) special value 'ALL' that will expand to include
all REANA repositories.
:type component: str
"""
components = select_components(component)
for component in components:
for cmd in ["git push origin master"]:
run_command(cmd, component)
@git_commands.command(name="git-upgrade-shared-modules")
@click.option(
"--component",
"-c",
multiple=True,
default=["CLUSTER", "CLIENT"],
help="Which components? [shortname|name|.|CLUSTER|ALL]",
)
@click.option(
"--use-latest-known-tag",
is_flag=True,
default=False,
help="Should the upgrade use the latest known tag of the shared modules?"
" If so, the latest known tag of the shared modules will be used to upgrade the"
" provided components. Else, the upgrade will only occur if the latest commit of the"
" shared modules points at a tag, in other case, the command will be aborted.",
)
@click.option(
"--amend",
is_flag=True,
default=False,
help="Should the changes be integrated as part of the latest commit of each component?",
)
@click.option(
"--push",
is_flag=True,
default=False,
help="Should the feature branches with the upgrades be pushed to origin?",
)
@click.pass_context
def git_upgrade_shared_modules(
ctx, component, use_latest_known_tag, amend, push
): # noqa: D301
"""Upgrade selected components to latest REANA-Commons/DB version.
\b
:param components: The option ``component`` can be repeated. The value may
consist of:
* (1) standard component name such as
'reana-workflow-controller';
* (2) short component name such as 'r-w-controller';
* (3) special value '.' indicating component of the
current working directory;
* (4) special value 'CLUSTER' that will expand to
cover all REANA cluster components [default];
* (5) special value 'CLIENT' that will expand to
cover all REANA client components;
* (6) special value 'DEMO' that will expand
to include several runable REANA demo examples;
* (7) special value 'ALL' that will expand to include
all REANA repositories.
:type component: str
"""
def _create_commit_or_amend(components):
for component in components:
commit_cmd = 'git commit -m "installation: bump shared modules"'
if amend:
commit_cmd = "git commit --amend --no-edit"
files_to_commit = ["setup.py"]
if os.path.exists(get_srcdir(component) + os.sep + "requirements.txt"):
files_to_commit.append("requirements.txt")
run_command(
f"git add {' '.join(files_to_commit)} && {commit_cmd}", component,
)
components = select_components(component)
for module in REPO_LIST_SHARED:
last_version = fetch_latest_pypi_version(module)
update_module_in_cluster_components(
module,
last_version,
components_to_update=components,
use_latest_known_tag=use_latest_known_tag,
force_feature_branch=True,
)
_create_commit_or_amend(components)
ctx.invoke(git_diff, component=[component])
if push:
git_push_to_origin(components)
@click.option(
"--component",
"-c",
required=True,
multiple=True,
help="Which components? [shortname|name|.|CLUSTER|ALL]",
)
@click.option(
"--version", "-v", help="Shall we manually specify component's next version?",
)
@git_commands.command(name="git-create-release-commit")
def git_create_release_commit_command(component, version): # noqa: D301
"""Create a release commit for the specified components.
\b
:param components: The option ``component`` can be repeated. The value may
consist of:
* (1) standard component name such as
'reana-workflow-controller';
* (2) short component name such as 'r-w-controller';
* (3) special value '.' indicating component of the
current working directory;
* (4) special value 'CLUSTER' that will expand to
cover all REANA cluster components [default];
* (5) special value 'CLIENT' that will expand to
cover all REANA client components;
* (6) special value 'DEMO' that will expand
to include several runable REANA demo examples;
* (7) special value 'ALL' that will expand to include
all REANA repositories.
:param version: Manually specifies the version for the component. If not provided,
the last version will be auto-incremented..
:type component: str
:type version: str
"""
components = select_components(component)
for component in components:
if git_create_release_commit(component, next_version=version):
display_message("Release commit created.", component)
@click.option(
"--component",
"-c",
required=True,
multiple=True,
help="Which components? [shortname|name|.|CLUSTER|ALL]",
)
@git_commands.command(name="git-create-pr")
def git_create_pr_command(component): # noqa: D301
"""Create a GitHub pull request for each selected component.
\b
:param components: The option ``component`` can be repeated. The value may
consist of:
* (1) standard component name such as
'reana-workflow-controller';
* (2) short component name such as 'r-w-controller';
* (3) special value '.' indicating component of the
current working directory;
* (4) special value 'CLUSTER' that will expand to
cover all REANA cluster components [default];
* (5) special value 'CLIENT' that will expand to
cover all REANA client components;
* (6) special value 'DEMO' that will expand
to include several runable REANA demo examples;
* (7) special value 'ALL' that will expand to include
all REANA repositories.
:type component: str
"""
def _git_create_pr(comp):
"""Create a pull request for the provided component."""
for cmd in [
"git push origin HEAD",
"hub pull-request -p --no-edit",
"hub pr list -L 1",
]:
run_command(cmd, component)
for component in select_components(component):
if not is_feature_branch(component): # replace with is_feature_branch from #371
display_message(
"You are trying to create PR but the current branch is master, please "
"switch to the wanted feature branch.",
component,
)
sys.exit(1)
else:
if is_component_behind_branch(component, "upstream/master"):
print_branch_difference_report(component, "upstream/master")
display_message(
"Error: please rebase your feature branch against latest master.",
component,
)
sys.exit(1)