summaryrefslogtreecommitdiff
path: root/git_review/cmd.py
blob: b3e7e38e8146847041571ba71555e85b980f4957 (plain)
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
# -*- coding: utf-8 -*-

# Copyright OpenStack Foundation and OpenDev Contributors
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License.  You may obtain
# a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

from __future__ import print_function

import argparse
import configparser
import datetime
import getpass
import json
import os
import re
import shlex
import subprocess
import sys
import textwrap
from urllib.parse import urlencode
from urllib.parse import urljoin
from urllib.parse import urlparse

import pkg_resources
import requests


VERBOSE = False
UPDATE = False
LOCAL_MODE = 'GITREVIEW_LOCAL_MODE' in os.environ
CONFIGDIR = os.path.expanduser("~/.config/git-review")
GLOBAL_CONFIG = "/etc/git-review/git-review.conf"
USER_CONFIG = os.path.join(CONFIGDIR, "git-review.conf")
DEFAULTS = dict(scheme='ssh', hostname=False, port=None, project=False,
                branch='master', remote="gerrit", rebase="1",
                track="0", usepushurl="0", notopic=False, branchauthor="name")
COPYRIGHT = """\
Copyright OpenStack Foundation and OpenDev Contributors

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied.

See the License for the specific language governing permissions and
limitations under the License.
"""


_branch_name = None
_has_color = None
_use_color = None
_orig_head = None
_rewrites = None
_rewrites_push = None
BRANCHAUTHOR = None


class colors(object):
    yellow = '\033[33m'
    green = '\033[92m'
    reset = '\033[0m'
    blue = '\033[36m'


class GitReviewException(Exception):
    EXIT_CODE = 1


class CommandFailed(GitReviewException):

    def __init__(self, *args):
        Exception.__init__(self, *args)
        (self.rc, self.output, self.argv, self.envp) = args
        self.quickmsg = dict([
            ("argv", " ".join(self.argv)),
            ("rc", self.rc),
            ("output", self.output)])

    def __str__(self):
        return self.__doc__ + """
The following command failed with exit code %(rc)d
    "%(argv)s"
-----------------------
%(output)s
-----------------------""" % self.quickmsg


class ChangeSetException(GitReviewException):

    def __init__(self, e):
        GitReviewException.__init__(self)
        self.e = str(e)

    def __str__(self):
        return self.__doc__ % self.e


def printwrap(unwrapped):
    print('\n'.join(textwrap.wrap(unwrapped)))


def warn(warning):
    printwrap("WARNING: %s" % warning)


def parse_review_number(review):
    parts = review.split(',')
    if len(parts) < 2:
        parts.append(None)
    return parts


def build_review_number(review, patchset):
    if patchset is not None:
        return '%s,%s' % (review, patchset)
    return review


def run_command_status(*argv, **kwargs):
    if VERBOSE:
        print(datetime.datetime.now(), "Running:", " ".join(argv))
    if len(argv) == 1:
        argv = shlex.split(str(argv[0]))
    stdin = kwargs.pop('stdin', None)
    newenv = os.environ.copy()
    newenv['LANG'] = 'C'
    newenv['LANGUAGE'] = 'C'
    newenv.update(kwargs)
    p = subprocess.Popen(argv,
                         stdin=subprocess.PIPE if stdin else None,
                         stdout=subprocess.PIPE,
                         stderr=subprocess.STDOUT,
                         env=newenv, universal_newlines=True)
    (out, nothing) = p.communicate(stdin)
    return (p.returncode, out.strip())


def run_command(*argv, **kwargs):
    (rc, output) = run_command_status(*argv, **kwargs)
    return output


def run_command_exc(klazz, *argv, **env):
    """Run command *argv, on failure raise klazz

    klazz should be derived from CommandFailed
    """
    (rc, output) = run_command_status(*argv, **env)
    if rc:
        raise klazz(rc, output, argv, env)
    return output


def git_credentials(url):
    """Return credentials using git credential or None."""
    cmd = 'git', 'credential', 'fill'
    stdin = 'url=%s' % url
    rc, out = run_command_status(*cmd, stdin=stdin)
    if rc:
        return None
    data = dict(line.split('=', 1) for line in out.splitlines())
    return data['username'], data['password']


def http_code_2_return_code(code):
    """Tranform http status code to system return code."""
    return (code - 301) % 255 + 1


def run_http_exc(klazz, url, **env):
    """Run http GET request url, on failure raise klazz

    klazz should be derived from CommandFailed
    """
    if url.startswith("https://") and "verify" not in env:
        if "GIT_SSL_NO_VERIFY" in os.environ:
            env["verify"] = False
        else:
            verify = git_config_get_value("http", "sslVerify", as_bool=True)
            env["verify"] = verify != 'false'

    try:
        res = requests.get(url, **env)
        if res.status_code == 401:
            creds = git_credentials(url)
            if creds:
                env['auth'] = creds
                res = requests.get(url, **env)
    except klazz:
        raise
    except Exception as err:
        raise klazz(255, str(err), ('GET', url), env)
    if not 200 <= res.status_code < 300:
        raise klazz(http_code_2_return_code(res.status_code),
                    res.text, ('GET', url), env)
    return res


def get_version():
    requirement = pkg_resources.Requirement.parse('git-review')
    provider = pkg_resources.get_provider(requirement)
    return provider.version


def git_directories():
    """Determine (absolute git work directory path, .git subdirectory path)."""
    cmd = ("git", "rev-parse", "--show-toplevel", "--git-dir")
    out = run_command_exc(GitDirectoriesException, *cmd)
    try:
        return out.splitlines()
    except ValueError:
        raise GitDirectoriesException(0, out, cmd, {})


class GitDirectoriesException(CommandFailed):
    "Cannot determine where .git directory is."
    EXIT_CODE = 70


class CustomScriptException(CommandFailed):
    """Custom script execution failed."""
    EXIT_CODE = 71


def run_custom_script(action):
    """Get status and output of .git/hooks/$action-review or/and
    ~/.config/hooks/$action-review if existing.
    """
    returns = []
    script_file = "%s-review" % (action)
    (top_dir, git_dir) = git_directories()
    paths = [os.path.join(CONFIGDIR, "hooks", script_file),
             os.path.join(git_dir, "hooks", script_file)]
    for fpath in paths:
        if os.path.isfile(fpath) and os.access(fpath, os.X_OK):
            status, output = run_command_status(fpath)
            returns.append((status, output, fpath))

    for (status, output, path) in returns:
        if status:
            raise CustomScriptException(status, output, [path], {})
        elif output and VERBOSE:
            print("script %s output is:" % (path))
            print(output)


