Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(860)

Side by Side Diff: git_common.py

Issue 1566343002: Fix rebase-update on windows on git-svn repos. (Closed) Base URL: https://chromium.googlesource.com/chromium/tools/depot_tools.git@master
Patch Set: nocover Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | git_rebase_update.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 # Copyright 2014 The Chromium Authors. All rights reserved. 1 # Copyright 2014 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 # Monkeypatch IMapIterator so that Ctrl-C can kill everything properly. 5 # Monkeypatch IMapIterator so that Ctrl-C can kill everything properly.
6 # Derived from https://gist.github.com/aljungberg/626518 6 # Derived from https://gist.github.com/aljungberg/626518
7 import multiprocessing.pool 7 import multiprocessing.pool
8 from multiprocessing.pool import IMapIterator 8 from multiprocessing.pool import IMapIterator
9 def wrapper(func): 9 def wrapper(func):
10 def wrap(self, timeout=None): 10 def wrap(self, timeout=None):
(...skipping 15 matching lines...) Expand all
26 import signal 26 import signal
27 import sys 27 import sys
28 import tempfile 28 import tempfile
29 import textwrap 29 import textwrap
30 import threading 30 import threading
31 31
32 import subprocess2 32 import subprocess2
33 33
34 ROOT = os.path.abspath(os.path.dirname(__file__)) 34 ROOT = os.path.abspath(os.path.dirname(__file__))
35 35
36 GIT_EXE = ROOT+'\\git.bat' if sys.platform.startswith('win') else 'git' 36 IS_WIN = sys.platform == 'win32'
37 GIT_EXE = ROOT+'\\git.bat' if IS_WIN else 'git'
37 TEST_MODE = False 38 TEST_MODE = False
38 39
39 FREEZE = 'FREEZE' 40 FREEZE = 'FREEZE'
40 FREEZE_SECTIONS = { 41 FREEZE_SECTIONS = {
41 'indexed': 'soft', 42 'indexed': 'soft',
42 'unindexed': 'mixed' 43 'unindexed': 'mixed'
43 } 44 }
44 FREEZE_MATCHER = re.compile(r'%s.(%s)' % (FREEZE, '|'.join(FREEZE_SECTIONS))) 45 FREEZE_MATCHER = re.compile(r'%s.(%s)' % (FREEZE, '|'.join(FREEZE_SECTIONS)))
45 46
46 47
(...skipping 230 matching lines...) Expand 10 before | Expand all | Expand 10 after
277 return _inner_gen().next 278 return _inner_gen().next
278 279
279 280
280 ## Git functions 281 ## Git functions
281 282
282 283
283 def branch_config(branch, option, default=None): 284 def branch_config(branch, option, default=None):
284 return config('branch.%s.%s' % (branch, option), default=default) 285 return config('branch.%s.%s' % (branch, option), default=default)
285 286
286 287
288 def config_regexp(pattern):
289 if IS_WIN: # pragma: no cover
290 # this madness is because we call git.bat which calls git.exe which calls
291 # bash.exe (or something to that effect). Each layer divides the number of
292 # ^'s by 2.
293 pattern = pattern.replace('^', '^' * 8)
294 return run('config', '--get-regexp', pattern).splitlines()
295
296
287 def branch_config_map(option): 297 def branch_config_map(option):
288 """Return {branch: <|option| value>} for all branches.""" 298 """Return {branch: <|option| value>} for all branches."""
289 try: 299 try:
290 reg = re.compile(r'^branch\.(.*)\.%s$' % option) 300 reg = re.compile(r'^branch\.(.*)\.%s$' % option)
291 lines = run('config', '--get-regexp', reg.pattern).splitlines() 301 lines = config_regexp(reg.pattern)
292 return {reg.match(k).group(1): v for k, v in (l.split() for l in lines)} 302 return {reg.match(k).group(1): v for k, v in (l.split() for l in lines)}
293 except subprocess2.CalledProcessError: 303 except subprocess2.CalledProcessError:
294 return {} 304 return {}
295 305
296 306
297 def branches(*args): 307 def branches(*args):
298 NO_BRANCH = ('* (no branch', '* (detached', '* (HEAD detached') 308 NO_BRANCH = ('* (no branch', '* (detached', '* (HEAD detached')
299 309
300 key = 'depot-tools.branch-limit' 310 key = 'depot-tools.branch-limit'
301 limit = 20 311 limit = 20
(...skipping 244 matching lines...) Expand 10 before | Expand all | Expand 10 after
546 556
547 557
548 def run_with_retcode(*cmd, **kwargs): 558 def run_with_retcode(*cmd, **kwargs):
549 """Run a command but only return the status code.""" 559 """Run a command but only return the status code."""
550 try: 560 try:
551 run(*cmd, **kwargs) 561 run(*cmd, **kwargs)
552 return 0 562 return 0
553 except subprocess2.CalledProcessError as cpe: 563 except subprocess2.CalledProcessError as cpe:
554 return cpe.returncode 564 return cpe.returncode
555 565
556
557 def run_stream(*cmd, **kwargs): 566 def run_stream(*cmd, **kwargs):
558 """Runs a git command. Returns stdout as a PIPE (file-like object). 567 """Runs a git command. Returns stdout as a PIPE (file-like object).
559 568
560 stderr is dropped to avoid races if the process outputs to both stdout and 569 stderr is dropped to avoid races if the process outputs to both stdout and
561 stderr. 570 stderr.
562 """ 571 """
563 kwargs.setdefault('stderr', subprocess2.VOID) 572 kwargs.setdefault('stderr', subprocess2.VOID)
564 kwargs.setdefault('stdout', subprocess2.PIPE) 573 kwargs.setdefault('stdout', subprocess2.PIPE)
574 kwargs.setdefault('shell', False)
565 cmd = (GIT_EXE, '-c', 'color.ui=never') + cmd 575 cmd = (GIT_EXE, '-c', 'color.ui=never') + cmd
566 proc = subprocess2.Popen(cmd, **kwargs) 576 proc = subprocess2.Popen(cmd, **kwargs)
567 return proc.stdout 577 return proc.stdout
568 578
569 579
570 @contextlib.contextmanager 580 @contextlib.contextmanager
571 def run_stream_with_retcode(*cmd, **kwargs): 581 def run_stream_with_retcode(*cmd, **kwargs):
572 """Runs a git command as context manager yielding stdout as a PIPE. 582 """Runs a git command as context manager yielding stdout as a PIPE.
573 583
574 stderr is dropped to avoid races if the process outputs to both stdout and 584 stderr is dropped to avoid races if the process outputs to both stdout and
575 stderr. 585 stderr.
576 586
577 Raises subprocess2.CalledProcessError on nonzero return code. 587 Raises subprocess2.CalledProcessError on nonzero return code.
578 """ 588 """
579 kwargs.setdefault('stderr', subprocess2.VOID) 589 kwargs.setdefault('stderr', subprocess2.VOID)
580 kwargs.setdefault('stdout', subprocess2.PIPE) 590 kwargs.setdefault('stdout', subprocess2.PIPE)
591 kwargs.setdefault('shell', False)
581 cmd = (GIT_EXE, '-c', 'color.ui=never') + cmd 592 cmd = (GIT_EXE, '-c', 'color.ui=never') + cmd
582 try: 593 try:
583 proc = subprocess2.Popen(cmd, **kwargs) 594 proc = subprocess2.Popen(cmd, **kwargs)
584 yield proc.stdout 595 yield proc.stdout
585 finally: 596 finally:
586 retcode = proc.wait() 597 retcode = proc.wait()
587 if retcode != 0: 598 if retcode != 0:
588 raise subprocess2.CalledProcessError(retcode, cmd, os.getcwd(), 599 raise subprocess2.CalledProcessError(retcode, cmd, os.getcwd(),
589 None, None) 600 None, None)
590 601
591 602
592 def run_with_stderr(*cmd, **kwargs): 603 def run_with_stderr(*cmd, **kwargs):
593 """Runs a git command. 604 """Runs a git command.
594 605
595 Returns (stdout, stderr) as a pair of strings. 606 Returns (stdout, stderr) as a pair of strings.
596 607
597 kwargs 608 kwargs
598 autostrip (bool) - Strip the output. Defaults to True. 609 autostrip (bool) - Strip the output. Defaults to True.
599 indata (str) - Specifies stdin data for the process. 610 indata (str) - Specifies stdin data for the process.
600 """ 611 """
601 kwargs.setdefault('stdin', subprocess2.PIPE) 612 kwargs.setdefault('stdin', subprocess2.PIPE)
602 kwargs.setdefault('stdout', subprocess2.PIPE) 613 kwargs.setdefault('stdout', subprocess2.PIPE)
603 kwargs.setdefault('stderr', subprocess2.PIPE) 614 kwargs.setdefault('stderr', subprocess2.PIPE)
615 kwargs.setdefault('shell', False)
604 autostrip = kwargs.pop('autostrip', True) 616 autostrip = kwargs.pop('autostrip', True)
605 indata = kwargs.pop('indata', None) 617 indata = kwargs.pop('indata', None)
606 618
607 cmd = (GIT_EXE, '-c', 'color.ui=never') + cmd 619 cmd = (GIT_EXE, '-c', 'color.ui=never') + cmd
608 proc = subprocess2.Popen(cmd, **kwargs) 620 proc = subprocess2.Popen(cmd, **kwargs)
609 ret, err = proc.communicate(indata) 621 ret, err = proc.communicate(indata)
610 retcode = proc.wait() 622 retcode = proc.wait()
611 if retcode != 0: 623 if retcode != 0:
612 raise subprocess2.CalledProcessError(retcode, cmd, os.getcwd(), ret, err) 624 raise subprocess2.CalledProcessError(retcode, cmd, os.getcwd(), ret, err)
613 625
(...skipping 234 matching lines...) Expand 10 before | Expand all | Expand 10 after
848 ['HEAD']) 860 ['HEAD'])
849 861
850 862
851 def clone_file(repository, new_workdir, link, operation): 863 def clone_file(repository, new_workdir, link, operation):
852 if not os.path.exists(os.path.join(repository, link)): 864 if not os.path.exists(os.path.join(repository, link)):
853 return 865 return
854 link_dir = os.path.dirname(os.path.join(new_workdir, link)) 866 link_dir = os.path.dirname(os.path.join(new_workdir, link))
855 if not os.path.exists(link_dir): 867 if not os.path.exists(link_dir):
856 os.makedirs(link_dir) 868 os.makedirs(link_dir)
857 operation(os.path.join(repository, link), os.path.join(new_workdir, link)) 869 operation(os.path.join(repository, link), os.path.join(new_workdir, link))
OLDNEW
« no previous file with comments | « no previous file | git_rebase_update.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698