committing-code.txt 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. ===============
  2. Committing code
  3. ===============
  4. This section is addressed to the :ref:`committers` and to anyone interested in
  5. knowing how code gets committed into Django core. If you're a community member
  6. who wants to contribute code to Django, have a look at
  7. :doc:`writing-code/working-with-git` instead.
  8. .. _handling-pull-requests:
  9. Handling pull requests
  10. ======================
  11. Since Django is now hosted at GitHub, most patches are provided in the form of
  12. pull requests.
  13. When committing a pull request, make sure each individual commit matches the
  14. commit guidelines described below. Contributors are expected to provide the
  15. best pull requests possible. In practice however, committers - who will likely
  16. be more familiar with the commit guidelines - may decide to bring a commit up
  17. to standard themselves.
  18. .. note::
  19. Before merging, but after reviewing, have Jenkins test the pull request by
  20. commenting "buildbot, test this please" on the PR.
  21. See our `Jenkins wiki page`_ for more details.
  22. .. _Jenkins wiki page: https://code.djangoproject.com/wiki/Jenkins
  23. An easy way to checkout a pull request locally is to add an alias to your
  24. ``~/.gitconfig`` (``upstream`` is assumed to be ``django/django``)::
  25. [alias]
  26. pr = !sh -c \"git fetch upstream pull/${1}/head:pr/${1} && git checkout pr/${1}\"
  27. Now you can simply run ``git pr ####`` to checkout the corresponding pull
  28. request.
  29. At this point, you can work on the code. Use ``git rebase -i`` and ``git
  30. commit --amend`` to make sure the commits have the expected level of quality.
  31. Once you're ready:
  32. .. code-block:: console
  33. $ # Pull in the latest changes from master.
  34. $ git checkout master
  35. $ git pull upstream master
  36. $ # Rebase the pull request on master.
  37. $ git checkout pr/####
  38. $ git rebase master
  39. $ git checkout master
  40. $ # Merge the work as "fast-forward" to master to avoid a merge commit.
  41. $ # (in practice, you can omit "--ff-only" since you just rebased)
  42. $ git merge --ff-only pr/XXXX
  43. $ # If you're not sure if you did things correctly, check that only the
  44. $ # changes you expect will be pushed to upstream.
  45. $ git push --dry-run upstream master
  46. $ # Push!
  47. $ git push upstream master
  48. $ # Delete the pull request branch.
  49. $ git branch -d pr/xxxx
  50. For changes on your own branches, force push to your fork after rebasing on
  51. master but before merging and pushing to upstream. This allows the commit
  52. hashes on master and your branch to match which automatically closes the pull
  53. request. Since you can't push to other contributors' branches, comment on the
  54. pull request "Merged in XXXXXXX" (replacing with the commit hash) after you
  55. merge it. Trac checks for this message format to indicate on the ticket page
  56. whether or not a pull request is merged.
  57. If a pull request doesn't need to be merged as multiple commits, you can use
  58. GitHub's "Squash and merge" button on the website. Edit the commit message as
  59. needed to conform to :ref:`the guidelines <committing-guidelines>` and remove
  60. the pull request number that's automatically appended to the message's first
  61. line.
  62. When rewriting the commit history of a pull request, the goal is to make
  63. Django's commit history as usable as possible:
  64. * If a patch contains back-and-forth commits, then rewrite those into one.
  65. For example, if a commit adds some code and a second commit fixes stylistic
  66. issues introduced in the first commit, those commits should be squashed
  67. before merging.
  68. * Separate changes to different commits by logical grouping: if you do a
  69. stylistic cleanup at the same time as you do other changes to a file,
  70. separating the changes into two different commits will make reviewing
  71. history easier.
  72. * Beware of merges of upstream branches in the pull requests.
  73. * Tests should pass and docs should build after each commit. Neither the
  74. tests nor the docs should emit warnings.
  75. * Trivial and small patches usually are best done in one commit. Medium to
  76. large work may be split into multiple commits if it makes sense.
  77. Practicality beats purity, so it is up to each committer to decide how much
  78. history mangling to do for a pull request. The main points are engaging the
  79. community, getting work done, and having a usable commit history.
  80. .. _committing-guidelines:
  81. Committing guidelines
  82. =====================
  83. In addition, please follow the following guidelines when committing code to
  84. Django's Git repository:
  85. * Never change the published history of django/django branches! **Never
  86. force-push your changes to django/django.** If you absolutely must (for
  87. security reasons for example) first discuss the situation with the core team.
  88. * For any medium-to-big changes, where "medium-to-big" is according to
  89. your judgment, please bring things up on the |django-developers|
  90. mailing list before making the change.
  91. If you bring something up on |django-developers| and nobody responds,
  92. please don't take that to mean your idea is great and should be
  93. implemented immediately because nobody contested it. Django's core
  94. developers don't have a lot of time to read mailing-list discussions
  95. immediately, so you may have to wait a couple of days before getting a
  96. response.
  97. * Write detailed commit messages in the past tense, not present tense.
  98. * Good: "Fixed Unicode bug in RSS API."
  99. * Bad: "Fixes Unicode bug in RSS API."
  100. * Bad: "Fixing Unicode bug in RSS API."
  101. The commit message should be in lines of 72 chars maximum. There should be
  102. a subject line, separated by a blank line and then paragraphs of 72 char
  103. lines. The limits are soft. For the subject line, shorter is better. In the
  104. body of the commit message more detail is better than less::
  105. Fixed #18307 -- Added git workflow guidelines
  106. Refactored the Django's documentation to remove mentions of SVN
  107. specific tasks. Added guidelines of how to use Git, GitHub, and
  108. how to use pull request together with Trac instead.
  109. If the patch wasn't a pull request, you should credit the contributors in
  110. the commit message: "Thanks A for report, B for the patch and C for the
  111. review."
  112. * For commits to a branch, prefix the commit message with the branch name.
  113. For example: "[1.4.x] Fixed #xxxxx -- Added support for mind reading."
  114. * Limit commits to the most granular change that makes sense. This means,
  115. use frequent small commits rather than infrequent large commits. For
  116. example, if implementing feature X requires a small change to library Y,
  117. first commit the change to library Y, then commit feature X in a
  118. separate commit. This goes a *long way* in helping all Django core
  119. developers follow your changes.
  120. * Separate bug fixes from feature changes. Bugfixes may need to be backported
  121. to the stable branch, according to the :ref:`backwards-compatibility policy
  122. <backwards-compatibility-policy>`.
  123. * If your commit closes a ticket in the Django `ticket tracker`_, begin
  124. your commit message with the text "Fixed #xxxxx", where "xxxxx" is the
  125. number of the ticket your commit fixes. Example: "Fixed #123 -- Added
  126. whizbang feature.". We've rigged Trac so that any commit message in that
  127. format will automatically close the referenced ticket and post a comment
  128. to it with the full commit message.
  129. If your commit closes a ticket and is in a branch, use the branch name
  130. first, then the "Fixed #xxxxx." For example:
  131. "[1.4.x] Fixed #123 -- Added whizbang feature."
  132. For the curious, we're using a `Trac plugin`_ for this.
  133. .. note::
  134. Note that the Trac integration doesn't know anything about pull requests.
  135. So if you try to close a pull request with the phrase "closes #400" in your
  136. commit message, GitHub will close the pull request, but the Trac plugin
  137. will also close the same numbered ticket in Trac.
  138. .. _Trac plugin: https://github.com/trac-hacks/trac-github
  139. * If your commit references a ticket in the Django `ticket tracker`_ but
  140. does *not* close the ticket, include the phrase "Refs #xxxxx", where "xxxxx"
  141. is the number of the ticket your commit references. This will automatically
  142. post a comment to the appropriate ticket.
  143. * Write commit messages for backports using this pattern::
  144. [<Django version>] Fixed <ticket> -- <description>
  145. Backport of <revision> from <branch>.
  146. For example::
  147. [1.3.x] Fixed #17028 -- Changed diveintopython.org -> diveintopython.net.
  148. Backport of 80c0cbf1c97047daed2c5b41b296bbc56fe1d7e3 from master.
  149. There's a `script on the wiki
  150. <https://code.djangoproject.com/wiki/CommitterTips#AutomatingBackports>`_
  151. to automate this.
  152. Reverting commits
  153. =================
  154. Nobody's perfect; mistakes will be committed.
  155. But try very hard to ensure that mistakes don't happen. Just because we have a
  156. reversion policy doesn't relax your responsibility to aim for the highest
  157. quality possible. Really: double-check your work, or have it checked by
  158. another committer, **before** you commit it in the first place!
  159. When a mistaken commit is discovered, please follow these guidelines:
  160. * If possible, have the original author revert their own commit.
  161. * Don't revert another author's changes without permission from the
  162. original author.
  163. * Use git revert -- this will make a reverse commit, but the original
  164. commit will still be part of the commit history.
  165. * If the original author can't be reached (within a reasonable amount
  166. of time -- a day or so) and the problem is severe -- crashing bug,
  167. major test failures, etc. -- then ask for objections on the
  168. |django-developers| mailing list then revert if there are none.
  169. * If the problem is small (a feature commit after feature freeze,
  170. say), wait it out.
  171. * If there's a disagreement between the committer and the
  172. reverter-to-be then try to work it out on the |django-developers|
  173. mailing list. If an agreement can't be reached then it should
  174. be put to a vote.
  175. * If the commit introduced a confirmed, disclosed security
  176. vulnerability then the commit may be reverted immediately without
  177. permission from anyone.
  178. * The release branch maintainer may back out commits to the release
  179. branch without permission if the commit breaks the release branch.
  180. * If you mistakenly push a topic branch to django/django, just delete it.
  181. For instance, if you did: ``git push upstream feature_antigravity``,
  182. just do a reverse push: ``git push upstream :feature_antigravity``.
  183. .. _ticket tracker: https://code.djangoproject.com/