提交代码

本部分针对提交者以及任何有兴趣了解如何为Django贡献代码的人员。 如果您是想向Django贡献代码的社区成员,请查看:doc:writing-code / working-with-git

处理 pull 请求

由于Django现在在GitHub上托管,因此大多数补丁都是以pull请求的形式提供的。

提交请求时,请确保每个单独的提交符合下面提到的提交准则。贡献者将尽可能提供最佳的请求。 然而,在实践中,提交者 - 谁可能更熟悉提交准则 - 可能会决定提交一个自己的提交标准。

You may want to have Jenkins test the pull request with one of the pull requestbuilders that doesn't run automatically, such as Oracle or Selenium. See theJenkins wiki page for instructions.

An easy way to checkout a pull request locally is to add an alias to your~/.gitconfig (upstream is assumed to be django/django):

  1. [alias]
  2. pr = !sh -c \"git fetch upstream pull/${1}/head:pr/${1} && git checkout pr/${1}\"

Now you can simply run git pr #### to checkout the corresponding pullrequest.

At this point, you can work on the code. Use git rebase -i and git
commit —amend
to make sure the commits have the expected level of quality.Once you're ready:

  1. $ # Pull in the latest changes from master.
  2. $ git checkout master
  3. $ git pull upstream master
  4. $ # Rebase the pull request on master.
  5. $ git checkout pr/####
  6. $ git rebase master
  7. $ git checkout master
  8. $ # Merge the work as "fast-forward" to master to avoid a merge commit.
  9. $ # (in practice, you can omit "--ff-only" since you just rebased)
  10. $ git merge --ff-only pr/XXXX
  11. $ # If you're not sure if you did things correctly, check that only the
  12. $ # changes you expect will be pushed to upstream.
  13. $ git push --dry-run upstream master
  14. $ # Push!
  15. $ git push upstream master
  16. $ # Delete the pull request branch.
  17. $ git branch -d pr/xxxx
  1. ...\> REM Pull in the latest changes from master.
  2. ...\> git checkout master
  3. ...\> git pull upstream master
  4. ...\> REM Rebase the pull request on master.
  5. ...\> git checkout pr/####
  6. ...\> git rebase master
  7. ...\> git checkout master
  8. ...\> REM Merge the work as "fast-forward" to master to avoid a merge commit.
  9. ...\> REM (in practice, you can omit "--ff-only" since you just rebased)
  10. ...\> git merge --ff-only pr/XXXX
  11. ...\> REM If you're not sure if you did things correctly, check that only the
  12. ...\> REM changes you expect will be pushed to upstream.
  13. ...\> git push --dry-run upstream master
  14. ...\> REM Push!
  15. ...\> git push upstream master
  16. ...\> REM Delete the pull request branch.
  17. ...\> git branch -d pr/xxxx

Force push to the branch after rebasing on master but before merging andpushing to upstream. This allows the commit hashes on master and the branch tomatch which automatically closes the pull request.

If a pull request doesn't need to be merged as multiple commits, you can useGitHub's "Squash and merge" button on the website. Edit the commit message asneeded to conform to the guidelines and removethe pull request number that's automatically appended to the message's firstline.

When rewriting the commit history of a pull request, the goal is to makeDjango's commit history as usable as possible:

  • If a patch contains back-and-forth commits, then rewrite those into one.For example, if a commit adds some code and a second commit fixes stylisticissues introduced in the first commit, those commits should be squashedbefore merging.
  • Separate changes to different commits by logical grouping: if you do astylistic cleanup at the same time as you do other changes to a file,separating the changes into two different commits will make reviewinghistory easier.
  • Beware of merges of upstream branches in the pull requests.
  • Tests should pass and docs should build after each commit. Neither thetests nor the docs should emit warnings.
  • Trivial and small patches usually are best done in one commit. Medium tolarge work may be split into multiple commits if it makes sense.
    Practicality beats purity, so it is up to each committer to decide how muchhistory mangling to do for a pull request. The main points are engaging thecommunity, getting work done, and having a usable commit history.

提交指南

In addition, please follow the following guidelines when committing code toDjango's Git repository:

  • Never change the published history of django/django branches by forcepushing. If you absolutely must (for security reasons for example), firstdiscuss the situation with the team.

  • For any medium-to-big changes, where "medium-to-big" is according toyour judgment, please bring things up on the django-developersmailing list before making the change.

