Commit fully-formatted Objective-C as a team without even trying.
Objective-C Shell Python Ruby
Latest commit 30b30a0 Jan 19, 2017 @rhgills rhgills committed on GitHub Merge pull request #63 from square/rhg/make-space-commander-play-nice…
…-with-git-worktree

Make sure to create the hooks/ directory if it does not exist
Permalink
Failed to load latest commit information.
Testing Support Double new line formatter was missing block comments. Sep 2, 2016
bin Add more binary config info. Feb 5, 2016
custom Fix unassigned variable error Sep 19, 2016
lib Add directories to ignore Mar 22, 2016
.clang-format Don't sort includes. Feb 9, 2016
.gitignore Initial commit. Jun 3, 2015
.hubkins Initial commit. Jun 3, 2015
CocoaPodsDummy.m Podify SpaceCommander Aug 25, 2015
Contributing.md Add Contributing.md -- Backwards compatibility feedback Dec 14, 2015
Gemfile Add Gemfile. Mar 2, 2016
Gemfile.lock Add Gemfile. Mar 2, 2016
LICENSE Initial commit. Jun 3, 2015
README.md Update configuration options in README Mar 26, 2016
SpaceCommander.podspec Make sure to create the hooks/ directory if it does not exist. Jan 19, 2017
banner.jpg Update banner. Jun 3, 2015
format-objc-file-dry-run.sh Fix #42, where trailing parameters are indented on a newline after a … Mar 1, 2016
format-objc-file.sh Fix #42, where trailing parameters are indented on a newline after a … Mar 1, 2016
format-objc-files-in-repo.sh Use xargs to run format-objc-file.sh in parallel. Dec 19, 2015
format-objc-files.sh Initial commit. Jun 3, 2015
format-objc-hook Clarify the fixup instructions for the precommit hook. Oct 27, 2015
format-objc-mobuild Reflect the expected Pods path in mobuild script. Feb 10, 2016
setup-repo.sh Make sure to create the hooks/ directory if it does not exist. Jan 19, 2017
test.sh Update tests to prevent regression from differing clang-format behavior. Feb 9, 2016

README.md

[ Space Commander]

[ Space Commander] provides tools which enable a team of iOS developers to commit Objective-C code to a git repository using a unified style format, without requiring any manual fixup.

Corgi image

You can use it to:

  • Enforce formatting conventions before code is committed.
  • Format code with a single command (both individual files or the entire repo).
  • Fail a build (during a pull request) if unformatted code made it into the branch.

At Square, [ Space Commander] has streamlined iOS development, especially when it comes to pull requests. Applying formatting standards no longer requires manual developer attention; that time is better spent elsewhere!

You may wish to fork [ Space Commander] to apply your team's particular set of formatting rules (more details below), or clone to enjoy Square's flavor of Objective-C formatting.

Installation Locally

To add formatting pre-commit checks to your repo, from the target repo, run path/to/spacecommander/setup-repo.sh.

Usage

After running setup-repo.sh, formatting checks will run automatically before every commit.

To format an individual file and modify it in place, run format-objc-file.sh <file>. To format it without modification, run format-objc-file-dry-run.sh <file>

To format all of the Objective-C files in your repository in-place, run format-objc-files-in-repo.sh.

Details

To install the pre-commit hook, each developer on the project runs the setup script. This installs a precommit hook which will verify that code is formatted before the commit succeeds.

If there were formatting errors during the commit, a script to fixup code automatically can be run in order to commit without error.

At Square, this formatting repository is referenced as a submodule of a larger iOS project, so that the formatting rules and scripts are locked to a revision of the parent repository. This way, we can check the formatting as part of the build process, and fail the build if the formatting is not current (we can also check out older SHAs without any difficulty).

clang-format expects the custom rules file to exist in the same directory that the command is run from, and so a .gitignore-d symlink of the rules file is added to the target repository. It is a symlink so that the developer only needs to update the git SHA of the formatting repository to get the latest formatting rules from upstream.

Configuration

To format files only within selected directories, specify the name each directory in a file named .formatting-directory, separated by newlines (and without whitespace escaped). Otherwise, all Objective-C files tracked in the repo will be checked.

To ignore files within directories, add the name of each directory on a new line to a file named .formatting-directory-ignore.

To modify the formatting output, edit the following:

  • .clang-format for built in clang-format options.
  • format-objc-file-dry-run.sh and format-objc-file.sh for rules that are implemented in custom/.
  • Testing Support/ files to validate your changes.

Add #pragma Formatter Exempt or // MARK: Formatter Exempt as the first line of the file if the formatter should ignore it.

Installation for Pull Request Validation

The following instructions are Square-specific. We use a build system called *mobuild*. The hook that we use, which can be integrated into other build systems, is format-objc-mobuild

If you want style checking as a mandatory step to get a mergeable PR, do the following:

  • Add this repository as a cocoapod, or add it as a submodule in a Scripts/ directory.
  • Ensure that your repository has setup .sqiosbuild.json and .stashkins files at the top level (more info on the Square wiki page titled All About Mobuild).
  • The build machines are setup to check for the above conditions, and if they're met, automatically run format-objc-mobuild.
  • Open a PR with a modified Objective-C file to verify these checks are running.

Updating Style Options

Change formatting policies by modifying .clang-format. Available style options are listed on the clang website.

Please also update UnformattedExample.m (under ./Testing Support/) with an example of code that your formatting changes should correct.

Then, update FormattedExample.m (in the same place) with the expected result, and verify that your changes produce the desired result by running a simple test: ./test.sh

Custom Formatters

clang-format is fantastic and we love it, but it has some limitations. We've added our own ad-hoc formatting capabilities through scripts which live in custom/. If you add a custom file formatting script to custom/, invoke it in format-objc-file.sh and format-objc-file-dry-run.sh and add examples of input / output to files in Testing Support/.

Undesired Result?

The formatter can't do everything. It may occasionally produce an undesirable result, in which case you can either:

  • Refactor code to produce a line that is simpler and less confusing to the formatter.
  • Use // clang-format off and // clang-format on to selectively enable/disable clang-format for specific lines of a file.
  • Add #pragma Formatter Exempt or // MARK: Formatter Exempt as the first line of the file, and it will not be formatted at all.
  • Wislawa Szymborska said "All imperfection is easier to tolerate if served up in small doses." [ Space Commander] will remove nearly all formatting imperfections, but you may need to tolerate an occasional deviation from the expected result.

Contributing

We’re glad you’re interested in [ Space Commander], and we’d love to see where you take it. Please read our contributing guidelines prior to submitting a Pull Request.

Thanks, and happy formatting!