[clang] bee61aa - [clang-format] Add --staged/--cached option to git-clang-format
via cfe-commits
cfe-commits at lists.llvm.org
Sat Oct 30 09:39:11 PDT 2021
Author: Erik Larsson
Date: 2021-10-30T17:37:58+01:00
New Revision: bee61aa7b638726c3e0eec017fbd89d79a181434
URL: https://github.com/llvm/llvm-project/commit/bee61aa7b638726c3e0eec017fbd89d79a181434
DIFF: https://github.com/llvm/llvm-project/commit/bee61aa7b638726c3e0eec017fbd89d79a181434.diff
LOG: [clang-format] Add --staged/--cached option to git-clang-format
When running git-clang-format in a pre-commit hook it's very useful to be able to tell git-clang-format to only look at the --staged/--cached files and not the working directory.
Note this patch is a rebase/fork from {D41147 } which is a fork of {D15465 }
Reviewed By: MyDeveloperDay, HazardyKnusperkeks, lodato
Differential Revision: https://reviews.llvm.org/D90996
Co-authored-by: Mark Lodato <lodato at google.com>
Added:
Modified:
clang/tools/clang-format/git-clang-format
Removed:
################################################################################
diff --git a/clang/tools/clang-format/git-clang-format b/clang/tools/clang-format/git-clang-format
index c7e15eb7b483..bb5b5546d6a5 100755
--- a/clang/tools/clang-format/git-clang-format
+++ b/clang/tools/clang-format/git-clang-format
@@ -32,12 +32,13 @@ import re
import subprocess
import sys
-usage = 'git clang-format [OPTIONS] [<commit>] [<commit>] [--] [<file>...]'
+usage = ('git clang-format [OPTIONS] [<commit>] [<commit>|--staged] '
+ '[--] [<file>...]')
desc = '''
If zero or one commits are given, run clang-format on all lines that
diff er
between the working directory and <commit>, which defaults to HEAD. Changes are
-only applied to the working directory.
+only applied to the working directory, or in the stage/index.
If two commits are given (requires --
diff ), run clang-format on all lines in the
second <commit> that
diff er from the first <commit>.
@@ -112,6 +113,8 @@ def main():
help='select hunks interactively')
p.add_argument('-q', '--quiet', action='count', default=0,
help='print less information')
+ p.add_argument('--staged', '--cached', action='store_true',
+ help='format lines in the stage instead of the working dir')
p.add_argument('--style',
default=config.get('clangformat.style', None),
help='passed to clang-format'),
@@ -131,12 +134,14 @@ def main():
commits, files = interpret_args(opts.args, dash_dash, opts.commit)
if len(commits) > 1:
+ if opts.staged:
+ die('--staged is not allowed when two commits are given')
if not opts.
diff :
die('--
diff is required when two commits are given')
else:
if len(commits) > 2:
die('at most two commits allowed; %d given' % len(commits))
- changed_lines = compute_
diff _and_extract_lines(commits, files)
+ changed_lines = compute_
diff _and_extract_lines(commits, files, opts.staged)
if opts.verbose >= 1:
ignored_files = set(changed_lines)
filter_by_extension(changed_lines, opts.extensions.lower().split(','))
@@ -275,9 +280,9 @@ def get_object_type(value):
return convert_string(stdout.strip())
-def compute_
diff _and_extract_lines(commits, files):
+def compute_
diff _and_extract_lines(commits, files, staged):
"""Calls compute_
diff () followed by extract_lines()."""
-
diff _process = compute_
diff (commits, files)
+
diff _process = compute_
diff (commits, files, staged)
changed_lines = extract_lines(
diff _process.stdout)
diff _process.stdout.close()
diff _process.wait()
@@ -287,17 +292,21 @@ def compute_
diff _and_extract_lines(commits, files):
return changed_lines
-def compute_
diff (commits, files):
+def compute_
diff (commits, files, staged):
"""Return a subprocess object producing the
diff from `commits`.
The return value's `stdin` file object will produce a patch with the
-
diff erences between the working directory and the first commit if a single
- one was specified, or the
diff erence between both specified commits, filtered
- on `files` (if non-empty). Zero context lines are used in the patch."""
+
diff erences between the working directory (or stage if --staged is used) and
+ the first commit if a single one was specified, or the
diff erence between
+ both specified commits, filtered on `files` (if non-empty).
+ Zero context lines are used in the patch."""
git_tool = '
diff -index'
+ extra_args = []
if len(commits) > 1:
git_tool = '
diff -tree'
- cmd = ['git', git_tool, '-p', '-U0'] + commits + ['--']
+ elif staged:
+ extra_args += ['--cached']
+ cmd = ['git', git_tool, '-p', '-U0'] + extra_args + commits + ['--']
cmd.extend(files)
p = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
p.stdin.close()
More information about the cfe-commits
mailing list