| OLD | NEW |
| 1 # Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 # Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 """SCM-specific utility classes.""" | 5 """SCM-specific utility classes.""" |
| 6 | 6 |
| 7 import cStringIO | 7 import cStringIO |
| 8 import glob | 8 import glob |
| 9 import logging | 9 import logging |
| 10 import os | 10 import os |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 79 subprocess2.check_call( | 79 subprocess2.check_call( |
| 80 ['git', 'rev-parse', '--show-cdup'], | 80 ['git', 'rev-parse', '--show-cdup'], |
| 81 stdout=subprocess2.VOID, | 81 stdout=subprocess2.VOID, |
| 82 stderr=subprocess2.VOID, | 82 stderr=subprocess2.VOID, |
| 83 cwd=root) | 83 cwd=root) |
| 84 return 'git' | 84 return 'git' |
| 85 except (OSError, subprocess2.CalledProcessError): | 85 except (OSError, subprocess2.CalledProcessError): |
| 86 return None | 86 return None |
| 87 | 87 |
| 88 | 88 |
| 89 def only_int(val): | |
| 90 if val.isdigit(): | |
| 91 return int(val) | |
| 92 else: | |
| 93 return 0 | |
| 94 | |
| 95 | |
| 96 class GIT(object): | 89 class GIT(object): |
| 97 current_version = None | |
| 98 | |
| 99 @staticmethod | 90 @staticmethod |
| 100 def Capture(args, **kwargs): | 91 def Capture(args, **kwargs): |
| 101 return subprocess2.check_output( | 92 return subprocess2.check_output( |
| 102 ['git'] + args, stderr=subprocess2.PIPE, **kwargs) | 93 ['git'] + args, stderr=subprocess2.PIPE, **kwargs) |
| 103 | 94 |
| 104 @staticmethod | 95 @staticmethod |
| 105 def CaptureStatus(files, upstream_branch=None): | 96 def CaptureStatus(files, upstream_branch=None): |
| 106 """Returns git status. | 97 """Returns git status. |
| 107 | 98 |
| 108 @files can be a string (one file) or a list of files. | 99 @files can be a string (one file) or a list of files. |
| (...skipping 263 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 372 short_sha = GIT.Capture(['rev-parse', '--short=4', 'HEAD'], cwd=cwd).strip() | 363 short_sha = GIT.Capture(['rev-parse', '--short=4', 'HEAD'], cwd=cwd).strip() |
| 373 return "%s#%s" % (GIT.GetBranch(cwd), short_sha) | 364 return "%s#%s" % (GIT.GetBranch(cwd), short_sha) |
| 374 | 365 |
| 375 @staticmethod | 366 @staticmethod |
| 376 def GetCheckoutRoot(cwd): | 367 def GetCheckoutRoot(cwd): |
| 377 """Returns the top level directory of a git checkout as an absolute path. | 368 """Returns the top level directory of a git checkout as an absolute path. |
| 378 """ | 369 """ |
| 379 root = GIT.Capture(['rev-parse', '--show-cdup'], cwd=cwd).strip() | 370 root = GIT.Capture(['rev-parse', '--show-cdup'], cwd=cwd).strip() |
| 380 return os.path.abspath(os.path.join(cwd, root)) | 371 return os.path.abspath(os.path.join(cwd, root)) |
| 381 | 372 |
| 382 @classmethod | 373 @staticmethod |
| 383 def AssertVersion(cls, min_version): | 374 def AssertVersion(min_version): |
| 384 """Asserts git's version is at least min_version.""" | 375 """Asserts git's version is at least min_version.""" |
| 385 if cls.current_version is None: | 376 def only_int(val): |
| 386 cls.current_version = cls.Capture(['--version']).split()[-1] | 377 if val.isdigit(): |
| 387 current_version_list = map(only_int, cls.current_version.split('.')) | 378 return int(val) |
| 379 else: |
| 380 return 0 |
| 381 current_version = GIT.Capture(['--version']).split()[-1] |
| 382 current_version_list = map(only_int, current_version.split('.')) |
| 388 for min_ver in map(int, min_version.split('.')): | 383 for min_ver in map(int, min_version.split('.')): |
| 389 ver = current_version_list.pop(0) | 384 ver = current_version_list.pop(0) |
| 390 if ver < min_ver: | 385 if ver < min_ver: |
| 391 return (False, cls.current_version) | 386 return (False, current_version) |
| 392 elif ver > min_ver: | 387 elif ver > min_ver: |
| 393 return (True, cls.current_version) | 388 return (True, current_version) |
| 394 return (True, cls.current_version) | 389 return (True, current_version) |
| 395 | 390 |
| 396 | 391 |
| 397 class SVN(object): | 392 class SVN(object): |
| 398 current_version = None | 393 current_version = None |
| 399 | 394 |
| 400 @staticmethod | 395 @staticmethod |
| 401 def Capture(args, **kwargs): | 396 def Capture(args, **kwargs): |
| 402 """Always redirect stderr. | 397 """Always redirect stderr. |
| 403 | 398 |
| 404 Throws an exception if non-0 is returned. | 399 Throws an exception if non-0 is returned. |
| (...skipping 516 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 921 info = SVN.CaptureInfo(parent) | 916 info = SVN.CaptureInfo(parent) |
| 922 if (info['Repository Root'] != cur_dir_repo_root or | 917 if (info['Repository Root'] != cur_dir_repo_root or |
| 923 info['URL'] != os.path.dirname(url)): | 918 info['URL'] != os.path.dirname(url)): |
| 924 break | 919 break |
| 925 url = info['URL'] | 920 url = info['URL'] |
| 926 except subprocess2.CalledProcessError: | 921 except subprocess2.CalledProcessError: |
| 927 break | 922 break |
| 928 directory = parent | 923 directory = parent |
| 929 return GetCasedPath(directory) | 924 return GetCasedPath(directory) |
| 930 | 925 |
| 931 @classmethod | 926 @staticmethod |
| 932 def AssertVersion(cls, min_version): | 927 def AssertVersion(min_version): |
| 933 """Asserts svn's version is at least min_version.""" | 928 """Asserts svn's version is at least min_version.""" |
| 934 if cls.current_version is None: | 929 def only_int(val): |
| 935 cls.current_version = cls.Capture(['--version']).split()[2] | 930 if val.isdigit(): |
| 936 current_version_list = map(only_int, cls.current_version.split('.')) | 931 return int(val) |
| 932 else: |
| 933 return 0 |
| 934 if not SVN.current_version: |
| 935 SVN.current_version = SVN.Capture(['--version']).split()[2] |
| 936 current_version_list = map(only_int, SVN.current_version.split('.')) |
| 937 for min_ver in map(int, min_version.split('.')): | 937 for min_ver in map(int, min_version.split('.')): |
| 938 ver = current_version_list.pop(0) | 938 ver = current_version_list.pop(0) |
| 939 if ver < min_ver: | 939 if ver < min_ver: |
| 940 return (False, cls.current_version) | 940 return (False, SVN.current_version) |
| 941 elif ver > min_ver: | 941 elif ver > min_ver: |
| 942 return (True, cls.current_version) | 942 return (True, SVN.current_version) |
| 943 return (True, cls.current_version) | 943 return (True, SVN.current_version) |
| 944 | 944 |
| 945 @staticmethod | 945 @staticmethod |
| 946 def Revert(repo_root, callback=None, ignore_externals=False): | 946 def Revert(repo_root, callback=None, ignore_externals=False): |
| 947 """Reverts all svn modifications in repo_root, including properties. | 947 """Reverts all svn modifications in repo_root, including properties. |
| 948 | 948 |
| 949 Deletes any modified files or directory. | 949 Deletes any modified files or directory. |
| 950 | 950 |
| 951 A "svn update --revision BASE" call is required after to revive deleted | 951 A "svn update --revision BASE" call is required after to revive deleted |
| 952 files. | 952 files. |
| 953 """ | 953 """ |
| (...skipping 29 matching lines...) Expand all Loading... |
| 983 if (file_status[0][0] in ('D', 'A', '!') or | 983 if (file_status[0][0] in ('D', 'A', '!') or |
| 984 not file_status[0][1:].isspace()): | 984 not file_status[0][1:].isspace()): |
| 985 # Added, deleted file requires manual intervention and require calling | 985 # Added, deleted file requires manual intervention and require calling |
| 986 # revert, like for properties. | 986 # revert, like for properties. |
| 987 try: | 987 try: |
| 988 SVN.Capture(['revert', file_status[1]], cwd=repo_root) | 988 SVN.Capture(['revert', file_status[1]], cwd=repo_root) |
| 989 except subprocess2.CalledProcessError: | 989 except subprocess2.CalledProcessError: |
| 990 if not os.path.exists(file_path): | 990 if not os.path.exists(file_path): |
| 991 continue | 991 continue |
| 992 raise | 992 raise |
| OLD | NEW |