Index: git_common.py |
diff --git a/git_common.py b/git_common.py |
index 506365257ecd63556258f4656350cd3240142f2d..d571f820b7de02bd4bd3814716912d89dffa7f3d 100644 |
--- a/git_common.py |
+++ b/git_common.py |
@@ -281,6 +281,16 @@ def once(function): |
## Git functions |
+def blame(filename, revision=None, porcelain=False, *args): |
+ command = ['blame'] |
+ if porcelain: |
+ command.append('-p') |
+ if revision is not None: |
+ command.append(revision) |
+ command.extend(['--', filename]) |
+ return run(*command) |
+ |
+ |
def branch_config(branch, option, default=None): |
return config('branch.%s.%s' % (branch, option), default=default) |
@@ -546,10 +556,39 @@ def remove_merge_base(branch): |
del_branch_config(branch, 'base-upstream') |
+def repo_root(): |
+ """Returns the absolute path to the repository root.""" |
+ return run('rev-parse', '--show-toplevel') |
+ |
+ |
def root(): |
return config('depot-tools.upstream', 'origin/master') |
+@contextlib.contextmanager |
+def less(): # pragma: no cover |
+ """Runs 'less' as context manager yielding its stdin as a PIPE. |
+ |
+ Automatically checks if sys.stdout is a non-TTY stream. If so, it avoids |
+ running less and just yields sys.stdout. |
+ """ |
+ if not sys.stdout.isatty(): |
+ yield sys.stdout |
+ return |
+ |
+ # Run with the same options that git uses (see setup_pager in git repo). |
+ # -F: Automatically quit if the output is less than one screen. |
+ # -R: Don't escape ANSI color codes. |
+ # -X: Don't clear the screen before starting. |
+ cmd = ('less', '-FRX') |
+ try: |
+ proc = subprocess2.Popen(cmd, stdin=subprocess2.PIPE) |
+ yield proc.stdin |
+ finally: |
+ proc.stdin.close() |
+ proc.wait() |
+ |
+ |
def run(*cmd, **kwargs): |
"""The same as run_with_stderr, except it only returns stdout.""" |
return run_with_stderr(*cmd, **kwargs)[0] |