Skip to content
Snippets Groups Projects
Commit b56014f2 authored by Ankit Meel's avatar Ankit Meel Committed by Campbell Barton
Browse files

make format: add option to format only edited files

Ref D8991
parent f8e27704
No related branches found
No related tags found
No related merge requests found
...@@ -31,9 +31,9 @@ ignore_files = { ...@@ -31,9 +31,9 @@ ignore_files = {
} }
def compute_paths(paths): def compute_paths(paths, use_default_paths):
# Optionally pass in files to operate on. # Optionally pass in files to operate on.
if not paths: if use_default_paths:
paths = ( paths = (
"intern/atomic", "intern/atomic",
"intern/audaspace", "intern/audaspace",
...@@ -64,8 +64,11 @@ def compute_paths(paths): ...@@ -64,8 +64,11 @@ def compute_paths(paths):
return paths return paths
def source_files_from_git(paths): def source_files_from_git(paths, changed_only):
cmd = ("git", "ls-tree", "-r", "HEAD", *paths, "--name-only", "-z") if changed_only:
cmd = ("git", "diff", "HEAD", "--name-only", "-z", "--", *paths)
else:
cmd = ("git", "ls-tree", "-r", "HEAD", *paths, "--name-only", "-z")
files = subprocess.check_output(cmd).split(b'\0') files = subprocess.check_output(cmd).split(b'\0')
return [f.decode('ascii') for f in files] return [f.decode('ascii') for f in files]
...@@ -159,6 +162,18 @@ def argparse_create(): ...@@ -159,6 +162,18 @@ def argparse_create():
"(default=False)", "(default=False)",
required=False, required=False,
) )
parser.add_argument(
"--changed-only",
dest="changed_only",
default=False,
action='store_true',
help=(
"Format only edited files, including the staged ones. "
"Using this with \"paths\" will pick the edited files lying on those paths. "
"(default=False)"
),
required=False,
)
parser.add_argument( parser.add_argument(
"paths", "paths",
nargs=argparse.REMAINDER, nargs=argparse.REMAINDER,
...@@ -189,22 +204,22 @@ def main(): ...@@ -189,22 +204,22 @@ def main():
args = argparse_create().parse_args() args = argparse_create().parse_args()
use_default_paths = not bool(args.paths) use_default_paths = not (bool(args.paths) or bool(args.changed_only))
paths = compute_paths(args.paths) paths = compute_paths(args.paths, use_default_paths)
print("Operating on:") print("Operating on:" + (" (%d changed paths)" % len(paths) if args.changed_only else ""))
for p in paths: for p in paths:
print(" ", p) print(" ", p)
files = [ files = [
f for f in source_files_from_git(paths) f for f in source_files_from_git(paths, args.changed_only)
if f.endswith(extensions) if f.endswith(extensions)
if f not in ignore_files if f not in ignore_files
] ]
# Always operate on all cmake files (when expanding tabs and no paths given). # Always operate on all cmake files (when expanding tabs and no paths given).
files_retab = [ files_retab = [
f for f in source_files_from_git((".",) if use_default_paths else paths) f for f in source_files_from_git((".",) if use_default_paths else paths, args.changed_only)
if f.endswith(extensions_only_retab) if f.endswith(extensions_only_retab)
if f not in ignore_files if f not in ignore_files
] ]
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment