committing-code.txt 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. Avoid using GitHub's "Merge pull request" button on the Web site as its creates
  58. an ugly "merge commit" and makes navigating history more difficult.
  59. When rewriting the commit history of a pull request, the goal is to make
  60. Django's commit history as usable as possible:
  61. * If a patch contains back-and-forth commits, then rewrite those into one.
  62. For example, if a commit adds some code and a second commit fixes stylistic
  63. issues introduced in the first commit, those commits should be squashed
  64. before merging.
  65. * Separate changes to different commits by logical grouping: if you do a
  66. stylistic cleanup at the same time as you do other changes to a file,
  67. separating the changes into two different commits will make reviewing
  68. history easier.
  69. * Beware of merges of upstream branches in the pull requests.
  70. * Tests should pass and docs should build after each commit. Neither the
  71. tests nor the docs should emit warnings.
  72. * Trivial and small patches usually are best done in one commit. Medium to
  73. large work may be split into multiple commits if it makes sense.
  74. Practicality beats purity, so it is up to each committer to decide how much
  75. history mangling to do for a pull request. The main points are engaging the
  76. community, getting work done, and having a usable commit history.
  77. .. _committing-guidelines:
  78. Committing guidelines
  79. ---------------------
  80. In addition, please follow the following guidelines when committing code to
  81. Django's Git repository:
  82. * Never change the published history of django/django branches! **Never force-
  83. push your changes to django/django.** If you absolutely must (for security
  84. reasons for example) first discuss the situation with the core team.
  85. * For any medium-to-big changes, where "medium-to-big" is according to
  86. your judgment, please bring things up on the |django-developers|
  87. mailing list before making the change.
  88. If you bring something up on |django-developers| and nobody responds,
  89. please don't take that to mean your idea is great and should be
  90. implemented immediately because nobody contested it. Django's core
  91. developers don't have a lot of time to read mailing-list discussions
  92. immediately, so you may have to wait a couple of days before getting a
  93. response.
  94. * Write detailed commit messages in the past tense, not present tense.
  95. * Good: "Fixed Unicode bug in RSS API."
  96. * Bad: "Fixes Unicode bug in RSS API."
  97. * Bad: "Fixing Unicode bug in RSS API."
  98. The commit message should be in lines of 72 chars maximum. There should be
  99. a subject line, separated by a blank line and then paragraphs of 72 char
  100. lines. The limits are soft. For the subject line, shorter is better. In the
  101. body of the commit message more detail is better than less::
  102. Fixed #18307 -- Added git workflow guidelines
  103. Refactored the Django's documentation to remove mentions of SVN
  104. specific tasks. Added guidelines of how to use Git, GitHub, and
  105. how to use pull request together with Trac instead.
  106. If the patch wasn't a pull request, you should credit the contributors in
  107. the commit message: "Thanks A for report, B for the patch and C for the
  108. review."
  109. * For commits to a branch, prefix the commit message with the branch name.
  110. For example: "[1.4.x] Fixed #xxxxx -- Added support for mind reading."
  111. * Limit commits to the most granular change that makes sense. This means,
  112. use frequent small commits rather than infrequent large commits. For
  113. example, if implementing feature X requires a small change to library Y,
  114. first commit the change to library Y, then commit feature X in a
  115. separate commit. This goes a *long way* in helping all Django core
  116. developers follow your changes.
  117. * Separate bug fixes from feature changes. Bugfixes may need to be backported
  118. to the stable branch, according to the :ref:`backwards-compatibility policy
  119. <backwards-compatibility-policy>`.
  120. * If your commit closes a ticket in the Django `ticket tracker`_, begin
  121. your commit message with the text "Fixed #xxxxx", where "xxxxx" is the
  122. number of the ticket your commit fixes. Example: "Fixed #123 -- Added
  123. whizbang feature.". We've rigged Trac so that any commit message in that
  124. format will automatically close the referenced ticket and post a comment
  125. to it with the full commit message.
  126. If your commit closes a ticket and is in a branch, use the branch name
  127. first, then the "Fixed #xxxxx." For example:
  128. "[1.4.x] Fixed #123 -- Added whizbang feature."
  129. For the curious, we're using a `Trac plugin`_ for this.
  130. .. note::
  131. Note that the Trac integration doesn't know anything about pull requests.
  132. So if you try to close a pull request with the phrase "closes #400" in your
  133. commit message, GitHub will close the pull request, but the Trac plugin
  134. will also close the same numbered ticket in Trac.
  135. .. _Trac plugin: https://github.com/aaugustin/trac-github
  136. * If your commit references a ticket in the Django `ticket tracker`_ but
  137. does *not* close the ticket, include the phrase "Refs #xxxxx", where "xxxxx"
  138. is the number of the ticket your commit references. This will automatically
  139. post a comment to the appropriate ticket.
  140. * Write commit messages for backports using this pattern::
  141. [<Django version>] Fixed <ticket> -- <description>
  142. Backport of <revision> from <branch>.
  143. For example::
  144. [1.3.x] Fixed #17028 -- Changed diveintopython.org -> diveintopython.net.
  145. Backport of 80c0cbf1c97047daed2c5b41b296bbc56fe1d7e3 from master.
  146. There's a `script on the wiki
  147. <https://code.djangoproject.com/wiki/CommitterTips#AutomatingBackports>`_
  148. to automate this.
  149. Reverting commits
  150. -----------------
  151. Nobody's perfect; mistakes will be committed.
  152. But try very hard to ensure that mistakes don't happen. Just because we have a
  153. reversion policy doesn't relax your responsibility to aim for the highest
  154. quality possible. Really: double-check your work, or have it checked by
  155. another committer, **before** you commit it in the first place!
  156. When a mistaken commit is discovered, please follow these guidelines:
  157. * If possible, have the original author revert their own commit.
  158. * Don't revert another author's changes without permission from the
  159. original author.
  160. * Use git revert -- this will make a reverse commit, but the original
  161. commit will still be part of the commit history.
  162. * If the original author can't be reached (within a reasonable amount
  163. of time -- a day or so) and the problem is severe -- crashing bug,
  164. major test failures, etc -- then ask for objections on the
  165. |django-developers| mailing list then revert if there are none.
  166. * If the problem is small (a feature commit after feature freeze,
  167. say), wait it out.
  168. * If there's a disagreement between the committer and the
  169. reverter-to-be then try to work it out on the |django-developers|
  170. mailing list. If an agreement can't be reached then it should
  171. be put to a vote.
  172. * If the commit introduced a confirmed, disclosed security
  173. vulnerability then the commit may be reverted immediately without
  174. permission from anyone.
  175. * The release branch maintainer may back out commits to the release
  176. branch without permission if the commit breaks the release branch.
  177. * If you mistakenly push a topic branch to django/django, just delete it.
  178. For instance, if you did: ``git push upstream feature_antigravity``,
  179. just do a reverse push: ``git push upstream :feature_antigravity``.
  180. .. _ticket tracker: https://code.djangoproject.com/newticket