def git_config_get_value(section, option, default=None, as_bool=False):
    """Get config value for section/option."""
    cmd = ["git", "config", "--get", "%s.%s" % (section, option)]
    if as_bool:
        cmd.insert(2, "--bool")
    if LOCAL_MODE:
        __, git_dir = git_directories()
        cmd[2:2] = ['-f', os.path.join(git_dir, 'config')]
    try:
        result = run_command_exc(GitConfigException, *cmd).strip()
        if VERBOSE:
            print(datetime.datetime.now(), "... %s.%s = %s"
                  % (section, option, result))
        return result
    except GitConfigException as exc:
        if exc.rc == 1:
            if VERBOSE and default is not None:
                print(datetime.datetime.now(),
                      "... nothing in git config, returning func parameter:",
                      default)
            return default
        raise


class Config(object):
    """Expose as dictionary configuration options."""

    def __init__(self, config_file=None):
        self.config = DEFAULTS.copy()
        filenames = [] if LOCAL_MODE else [GLOBAL_CONFIG, USER_CONFIG]
        if config_file:
            filenames.append(config_file)
        for filename in filenames:
            if os.path.exists(filename):
                if filename != config_file:
                    msg = ("Using global/system git-review config files (%s) "
                           "is deprecated and will be removed in a future "
                           "release")
                    warn(msg % filename)
                self.config.update(load_config_file(filename))

    def __getitem__(self, key):
        """Let 'git config --get' override every Config['key'] access"""
        value = git_config_get_value('gitreview', key)
        if value is None:
            value = self.config[key]
        # "--verbose" doesn't trace *early* invocations; for that you
        # must change the value at the top of this file (*and* pass
        # --verbose)
        if VERBOSE:
            print(datetime.datetime.now(),
                  "Config['%s'] = %s " % (key, value))
        return value


class GitConfigException(CommandFailed):
    """Git config value retrieval failed."""
    EXIT_CODE = 128


class CannotInstallHook(CommandFailed):
    "Problems encountered installing commit-msg hook"
    EXIT_CODE = 2


def set_hooks_commit_msg(remote, target_file):
    """Install the commit message hook if needed."""

    # Create the hooks directory if it's not there already
    hooks_dir = os.path.dirname(target_file)
    if not os.path.isdir(hooks_dir):
        os.mkdir(hooks_dir)

    if not os.path.exists(target_file) or UPDATE:
        remote_url = get_remote_url(remote)
        if (remote_url.startswith('http://') or
                remote_url.startswith('https://')):
            hook_url = urljoin(remote_url, '/tools/hooks/commit-msg')
            if VERBOSE:
                print("Fetching commit hook from: %s" % hook_url)
            res = run_http_exc(CannotInstallHook, hook_url, stream=True)
            with open(target_file, 'wb') as f:
                for x in res.iter_content(1024):
                    f.write(x)
        else:
            (hostname, username, port, project_name) = \
                parse_gerrit_ssh_params_from_git_url(remote_url)
            if username:
                userhost = "%s@%s" % (username, hostname)
            else:
                userhost = hostname
            # OS independent target file
            scp_target_file = target_file.replace(os.sep, "/")
            cmd = ["scp", userhost + ":hooks/commit-msg", scp_target_file]
            if port is not None:
                cmd.insert(1, "-P%s" % port)

            if VERBOSE:
                hook_url = 'scp://%s%s/hooks/commit-msg' \
                           % (userhost, (":%s" % port) if port else "")
                print("Fetching commit hook from: %s" % hook_url)
            run_command_exc(CannotInstallHook, *cmd)
        # If there are submodules, the hook needs to be installed into
        # each of them.
        run_command_exc(
            CannotInstallHook,
            "git", "submodule", "foreach",
            'cp -p %s "$(git rev-parse --git-dir)/hooks/"' % target_file)

    if not os.access(target_file, os.X_OK):
        os.chmod(target_file, os.path.stat.S_IREAD | os.path.stat.S_IEXEC)


def test_remote_url(remote_url):
    """Tests that a possible gerrit remote url works."""
    status, description = run_command_status("git", "push", "--dry-run",
                                             remote_url, "--all")
    if status != 128:
        if VERBOSE:
            print("%s worked. Description: %s" % (remote_url, description))
        return True
    else:
        print("%s did not work. Description: %s" % (remote_url, description))
        return False


def make_remote_url(scheme, username, hostname, port, project):
    """Builds a gerrit remote URL."""
    if port is None and scheme == 'ssh':
        port = 29418
    hostport = '%s:%s' % (hostname, port) if port else hostname
    if username:
        return "%s://%s@%s/%s" % (scheme, username, hostport, project)
    else:
        return "%s://%s/%s" % (scheme, hostport, project)


def add_remote(scheme, hostname, port, project, remote, usepushurl):
    """Adds a gerrit remote."""
    asked_for_username = False

    username = git_config_get_value("gitreview", "username")
    if not username:
        username = getpass.getuser()

    remote_url = make_remote_url(scheme, username, hostname, port, project)
    if VERBOSE:
        print("No remote set, testing %s" % remote_url)
    if not test_remote_url(remote_url):
        print("Could not connect to gerrit.")
        username = input("Enter your gerrit username: ")
        remote_url = make_remote_url(scheme, username, hostname, port, project)
        print("Trying again with %s" % remote_url)
        if not test_remote_url(remote_url):
            raise GerritConnectionException(
                "Could not connect to gerrit at %s" % remote_url
            )
        asked_for_username = True

    if usepushurl:
        cmd = "git remote set-url --push %s %s" % (remote, remote_url)
        print("Adding a git push url to '%s' that maps to:" % remote)
    else:
        cmd = "git remote add -f %s %s" % (remote, remote_url)
        print("Creating a git remote called '%s' that maps to:" % remote)
    print("\t%s" % remote_url)

    (status, remote_output) = run_command_status(cmd)
    if status:
        raise CommandFailed(status, remote_output, cmd, {})

    if asked_for_username:
        print()
        printwrap("This repository is now set up for use with git-review. "
                  "You can set the default username for future repositories "
                  "with:")
        print('  git config --global --add gitreview.username "%s"' % username)
        print()


def populate_rewrites():
    """Populate the global _rewrites and _rewrites_push maps based on the
    output of "git-config".
    """

    cmd = ['git', 'config', '--list']
    out = run_command_exc(CommandFailed, *cmd).strip()

    global _rewrites, _rewrites_push
    _rewrites = {}
    _rewrites_push = {}

    for entry in out.splitlines():
        key, _, value = entry.partition('=')
        key = key.lower()

        if key.startswith('url.') and key.endswith('.insteadof'):
            rewrite = key[len('url.'):-len('.insteadof')]
            if rewrite:
                _rewrites[value] = rewrite
        elif key.startswith('url.') and key.endswith('.pushinsteadof'):
            rewrite = key[len('url.'):-len('.pushinsteadof')]
            if rewrite:
                _rewrites_push[value] = rewrite


