OLD | NEW |
1 # Copyright 2009 Google Inc. All Rights Reserved. | 1 # Copyright 2009 Google Inc. All Rights Reserved. |
2 # | 2 # |
3 # Licensed under the Apache License, Version 2.0 (the "License"); | 3 # Licensed under the Apache License, Version 2.0 (the "License"); |
4 # you may not use this file except in compliance with the License. | 4 # you may not use this file except in compliance with the License. |
5 # You may obtain a copy of the License at | 5 # You may obtain a copy of the License at |
6 # | 6 # |
7 # http://www.apache.org/licenses/LICENSE-2.0 | 7 # http://www.apache.org/licenses/LICENSE-2.0 |
8 # | 8 # |
9 # Unless required by applicable law or agreed to in writing, software | 9 # Unless required by applicable law or agreed to in writing, software |
10 # distributed under the License is distributed on an "AS IS" BASIS, | 10 # distributed under the License is distributed on an "AS IS" BASIS, |
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 # See the License for the specific language governing permissions and | 12 # See the License for the specific language governing permissions and |
13 # limitations under the License. | 13 # limitations under the License. |
14 | 14 |
15 | 15 |
16 import logging | 16 import logging |
17 import os | 17 import os |
18 import re | 18 import re |
19 import subprocess | 19 import subprocess |
20 import sys | 20 import sys |
21 import xml.dom.minidom | 21 import xml.dom.minidom |
22 | 22 |
23 import gclient_utils | 23 import gclient_utils |
24 | 24 |
25 SVN_COMMAND = "svn" | 25 SVN_COMMAND = "svn" |
| 26 GIT_COMMAND = "git" |
26 | 27 |
27 | 28 |
28 ### SCM abstraction layer | 29 ### SCM abstraction layer |
29 | 30 |
30 | 31 |
31 # Factory Method for SCM wrapper creation | 32 # Factory Method for SCM wrapper creation |
32 | 33 |
33 def CreateSCM(url=None, root_dir=None, relpath=None, scm_name='svn'): | 34 def CreateSCM(url=None, root_dir=None, relpath=None, scm_name='svn'): |
34 # TODO(maruel): Deduce the SCM from the url. | 35 # TODO(maruel): Deduce the SCM from the url. |
35 scm_map = { | 36 scm_map = { |
(...skipping 394 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
430 line.startswith(self.working_prefix)): | 431 line.startswith(self.working_prefix)): |
431 self.ReplaceAndPrint(line) | 432 self.ReplaceAndPrint(line) |
432 else: | 433 else: |
433 print line | 434 print line |
434 | 435 |
435 filterer = DiffFilterer(self.relpath) | 436 filterer = DiffFilterer(self.relpath) |
436 RunSVNAndFilterOutput(command, path, False, False, filterer.Filter) | 437 RunSVNAndFilterOutput(command, path, False, False, filterer.Filter) |
437 | 438 |
438 | 439 |
439 # ----------------------------------------------------------------------------- | 440 # ----------------------------------------------------------------------------- |
| 441 # Git utils: |
| 442 |
| 443 |
| 444 def CaptureGit(args, in_directory=None, print_error=True): |
| 445 """Runs git, capturing output sent to stdout as a string. |
| 446 |
| 447 Args: |
| 448 args: A sequence of command line parameters to be passed to git. |
| 449 in_directory: The directory where git is to be run. |
| 450 |
| 451 Returns: |
| 452 The output sent to stdout as a string. |
| 453 """ |
| 454 c = [GIT_COMMAND] |
| 455 c.extend(args) |
| 456 |
| 457 # *Sigh*: Windows needs shell=True, or else it won't search %PATH% for |
| 458 # the git.exe executable, but shell=True makes subprocess on Linux fail |
| 459 # when it's called with a list because it only tries to execute the |
| 460 # first string ("git"). |
| 461 stderr = None |
| 462 if not print_error: |
| 463 stderr = subprocess.PIPE |
| 464 return subprocess.Popen(c, |
| 465 cwd=in_directory, |
| 466 shell=sys.platform.startswith('win'), |
| 467 stdout=subprocess.PIPE, |
| 468 stderr=stderr).communicate()[0] |
| 469 |
| 470 |
| 471 def CaptureGitStatus(files): |
| 472 """Returns git status. |
| 473 |
| 474 @files can be a string (one file) or a list of files. |
| 475 |
| 476 Returns an array of (status, file) tuples.""" |
| 477 command = ["diff", "--name-status", "-r", "origin.."] |
| 478 if not files: |
| 479 pass |
| 480 elif isinstance(files, basestring): |
| 481 command.append(files) |
| 482 else: |
| 483 command.extend(files) |
| 484 |
| 485 status = CaptureGit(command).rstrip() |
| 486 results = [] |
| 487 if status: |
| 488 for statusline in status.split('\n'): |
| 489 m = re.match('^(\w)\t(.+)$', statusline) |
| 490 if not m: |
| 491 raise Exception("status currently unsupported: %s" % statusline) |
| 492 results.append(('%s ' % m.group(1), m.group(2))) |
| 493 return results |
| 494 |
| 495 |
| 496 # ----------------------------------------------------------------------------- |
440 # SVN utils: | 497 # SVN utils: |
441 | 498 |
442 | 499 |
443 def RunSVN(args, in_directory): | 500 def RunSVN(args, in_directory): |
444 """Runs svn, sending output to stdout. | 501 """Runs svn, sending output to stdout. |
445 | 502 |
446 Args: | 503 Args: |
447 args: A sequence of command line parameters to be passed to svn. | 504 args: A sequence of command line parameters to be passed to svn. |
448 in_directory: The directory where svn is to be run. | 505 in_directory: The directory where svn is to be run. |
449 | 506 |
(...skipping 241 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
691 # Col 3 | 748 # Col 3 |
692 if wc_status[0].getAttribute('copied') == 'true': | 749 if wc_status[0].getAttribute('copied') == 'true': |
693 statuses[3] = '+' | 750 statuses[3] = '+' |
694 # Col 4 | 751 # Col 4 |
695 if wc_status[0].getAttribute('switched') == 'true': | 752 if wc_status[0].getAttribute('switched') == 'true': |
696 statuses[4] = 'S' | 753 statuses[4] = 'S' |
697 # TODO(maruel): Col 5 and 6 | 754 # TODO(maruel): Col 5 and 6 |
698 item = (''.join(statuses), file) | 755 item = (''.join(statuses), file) |
699 results.append(item) | 756 results.append(item) |
700 return results | 757 return results |
OLD | NEW |