Skip to content

Latest commit

 

History

History
856 lines (617 loc) · 32.1 KB

style.md

File metadata and controls

856 lines (617 loc) · 32.1 KB

Style Guide

(This is not at all complete.)

Index:

Git: commits, commit messages

See also

Zulip guide to clear and coherent commits. This has important principles about structuring your changes, and about writing commit messages.

Our Git guide. This includes some valuable tips you may not know, even if you've been using Git for a while. The Zulip maintainers spend a lot of time reading Git commits, which is a major reason we keep high standards for commit clarity. These tips will help you get a lot of value from reading Git history too.

Communicating in the commit message

See also: The Zulip guide to clear and coherent commits (same link as above.)

Answer in the branch the reviewer's questions. Any time a reviewer asks a "why" question about a PR, we'll want the answer to make it into the actual commits. Or if they find something confusing, we'll want it to be clear in the actual commits: ideally the code itself makes it clear, or if not then code comments or the commit messages explain it.

Answer questions the reviewer should ask. The reviewer will be trying to understand your change: what it does, and why. Help them out before they even see the branch, by answering their questions in the code, comments, and commit messages.

Here are some particularly important questions the reviewer may be asking -- at least in their head -- when they look at each piece of your change:

  • Hmm, was that change intentional? (E.g.: the commit message said the point is to implement feature A, but this also changes how feature B looks.)

  • Is this part something we should have been doing all along? Or is it newly needed because of the other changes you're making?

  • (If the old code had something wrong with it:) What were the consequences of the problem; did it have any effects on what the user saw?

  • What source were you relying on to understand [this API / these guidelines / this upgrade process / this platform feature]? (A link or two can be very helpful.)

  • How did you generate the new [foo] -- what kind of input did you provide to [foo-making tool]? (Where "foo" is an icon, an Xcode config file, etc.)

Squashing and not-squashing commits

Here again, see also the Zulip guide to clear and coherent commits.

When in doubt, leave as separate commits. It's easy to squash commits that are separate, and can be more work to separate changes that were squashed. So when in doubt about whether to squash some changes, send the PR with them unsquashed, and ask the reviewer.

(This is in the "clear and coherent commits" guide, but bears repeating next to any advice about squashing commits.)

Move code in one commit, rather than add + delete in two. Whenever code is being moved, it's usually best to make the move in one commit rather than separately add and delete. For an example, see #3310.

This makes it self-contained to see that the old and new code match up and to understand where the new code comes from (in particular, that it wasn't newly made up.)

Sometimes in complex cases it's a better tradeoff to do them separately for other reasons. When doing so, be sure to be more explicit about what's going on to help the reader make up for it.

Squash small commits when they're easier to understand together. This often applies to a change that produces some data and another that consumes it. For an example, see the merge of #3263.

On the other hand if either change is large or complex, then it's often a better tradeoff to do them separately. Just be explicit about what's going on: in particular, in the earlier commit message make clear that the other side is coming soon, and in the later one mention that the other side was just added.

Commit messages, comments, code

Update commit messages when you update a branch. The commit message is a key part of the content of a commit. So when you revise in response to code review feedback, be sure to edit the commit message as needed to match.

Commit messages vs. comments. In general:

  • Use the commit message for information that's specific to the change between this commit and its parent. In particular, for information about why the new version is better than, or not worse than, the old version.

  • Use comments, or the code itself, for information that's relevant to the new version of the code on its own -- for understanding the new code from scratch as it is.

    • Where possible, it's best to use names and types to make the information clear in the code itself. Many kinds of information don't fit there; for those, use comments.

Mentioning people

No GitHub @-mentions: GitHub makes it tempting to refer to people by an @-mention of their GitHub username, like @gnprice. Unfortunately this is a misfeature in commit messages:

  • GitHub will turn this into a notification for the person every time a version of the commit is rebased and pushed somewhere, which is usually totally irrelevant for them -- e.g. someone took a long stretch of history and rebased it and pushed that to their own clone.

    That person is doing something perfectly reasonable because it's off in their own sandbox, and the author (you) had a perfectly reasonable intention to mention the person when making a change in the project, but GitHub turns the combination of those into spam.

  • It also ties the information unnecessarily to GitHub, which for a long-lived project may not be its home forever.

If you want to send someone a notification about a change, @-mention them in the PR thread, not in a commit message.

To refer to them in a commit message, use one of the conventions below.

Consider just a name: If there's further context -- like a chat link where you're referring to what someone said in that conversation -- then just a name like "Greg" can be natural and unambiguous.

Consider Reported-by: and friends: The convention of the Linux kernel project, and the upstream Git project, is to end the commit message with a block of lines referring to people who wrote the commit, reviewed it, approved it, etc. These identify people by name and email address, for complete unambiguity.

We don't have a habit of using this in our codebase, but that doesn't stop it from working just fine. For an example, see 338036e0c which has a line

Suggested-by: Anders Kaseorg <[email protected]>

In the kernel, the most numerous of these is actually Cc:, which basically amounts to an @-mention! Other common lines include

Reported-by:
Debugged-by:
Suggested-by:
Co-developed-by:
Tested-by:

and there's no fixed list; people invent others.

When inventing a label for a metadata line like this, note the formatting style: hyphens (-) instead of spaces, and in sentence case (i.e. capitalized only at the beginning).

Mentioning commits

Use 9 (or 10) hex digits to identify a commit. To help make this convenient, you can tell Git to routinely print 9-digit abbreviations for you by setting git config core.abbrev 9.

Rationale: The full 40 hex digits is a lot, and generally doesn't flow well in prose. On the other hand 7 hex digits, which is what GitHub shows in its UI whenever it doesn't show 40, is short enough that there's a material risk of collisions: in zulip.git there are already a handful of commits that are ambiguous when identified by just 7 digits, and in a very large project like the Linux kernel such collisions can become routine.

A 9-digit abbreviation is still short enough to fit well in running text; and it's long enough that it's extremely unlikely any given such abbreviation will ever be ambiguous.

GitHub: PRs, issues