| OLD | NEW |
| 1 #!/usr/bin/python | 1 #!/usr/bin/python |
| 2 # git-cl -- a git-command for integrating reviews on Rietveld | 2 # git-cl -- a git-command for integrating reviews on Rietveld |
| 3 # Copyright (C) 2008 Evan Martin <martine@danga.com> | 3 # Copyright (C) 2008 Evan Martin <martine@danga.com> |
| 4 | 4 |
| 5 import errno | 5 import errno |
| 6 import logging | 6 import logging |
| 7 import optparse | 7 import optparse |
| 8 import os | 8 import os |
| 9 import re | 9 import re |
| 10 import subprocess | 10 import subprocess |
| 11 import sys | 11 import sys |
| 12 import tempfile | 12 import tempfile |
| 13 import textwrap | 13 import textwrap |
| 14 import urlparse | 14 import urlparse |
| 15 import urllib2 | 15 import urllib2 |
| 16 | 16 |
| 17 try: | 17 try: |
| 18 import readline # pylint: disable=W0611 | 18 import readline # pylint: disable=W0611 |
| 19 except ImportError: | 19 except ImportError: |
| 20 pass | 20 pass |
| 21 | 21 |
| 22 # TODO(dpranke): don't use relative import. | |
| 23 import upload # pylint: disable=W0403 | |
| 24 try: | 22 try: |
| 25 # TODO(dpranke): We wrap this in a try block for a limited form of | 23 import simplejson as json # pylint: disable=F0401 |
| 26 # backwards-compatibility with older versions of git-cl that weren't | |
| 27 # dependent on depot_tools. This version should still work outside of | |
| 28 # depot_tools as long as --bypass-hooks is used. We should remove this | |
| 29 # once this has baked for a while and things seem safe. | |
| 30 depot_tools_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) | |
| 31 sys.path.append(depot_tools_path) | |
| 32 import breakpad # pylint: disable=W0611 | |
| 33 except ImportError: | 24 except ImportError: |
| 34 pass | 25 try: |
| 26 import json |
| 27 except ImportError: |
| 28 # Fall back to the packaged version. |
| 29 from third_party import simplejson as json |
| 30 |
| 31 |
| 32 from third_party import upload |
| 33 import breakpad # pylint: disable=W0611 |
| 34 import presubmit_support |
| 35 import scm |
| 36 import watchlists |
| 37 |
| 38 |
| 35 | 39 |
| 36 DEFAULT_SERVER = 'http://codereview.appspot.com' | 40 DEFAULT_SERVER = 'http://codereview.appspot.com' |
| 37 POSTUPSTREAM_HOOK_PATTERN = '.git/hooks/post-cl-%s' | 41 POSTUPSTREAM_HOOK_PATTERN = '.git/hooks/post-cl-%s' |
| 38 DESCRIPTION_BACKUP_FILE = '~/.git_cl_description_backup' | 42 DESCRIPTION_BACKUP_FILE = '~/.git_cl_description_backup' |
| 39 | 43 |
| 40 def DieWithError(message): | 44 def DieWithError(message): |
| 41 print >> sys.stderr, message | 45 print >> sys.stderr, message |
| 42 sys.exit(1) | 46 sys.exit(1) |
| 43 | 47 |
| 44 | 48 |
| (...skipping 719 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 764 def ConvertToInteger(inputval): | 768 def ConvertToInteger(inputval): |
| 765 """Convert a string to integer, but returns either an int or None.""" | 769 """Convert a string to integer, but returns either an int or None.""" |
| 766 try: | 770 try: |
| 767 return int(inputval) | 771 return int(inputval) |
| 768 except (TypeError, ValueError): | 772 except (TypeError, ValueError): |
| 769 return None | 773 return None |
| 770 | 774 |
| 771 | 775 |
| 772 def RunHook(committing, upstream_branch, rietveld_server, tbr, may_prompt): | 776 def RunHook(committing, upstream_branch, rietveld_server, tbr, may_prompt): |
| 773 """Calls sys.exit() if the hook fails; returns a HookResults otherwise.""" | 777 """Calls sys.exit() if the hook fails; returns a HookResults otherwise.""" |
| 774 import presubmit_support | |
| 775 import scm | |
| 776 import watchlists | |
| 777 | |
| 778 root = RunCommand(['git', 'rev-parse', '--show-cdup']).strip() | 778 root = RunCommand(['git', 'rev-parse', '--show-cdup']).strip() |
| 779 if not root: | 779 if not root: |
| 780 root = '.' | 780 root = '.' |
| 781 absroot = os.path.abspath(root) | 781 absroot = os.path.abspath(root) |
| 782 if not root: | 782 if not root: |
| 783 raise Exception('Could not get root directory.') | 783 raise Exception('Could not get root directory.') |
| 784 | 784 |
| 785 # We use the sha1 of HEAD as a name of this change. | 785 # We use the sha1 of HEAD as a name of this change. |
| 786 name = RunCommand(['git', 'rev-parse', 'HEAD']).strip() | 786 name = RunCommand(['git', 'rev-parse', 'HEAD']).strip() |
| 787 files = scm.GIT.CaptureStatus([root], upstream_branch) | 787 files = scm.GIT.CaptureStatus([root], upstream_branch) |
| (...skipping 516 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1304 return 'closed' | 1304 return 'closed' |
| 1305 elif status.find('open') != -1 or status == '1': | 1305 elif status.find('open') != -1 or status == '1': |
| 1306 return 'open' | 1306 return 'open' |
| 1307 return 'unknown' | 1307 return 'unknown' |
| 1308 return 'unset' | 1308 return 'unset' |
| 1309 | 1309 |
| 1310 | 1310 |
| 1311 def GetTreeStatusReason(): | 1311 def GetTreeStatusReason(): |
| 1312 """Fetches the tree status from a json url and returns the message | 1312 """Fetches the tree status from a json url and returns the message |
| 1313 with the reason for the tree to be opened or closed.""" | 1313 with the reason for the tree to be opened or closed.""" |
| 1314 # Don't import it at file level since simplejson is not installed by default | |
| 1315 # on python 2.5 and it is only used for git-cl tree which isn't often used, | |
| 1316 # forcing everyone to install simplejson isn't efficient. | |
| 1317 try: | |
| 1318 import simplejson as json # pylint: disable=F0401 | |
| 1319 except ImportError: | |
| 1320 try: | |
| 1321 import json | |
| 1322 except ImportError: | |
| 1323 print >> sys.stderr, 'Please install simplejson' | |
| 1324 sys.exit(1) | |
| 1325 | |
| 1326 url = settings.GetTreeStatusUrl() | 1314 url = settings.GetTreeStatusUrl() |
| 1327 json_url = urlparse.urljoin(url, '/current?format=json') | 1315 json_url = urlparse.urljoin(url, '/current?format=json') |
| 1328 connection = urllib2.urlopen(json_url) | 1316 connection = urllib2.urlopen(json_url) |
| 1329 status = json.loads(connection.read()) | 1317 status = json.loads(connection.read()) |
| 1330 connection.close() | 1318 connection.close() |
| 1331 return status['message'] | 1319 return status['message'] |
| 1332 | 1320 |
| 1333 | 1321 |
| 1334 def CMDtree(parser, args): | 1322 def CMDtree(parser, args): |
| 1335 """show the status of the tree""" | 1323 """show the status of the tree""" |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1415 ('AppEngine is misbehaving and returned HTTP %d, again. Keep faith ' | 1403 ('AppEngine is misbehaving and returned HTTP %d, again. Keep faith ' |
| 1416 'and retry or visit go/isgaeup.\n%s') % (e.code, str(e))) | 1404 'and retry or visit go/isgaeup.\n%s') % (e.code, str(e))) |
| 1417 | 1405 |
| 1418 # Not a known command. Default to help. | 1406 # Not a known command. Default to help. |
| 1419 GenUsage(parser, 'help') | 1407 GenUsage(parser, 'help') |
| 1420 return CMDhelp(parser, argv) | 1408 return CMDhelp(parser, argv) |
| 1421 | 1409 |
| 1422 | 1410 |
| 1423 if __name__ == '__main__': | 1411 if __name__ == '__main__': |
| 1424 sys.exit(main(sys.argv[1:])) | 1412 sys.exit(main(sys.argv[1:])) |
| OLD | NEW |