def alias_url(url, rewrite_push):
    """Expand a remote URL. Use the global map _rewrites to replace the
    longest match with its equivalent. If rewrite_push is True, try
    _rewrites_push before _rewrites.
    """

    if _rewrites is None:
        populate_rewrites()

    if rewrite_push:
        maps = [_rewrites_push, _rewrites]
    else:
        maps = [_rewrites]

    for rewrites in maps:
        # If Git finds a pushInsteadOf alias, it uses that even if
        # there is a longer insteadOf alias.
        longest = None
        for alias in rewrites:
            if url.startswith(alias) and (
                    longest is None or len(longest) < len(alias)):
                longest = alias

        if longest:
            return url.replace(longest, rewrites[longest])

    return url


def get_remote_url(remote):
    """Retrieve the remote URL. Read the configuration to expand the URL of a
    remote repository taking into account any "url.<base>.insteadOf" or
    "url.<base>.pushInsteadOf" config setting.

    TODO: Replace current code with something like "git ls-remote
    --get-url" after Git grows a version of it that returns the push
    URL rather than the fetch URL.
    """

    push_url = git_config_get_value('remote.%s' % remote, 'pushurl')
    if push_url is not None:
        # Git rewrites pushurl using insteadOf but not pushInsteadOf.
        push_url = alias_url(push_url, False)
    else:
        url = git_config_get_value('remote.%s' % remote, 'url')
        # Git rewrites url using pushInsteadOf or insteadOf.
        push_url = alias_url(url, True)
    if VERBOSE:
        print("Found origin Push URL:", push_url)
    return push_url


def parse_gerrit_ssh_params_from_git_url(git_url):
    """Parse a given Git "URL" into Gerrit parameters. Git "URLs" are either
    real URLs or SCP-style addresses.
    """

    # The exact code for this in Git itself is a bit obtuse, so just do
    # something sensible and pythonic here instead of copying the exact
    # minutiae from Git.

    # Handle real(ish) URLs
    if "://" in git_url:
        parsed_url = urlparse(git_url)
        path = parsed_url.path

        hostname = parsed_url.netloc
        username = None
        port = parsed_url.port

        # Workaround bug in urlparse on OSX
        if parsed_url.scheme == "ssh" and parsed_url.path[:2] == "//":
            hostname = parsed_url.path[2:].split("/")[0]

        if "@" in hostname:
            (username, _, hostname) = hostname.rpartition("@")
        if ":" in hostname:
            (hostname, port) = hostname.split(":")

        if port is not None:
            port = str(port)

    # Handle SCP-style addresses
    else:
        username = None
        port = None
        (hostname, path) = git_url.split(":", 1)
        if "@" in hostname:
            (username, hostname) = hostname.split("@", 1)

    # Strip leading slash and trailing .git from the path to form the project
    # name.
    project_name = re.sub(r"^/|(\.git$)", "", path)

    return (hostname, username, port, project_name)


def query_reviews(remote_url, project=None, change=None,
                  current_patch_set=True, exception=CommandFailed,
                  parse_exc=Exception):
    if remote_url.startswith('http://') or remote_url.startswith('https://'):
        query = query_reviews_over_http
    else:
        query = query_reviews_over_ssh
    return query(remote_url,
                 project=project,
                 change=change,
                 current_patch_set=current_patch_set,
                 exception=exception,
                 parse_exc=parse_exc)


def query_reviews_over_http(remote_url, project=None, change=None,
                            current_patch_set=True, exception=CommandFailed,
                            parse_exc=Exception):
    if project:
        # Remove any trailing .git suffixes for project to url comparison
        clean_url = os.path.splitext(remote_url)[0]
        clean_project = os.path.splitext(project)[0]
        if clean_url.endswith(clean_project):
            # Get the "root" url for gerrit by removing the project from the
            # url. For example:
            # https://example.com/foo/project.git gets truncated to
            # https://example.com/foo/ regardless of whether or not none,
            # either, or both of the remote_url or project strings end
            # with .git.
            remote_url = clean_url[:-len(clean_project)]
    url = urljoin(remote_url, 'changes/')
    if change:
        if current_patch_set:
            url += '?q=%s&o=CURRENT_REVISION' % change
        else:
            url += '?q=%s&o=ALL_REVISIONS' % change
    else:
        if project:
            project_name = re.sub(r"^/|(\.git$)", "",
                                  project)
        else:
            project_name = re.sub(r"^/|(\.git$)", "",
                                  urlparse(remote_url).path)
        params = urlencode({'q': 'project:%s status:open' % project_name})
        url += '?' + params

    if VERBOSE:
        print("Query gerrit %s" % url)
    request = run_http_exc(exception, url)
    if VERBOSE:
        print(request.text)
    reviews = json.loads(request.text[4:])

    # Reformat output to match ssh output
    try:
        for review in reviews:
            review["number"] = str(review.pop("_number"))
            if "revisions" not in review:
                continue
            patchsets = {}
            for key, revision in review["revisions"].items():
                fetch_value = list(revision["fetch"].values())[0]
                patchset = {"number": str(revision["_number"]),
                            "ref": fetch_value["ref"]}
                patchsets[key] = patchset
            review["patchSets"] = patchsets.values()
            review["currentPatchSet"] = patchsets[review["current_revision"]]
    except Exception as err:
        raise parse_exc(err)

    return reviews


def query_reviews_over_ssh(remote_url, project=None, change=None,
                           current_patch_set=True, exception=CommandFailed,
                           parse_exc=Exception):
    (hostname, username, port, project_name) = \
        parse_gerrit_ssh_params_from_git_url(remote_url)

    if change:
        if current_patch_set:
            query = "--current-patch-set change:%s" % change
        else:
            query = "--patch-sets change:%s" % change
    else:
        query = "project:%s status:open" % project_name

    port_data = "p%s" % port if port is not None else ""
    if username is None:
        userhost = hostname
    else:
        userhost = "%s@%s" % (username, hostname)

    if VERBOSE:
        print("Query gerrit %s %s" % (remote_url, query))
    output = run_command_exc(
        exception,
        "ssh", "-x" + port_data, userhost,
        "gerrit", "query",
        "--format=JSON %s" % query)
    if VERBOSE:
        print(output)

    changes = []
    try:
        for line in output.split("\n"):
            if line[0] == "{":
                try:
                    data = json.loads(line)
                    if "type" not in data:
                        changes.append(data)
                except Exception:
                    if VERBOSE:
                        print(output)
    except Exception as err:
        raise parse_exc(err)
    return changes


def set_color_output(color="auto"):
    global _use_color
    if check_color_support():
        if color == "auto":
            check_use_color_output()
        else:
            _use_color = color == "always"


