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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
|
#!/usr/bin/perl
# Copyright (C) 2017 The Qt Company Ltd.
# Copyright (C) 2014 Intel Corporation.
# Copyright (C) 2019 Oswald Buddenhagen
# Contact: http://www.qt.io/licensing/
#
# You may use this file under the terms of the 3-clause BSD license.
# See the file LICENSE from this package for details.
#
use v5.14;
use strict;
use warnings;
our ($script, $script_path);
BEGIN {
use Cwd qw(abs_path);
if ($^O eq "msys") {
$0 =~ s,\\,/,g;
$0 =~ s,^(.):/,/$1/,g;
}
$script_path = $script = abs_path($0);
$script_path =~ s,/[^/]+$,,;
unshift @INC, $script_path;
}
use git_gpush;
use List::Util qw(first any all);
use JSON;
# Cannot use Pod::Usage for this file, since git on Windows will invoke its own perl version, which
# may not (msysgit for example) support this module, even if it's considered a Core module.
sub usage
{
print << "EOM";
Usage:
git gpush [opts] [from] [+<reviewer>] [=<CC user>]
Pushes Changes to Gerrit and adds reviewers and CC to the PatchSets.
Description:
This program is used to push PatchSets to Gerrit, and at the same
time add reviewers and CCs to the PatchSets pushed.
You can use email addresses, Gerrit usernames, or aliases for the
name of the reviewers/CCs.
The pushed commits are listed by default. Conforming with Gerrit,
these are the commits which are not on any branch of the pushed
branch's upstream remote.
Prior to actually pushing any commits, gpush will temporarily rebase
them onto a new base. This has the advantage that you can keep many
unrelated "series" in your local branch without creating spurious
dependencies on Gerrit, effectively pretending that you have a separate
branch for every series. Furthermore, gpush will keep the base of
subsequent pushes of the same series constant (unless told otherwise),
which means that you can closely track the upstream branch without
pushing needless rebases to Gerrit (which would obfuscate inter-PatchSet
diffs).
'From' may be specified as either a ref, a SHA1, or a Gerrit Change-Id,
possibly abbreviated. Git rev-spec suffixes like '~2' are allowed; if
only a suffix is specified, it is understood to be relative to 'HEAD'.
'From' may also be a range, specified as either <base>..<tip> or
<tip>:<count>. Both <base> and <tip> may be omitted, in which case
they default to '\@{upstream}' and 'HEAD', respectively.
If 'from' is not specified, 'HEAD' is used.
When pushing a series for the first time, the exact range of commits
needs to be specified. For subsequent pushes of the same series, it is
sufficient to specify any commit which is part of the series. Loose
Changes in the middle of the series will be automatically captured
by it; to append loose Changes to the series right in front of them,
use --extend; to capture loose Changes right in front of the series,
use --capture.
It is possible to regroup series any time by specifying new exact
ranges. The --group option can be used to (re-)group series without
actually pushing them at that time.
Note that this program can be used in the middle of an interactive
rebase, to push out the amended commits instantly.
Options:
-a, --all
Push all series reachable from 'from'. Error out if any
free-standing loose commits are encountered.
-e, --extend
Append new Changes to the series right in front of them.
-c, --capture
Make the series capture loose Changes right in front of it.
-g, --group
Group Changes into series without pushing them yet.
Note that this resets the pending properties (branch, topic,
and base) from a previous grouping that was not pushed out yet.
-x, --exclude
Like --group, but the series is excluded from --all. It is
possible to exclude existing series. To (re-)include a series,
use --group or push it separately.
-m, --minimal
Try to avoid creating new PatchSets for unmodified Changes even
if they are on top of modified Changes. This avoids unnecessarily
invalidating reviews, at the cost of slower pushes and the Changes
having dependencies marked as "Not current" in the relation chain.
This is the default, unless configured otherwise via gpush.minimal.
-nm, --not-minimal
Push the entire series, including trailing unmodified Changes.
-f, --force
Push despite newer PatchSets being on Gerrit.
-r, --remote
Specify the git remote to push to.
-b, --branch
Specify the git branch to push for. If not specified, 'from's
upstream branch is used as the target branch.
This setting persists for the series, even when it grows.
-fb, --force-branch
Push for specified branch despite the pushed Changes having
been pushed previously, but only for different branches.
-t, --topic
Specify the Gerrit topic name for the pushed Changes.
This setting persists for the series, even when it grows.
-o, --onto, --base
Specify the commit to rebase the pushed series onto. It is possible
to specify arbitrary other commits already known to Gerrit, including
still pending PatchSets. Use 'ROOT' to orphan the series.
Except for merge commits and their children, once chosen, the base
will persist through subsequent pushes until overridden, even when
the series grows.
By default, the local branch's base is used.
--rebase
Reset the base of a previously pushed series to the local branch's
base. Typically used after a conflicted pull.
--move {new|<range>}[/<from>]
After cherry-picking Changes to the current branch, mark the picks
as the _only_ source for subsequent pushes of these Changes.
The Changes on the current branch inherit all persistent properties,
while those on the source branch are hidden, in case they were not
dropped to start with.
<from> must be supplied only if the source branch is ambiguous.
This option is necessary only when a move cannot be unambiguously
inferred.
--copy {new|<range>}
After cherry-picking Changes to the current branch, mark the picks
as an _additional_ source for subsequent pushes of these Changes.
The Changes on the source branch are left alone, while those on the
current branch start out with a clean slate (which implies a
different default target branch).
When a range is specified, the Changes are also --group'd.
--hide {new|<range>}
After cherry-picking Changes to the current branch, mark the picks
as an _unacceptable_ source for subsequent pushes of these Changes.
When a range is specified, the Changes are also --group'd.
The range can be specified as <base>..<tip> (either end can be left
off) or <tip>:<count>. When using 'new' instead of a range, all
Changes which were previously not seen on the current branch are
selected, while actual ranges select Changes regardless of whether
they were already seen, which makes it possible to revise previous
decisions about the authoritative source for pushes.
--move, --copy, and --hide can be specified multiple times. The
ranges may not overlap, but will override a single also specified
operation which uses 'new'.
Note that the ranges supplied to these options do NOT imply these
Changes getting pushed; to do so, specify them a second time as a
regular <from> argument.
-l, --list
Report all Changes that would be pushed, then quit.
This is a purely off-line operation.
Implies --all unless 'from' is specified.
-ll, --list-online
Report all Changes that would be pushed, then quit.
The Changes are annotated with state information from Gerrit.
Implies --all unless 'from' is specified.
--aliases
Report all registered aliases and quit.
-n, --dry-run
Do everything except actually pushing commits and updating state.
This is useful mostly for debugging.
-v, --verbose
Show the resolved aliases, SHA1s of commits, and other information.
-q, --quiet
Suppress the usual output about what is pushed where.
--debug
Print debug information.
Configuration:
This program uses options from the git configuration. The following
options are supported:
gpush.alias.<alias>
An alias definition. The value is a comma-separated list of Gerrit
login names and/or email addresses, so it's possible to map, for
example, IRC nicknames or entire teams. Note that git config keys
are constrained regarding allowed characters, so it is impossible
to map some IRC nicks via git configuration; see below for an
alternative.
gpush.minimal
Whether to use minimal push mode by default.
Defaults to true if not configured.
branch.<branch>.gpushremote
The git remote to use for pushing to Gerrit for branch <branch>.
When not configured, the default remote is used as described below.
gpush.remote
The default git remote to use for pushing to Gerrit.
When not configured, 'gerrit' is used if present, falling back to
git's branch.<branch>.pushremote and remote.pushdefault configs
if present, or the pushed branch's upstream remote as a last resort.
gpush.upstream
The git remote to assume to be the upstream if the pushed branch
does not have a remote-tracking branch configured.
Defaults to 'origin' or the only present remote if not configured.
In addition to the git configuration (which takes precedence), the file
.git-gpush-aliases located next to this program is also read. It may
contain two sections: 'config' where you can use the options in the
'gpush.' namespace specified above (but without the prefix), as well
as 'aliases'. The latter works just like alias definitions via the git
configuration, except that:
- The alias name itself may also be a comma-separated list, thus
supporting users with multiple handles.
- Most characters are permitted in the alias names.
Examples:
git gpush .. +alex
Push HEAD and its ancestors for the upstream branch of HEAD, and
add 'alex' as a reviewer. No rebasing is initially done, but note
that the remote base will stick even if you subsequently rebase
the local branch.
git gpush I2c9ccbc26..I9434d28fc
Push the range identified by Gerrit Change-Ids for the upstream
branch of HEAD, rebased on top of the local branch's base.
git gpush ~1:3 -b 5.4 -o 85af7f4538b
Push the range HEAD~4..HEAD~1 for branch 5.4, rebased on top of
commit 85af7f4538b.
git gpush Iedb5bd8ca8:3
...
git gpush Iedb5bd8ca8
Push the Change Iedb5bd8ca8 and the two Changes in front of it.
Subsequently, re-push the Changes, specifying only the tip.
git gpush :3 +alex
...
git commit -a -m "Another commit"
git gpush -e -g
...
git commit -a -m "An unrelated commit"
git gpush :1
...
git gpush ~1 +alex
First push the top three commits for review, then add another commit
to the series without re-pushing instantly, then push another commit,
and finally re-push the first series.
Copyright:
Copyright (C) 2017 The Qt Company Ltd.
Copyright (C) 2014 Intel Corporation.
Copyright (C) 2019 Oswald Buddenhagen
Contact: http://www.qt.io/licensing/
License:
You may use this file under the terms of the 3-clause BSD license.
EOM
}
my $from_base;
my $from;
my $commit_count = 0;
my $addbase; # default set by process_config()
my $minimal; # default set by process_config()
my $extend = 0;
my $capture = 0;
my $ref_base;
my $ref_base_ok = 1;
my $ref_to;
my $force_branch = 0;
my $topic;
my $force = 0;
my $push_all = 0;
my $group_only = 0;
my $exclude = 0;
my $list_only = 0;
my $list_online = 0;
my @reviewers;
my @CCs;
sub parse_arguments(@)
{
my $rebase = 0;
my $minimal_override = 0;
my $not_exclude = 0;
while (scalar @_) {
my $arg = shift @_;
if ($arg eq "-v" || $arg eq "--verbose") {
$verbose = 1;
} elsif ($arg eq "-q" || $arg eq "--quiet") {
$quiet = 1;
} elsif ($arg eq "--debug") {
$debug = 1;
$verbose = 1;
} elsif ($arg eq "-n" || $arg eq "--dry-run") {
$dry_run = 1;
} elsif ($arg eq "-e" || $arg eq "--extend") {
$extend = 1;
} elsif ($arg eq "-c" || $arg eq "--capture") {
$capture = 1;
} elsif ($arg eq "-g" || $arg eq "--group") {
$group_only = 1;
$not_exclude = 1;
} elsif ($arg eq "-x" || $arg eq "--exclude") {
$group_only = 1;
$exclude = 1;
} elsif ($arg eq "-m" || $arg eq "--minimal") {
$minimal = 1;
$minimal_override = 1;
} elsif ($arg eq "-nm" || $arg eq "--not-minimal") {
$minimal = 0;
$minimal_override = 1;
} elsif ($arg eq "-f" || $arg eq "--force") {
$force = 1;
} elsif ($arg eq "-r" || $arg eq "--remote") {
fail("--remote needs an argument.\n") if (!@_ || ($_[0] =~ /^-/));
$remote = shift @_;
} elsif ($arg eq "-t" || $arg eq "--topic") {
fail("--topic needs an argument.\n") if (!@_ || ($_[0] =~ /^-/));
$topic = shift @_;
} elsif ($arg eq "-b" || $arg eq "--branch") {
fail("--branch needs an argument.\n") if (!@_ || ($_[0] =~ /^-/));
$ref_to = shift @_;
} elsif ($arg eq "-fb" || $arg eq "--force-branch") {
$force_branch = 1;
} elsif ($arg eq "-o" || $arg eq "--onto" || $arg eq "--base") {
fail("--onto needs an argument.\n") if (!@_ || ($_[0] =~ /^-/));
$ref_base = shift @_;
} elsif ($arg eq "--rebase") {
$rebase = 1;
} elsif ($arg eq "-a" || $arg eq "--all") {
$push_all = 1;
} elsif ($arg eq "-l" || $arg eq "--list") {
$list_only = 1;
} elsif ($arg eq "-ll" || $arg eq "--list-online") {
$list_only = 1;
$list_online = 1;
} elsif ($arg eq "--aliases") {
foreach my $key (sort(keys %aliases)) {
print "$key = $aliases{$key}\n";
}
exit 0;
} elsif ($arg eq "-?" || $arg eq "--?" || $arg eq "-h" || $arg eq "--help") {
usage();
exit 0;
} elsif (parse_source_option($arg, 0, @_)) {
# Nothing
} elsif ($arg =~ /^\+(.+)/) {
push @reviewers, split(/,/, lookup_alias($1));
} elsif ($arg =~ /^\=(.+)/) {
push @CCs, split(/,/, lookup_alias($1));
} elsif ($arg !~ /^\-/) {
fail("Specifying multiple sources is currently not supported.\n")
if (defined($from));
# The assumption that no legitimate branch would have a purely numerical
# name seems reasonable, so this overloading of the syntax should be safe.
if ($arg =~ /^(.*):(\d+)$/) {
$from = length($1) ? $1 : 'HEAD';
$commit_count = $2;
} elsif ($arg =~ /(.*):(.*)/) {
if (length($1)) {
$from = $1;
print STDERR "Warning: Specifying <from>: is deprecated.".
" Use just <from> instead.\n";
}
if (length($2)) {
$ref_to = $2;
print STDERR "Warning: Specifying :<ref-to> is deprecated.".
" Use --branch instead.\n";
}
} else {
$from = $arg;
}
} else {
fail("Unrecognized option '$arg'.\n");
}
}
fail("--quiet and --verbose/--debug are mutually exclusive.\n")
if ($quiet && $verbose);
fail("--base and --rebase are mutually exclusive.\n")
if (defined($ref_base) && $rebase);
$ref_base = "" if ($rebase);
if (!defined($from)) {
$from = "HEAD";
$push_all = 1 if ($list_only);
} else {
fail("--all is incompatible with specifying commits.\n")
if ($push_all);
if ($from =~ /^(.*)\.\.(.*)$/) {
fail("Specifying a commit count and a range base are mutually exclusive.\n")
if ($commit_count);
($from_base, $from) = (length($1) ? $1 : '@{u}', length($2) ? $2 :'HEAD');
}
}
my $series_specific =
$commit_count || $from_base || $extend || $capture
|| defined($ref_to) || defined($topic);
my $push_specific =
@reviewers || @CCs || $force || $force_branch
|| defined($remote);
if ($group_only) {
fail("--group/--exclude and --list are mutually exclusive.\n")
if ($list_only);
fail("--group/--exclude and --all are mutually exclusive.\n")
if ($push_all);
fail("--group/--exclude is incompatible with push-modifying options.\n")
if ($push_specific);
fail("--group and --exclude are mutually exclusive.\n")
if ($exclude && $not_exclude);
} elsif ($list_only) {
fail("--list/--list-online is incompatible with --quiet/--verbose.\n")
if ($quiet || ($verbose && !$debug));
fail("--list/--list-online is incompatible with series-specific options.\n")
if ($series_specific);
fail("--list/--list-online is incompatible with push-modifying options.\n")
if ($push_specific || (!$list_online && $minimal_override));
fail("--list/--list-online is incompatible with base-modifying options.\n")
if (defined($ref_base));
} elsif ($push_all) {
fail("--all is incompatible with series-specific options.\n")
if ($series_specific);
# Push options are perfectly legit with --all, even if some are
# probably not too useful (e.g., --base).
}
}
use constant {
BASE_NO => 0,
BASE_MAYBE => 1,
BASE_YES => 2
};
sub process_config()
{
load_config();
my $mi = git_config('gpush.minimal', 'true');
if ($mi eq 'true' || $mi eq 'yes') {
$minimal = 1;
} elsif ($mi eq 'false' || $mi eq 'no') {
$minimal = 0;
} else {
fail("Unrecognized value for gpush.minimal.\n");
}
my $ab = git_config('gpush.addbase', 'maybe');
if ($ab eq 'maybe') {
$addbase = BASE_MAYBE;
} elsif ($ab eq 'true' || $ab eq 'yes') {
$addbase = BASE_YES;
} elsif ($ab eq 'false' || $ab eq 'no') {
$addbase = BASE_NO;
} else {
fail("Unrecognized value for gpush.addbase.\n");
}
}
sub lookup_alias($)
{
my ($user) = @_;
my $alias = $aliases{$user};
if (defined $alias && $alias ne "") {
print "Resolved $user to $alias.\n" if ($verbose);
return $alias;
}
return $user;
}
sub format_group_id($)
{
my ($group) = @_;
my $changes = $$group{changes};
return format_id($$changes[-1]{id}).":".int(@$changes);
}
sub set_group_error($$$)
{
my ($group, $style, $error) = @_;
($$group{error_style}, $$group{error}) = ($style, $error);
}
sub caption_group($)
{
my ($group) = @_;
my $changes = $$group{changes};
if (!$$group{gid}) {
return (sprintf("Set of %d loose Change(s):", int(@$changes)),
$changes);
}
my $to = $$group{branch};
my $tos = defined($to) ? " for $to" : "";
my $tpc = $$group{topic};
my $tpcs = length($tpc) ? ", topic '$tpc'" : "";
my ($pfx, $rmt) = $list_only
? ($$group{hide_mix}
? "Partially hidden series of"
: $$group{hide}
? "Hidden series of"
: $$group{exclude_mix}
? "Partially excluded series of"
: $$group{exclude}
? "Excluded series of"
: "Series of",
($list_online && length($tos)) ? " on '$remote'" : "")
: $group_only
? ("Grouping", "")
: ("Pushing", " to '$remote'");
return (sprintf("%s %d Change(s)%s%s%s:",
$pfx, int(@$changes), $tos, $rmt, $tpcs),
$changes);
}
sub report_pushed_group($$)
{
my ($reports, $group) = @_;
my ($title, $changes) = caption_group($group);
report_flowed($reports, $title);
report_local_changes($reports, $changes);
my $error = $$group{error};
report_text($reports, $$group{error_style}, $error) if (defined($error));
}
sub report_pushed_changes(@)
{
my (@groups) = @_;
my @reports;
foreach my $group (@groups) {
report_pushed_group(\@reports, $group);
}
return \@reports;
}
# Determine a singular value for a particular attribute from the Changes
# in a series. Conflicting values are an error.
# The way this function is used, values set via --group take precedence
# over the "live" values for each Change individually, not for the whole
# series. This avoids silently overriding the attributes of Changes which
# are moved into the series after establishing it.
sub aggregate_property($$$$)
{
my ($group, $prop_name, $get_prop, $get_ann) = @_;
my $changes = $$group{changes};
my %prop_map = map { $_ => 1 } grep { $_ } map { $get_prop->($_) } @$changes;
my @props = keys %prop_map;
if (@props > 1) {
foreach my $change (@$changes) {
my $p = $get_prop->($change);
$$change{annotation} = $get_ann->($p) if (defined($p));
}
set_group_error($group, 'flowed',
"Changes were previously pushed with mixed ${prop_name}s."
." Please specify one.");
fail_formatted(report_pushed_changes($group));
}
if (@props) {
my $p = $props[0];
printf("Series %s re-using %s '%s'.\n", format_group_id($group), $prop_name, $p)
if ($debug);
return $p;
}
return undef;
}
sub aggregate_bool_property($$$$$)
{
my ($group, $prop, $ann_prop, $err_label, $err_msg) = @_;
print "Testing $prop.\n" if ($debug);
my $changes = $$group{changes};
return if (!any { $$_{$prop} } @$changes);
print "Have some $prop.\n" if ($debug);
$$group{$prop} = 1;
return if (all { $$_{$prop} } @$changes);
print "Have mixed $prop.\n" if ($debug);
$$group{"${prop}_mix"} = 1;
return if ($list_only);
foreach my $change (@$changes) {
$$change{annotation} = " [$ann_prop]"
if ($$change{$prop});
}
my @reports;
report_flowed(\@reports,
"Series of ".int(@$changes)." Changes has mixed $err_label states:");
report_local_changes(\@reports, $changes);
report_fixed(\@reports, $err_msg."\n");
fail_formatted(\@reports);
}
# Find _the_ branch the specified commit lives on. This can be the current
# branch (and other branches are ignored), or _one_ other branch.
sub branch_for_commit($)
{
my ($commit) = @_;
my $in_rebase;
my $curbranch;
my @otherbranches;
my $branches = open_cmd_pipe(0, "git", "branch", "--contains", $commit);
while (read_process($branches)) {
if (/^\* \(/) {
# New git versions will tell us the currently rebased branch.
if (/^\* \(no branch, rebasing (.*)\)$/) {
$curbranch = $1;
$in_rebase = 1;
}
last;
} elsif (/^\* (.*)$/) {
$curbranch = $1;
last;
} elsif (/^ (.*)$/) {
push @otherbranches, $1;
}
}
close_process($branches);
if (!defined($curbranch)) {
# If the commit is not on the current branch, see if it is on _one_
# other branch with an upstream branch.
my @goodbranches;
foreach my $other (@otherbranches) {
push @goodbranches, $other if (defined(git_config("branch.$other.merge")));
}
$curbranch = $goodbranches[0] if (@goodbranches == 1);
}
return ($curbranch, $in_rebase);
}
# Extract the local branch from the specified source.
# There are four possibilities where the source revision may be located:
# - On any named branch in a regular state.
# The branch is usable up to its tip, which may be pointed to by HEAD.
# - On the current named branch mid-rebase.
# The branch is pointed to by HEAD, and is usable only up to this point.
# - On a detached head, possibly mid-rebase.
# HEAD is the only reference and thus also the tip. There is no name.
# - Nowhere, except possibly in the reflog.
# It is the tip, as there are no references. There is no name.
# Note that commits specified by SHA1 which live on exactly one named
# branch fall into the first case.
sub determine_local_branch($)
{
my ($source) = @_;
# First, try to extract a branch name directly.
$local_branch = ($source =~ s/[~^].*$//r);
if ($local_branch eq "HEAD") {
my $sref = read_cmd_line(SOFT_FAIL, "git", "symbolic-ref", "-q", "HEAD");
$local_branch = $sref if (!$?);
}
$local_branch =~ s,^refs/heads/,,;
$local_tip = $local_refs{$local_branch};
if (!defined($local_tip)) {
# Next, try to deduce a branch from the commit.
my $in_rebase;
($local_branch, $in_rebase) = branch_for_commit($source);
# Cannot use the branch's tip mid-rebase, because the
# referenced commits may have been rewritten already.
$local_tip = $local_refs{$local_branch}
if (defined($local_branch) && !$in_rebase);
}
setup_remotes($source);
set_gerrit_config($remote);
}
# Determine the target branch for the given series.
# The local branch's upstream branch will be used, unless a target branch
# has been specified by the user.
sub determine_remote_branch($)
{
my ($group) = @_;
my $br = $ref_to;
if (!defined($br)) {
$br = aggregate_property($group, 'target',
sub { $_[0]->{ntgt} // $_[0]->{tgt} },
sub { ' => '.$_[0] });
}
if (!defined($br)) {
fail("Cannot deduce source branch for $from. Please use --branch.\n")
if (!defined($local_branch));
$br = git_config("branch.$local_branch.merge");
fail("$local_branch has no upstream branch. Please use --branch.\n")
if (!defined($br));
$br =~ s,^refs/heads/,,;
}
$$group{branch} = $br;
}
sub determine_topic($)
{
my ($group) = @_;
my $tpc = $topic;
if (!defined($tpc)) {
# No topic was specified, so deduce it from the previous push(es).
$tpc = aggregate_property($group, 'topic',
sub { $_[0]->{ntopic} // $_[0]->{topic} },
sub { ' /'.$_[0] });
}
$$group{topic} = $tpc;
}
sub scan_pushed_group($$)
{
my ($group, $commits) = @_;
my $changes = $$group{changes};
foreach my $change (@$changes) {
my $pcommit = $$change{pushed};
if (!defined($pcommit)) {
# In minimal mode, any previously pushed Change can be unmodified.
next if ($minimal);
# We stop at the 1st not previously pushed commit, as subsequent
# commits are obviously modified anyway.
last;
}
push @$commits, $pcommit;
my $ocommit = $$change{orig};
push @$commits, $ocommit if (defined($ocommit));
}
}
# Retrieve metadata for commits corresponding with previous pushes
# of ordered series of Changes.
sub visit_pushed_changes($)
{
my ($groups) = @_;
my @commits;
foreach my $group (@$groups) {
scan_pushed_group($group, \@commits);
}
print "Visiting pushed commits ...\n" if ($debug);
visit_local_commits(\@commits);
}
sub initialize_get_changes()
{
my $commit = parse_local_rev($from, SPEC_TIP);
if (!defined($commit)) {
# The revspec might refer to a Change-Id.
# We need a place to start from. We try only the current local branch -
# trying multiple branches would be expensive, potentially ambiguous
# (as the same Change-Id can exist multiple times), and probably not
# very useful to start with.
determine_local_branch('HEAD');
# Get the pool of Changes to search in, and later to push from.
analyze_local_branch('HEAD') or
fail("No local Changes (from HEAD).\n");
# Now try again.
$commit = parse_local_rev_id($from, SPEC_TIP);
} else {
# We did not need to visit the current branch to find the tip,
# so determine the branch from the tip now.
$from = "HEAD".$from if ($from =~ /^[~^]/);
determine_local_branch($from);
# Get the pool of Changes to push from.
# We don't limit the range and even overshoot if possible; the
# performance impact of that should be negligible. A corner-case
# benefit of this is a better error message when 1) the range base
# is specified by change-id, 2) it is invalid by being a descendant
# of the tip, and 3) the tip is NOT specified by change-id. More
# importantly, reverse traversal in do_determine_series() also
# needs the full range.
analyze_local_branch($local_tip // ($from =~ s/[~^].*$//r)) or
fail("No local Changes (from $from).\n");
}
return $commit;
}
sub finalize_get_changes($$)
{
my ($changes, $gid) = @_;
my %group = (changes => $changes, gid => $gid);
aggregate_bool_property(\%group, "hide", "HIDDEN", "hiding",
"Use --move/--copy/--hide to make it consistent.");
return \%group;
}
sub define_series($)
{
my ($commits) = @_;
# A new group key is used if a series is being (re-)assigned;
# for simplicity, we do not bother preserving keys.
# Stealing Changes from existing series could dissolve these series
# entirely. This implementation does not do this for simplicity's
# sake, thus losing some expressiveness - but that might have been
# more annoying than helpful anyway.
return (changes_from_commits($commits), $next_group++);
}
sub determine_series($)
{
my ($pivot_id) = @_;
my $pivot = $commit_by_id{$pivot_id}{change};
my ($changes, $gid, $echange, $prospects) = do_determine_series($pivot, $extend, $capture);
my $action = $group_only ? "group" : "push";
if (!$gid && @$changes && !$list_only) {
my @reports;
report_fixed(\@reports, "Attempted to $action ".int(@$changes)." loose Change(s):\n");
report_local_changes(\@reports, [ $echange ], "[ Extend: ", " ]")
if ($echange);
report_local_changes(\@reports, $changes);
report_fixed(\@reports, "Please ".($echange ? "use --extend or " : "")
."specify exact ranges.\n");
fail_formatted(\@reports);
}
if (@$prospects && !$list_only) {
my @reports;
report_fixed(\@reports, "Encountered ".int(@$prospects)." leading loose Change(s):\n");
report_local_changes(\@reports, $prospects);
report_fixed(\@reports, "While attempting to $action ".int(@$changes)." Change(s):\n");
report_local_changes(\@reports, $changes);
report_fixed(\@reports, "Please use --capture or specify exact ranges.\n");
fail_formatted(\@reports);
}
# The group key is preserved even when the series gains new Changes.
return ($changes, $gid);
}
# Get the list of local commits to push.
sub get_changes()
{
my $tip = initialize_get_changes();
# Assemble the series of Changes to push from the pool.
my ($changes, $gid);
if (defined($from_base)) {
my $base = parse_local_rev($from_base, SPEC_BASE);
($changes, $gid) = define_series(get_commits_base($base, $tip, $from_base, $from));
} elsif ($commit_count) {
($changes, $gid) = define_series(get_commits_count($tip, $commit_count, $from));
} else {
($changes, $gid) = determine_series($tip);
}
fail("Specified commit range is empty.\n") if (!@$changes);
my $group = finalize_get_changes($changes, $gid);
# Note that grouping/excluding hidden series is also rejected,
# as there is just no point to it - --copy and --hide imply
# regrouping anyway, and --move uses the grouping of the source,
# so any grouping done while the Changes are hidden will be lost.
if ($$group{hide}) {
my @reports;
report_flowed(\@reports,
"Refusing to operate hidden series of ".int(@$changes)." Change(s):");
report_local_changes(\@reports, $changes);
report_fixed(\@reports,
"Operate it from the previously chosen source branch,\n",
"or use --move/--copy to unhide it.\n");
fail_formatted(\@reports);
}
return $group;
}
# Get the list of local commits to push when pushing all series.
sub get_all_changes()
{
my $tip = initialize_get_changes();
my @groups;
my $have_loose = 0;
my $change = $commit_by_id{$tip}{change};
while (1) {
my ($changes, $gid);
($changes, $gid, $change, undef) = do_determine_series($change, 0, 0, 1);
my $group = finalize_get_changes($changes, $gid);
aggregate_bool_property($group, "exclude", "EXCLUDED", "exclusion",
"Please gpush it separately or use --group/--exclude"
." to make it consistent.")
if (!$$group{hide});
unshift @groups, $group;
$have_loose++ if (!$gid);
last if (!$change);
}
return ($have_loose, \@groups);
}
sub check_merge($$);
sub check_merge($$)
{
my ($parents, $failed) = @_;
my $good = 1;
foreach my $parentid (@$parents) {
my $parent = $commit_by_id{$parentid};
# If the parent is upstream, the merge is good.
next if (!$parent);
my $rparents = $$parent{parents};
# Merges of (good) merges are also good.
next if (@$rparents > 1 && check_merge($rparents, $failed));
push @$failed, $parent;
$good = 0;
}
return $good;
}
# Ensure that we don't implicitly create new PatchSets for non-1st
# parents of merges, as these would most likely target the wrong branch.
# TODO: this could be handled more nicely by doing recursive pushes
# instead of bailing out.
sub parents_pushed($$)
{
my ($parents, $failed) = @_;
foreach my $parentid (@$parents[1 .. $#$parents]) {
next if (defined($gerrit_info_by_sha1{$parentid})); # Gerrit knows it
my $parent = $commit_by_id{$parentid};
next if (!$parent); # Already upstream
push @$failed, $parent;
}
return !@$failed;
}
sub check_merges($)
{
my ($group) = @_;
foreach my $change (@{$$group{changes}}) {
my $commit = $$change{local};
my $parents = $$commit{parents};
if (@$parents > 1) {
my (@failed, $header);
if (!check_merge($parents, \@failed)) {
$header = ",----- Merge of non-upstream commit(s) -----\n";
set_group_error($group, 'fixed',
"Maybe you forgot to use git gpull instead of git pull?\n");
} elsif (!parents_pushed($parents, \@failed)) {
$header = ",----- Parent merge(s) not pushed yet ------\n";
set_group_error($group, 'fixed', "Please push the parent(s) first.\n");
} else {
next;
}
$$change{annotation} = ' [FAIL]';
set_change_error($change, 'fixed',
$header.format_commits(\@failed, "| ")
."`-------------------------------------------\n");
fail_formatted(report_pushed_changes($group));
}
}
}
# Assign each local Change to a matching remote Change if possible,
# on the way complaining about creating duplicates.
sub map_remote_group($$$$)
{
my ($group, $bad_groups, $some, $multi) = @_;
my $changes = $$group{changes};
my $br = $$group{branch};
my (@bad_chg, %bad_br);
foreach my $change (@$changes) {
my $gis = $gerrit_infos_by_id{$$change{id}};
next if (!$gis);
my ($good, @bad);
foreach my $gi (@$gis) {
if ($$gi{branch} eq $br) {
$good = $gi;
} elsif ($$gi{status} ne "MERGED") {
# MERGED Changes are ignored, to avoid false positives for cherry-picks.
push @bad, $gi;
}
}
if ($good) {
$$change{gerrit} = $good;
# This overrides the current value, which is just a cache.
$$change{topic} = $$good{topic};
# If the pending topic was set to match the server, drop the update.
$$change{ntopic} = undef
if (($$change{ntopic} // "") eq ($$good{topic} // ""));
} elsif (@bad && !$force_branch) {
my @bbr = map { $$_{branch} } @bad;
$$change{annotation} = ' ['.join(" ", sort @bbr).']';
push @bad_chg, $change;
$bad_br{$_} = 1 foreach (@bbr);
}
}
if (@bad_chg) {
push @$bad_groups, $group;
$$some = 1 if (@bad_chg != @$changes);
$$multi = 1 if (keys(%bad_br) > 1);
}
}
sub map_remote_changes($)
{
my ($groups) = @_;
my (@bad_groups, $some, $multi);
foreach my $group (@$groups) {
map_remote_group($group, \@bad_groups, \$some, \$multi);
}
if (@bad_groups) {
my $reports = report_pushed_changes(@bad_groups);
my $tpfx = $some ? "Some of the" : "The";
my $tsfx = $multi ? "different branches" : "a different branch";
report_fixed($reports,
"$tpfx Change(s) were previously pushed only for $tsfx.\n",
"Please move them server-side, push for a matching branch,\n",
"or use --force-branch to continue nonetheless.\n");
fail_formatted($reports);
}
}
use constant {
NEW => 'NEW',
KNOWN => 'KNOWN',
FORCE => 'FORCE',
MISSING => 'MISSING',
MODIFIED => 'MODIFIED',
UNMODIFIED => 'UNMODIFIED',
OUTDATED => 'OUTDATED',
MERGED => 'MERGED',
REJECTED => 'REJECTED',
FAILED => 'FAILED'
};
sub resolve_ref_base($)
{
my ($queries) = @_;
return if (!length($ref_base) || ($ref_base eq "ROOT"));
print "Resolving specified push base ...\n" if ($debug);
# Make sure we don't get confusing errors later.
# Also, we need a full SHA1 we can compare.
$ref_base = read_cmd_line(SOFT_FAIL, "git", "rev-parse", "--verify", $ref_base."^{commit}");
fail("... while resolving --base commit.\n")
if ($?);
visit_local_commits([ $ref_base ]);
if (!$commit_by_id{$ref_base}) {
print "--base is upstream.\n" if ($debug);
} else {
print "--base needs Gerrit query.\n" if ($debug);
# Note: this will not recognize direct-pushed commits. That isn't
# really a problem, as the only expected use case for specifying
# a non-upstream base is pushing on top of another pending review.
push @$queries, "commit:".$ref_base;
$ref_base_ok = 0;
}
}
sub confirm_ref_base()
{
return if ($ref_base_ok);
fail("Specified --base commit is not known to Gerrit.\n")
if (!defined($gerrit_info_by_sha1{$ref_base}));
}
sub determine_base($)
{
my ($group) = @_;
confirm_ref_base();
my $base = $ref_base;
if (!defined($base)) {
# We have no base yet, so deduce it from the previous push(es).
# The simple approach would be using the merge-base(s) of the
# previous push(es) and upstream. However, that would not work
# if we based the series on another pending series. To address
# this, we could stop the ancestor traversal at the first Change
# which is not part of this series. But then Changes dropped from
# the bottom of the series would appear to be bases. That means
# that we need to record the base of each push explicitly.
#
# Note that gpick'ing (or re-syncing after manual picking) records
# the base of the Change's series as seen on Gerrit. That implies
# that if a series was picked only partially, re-pushing it with
# gpush will "rebase up" these Changes, thus making their order
# relative to the omitted Changes ambiguous on Gerrit. This may
# be surprising at first, but it makes sense, as a partial pick
# is functionally equivalent to gpick-ing the entire series and
# subsequently dropping some Changes locally.
$base = aggregate_property($group, 'base',
sub { $_[0]->{nbase} // $_[0]->{base} },
sub { ' @'.($_[0] ? substr($_[0], 0, 8) : "<reset>") });
# Technically, we should re-validate the base before each push,
# because pending PatchSets (or entire Changes) can be deleted,
# and upstream commits can disappear due to forced pushes.
# While querying base PatchSets along with the pushed Changes
# would be cheap and easy, we'd still need a fallback for direct-
# pushed base commits, and we'd need to fetch the target remote
# if we wanted to catch forced pushes reliably.
# This all doesn't appear to be worth it ...
}
if (!length($base)) {
$base = $local_base;
printf("Basing series %s on local branch's base.\n", format_group_id($group))
if ($verbose);
}
$$group{base} = $base;
}
# Get a set of Changes which were part of the previous push of each
# specified Change, but have been upstreamed since.
sub get_upstreamed_changes($)
{
my ($changes) = @_;
my %pushed_changes = map { $$_{id} => 1 } @$changes;
my %upstreamed_changes;
my %query_bases;
foreach my $change (@$changes) {
my ($sha1, $base) = ($$change{pushed}, $$change{base});
next if (!defined($sha1));
# This will miss Changes which were exclusively on top of the
# specified Changes, which is just fine for our purposes, as
# these Changes obviously cannot have been the parents.
my $any;
while (1) {
my $commit = $commit_by_id{$sha1};
if (!$commit) {
# Handle legacy data without recorded push base.
$base = $sha1;
last;
}
my $changeid = $$commit{changeid};
if (!defined($pushed_changes{$changeid})) {
$upstreamed_changes{$changeid} = 1;
$any = 1;
} else {
# We omit Changes which are also part of the current push -
# this saves time (mostly only when all are still there)
# and avoids anomalies with re-applied reverted Changes.
}
$sha1 = get_1st_parent($commit);
last if (defined($base) && ($base eq $sha1));
}
if ($any) {
# Record the push base of the Change - if the Change is actually
# in our upstream, it will be so between its push base (which
# cannot be younger than the branch's tip at that time) and the
# local branch's merge base with upstream.
# We collect bases separately, because one Change-Id can map to
# multiple bases due to partially updated series.
$query_bases{$base} = 1;
}
}
if (%upstreamed_changes) {
print "Enumerating upstreamed Changes ...\n" if ($debug);
my $upstream_cids = visit_upstream_commits([ $local_base ], [ keys %query_bases ]);
foreach my $changeid (keys %upstreamed_changes) {
if (!defined($$upstream_cids{$changeid})) {
# Changes which are not in our upstream were presumably
# dropped from the push intentionally.
print "Dropping omitted non-upstream $changeid.\n"
if ($debug);
delete $upstreamed_changes{$changeid};
} else {
print "Keeping upstreamed $changeid.\n" if ($debug);
}
}
}
return \%upstreamed_changes;
}
sub do_advance_base($$$$);
sub do_advance_base($$$$)
{
my ($base_id, $base_cid, $sha1, $upstreamed_changes) = @_;
# If the candidate commit is already the base, there is nothing to do.
return 1 if ($sha1 eq $$base_id);
my $commit = $commit_by_id{$sha1};
# Terminate if we reached the bottom of the series, or in non-minimal
# mode the pushed commit was not visited due to being on top of new
# Changes (which is ok, because the injection chain is then interrupted
# anyway).
return 0 if (!$commit);
my $changeid = $$commit{changeid};
# Terminate if we encounter the Change corresponding with the base
# commit, but with a different SHA1. This is merely an optimization
# which avoids that we always traverse down to the bottom of the
# series, which would be O(n^2) with its length.
return 0 if ($changeid eq $base_cid);
# Recurse until we encounter the base, and propagate termination.
return 0 if (!do_advance_base($base_id, $base_cid, get_1st_parent($commit), $upstreamed_changes));
# Terminate unless the candidate Change is in our upstream. Note that
# we test that only after returning from the recursion, so Changes
# which were dropped from or moved towards the end of a series are
# simply left off rather than entirely disrupting the mechanism.
return 0 if (!defined($$upstreamed_changes{$changeid}));
print "Injecting upstreamed $changeid ($sha1).\n" if ($debug);
$$base_id = $sha1;
return 1;
}
# If Changes from the previous push were already upstreamed and the local
# series was subsequently rebased, it would be necessary to rebase the
# push to make the remaining commits still apply. To avoid the churn
# resulting from this, we simply inject the upstreamed commits in front
# of the actually applied ones.
sub advance_base($$$)
{
my ($base_id, $sha1, $upstreamed_changes) = @_;
if (defined($sha1)) {
my $base = $commit_by_id{$$base_id};
# Pre-get the base's Change-Id for quick comparison later on.
# The base of the 1st commit in the series was likely not visited.
my $base_cid = $base ? $$base{changeid} : "";
do_advance_base($base_id, $base_cid, $sha1, $upstreamed_changes);
}
}
sub prepare_rebase($$$)
{
my ($change, $base_id, $try_minimal) = @_;
my $old_id = $$change{pushed};
if (!defined($old_id)) {
print "Not previously pushed.\n" if ($debug);
return;
}
my $old_commit = $commit_by_id{$old_id};
if (!$old_commit) {
# Previously pushed Changes on top of fresh ones may be unvisited,
# as visit_pushed_changes() skips them in non-minimal mode.
print "Previously pushed, but not visited.\n" if ($debug);
return;
}
my $old_base_id = get_1st_parent($old_commit);
if ($base_id eq $old_base_id) {
# We are picking the same Change to the same base.
print "Previously pushed, same base SHA1.\n" if ($debug);
return ($old_commit, $old_base_id);
}
if ($try_minimal) {
my $old_base = $commit_by_id{$old_base_id};
if ($old_base) {
if ($$old_base{changeid} eq $commit_by_id{$base_id}{changeid}) {
# The actual base changed, but we may still get lucky with
# applying the commit to the base of the previous push.
print "Previously pushed, trying old base.\n" if ($debug);
return ($old_commit, $old_base_id, 1);
}
}
# Even the logical base changed (Changes were re-ordered).
print "Previously pushed, but re-ordered.\n" if ($debug);
return;
}
# The Change had previously a different parent commit. It is
# irrelevant whether the Changes were re-shuffled or the parent
# Change was amended - a new base will yield a new commit.
print "Previously pushed, different base SHA1.\n" if ($debug);
return;
}
sub commits_equal($$$$)
{
my ($base_id, $tree, $commit, $old_commit) = @_;
return $old_commit
&& ($tree eq $$old_commit{tree})
&& ($base_id eq get_1st_parent($old_commit))
&& (get_more_parents($commit) eq get_more_parents($old_commit))
&& ($$commit{message} eq $$old_commit{message})
&& ("@{$$commit{author}}" eq "@{$$old_commit{author}}");
}
sub rebase_commit($$$$$)
{
my ($commit, $base_id, $old_commit, $old_base_id, $orig_id) = @_;
my $parents = $$commit{parents};
my $parent_id = get_1st_commit($parents);
if ($base_id eq $parent_id) {
# Same base, so no need to actually rebase.
if (commits_equal($base_id, $$commit{tree}, $commit, $old_commit)) {
# The source commit was amended, but without real change.
return ($old_commit, 're-created w/o rebase');
}
# No previously pushed commit or it does not match,
# so use the source commit as-is.
return ($commit, 'verbatim');
}
# Technically, it would be possible to rebase merges along the
# first parents, but there is no point in supporting this here.
return if (@$parents > 1);
my ($tree, $errors);
my $parent = $commit_by_id{$parent_id};
if ($old_commit && ($base_id eq $old_base_id) && defined($orig_id)) {
if ($$commit{id} eq $orig_id) {
# We are picking the same commit to the same base, so
# we can just recycle the previously created commit.
return ($old_commit, 'recycled unmodified');
}
my $orig_commit = $commit_by_id{$orig_id};
if ($orig_commit && ($$commit{tree} eq $$orig_commit{tree})) {
# Upstream commits are not visited, so we get no tree id. However,
# as this code aims at series which were not rebased, using the base
# commit itself will work just as well for the series' first commit.
if (($parent ? $$parent{tree} : $parent_id)
eq get_1st_parent_tree($orig_commit)) {
if (($$commit{message} eq $$orig_commit{message})
&& ("@{$$commit{author}}" eq "@{$$orig_commit{author}}")) {
# We are picking the same content to the same base, so
# we can just recycle the previously created commit.
return ($old_commit, 'recycled amended');
}
# We are picking the same diff to the same base, so
# we can recycle the previously created tree.
$tree = $$old_commit{tree};
goto COMMIT;
}
}
}
my $base = $commit_by_id{$base_id};
if ($base && $parent && ($$base{tree} eq $$parent{tree})) {
# The parent commits differ, but their trees are the same.
$tree = $$commit{tree};
} else {
($tree, $errors) = apply_diff($commit, $base_id, USE_STDERR);
return (undef, undef, $errors) if (!defined($tree));
}
if (commits_equal($base_id, $tree, $commit, $old_commit)) {
# If we produced the same contents as last time, we re-use the
# commit we already have in the _pushed ref - that way, we save
# some work, and any local rebasing will not affect the final SHA1.
return ($old_commit, 're-created w/rebase');
}
# No previously pushed commit or it does not match,
# so create a new one.
COMMIT:
my $new_parents = ($base_id eq 'ROOT') ? [] : [ $base_id ];
my $new_commit = create_commit(
$new_parents, $tree, $$commit{message},
$$commit{author}, $$commit{committer});
$$new_commit{changeid} = $$commit{changeid};
$$new_commit{subject} = $$commit{subject};
return ($new_commit, 'fresh');
}
my $have_conflicts = 0;
sub do_rebase_changes($$)
{
my ($group, $upstreamed_changes) = @_;
my ($base, $changes) = ($$group{base}, $$group{changes});
printf("Rebasing %s to %s.\n", format_group_id($group), format_id($base))
if ($verbose);
# Preparation for advance_base(). We "pull down" the parent commits from
# subsequent previously pushed Changes into preceding new ones, to avoid
# that the "gap" in the chain disrupts the mechanism.
my $pchg;
foreach my $change (@$changes) {
$pchg = $change if (!$pchg);
my $old_id = $$change{pushed};
next if (!defined($old_id));
my $old_commit = $commit_by_id{$old_id};
next if (!$old_commit);
$$pchg{old_parent_id} = get_1st_parent($old_commit);
$pchg = undef;
}
# We don't attempt minimal mode when the base of the entire push
# changed.
# We could in principle try to base fragments on the old base,
# but then the saved base will be either a lie or the push would
# have multiple bases. Both would cause problems later on.
# FIXME: This reasoning is debatable.
my $try_minimal = $minimal
&& ($base eq ((first { defined($_) } map { $$_{base} } @$changes) // ""));
my @gaps; # Stack of fragmentation points.
my $holdoff = 0; # Remaining size of rewound gap.
my $failed = 0; # No final success possible any more.
for (my $idx = 0; $idx < @$changes; ) {
my $change = $$changes[$idx];
my $commit = $$change{local};
printf("Rebasing [%d] %s\n", $idx, format_commit($commit, -14)) if ($debug);
# We attempt to re-create the exact same commits we pushed previously,
# so we don't create new PatchSets for umodified Changes regardless of
# any local rebasing. Optionally, we may even attempt that for Changes
# on top of modified Changes (thus fragmenting the push).
my ($old_commit, $old_base);
my ($new_base, $try_old_base) = ($base, 0);
if ($holdoff > 0) {
# When re-using a previous base fails, we rewind and try to apply
# the commits again. Obviously, this time we must not try old bases.
# This also implies that none of the recycling magic can possibly be
# successful, so it's all in the else branch.
$holdoff--;
print "Not trying old base.\n" if ($debug);
} else {
advance_base(\$new_base, $$change{old_parent_id}, $upstreamed_changes);
($old_commit, $old_base, $try_old_base) =
prepare_rebase($change, $new_base, $try_minimal && $idx);
$new_base = $old_base if ($try_old_base);
}
if ($failed && !$try_old_base && !@gaps) {
print "Already failed and not nested; skipping.\n" if ($debug);
$$change{freshness} = FAILED;
$base = $$commit{id};
$idx++;
next;
}
my ($new_commit, $did, $errors) =
rebase_commit($commit, $new_base, $old_commit, $old_base, $$change{orig});
if (!$new_commit) {
if (!defined($errors)) {
set_change_error($change, 'oneline',
"Rebasing merges is not supported.");
} else {
if ($try_old_base) {
# This was an attempt to fragment the push.
# Just try again on top of the pending push.
print "Failed to apply; retrying with new base.\n" if ($debug);
$holdoff = 1;
next;
}
if (@gaps) {
# We tried to apply on top of a pending push, but that one was
# fragmented itself. So rewind to the last fragmentation point
# and retry without it. Of course, this causes creation of new
# PatchSets for unmodified Changes.
$holdoff = $idx;
$idx = pop @gaps;
$holdoff -= $idx;
print "Failed to apply; rewinding by $holdoff.\n" if ($debug);
$holdoff++;
$change = $$changes[$idx - 1];
$base = ($$change{final} // $$change{local})->{id};
next;
}
# Failure to apply to the current base is fatal.
set_change_error($change, 'fixed',
",----- Failed to rebase commit ------\n"
.($errors =~ s/^/| /mgr)
."\`------------------------------------\n")
if (!$failed); # Otherwise it's too much noise.
}
$$change{freshness} = FAILED;
$have_conflicts++;
if ($try_minimal) {
# In minimal mode we continue even after a real failure, to
# produce useful annotations for as many commits as possible.
print "Failed to apply.\n" if ($debug);
$failed = 1;
# Use the unrebased commit as the base. This is necessary for
# base comparison by Change-Id in prepare_rebase(). Comparison
# by SHA1 will fail anyway, so this is safe.
$base = $$commit{id};
$idx++;
next;
}
last;
}
printf("Picked as %s (%s)\n", format_id($$new_commit{id}), $did) if ($debug);
if ($try_old_base) {
if ($new_commit != $old_commit) {
# The commit applied on top of the old base, but it was actually
# modified. It is counterproductive to fragment the push in this
# case, so just apply it on top of the pending push, after all.
print "Produced different commit; re-doing with new base.\n" if ($debug);
$holdoff = 1;
next;
}
# The commit applied on top of the old base and yielded the previously
# pushed commit. This means that there is no need to re-push this commit.
# However, a subsequent commit may have been modified, in which case it
# will need re-pushing. In case it applies on top of its old base as well,
# the push needs to be fragmented for the different bases; we call the
# unpushed commits in the middle a "gap". However, if the subsequent
# commit does not apply on top of its old base (because it has a dependency
# on a modification before the gap), we need to close the gap and retry.
# Therefore, we record the fragmentation point before continuing. Gaps can
# be nested, so this is a stack.
print "Started new gap.\n" if ($debug);
push @gaps, $idx;
}
%commit2diff = () if (!@gaps && !$holdoff);
$$change{final} = $new_commit;
$base = $$new_commit{id};
$idx++;
}
}
sub rebase_changes($$)
{
my ($group, $upstreamed_changes) = @_;
with_local_git_index(\&do_rebase_changes, $group, $upstreamed_changes);
}
sub classify_changes_offline($)
{
my ($group) = @_;
foreach my $change (@{$$group{changes}}) {
if (defined($$change{pushed})) {
# We would need to rebase to know, but we didn't, so make no
# particular claims about the exact state.
$$change{freshness} = KNOWN;
} else {
$$change{freshness} = NEW;
}
}
}
my $have_rejected = 0;
my $have_modified = 0;
my $need_force = 0;
sub classify_changes_online($)
{
my ($group) = @_;
my $any_modified = 0;
foreach my $change (@{$$group{changes}}) {
my $commit = $$change{final};
next if (!$commit); # Rebase failure
my $sha1 = $$commit{id};
my $ginfo = $$change{gerrit};
my $status = $ginfo && $$ginfo{status};
my $curr_sha1 = $ginfo && $$ginfo{revs}[-1]{id};
my $chg_revs = $ginfo && $$ginfo{rev_by_id};
my $this_rev = $chg_revs && $$chg_revs{$sha1};
if ($this_rev) {
$$change{patchset} = $$this_rev{ps};
$$change{freshness} =
($status eq 'MERGED') ? MERGED :
($sha1 eq $curr_sha1) ? UNMODIFIED : OUTDATED;
} elsif ($ginfo && ($status ne 'NEW')) {
# We are attempting a push which Gerrit will reject anyway.
my $err;
if ($status eq 'MERGED') {
$err = "Change is already MERGED; use git gpull to rebase.";
} elsif ($status eq 'STAGED' || $status eq 'INTEGRATING') {
$err = "Change is currently $status; cannot proceed.";
} elsif ($status eq 'DEFERRED' || $status eq 'ABANDONED') {
$err = "Change is $status; please restore it first.";
} else {
$err = "Change is in unknown state '$status'. I'm stumped. :}";
}
set_change_error($change, 'oneline', $err);
$$change{freshness} = REJECTED;
$have_rejected++;
} else {
# Finally, check whether the current PatchSet on Gerrit meets our
# expectations, so we don't accidentally play ping-pong.
my $pushed = $$change{pushed};
if ($ginfo) {
if (!defined($pushed) || ($pushed ne $curr_sha1)) {
# Having no pushed commit may indicate either that gpick --check
# was not used or that it found divergence. It doesn't appear
# useful to differentiate between the two.
my $pushed_rev = defined($pushed) && $$chg_revs{$pushed};
$$change{patchset} = $pushed_rev ? $$pushed_rev{ps} : "?";
$$change{freshness} = FORCE;
$need_force = 1;
} else {
$$change{freshness} = MODIFIED;
}
} else {
if (defined($pushed)) {
$$change{freshness} = MISSING;
$need_force = 1;
} else {
$$change{freshness} = NEW;
}
}
$any_modified++;
}
}
if ($any_modified) {
$$group{have_modified} = 1;
$have_modified = 1;
}
}
sub annotate_changes($)
{
my ($group) = @_;
my $hide_mix = $list_only && $$group{hide_mix};
my $excl_mix = !$hide_mix && $list_only && $$group{exclude_mix};
foreach my $change (@{$$group{changes}}) {
my @attribs;
push @attribs, 'HIDDEN' if ($hide_mix && $$change{hide});
push @attribs, 'EXCLUDED' if ($excl_mix && $$change{exclude});
# Changes can be loose only when pushing all or listing, as
# otherwise they are currently being assigned, and showing
# them as still loose would be weird.
my $loose = ($push_all || $list_only) && !defined($$change{grp});
push @attribs, 'LOOSE' if ($loose);
# Changes in the 'modified' state (that is, the ones for which pushing
# actually has an effect) are annotated, while 'unmodified' ones are not.
# This behavior has been chosen after much deliberation following the
# principle that "no-op" should be silent, despite the fact that "doing
# nothing" is a diversion from what a regular git push would do, and is
# thus potentially confusing - but as having no modified changes at all
# leads to an additional message, the less noisy output (assuming that
# most Changes are usually not modified) seems most sensible.
# For loose Changes the most sensible default assumption is that they
# are 'new'; deviations occur for example when cherry-picking was done
# without gpick, or pushing was done without (or with an older) gpush.
my $freshness = $$change{freshness};
if (defined($freshness)
&& ($loose ? ($freshness ne NEW)
: ($freshness ne UNMODIFIED) && ($freshness ne KNOWN))) {
$freshness = "PS$$change{patchset}/$freshness"
if ($freshness eq OUTDATED || $freshness eq FORCE);
push @attribs, $freshness;
}
$$change{annotation} = ' ['.join('; ', @attribs).']'
if (@attribs);
}
}
sub make_listing($)
{
my ($groups) = @_;
foreach my $group (@$groups) {
annotate_changes($group);
}
return report_pushed_changes(@$groups);
}
sub show_changes($)
{
my ($groups) = @_;
print format_reports(make_listing($groups));
}
sub fail_push($@)
{
my ($groups, @msgs) = @_;
my $reports = make_listing($groups);
report_fixed($reports, @msgs);
fail_formatted($reports);
}
sub print_errors($)
{
my ($groups) = @_;
fail_push($groups, "Please use --rebase or specify "
.($have_conflicts > 1 ? "different bases.\n" : "a different base.\n"))
if ($have_conflicts);
fail_push($groups, "Giving up - push is going to be rejected.\n")
if ($have_rejected);
fail_push($groups, "Local state is out of sync with Gerrit.\n",
"Please use git gpick, or specify --force to push nonetheless.\n")
if ($need_force && !$force);
}
sub add_if_unmodified($$)
{
my ($change, $list) = @_;
my $freshness = $$change{freshness};
if (($freshness eq UNMODIFIED) || ($freshness eq OUTDATED)) {
push @$list, $$change{final}{id};
}
}
sub prepare_meta($)
{
my ($group) = @_;
# In principle, it would be possible to do this per fragment
# instead of per group, but that does not seem worth the effort.
my @invite_list;
my (%invite_rvrs, %invite_ccs);
if (@reviewers || @CCs) {
foreach my $change (@{$$group{changes}}) {
my $ginfo = $$change{gerrit};
my $rvrs = $ginfo ? $$ginfo{reviewers} : {};
my $any;
foreach my $rvr (@reviewers) {
if (($$rvrs{$rvr} // RVRTYPE_NONE) != RVRTYPE_REV) {
$invite_rvrs{$rvr} = 1;
$any = 1;
}
}
foreach my $cc (@CCs) {
if (($$rvrs{$cc} // RVRTYPE_NONE) != RVRTYPE_CC) {
$invite_ccs{$cc} = 1;
$any = 1;
}
}
# Can't add reviewers to unmodified Changes with a push.
add_if_unmodified($change, \@invite_list) if ($any);
}
}
$$group{add_rvrs} = [ keys %invite_rvrs ];
$$group{add_ccs} = [ keys %invite_ccs ];
$$group{invite_list} = \@invite_list;
my @topic_list;
my $tpc = $$group{topic};
if (defined($tpc)) {
my $any;
foreach my $change (@{$$group{changes}}) {
next if ($tpc eq ($$change{topic} // ""));
$any = 1;
# Can't set topic of unmodified Changes with a push.
add_if_unmodified($change, \@topic_list);
}
$$group{topic} = undef if (!$any);
}
$$group{topic_list} = \@topic_list;
}
sub push_fragment($$)
{
my ($from, $group) = @_;
my $tip = $$from{final}{id};
my $to = $$group{branch};
my $tpc = $$group{topic};
my ($rvrs, $ccs) = ($$group{add_rvrs} // [], $$group{add_ccs} // []);
my $base = $$group{base};
if ($addbase == BASE_YES) {
print "Configuration mandates sending base.\n" if ($debug);
} elsif ($addbase == BASE_MAYBE) {
if ($commit_by_id{$base}) {
# Series which are pushed on top of other pending Changes have their
# base stored in an extra field on Gerrit, as otherwise gpick could
# not reliably determine it.
print "Sending base, as it is not in the upstream branch.\n" if ($debug);
} else {
$base = undef;
}
} else {
print "Configuration forbids sending base.\n" if ($debug);
$base = undef;
}
my @push_options;
push @push_options, "gpush-base=$base" if (defined($base));
push @push_options, "topic=$tpc" if (defined($tpc));
push @push_options, map { "r=$_" } @$rvrs;
push @push_options, map { "cc=$_" } @$ccs;
my @gitcmd = ("git", "push");
push @gitcmd, '-v' if ($verbose);
push @gitcmd, '-q' if ($quiet);
push @gitcmd, '-n' if ($dry_run);
push @gitcmd, map { ("-o", "$_") } @push_options;
push @gitcmd, $remote, "$tip:refs/for/$to";
run_process(FWD_OUTPUT, @gitcmd);
}
sub push_changes($)
{
my ($group) = @_;
my @heads;
my $base = "";
foreach my $change (@{$$group{changes}}) {
my $commit = $$change{final};
push @heads, undef if (get_1st_parent($commit) ne $base);
my $freshness = $$change{freshness};
$heads[-1] = $change
if (($freshness ne UNMODIFIED) && ($freshness ne OUTDATED) && ($freshness ne MERGED));
$base = $$commit{id};
}
foreach my $change (@heads) {
push_fragment($change, $group) if ($change);
}
}
sub update_unpushed($)
{
my ($group) = @_;
my $invite_list = $$group{invite_list};
if (@$invite_list) {
state $printed;
print "Inviting reviewers/CCs to unmodified commit(s) ...\n"
if (!$quiet && !$printed);
$printed = 1;
my ($rvrs, $ccs) = ($$group{add_rvrs} // [], $$group{add_ccs} // []);
my @rlist;
foreach my $rvr (@$rvrs) {
push @rlist, { "reviewer" => $rvr, "state" => "REVIEWER" };
}
foreach my $cc (@$ccs) {
push @rlist, { "reviewer" => $cc, "state" => "CC" };
}
my $json = { "reviewers" => \@rlist };
my $pipe = open_process(USE_STDIN | FWD_OUTPUT | DRY_RUN,
'ssh', @gerrit_ssh, 'gerrit', 'review', '--json',
@$invite_list);
write_process($pipe, encode_json($json)."\n");
close_process($pipe);
}
my $topic_list = $$group{topic_list};
if (@$topic_list) {
# TODO: This can be done with the REST API.
state $printed;
print "Warning: Cannot set topic on unmodified commits.\n"
if (!$printed);
$printed = 1;
}
}
# We save the "prospective" state separately, so it remains authoritative
# over gpick'd updates from Gerrit. However, we don't do that for the
# series grouping, as that would lead to some unintuitive corner cases.
sub update_state_grouping($)
{
my ($group) = @_;
my $gid = $$group{gid};
foreach my $change (@{$$group{changes}}) {
$$change{grp} = $gid;
# We persist only explicitly specified values. Fallbacks
# are applied when the series is actually pushed.
$$change{ntopic} = $topic;
$$change{ntgt} = $ref_to;
$$change{nbase} = $ref_base;
$$change{exclude} = $exclude ? 1 : undef;
}
}
sub update_state($)
{
my ($group) = @_;
my ($gid, $branch, $tpc, $base) =
($$group{gid}, $$group{branch}, $$group{topic}, $$group{base});
# Setting an empty topic clears the previous topic from the server.
$tpc = undef if (defined($tpc) && !length($tpc));
foreach my $change (@{$$group{changes}}) {
$$change{grp} = $gid;
my $sha1 = $$change{final}{id};
if (($$change{pushed} // "") ne $sha1) {
$$change{pushed} = $sha1;
$$change{topic} = $tpc;
$$change{ntopic} = undef;
}
$$change{tgt} = $branch;
$$change{ntgt} = undef;
my $commit = $$change{local};
$$change{orig} = $$commit{id};
# --rebase would be required after redoing a merge with a different
# 1st parent anyway, so just don't save the base for merges.
# Regular commits on top of merges are auto-rebased as well.
$base = undef if (@{$$commit{parents}} > 1);
$$change{base} = $base;
$$change{nbase} = undef;
$$change{exclude} = undef;
}
}
sub execute_grouping()
{
my $group = get_changes();
# We validate a possibly specified push base early on, mostly
# because delaying it would be a lot more complicated or costly.
my @queries;
resolve_ref_base(\@queries);
if (@queries) {
query_gerrit(\@queries);
confirm_ref_base();
}
# We could rebase here, as that would tell us whether the desired
# grouping is feasible (re dependencies) early on. However, it would
# significantly slow down the operation for this little benefit.
# We also don't query Gerrit, as online-classifying unrebased Changes
# yields mostly irrelevant and potentially even confusing annotations;
# branch tracking is not necessary here, either.
if (!$quiet) {
classify_changes_offline($group);
show_changes([ $group ]);
}
print "Not pushing - group mode.\n" if ($debug);
update_state_grouping($group);
}
sub execute_pushing()
{
my $online = !$list_only || $list_online;
my ($groups, $all_groups);
if ($push_all) {
my $have_loose;
($have_loose, $all_groups) = get_all_changes();
if ($have_loose && !$list_only) {
foreach my $group (@$all_groups) {
classify_changes_offline($group);
}
fail_push($all_groups, "Cannot push all with any free-standing loose Changes.\n");
}
$groups = [ grep { !$$_{hide} && !$$_{exclude} } @$all_groups ];
} else {
$groups = $all_groups = [ get_changes() ];
}
my $pushed_changes = [ map { @{$$_{changes}} } @$groups ];
if ($online) {
my @queries = map { "change:".$$_{id} } @$pushed_changes;
push @queries,
map { "commit:".$_ }
# Only ones that are not upstream yet.
grep { $commit_by_id{$_} }
# Only 2nd+ parents of local commits.
map { @$_ > 1 ? @$_[1 .. $#$_] : () }
map { $$_{local}{parents} } @$pushed_changes;
resolve_ref_base(\@queries);
my @args;
push @args, "--all-reviewers" if (@reviewers || @CCs);
query_gerrit(\@queries, \@args) if (@queries);
}
foreach my $group (@$groups) {
determine_remote_branch($group);
}
if ($online) {
foreach my $group (@$groups) {
check_merges($group);
}
map_remote_changes($groups);
}
foreach my $group (@$groups) {
determine_topic($group);
}
if ($online) {
# Rebasing may take a while, so make sure all base determination
# errors are reported before we start.
foreach my $group (@$groups) {
if (defined($$group{gid})) {
determine_base($group);
}
}
visit_pushed_changes($groups);
my $upstreamed_changes = get_upstreamed_changes($pushed_changes);
foreach my $group (@$groups) {
if (defined($$group{gid})) {
rebase_changes($group, $upstreamed_changes);
classify_changes_online($group);
} else {
# We don't rebase sets of loose Changes, as this will often
# create garbage or fail. We can get here only in --list mode.
classify_changes_offline($group);
}
}
} elsif (!$quiet) {
foreach my $group (@$groups) {
classify_changes_offline($group);
}
}
if ($list_only) {
show_changes($all_groups);
print "Not pushing - list mode.\n" if ($debug);
} else {
print_errors($groups);
show_changes($groups) if (!$quiet);
foreach my $group (@$groups) {
prepare_meta($group);
}
if ($have_modified) {
foreach my $group (@$groups) {
push_changes($group) if ($$group{have_modified});
}
} else {
print "No modified commits - nothing to push.\n" if (!$quiet);
}
foreach my $group (@$groups) {
update_unpushed($group);
}
# This makes sense even if no modified commits are pushed
# (e.g., syncing state after a dumb push).
foreach my $group (@$groups) {
update_state($group);
}
}
}
process_config();
parse_arguments(@ARGV);
goto_gitdir();
load_state(0);
if ($group_only) {
execute_grouping();
} else {
execute_pushing();
}
save_state($dry_run);
|