mirror of
https://github.com/mozilla/gecko-dev.git
synced 2024-10-20 00:35:44 +00:00
Bug 1514770 - hooks_clang_format for git: Run clang-format on the changed files r=sheehan
Depends on D23789 Differential Revision: https://phabricator.services.mozilla.com/D23798 --HG-- extra : moz-landing-system : lando
This commit is contained in:
parent
e1ab9dbe90
commit
1e86e44154
@ -5,6 +5,7 @@
|
||||
|
||||
import os
|
||||
import subprocess
|
||||
from subprocess import check_output, CalledProcessError
|
||||
import sys
|
||||
|
||||
here = os.path.dirname(os.path.realpath(__file__))
|
||||
@ -16,18 +17,17 @@ sys.path[:0] = [os.path.join(topsrcdir, p) for p in EXTRA_PATHS]
|
||||
from mozversioncontrol import get_repository_object, InvalidRepoPath
|
||||
|
||||
|
||||
def run_clang_format(hooktype, args):
|
||||
def run_clang_format(hooktype, changedFiles):
|
||||
try:
|
||||
vcs = get_repository_object(topsrcdir)
|
||||
except InvalidRepoPath:
|
||||
return
|
||||
|
||||
changedFiles = vcs.get_outgoing_files("AM")
|
||||
if not changedFiles:
|
||||
# No files have been touched
|
||||
return
|
||||
|
||||
arguments = ["clang-format", "-s", "-p"] + changedFiles
|
||||
arguments = ["clang-format", "-p"] + changedFiles
|
||||
# On windows we need this to call the command in a shell, see Bug 1511594
|
||||
if os.name == "nt":
|
||||
clang_format_cmd = ["sh", "mach"] + arguments
|
||||
@ -35,9 +35,10 @@ def run_clang_format(hooktype, args):
|
||||
clang_format_cmd = [os.path.join(topsrcdir, "mach")] + arguments
|
||||
if "commit" in hooktype:
|
||||
# don't prevent commits, just display the clang-format results
|
||||
subprocess.Popen(clang_format_cmd)
|
||||
subprocess.call(clang_format_cmd)
|
||||
# Add the modified files back to the repo (expect a string)
|
||||
vcs.add_remove_files(" ".join(changedFiles))
|
||||
return False
|
||||
|
||||
print("warning: '{}' is not a valid clang-format hooktype".format(hooktype))
|
||||
return False
|
||||
|
||||
@ -56,7 +57,17 @@ def git():
|
||||
hooktype = os.path.basename(__file__)
|
||||
if hooktype == "hooks_clang_format.py":
|
||||
hooktype = "pre-push"
|
||||
return run_clang_format(hooktype, [])
|
||||
|
||||
try:
|
||||
changedFiles = check_output(
|
||||
["git", "diff", "--staged", "--name-only", "HEAD"]
|
||||
).split()
|
||||
# TODO we should detect if we are in a "add -p" mode and show a warning
|
||||
return run_clang_format(hooktype, changedFiles)
|
||||
|
||||
except CalledProcessError:
|
||||
print("Command to retrieve local files failed")
|
||||
return 1
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
Loading…
Reference in New Issue
Block a user