def check_use_color_output():
    global _use_color
    if _use_color is None:
        if check_color_support():
            # we can support color, now check if we should use it
            stdout = "true" if sys.stdout.isatty() else "false"
            test_command = "git config --get-colorbool color.review " + stdout
            color = run_command(test_command)
            _use_color = color == "true"
        else:
            _use_color = False
    return _use_color


def check_color_support():
    global _has_color
    if _has_color is None:
        test_command = "git log --color=never --oneline HEAD^1..HEAD"
        (status, output) = run_command_status(test_command)
        if status == 0:
            _has_color = True
        else:
            _has_color = False
    return _has_color


def load_config_file(config_file):
    """Load configuration options from a file."""
    configParser = configparser.ConfigParser()
    configParser.read(config_file)
    options = {
        'scheme': 'scheme',
        'hostname': 'host',
        'port': 'port',
        'project': 'project',
        'branch': 'defaultbranch',
        'remote': 'defaultremote',
        'rebase': 'defaultrebase',
        'track': 'track',
        'notopic': 'notopic',
        'usepushurl': 'usepushurl',
        'branchauthor': 'branchauthor',
    }
    config = {}
    for config_key, option_name in options.items():
        if configParser.has_option('gerrit', option_name):
            config[config_key] = configParser.get('gerrit', option_name)
    return config


def update_remote(remote):
    cmd = "git remote update %s" % remote
    (status, output) = run_command_status(cmd)
    if VERBOSE:
        print(output)
    if status != 0:
        print("Problem running '%s'" % cmd)
        if not VERBOSE:
            print(output)
        return False
    return True


def parse_tracking(ref=None):
    """Return tracked (remote, branch) of current HEAD or other named
       branch if tracking remote.
    """
    if ref is None:
        ref = run_command_exc(
            SymbolicRefFailed,
            "git", "symbolic-ref", "-q", "HEAD")
    tracked = run_command_exc(
        ForEachRefFailed,
        "git", "for-each-ref", "--format=%(upstream)", ref)

    # Only on explicitly tracked remote branch do we diverge from default
    if tracked and tracked.startswith('refs/remotes/'):
        return tracked[13:].partition('/')[::2]

    return None, None


def resolve_tracking(remote, branch):
    """Resolve tracked upstream remote/branch if current branch is tracked."""
    tracked_remote, tracked_branch = parse_tracking()
    # tracked_branch will be empty when tracking a local branch
    if tracked_branch:
        if VERBOSE:
            print('Following tracked %s/%s rather than default %s/%s' % (
                  tracked_remote, tracked_branch,
                  remote, branch))
        return tracked_remote, tracked_branch

    return remote, branch


def check_remote(branch, remote, scheme, hostname, port, project,
                 usepushurl=False):
    """Check that a Gerrit Git remote repo exists, if not, set one."""

    if usepushurl:
        push_url = git_config_get_value('remote.%s' % remote, 'pushurl', None)
        if push_url:
            return
    else:
        has_color = check_color_support()
        if has_color:
            color_never = "--color=never"
        else:
            color_never = ""

        if remote in run_command("git remote").split("\n"):

            remotes = run_command("git branch -a %s" % color_never).split("\n")
            for current_remote in remotes:
                remote_string = "remotes/%s/%s" % (remote, branch)
                if (current_remote.strip() == remote_string and not UPDATE):
                    return
            # We have the remote, but aren't set up to fetch. Fix it
            if VERBOSE:
                print("Setting up gerrit branch tracking for better rebasing")
            update_remote(remote)
            return

    if hostname is False or project is False:
        # This means there was no .gitreview file
        printwrap("No '.gitreview' file found in this repository. We don't "
                  "know where your gerrit is.")
        if usepushurl:
            printwrap("Please set the push-url on your origin remote to the "
                      "location of your gerrit server and try again")
        else:
            printwrap("Please manually create a remote named \"%s\" or "
                      "rename the default one and try again." % remote)
        sys.exit(1)

    # Gerrit remote not present, try to add it
    try:
        add_remote(scheme, hostname, port, project, remote, usepushurl)
    except Exception:
        if usepushurl:
            printwrap("We don't know where your gerrit is. Please manually"
                      " add a push-url to the '%s' remote and try again."
                      % remote)
        else:
            printwrap("We don't know where your gerrit is. Please manually"
                      " create a remote named '%s' and try again." % remote)
        raise


def rebase_changes(branch, remote, interactive=True):

    global _orig_head

    remote_branch = "remotes/%s/%s" % (remote, branch)

    if not update_remote(remote):
        return False

    # since the value of ORIG_HEAD may not be set by rebase as expected
    # for use in undo_rebase, make sure to save it explicitly
    cmd = "git rev-parse HEAD"
    (status, output) = run_command_status(cmd)
    if status != 0:
        print("Errors running %s" % cmd)
        if interactive:
            print(output)
        return False
    _orig_head = output

    # Avoid trying to do a test rebase if there are uncommitted changes.
    # Either the rebase will fail with a similar message, or if the user
    # has turned on rebase.autostash then the subsequent reset will
    # silently discard those changes.
    cmd = "git diff --quiet"
    (status, output) = run_command_status(cmd)
    if status != 0:
        printwrap("You have unstaged changes. Please commit or stash them "
                  "first, and then try again.")
        sys.exit(1)
    cmd = "git diff --cached --quiet"
    (status, output) = run_command_status(cmd)
    if status != 0:
        printwrap("You have uncommitted changes. Please commit or stash them "
                  "first, and then try again.")
        sys.exit(1)

    cmd = "git show-ref --quiet --verify refs/%s" % remote_branch
    (status, output) = run_command_status(cmd)
    if status != 0:
        printwrap("The branch '%s' does not exist on the given remote '%s'. "
                  "If these changes are intended to start a new branch, "
                  "re-run with the '-R' option enabled." % (branch, remote))
        sys.exit(1)

    if interactive:
        cmd = "git rebase -p -i %s" % remote_branch
    else:
        cmd = "git rebase -p %s" % remote_branch

    (status, output) = run_command_status(cmd, GIT_EDITOR='true')
    if status != 0:
        print("Errors running %s" % cmd)
        if interactive:
            print(output)
            printwrap("It is likely that your change has a merge conflict. "
                      "You may resolve it in the working tree now as "
                      "described above and then run 'git review' again, or "
                      "if you do not want to resolve it yet (note that the "
                      "change can not merge until the conflict is resolved) "
                      "you may run 'git rebase --abort' then 'git review -R' "
                      "to upload the change without rebasing.")
        return False
    return True


