committing-code.txt 10 KB

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