We have switched from a custom, manually enforced style guide to using the
clang-format tool. Our automated testing now fails with an error for pull requests that don't conform to the new clang-format style - it prints out a diff/patch of the changes necessary to conform with our coding style.
You will need to have clang 3.9 or later installed. If your clang install is from the MOOSE environment package (i.e. the ones downloadable via the links on http://mooseframework.org/getting-started/) and you use Linux, you may need to upgrade to the newest MOOSE environment package version in order for
clang-format to be available in your PATH. Many text editors have plugins and support for automatically running clang-format on the files you edit. We recommend using those. For example:
If you wish, you can also install a pre-commit hook that will prevent you from committing changes that don't comply with the new style. To install this hook, run the
scripts/install-format-hook.sh script in the MOOSE repository.
You can manually run
git clang-format [<commit>] to automatically format files changed since
<commit> (or HEAD if omitted). You can also run
clang-format -i [<file>...] to update the desired files. We recommend making sure your files comply with the clang-format style on every commit.
Changes (pull requests, branches, etc.) that were started before we reformatted our codebase will have a lot of conflicts when attempting to merge into or rebase with the new mainline code. We have provided some tools to aid in this process. First you will need to install a custom git configuration by running (in the main MOOSE repo):
Then make sure
./scripts/clang_format_merge_driver is in your PATH.
To rebase a branch onto the newly formatted MOOSE mainline devel branch run the rebase command like normal, e.g.:
git checkout [your-branch] git checkout devel git pull upstream devel git checkout [your-branch] git rebase devel ... # continue rebase like normal resolving any conflicts
Running a merge like normal will also use the the special clang-format merge driver.
; Note: This hardcoded path will likely change in the future ; Need to set a variable or copy this file to a new location (load "/opt/moose/llvm-3.9.0/share/clang/clang-format.el") (global-set-key [C-M-tab] 'clang-format-region) (if (null (getenv "MOOSE_DIR")) (setq MOOSE_DIR "~/projects/moose") (setq MOOSE_DIR (getenv "MOOSE_DIR"))) (setq my-moose-style-path-alist (list (expand-file-name MOOSE_DIR))) (add-hook 'c++-mode-hook (lambda () (dolist (path my-moose-style-path-alist) (if (string-match path (buffer-file-name)) (add-hook 'before-save-hook 'clang-format-buffer)))))