def undo_rebase():
    global _orig_head
    if not _orig_head:
        return True

    cmd = "git reset --hard %s" % _orig_head
    (status, output) = run_command_status(cmd)
    if status != 0:
        print("Errors running %s" % cmd)
        print(output)
        return False
    return True


def get_branch_name(target_branch):
    global _branch_name
    if _branch_name is not None:
        return _branch_name
    cmd = "git rev-parse --symbolic-full-name --abbrev-ref HEAD"
    _branch_name = run_command(cmd)
    if _branch_name == "HEAD":
        # detached head or no branch found
        _branch_name = target_branch
    return _branch_name


def assert_one_change(remote, branch, yes, have_hook):
    if check_use_color_output():
        use_color = "--color=always"
    else:
        use_color = "--color=never"
    cmd = ("git log %s --decorate --oneline HEAD --not --remotes=%s" % (
           use_color, remote))
    (status, output) = run_command_status(cmd)
    if status != 0:
        print("Had trouble running %s" % cmd)
        print(output)
        sys.exit(1)
    filtered = filter(None, output.split("\n"))
    output_lines = sum(1 for s in filtered)
    if output_lines == 1 and not have_hook:
        printwrap("Your change was committed before the commit hook was "
                  "installed. Amending the commit to add a gerrit change id.")
        run_command("git commit --amend", GIT_EDITOR='true')
    elif output_lines == 0:
        printwrap("No changes between HEAD and %s/%s. Submitting for review "
                  "would be pointless." % (remote, branch))
        sys.exit(1)
    elif output_lines > 1:
        if not yes:
            printwrap("You are about to submit multiple commits. This is "
                      "expected if you are submitting a commit that is "
                      "dependent on one or more in-review commits, or if you "
                      "are submitting multiple self-contained but dependent "
                      "changes. Otherwise you should consider squashing your "
                      "changes into one commit before submitting (for "
                      "indivisible changes) or submitting from separate "
                      "branches (for independent changes).")
            print("\nThe outstanding commits are:\n\n%s\n\n"
                  "Do you really want to submit the above commits?" % output)
            yes_no = input("Type 'yes' to confirm, other to cancel: ")
            if yes_no.lower().strip() != "yes":
                print("Aborting.")
                sys.exit(1)


def get_topic(target_branch):
    branch_name = get_branch_name(target_branch)

    branch_parts = branch_name.split("/")
    if len(branch_parts) >= 3 and branch_parts[0] == "review":
        # We don't want to set the review number as the topic
        if branch_parts[2].isdigit():
            return

        topic = "/".join(branch_parts[2:])
        if VERBOSE:
            print("Using change number %s for the topic of the change "
                  "submitted" % topic)
        return topic

    if VERBOSE:
        print("Using local branch name %s for the topic of the change "
              "submitted" % branch_name)
    return branch_name


class CannotQueryOpenChangesets(CommandFailed):
    "Cannot fetch review information from gerrit"
    EXIT_CODE = 32


class CannotParseOpenChangesets(ChangeSetException):
    "Cannot parse JSON review information from gerrit"
    EXIT_CODE = 33


class Review(dict):
    _default_fields = ('branch', 'topic', 'subject')

    def __init__(self, data):
        if 'number' not in data:
            raise TypeError("<Review> requires 'number' key in data")

        super(Review, self).__init__(data)

        # provide default values for some fields
        for field in self._default_fields:
            self[field] = self.get(field, '-')


class ReviewsPrinter(object):
    def __init__(self, with_topic=False):
        if with_topic:
            self.fields = ('number', 'branch', 'topic', 'subject')
            # > is right justify, < is left, field indices for py26
            self.fields_format = [
                u"{0:>{1}}", u"{2:>{3}}", u"{4:>{5}}", u"{6:<{7}}"]
        else:
            self.fields = ('number', 'branch', 'subject')
            # > is right justify, < is left, field indices for py26
            self.fields_format = [u"{0:>{1}}", u"{2:>{3}}", u"{4:<{5}}"]

        self.fields_colors = ("", "", "", "")
        self.color_reset = ""
        if check_use_color_output():
            self.fields_colors = (
                colors.yellow, colors.green, colors.blue, "")
            self.color_reset = colors.reset

        self.reviews = []

    @property
    def fields_width(self):
        return [
            max(len(str(review[field])) for review in self.reviews)
            for field in self.fields[:-1]
        ] + [1]

    def _get_field_format_str(self, field):
        index = self.fields.index(field)
        return (
            self.fields_colors[index] +
            self.fields_format[index] +
            self.color_reset
        )

    def add_review(self, review):
        self.reviews.append(review)

    def _get_fields_format_str(self):
        return "  ".join([
            self._get_field_format_str(field)
            for field in self.fields])

    def print_review(self, review):
        fields_format_str = self._get_fields_format_str()

        formatted_fields = []
        for field, width in zip(self.fields, self.fields_width):
            formatted_fields.extend((
                review[field], width
            ))

        print(fields_format_str.format(*formatted_fields))

    def do_print(self, reviews):

        total_reviews = len(reviews)

        for review in reviews:
            self.print_review(review)

        print("Found %d items for review" % total_reviews)


def list_reviews(remote, project, with_topic=False):
    remote_url = get_remote_url(remote)

    reviews = []
    for r in query_reviews(remote_url,
                           project=project,
                           exception=CannotQueryOpenChangesets,
                           parse_exc=CannotParseOpenChangesets):
        reviews.append(Review(r))

    if not reviews:
        print("No pending reviews")
        return

    printer = ReviewsPrinter(with_topic=with_topic)
    for review in reviews:
        printer.add_review(review)

    printer.do_print(reviews)
    return 0


class CannotQueryPatchSet(CommandFailed):
    "Cannot query patchset information"
    EXIT_CODE = 34


class ReviewInformationNotFound(ChangeSetException):
    "Could not fetch review information for change %s"
    EXIT_CODE = 35


class ReviewNotFound(ChangeSetException):
    "Gerrit review %s not found"
    EXIT_CODE = 36


class PatchSetGitFetchFailed(CommandFailed):
    """Cannot fetch patchset contents

Does specified change number belong to this project?
"""
    EXIT_CODE = 37


class PatchSetNotFound(ChangeSetException):
    "Review patchset %s not found"
    EXIT_CODE = 38


class GerritConnectionException(GitReviewException):
    """Problem to establish connection to gerrit."""
    EXIT_CODE = 40


class CheckoutNewBranchFailed(CommandFailed):
    "Cannot checkout to new branch"
    EXIT_CODE = 64


class CheckoutExistingBranchFailed(CommandFailed):
    "Cannot checkout existing branch"
    EXIT_CODE = 65


class ResetKeepFailed(CommandFailed):
    "Failed to reset downloaded branch"
    EXIT_CODE = 66