If you bring something up on django-developers and nobody responds,please don't take that to mean your idea is great and should beimplemented immediately because nobody contested it. Everyone doesn't alwayshave a lot of time to read mailing list discussions immediately, so you mayhave to wait a couple of days before getting a response.

  • Write detailed commit messages in the past tense, not present tense.

    • 正确:"Fixed Unicode bug in RSS API."
    • 错误:"Fixes Unicode bug in RSS API."
    • 错误:"Fixing Unicode bug in RSS API."
      The commit message should be in lines of 72 chars maximum. There should bea subject line, separated by a blank line and then paragraphs of 72 charlines. The limits are soft. For the subject line, shorter is better. In thebody of the commit message more detail is better than less:
  1. Fixed #18307 -- Added git workflow guidelines
  2.  
  3. Refactored the Django's documentation to remove mentions of SVN
  4. specific tasks. Added guidelines of how to use Git, GitHub, and
  5. how to use pull request together with Trac instead.

If the patch wasn't a pull request, you should credit the contributors inthe commit message: "Thanks A for report, B for the patch and C for thereview."

  • For commits to a branch, prefix the commit message with the branch name.For example: "[1.4.x] Fixed #xxxxx — Added support for mind reading."

  • Limit commits to the most granular change that makes sense. This means,use frequent small commits rather than infrequent large commits. Forexample, if implementing feature X requires a small change to library Y,first commit the change to library Y, then commit feature X in a separatecommit. This goes a long way in helping everyone follow your changes.

  • Separate bug fixes from feature changes. Bugfixes may need to be backportedto the stable branch, according to Supported versions.

  • If your commit closes a ticket in the Django ticket tracker, beginyour commit message with the text "Fixed #xxxxx", where "xxxxx" is thenumber of the ticket your commit fixes. Example: "Fixed #123 — Addedwhizbang feature.". We've rigged Trac so that any commit message in thatformat will automatically close the referenced ticket and post a commentto it with the full commit message.

For the curious, we're using a Trac plugin for this.

注解

Note that the Trac integration doesn't know anything about pull requests.So if you try to close a pull request with the phrase "closes #400" in yourcommit message, GitHub will close the pull request, but the Trac pluginwill also close the same numbered ticket in Trac.

  • If your commit references a ticket in the Django ticket tracker butdoes not close the ticket, include the phrase "Refs #xxxxx", where "xxxxx"is the number of the ticket your commit references. This will automaticallypost a comment to the appropriate ticket.

  • Write commit messages for backports using this pattern:

  1. [<Django version>] Fixed <ticket> -- <description>
  2.  
  3. Backport of <revision> from <branch>.

例如:

  1. [1.3.x] Fixed #17028 -- Changed diveintopython.org -> diveintopython.net.
  2.  
  3. Backport of 80c0cbf1c97047daed2c5b41b296bbc56fe1d7e3 from master.

There's a script on the wikito automate this.

If the commit fixes a regression, include this in the commit message:

  1. Regression in 6ecccad711b52f9273b1acb07a57d3f806e93928.

(use the commit hash where the regression was introduced).

恢复提交

没有人是完美的;一些错误可能会被提交

But try very hard to ensure that mistakes don't happen. Just because we have areversion policy doesn't relax your responsibility to aim for the highestquality possible. Really: double-check your work, or have it checked byanother committer, before you commit it in the first place!

当你发现了一个错误的提交,请遵循如下步骤:

  • 可能的话,让原始作者撤回他们的提交。
  • 不要在未获得原始作者同意的情况下还原其他作者的更改。
  • Use git revert — this will make a reverse commit, but the originalcommit will still be part of the commit history.
  • If the original author can't be reached (within a reasonable amountof time — a day or so) and the problem is severe — crashing bug,major test failures, etc. — then ask for objections on thedjango-developers mailing list then revert if there are none.
  • If the problem is small (a feature commit after feature freeze,say), wait it out.
  • If there's a disagreement between the committer and thereverter-to-be then try to work it out on the django-developersmailing list. If an agreement can't be reached then it shouldbe put to a vote.
  • If the commit introduced a confirmed, disclosed securityvulnerability then the commit may be reverted immediately withoutpermission from anyone.
  • The release branch maintainer may back out commits to the releasebranch without permission if the commit breaks the release branch.
  • If you mistakenly push a topic branch to django/django, just delete it.For instance, if you did: git push upstream feature_antigravity,just do a reverse push: git push upstream :feature_antigravity.