class SetUpstreamBranchFailed(CommandFailed):
    "Cannot set upstream to remote branch"
    EXIT_CODE = 67


class SymbolicRefFailed(CommandFailed):
    "Cannot find symbolic reference"
    EXIT_CODE = 68


class ForEachRefFailed(CommandFailed):
    "Cannot process symbolic reference"
    EXIT_CODE = 69


class BranchTrackingMismatch(GitReviewException):
    "Branch exists but is tracking unexpected branch"
    EXIT_CODE = 70


def fetch_review(review, masterbranch, remote, project):
    remote_url = get_remote_url(remote)

    review_arg = review
    review, patchset_number = parse_review_number(review)
    current_patch_set = patchset_number is None

    review_infos = query_reviews(remote_url,
                                 project=project,
                                 change=review,
                                 current_patch_set=current_patch_set,
                                 exception=CannotQueryPatchSet,
                                 parse_exc=ReviewInformationNotFound)

    if not len(review_infos):
        raise ReviewInformationNotFound(review)
    for info in review_infos:
        if 'branch' in info and info['branch'] == masterbranch:
            if VERBOSE:
                print('Using review info from branch %s' % info['branch'])
            review_info = info
            break
    else:
        review_info = review_infos[0]
        if VERBOSE and 'branch' in review_info:
            print('Using default branch %s' % review_info['branch'])

    try:
        if patchset_number is None:
            refspec = review_info['currentPatchSet']['ref']
        else:
            refspec = [ps for ps in review_info['patchSets']
                       if int(ps['number']) == int(patchset_number)][0]['ref']
    except IndexError:
        raise PatchSetNotFound(review_arg)
    except KeyError:
        raise ReviewNotFound(review)

    try:
        topic = review_info['topic']
        if topic == masterbranch:
            topic = review
    except KeyError:
        topic = review
    try:
        author = re.sub(r'\W+', '_',
                        review_info['owner'][BRANCHAUTHOR]).lower()
    except KeyError:
        author = 'unknown'
    remote_branch = review_info['branch']

    if patchset_number is None:
        branch_name = "review/%s/%s" % (author, topic)
    else:
        branch_name = "review/%s/%s-patch%s" % (author, topic, patchset_number)

    print("Downloading %s from gerrit" % refspec)
    run_command_exc(PatchSetGitFetchFailed,
                    "git", "fetch", remote_url, refspec)
    return branch_name, remote_branch


def checkout_review(branch_name, remote, remote_branch):
    """Checkout a newly fetched (FETCH_HEAD) change
       into a branch
    """

    try:
        run_command_exc(CheckoutNewBranchFailed,
                        "git", "checkout", "-b",
                        branch_name, "FETCH_HEAD")
        # --set-upstream-to is supported starting in git 1.8
        if remote is not None:
            run_command_exc(SetUpstreamBranchFailed,
                            "git", "branch", "--set-upstream-to",
                            '%s/%s' % (remote, remote_branch),
                            branch_name)

    except CheckoutNewBranchFailed as e:
        if re.search(r"already exists\.?", e.output):
            print("Branch %s already exists - reusing" % branch_name)
            track_remote, track_branch = parse_tracking(
                ref='refs/heads/' + branch_name)
            if track_remote and not (track_remote == remote and
                                     track_branch == remote_branch):
                print("Branch %s incorrectly tracking %s/%s instead of %s/%s"
                      % (branch_name,
                         track_remote, track_branch,
                         remote, remote_branch))
                raise BranchTrackingMismatch
            run_command_exc(CheckoutExistingBranchFailed,
                            "git", "checkout", branch_name)
            run_command_exc(ResetKeepFailed,
                            "git", "reset", "--keep", "FETCH_HEAD")
        else:
            raise

    print("Switched to branch \"%s\"" % branch_name)


class PatchSetGitCherrypickFailed(CommandFailed):
    "There was a problem applying changeset contents to the current branch."
    EXIT_CODE = 69


def cherrypick_review(option=None):
    cmd = ["git", "cherry-pick"]
    if option:
        cmd.append(option)
    cmd.append("FETCH_HEAD")
    print(run_command_exc(PatchSetGitCherrypickFailed, *cmd))


class CheckoutBackExistingBranchFailed(CommandFailed):
    "Cannot switch back to existing branch"
    EXIT_CODE = 67


class DeleteBranchFailed(CommandFailed):
    "Failed to delete branch"
    EXIT_CODE = 68


class InvalidPatchsetsToCompare(GitReviewException):
    def __init__(self, patchsetA, patchsetB):
        Exception.__init__(
            self,
            "Invalid patchsets for comparison specified (old=%s,new=%s)" % (
                patchsetA,
                patchsetB))
    EXIT_CODE = 39


def compare_review(review_spec, branch, remote, project, rebase=False):
    new_ps = None    # none means latest

    if '-' in review_spec:
        review_spec, new_ps = review_spec.split('-')
    review, old_ps = parse_review_number(review_spec)

    if old_ps is None or old_ps == new_ps:
        raise InvalidPatchsetsToCompare(old_ps, new_ps)

    old_review = build_review_number(review, old_ps)
    new_review = build_review_number(review, new_ps)

    old_branch, _ = fetch_review(old_review, branch, remote, project)
    checkout_review(old_branch, None, None)

    if rebase:
        print('Rebasing %s' % old_branch)
        rebase = rebase_changes(branch, remote, False)
        if not rebase:
            print('Skipping rebase because of conflicts')
            run_command_exc(CommandFailed, 'git', 'rebase', '--abort')

    new_branch, remote_branch = fetch_review(
        new_review,
        branch,
        remote,
        project)
    checkout_review(new_branch, remote, remote_branch)

    if rebase:
        print('Rebasing also %s' % new_branch)
        if not rebase_changes(branch, remote, False):
            print("Rebasing of the new branch failed, "
                  "diff can be messed up (use -R to not rebase at all)!")
            run_command_exc(CommandFailed, 'git', 'rebase', '--abort')

    subprocess.check_call(['git', 'diff', old_branch])


def finish_branch(target_branch):
    local_branch = get_branch_name(target_branch)
    if VERBOSE:
        print("Switching back to '%s' and deleting '%s'" % (target_branch,
                                                            local_branch))
    run_command_exc(CheckoutBackExistingBranchFailed,
                    "git", "checkout", target_branch)
    print("Switched to branch '%s'" % target_branch)

    run_command_exc(DeleteBranchFailed,
                    "git", "branch", "-D", local_branch)
    print("Deleted branch '%s'" % local_branch)


def convert_bool(one_or_zero):
    "Return a bool on a one or zero string."
    return str(one_or_zero) in ["1", "true", "True"]


class MalformedInput(GitReviewException):
    EXIT_CODE = 3


def assert_valid_reviewers(reviewers):
    """Ensure no whitespace is found in reviewer names, as it will result
    in an invalid refspec.
    """
    for reviewer in reviewers:
        if re.search(r'\s', reviewer):
            raise MalformedInput(
                "Whitespace not allowed in reviewer: '%s'" % reviewer)


class _DownloadFlag(argparse.Action):
    """Special action for the various forms of downloading reviews.

    Additional option parsing: store value in 'dest', but
    at the same time set one of the flag options to True
    """
    def __call__(self, parser, namespace, value, option_string=None):
        url = urlparse(value)
        # Turn URLs into change ids:
        #   https://review.opendev.org/423436
        # and
        #   https://review.opendev.org/423436/
        # and
        #   https://review.opendev.org/#/c/423436
        # and
        #   https://review.opendev.org/c/<project>/+/423436
        # become
        #   "423436"
        # while
        #   https://review.opendev.org/423436/1
        # and
        #   https://review.opendev.org/#/c/423436/1
        # and
        #   https://review.opendev.org/c/<project>/+/423436/1
        # become
        #   "423436,1".
        #
        # If there is a #, the rest of the path is stored in the
        # "fragment", otherwise that will be empty.
        base = url.fragment or url.path
        parts = base.rstrip('/').lstrip('/c').split('/')
        # PolyGerrit places the change after a '+' symbol in the url
        try:
            parts = parts[parts.index('+') + 1:]
        except ValueError:
            pass
        change = parts[0]
        if len(parts) > 1:
            change = '%s,%s' % (change, parts[1])
        setattr(namespace, self.dest, change)
        setattr(namespace, self.const, True)


def _main():
    usage = "git review [OPTIONS] ... [BRANCH]"

    description = """\
A git command for submitting branches to Gerrit.

git-review is a tool that helps submitting git branches to gerrit for
review.
"""

    epilog = """\
additional information:

  Options configurable using: git config --global gitreview.OPTION VALUE

  branchauthor          name (default), username, email
"""

    parser = argparse.ArgumentParser(
        usage=usage,
        description=description,
        formatter_class=argparse.RawDescriptionHelpFormatter,
        epilog=epilog)

    topic_arg_group = parser.add_mutually_exclusive_group()
    topic_arg_group.add_argument("-t", "--topic", dest="topic",
                                 help="Topic to submit branch to")
    topic_arg_group.add_argument("-T", "--no-topic", dest="notopic",
                                 action="store_true",
                                 help="No topic except if explicitly provided")

    parser.add_argument("--reviewers", nargs="+",
                        help="Add reviewers to uploaded patch sets.")
    parser.add_argument("-n", "--dry-run", dest="dry", action="store_true",
                        help="Don't actually submit the branch for review")
    parser.add_argument("-i", "--new-changeid", dest="regenerate",
                        action="store_true",
                        help="Regenerate Change-id before submitting")
    parser.add_argument("-r", "--remote", dest="remote",
                        help="git remote to use for gerrit")
    parser.add_argument("--use-pushurl", dest="usepushurl",
                        action="store_true",
                        help="Use remote push-url logic instead of separate"
                             " remotes")
    parser.add_argument('--notify',
                        choices=['NONE', 'OWNER', 'OWNER_REVIEWERS', 'ALL'],
                        help="Control to whom email notifications are sent,"
                             " defaults to ALL.")

    rebase_group = parser.add_mutually_exclusive_group()
    rebase_group.add_argument("-R", "--no-rebase", dest="rebase",
                              action="store_false",
                              help="Don't rebase changes before submitting.")
    rebase_group.add_argument("-F", "--force-rebase", dest="force_rebase",
                              action="store_true",
                              help="Force rebase even when not needed.")

    track_group = parser.add_mutually_exclusive_group()
    track_group.add_argument("--track", dest="track",
                             action="store_true",
                             help="Use tracked branch as default.")
    track_group.add_argument("--no-track", dest="track",
                             action="store_false",
                             help="Ignore tracked branch.")

    fetch = parser.add_mutually_exclusive_group()
    fetch.set_defaults(download=False, compare=False, cherrypickcommit=False,
                       cherrypickindicate=False, cherrypickonly=False)
    fetch.add_argument("-d", "--download", dest="changeidentifier",
                       action=_DownloadFlag, metavar="CHANGE[,PS]",
                       const="download",
                       help="Download the contents of an existing gerrit "
                            "review into a branch. Include the patchset "
                            "number to download a specific version of the "
                            "change. The default is to take the most recent "
                            "version.")
    fetch.add_argument("-x", "--cherrypick", dest="changeidentifier",
                       action=_DownloadFlag, metavar="CHANGE",
                       const="cherrypickcommit",
                       help="Apply the contents of an existing gerrit "
                             "review onto the current branch and commit "
                             "(cherry pick; not recommended in most "
                             "situations)")
    fetch.add_argument("-X", "--cherrypickindicate", dest="changeidentifier",
                       action=_DownloadFlag, metavar="CHANGE",
                       const="cherrypickindicate",
                       help="Apply the contents of an existing gerrit "
                       "review onto the current branch and commit, "
                       "indicating its origin")
    fetch.add_argument("-N", "--cherrypickonly", dest="changeidentifier",
                       action=_DownloadFlag, metavar="CHANGE",
                       const="cherrypickonly",
                       help="Apply the contents of an existing gerrit "
                       "review to the working directory and prepare "
                       "for commit")
    fetch.add_argument("-m", "--compare", dest="changeidentifier",
                       action=_DownloadFlag, metavar="CHANGE,PS[-NEW_PS]",
                       const="compare",
                       help="Download specified and latest (or NEW_PS) "
                       "patchsets of an existing gerrit review into "
                       "a branches, rebase on master "
                       "(skipped on conflicts or when -R is specified) "
                       "and show their differences")

    parser.add_argument("-u", "--update", dest="update", action="store_true",
                        help="Force updates from remote locations")
    parser.add_argument("-s", "--setup", dest="setup", action="store_true",
                        help="Just run the repo setup commands but don't "
                             "submit anything")
    parser.add_argument("-f", "--finish", dest="finish", action="store_true",
                        help="Close down this branch and switch back to "
                             "master on successful submission")
    parser.add_argument("-l", "--list", dest="list", action="count",
                        help="List available reviews for the current project, "
                        "if passed more than once, will show more information")
    parser.add_argument("-y", "--yes", dest="yes", action="store_true",
                        help="Indicate that you do, in fact, understand if "
                             "you are submitting more than one patch")
    parser.add_argument("-v", "--verbose", dest="verbose", action="store_true",
                        help="Output more information about what's going on")

    wip_group = parser.add_mutually_exclusive_group()
    wip_group.add_argument("-w", "--work-in-progress", dest="wip",
                           action="store_true",
                           help="Send patch as work in progress for Gerrit "
                                "versions >= 2.15")
    wip_group.add_argument("-W", "--ready", dest="ready", action="store_true",
                           help="Send patch that is already work in progress"
                                " as ready for review. Gerrit versions >="
                                " 2.15")

    private_group = parser.add_mutually_exclusive_group()
    private_group.add_argument("-p", "--private", dest="private",
                               action="store_true",
                               help="Send patch as a private patch ready for "
                                    "review. Gerrit versions >= 2.15")
    private_group.add_argument("-P", "--remove-private", dest="remove_private",
                               action="store_true",
                               help="Send patch which already in private state"
                                    " to normal patch."" Gerrit versions >= "
                                    "2.15")

    parser.add_argument("--no-custom-script", dest="custom_script",
                        action="store_false", default=True,
                        help="Do not run custom scripts.")
    parser.add_argument("--color", dest="color", metavar="<when>",
                        nargs="?", choices=["always", "never", "auto"],
                        help="Show color output. --color (without [<when>]) "
                             "is the same as --color=always. <when> can be "
                             "one of %(choices)s. Behaviour can also be "
                             "controlled by the color.ui and color.review "
                             "configuration settings.")
    parser.add_argument("--no-color", dest="color", action="store_const",
                        const="never",
                        help="Turn off colored output. Can be used to "
                             "override configuration options. Same as "
                             "setting --color=never.")
    parser.add_argument("--license", dest="license", action="store_true",
                        help="Print the license and exit")
    parser.add_argument("--version", action="version",
                        version='%s version %s' %
                        (os.path.split(sys.argv[0])[-1], get_version()))
    parser.add_argument("branch", nargs="?")

    parser.set_defaults(dry=False,
                        verbose=False,
                        update=False,
                        setup=False,
                        list=False,
                        yes=False,
                        wip=False,
                        ready=False,
                        private=False,
                        remove_private=False)

    try:
        (top_dir, git_dir) = git_directories()
    except GitDirectoriesException as _no_git_dir:
        no_git_dir = _no_git_dir
    else:
        no_git_dir = False
        config = Config(os.path.join(top_dir, ".gitreview"))
        parser.set_defaults(rebase=convert_bool(config['rebase']),
                            track=convert_bool(config['track']),
                            remote=None,
                            notopic=convert_bool(config['notopic']),
                            usepushurl=convert_bool(config['usepushurl']))
    options = parser.parse_args()

    if options.license:
        print(COPYRIGHT)
        sys.exit(0)

    if no_git_dir:
        raise no_git_dir

    if options.branch is None:
        branch = config['branch']
    else:
        # explicitly-specified branch on command line overrides options.track
        branch = options.branch
        options.track = False

    global VERBOSE
    global UPDATE
    VERBOSE = options.verbose
    UPDATE = options.update
    remote = options.remote
    if not remote:
        if options.usepushurl:
            remote = 'origin'
        else:
            remote = config['remote']
    yes = options.yes
    status = 0

    global BRANCHAUTHOR
    BRANCHAUTHOR = config['branchauthor']
    if BRANCHAUTHOR not in ["name", "email", "username"]:
        warn("Invalid gitreview.branchauthor: %s (using default: %s)"
             % (BRANCHAUTHOR, DEFAULTS["branchauthor"]))
        BRANCHAUTHOR = DEFAULTS["branchauthor"]

    if options.track:
        remote, branch = resolve_tracking(remote, branch)

    check_remote(branch, remote, config['scheme'],
                 config['hostname'], config['port'], config['project'],
                 usepushurl=options.usepushurl)

    if options.color:
        set_color_output(options.color)

    if options.changeidentifier:
        if options.compare:
            compare_review(options.changeidentifier,
                           branch, remote, config['project'],
                           options.rebase)
            return
        local_branch, remote_branch = fetch_review(options.changeidentifier,
                                                   branch, remote,
                                                   config['project'])
        if options.download:
            checkout_review(local_branch, remote, remote_branch)
        else:
            if options.cherrypickcommit:
                cherrypick_review()
            elif options.cherrypickonly:
                cherrypick_review("-n")
            if options.cherrypickindicate:
                cherrypick_review("-x")
        return
    elif options.list:
        with_topic = options.list > 1
        list_reviews(remote, config['project'], with_topic=with_topic)
        return

    if options.custom_script:
        run_custom_script("pre")

    hook_file = os.path.join(git_dir, "hooks", "commit-msg")
    have_hook = os.path.exists(hook_file) and os.access(hook_file, os.X_OK)

    if not have_hook:
        set_hooks_commit_msg(remote, hook_file)

    if options.setup:
        if options.finish and not options.dry:
            finish_branch(branch)
        return

    if options.rebase or options.force_rebase:
        if not rebase_changes(branch, remote):
            sys.exit(1)
        if not options.force_rebase and not undo_rebase():
            sys.exit(1)
    assert_one_change(remote, branch, yes, have_hook)

    ref = "for"

    cmd = ("git push --no-follow-tags %s HEAD:refs/%s/%s" %
           (remote, ref, branch))
    push_options = []
    if options.topic is not None:
        topic = options.topic
    else:
        topic = None if options.notopic else get_topic(branch)

    if topic and topic != branch:
        push_options.append("topic=%s" % topic)

    if options.reviewers:
        assert_valid_reviewers(options.reviewers)
        push_options += ["r=%s" % r for r in options.reviewers]

    if options.regenerate:
        print("Amending the commit to regenerate the change id\n")
        regenerate_cmd = "git commit --amend"
        if options.dry:
            print("\tGIT_EDITOR=\"sed -i -e '/^Change-Id:/d'\" %s\n" %
                  regenerate_cmd)
        else:
            run_command(regenerate_cmd,
                        GIT_EDITOR="sed -i -e "
                        "'/^Change-Id:/d'")

    if options.wip:
        push_options.append('wip')

    if options.ready:
        push_options.append('ready')

    if options.private:
        push_options.append('private')

    if options.remove_private:
        push_options.append('remove-private')

    if options.notify is not None:
        push_options.append("notify=%s" % options.notify)

    if push_options:
        cmd += "%" + ",".join(push_options)
    if options.dry:
        print("Please use the following command "
              "to send your commits to review:\n")
        print("\t%s\n" % cmd)
    else:
        (status, output) = run_command_status(cmd)
        print(output)

    if options.finish and not options.dry and status == 0:
        finish_branch(branch)
        return

    if options.custom_script:
        run_custom_script("post")
    sys.exit(status)


def main():

    try:
        _main()
    except GitReviewException as e:
        print(e)
        sys.exit(e.EXIT_CODE)


if __name__ == "__main__":
    main()