committing-code.txt 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. You may want to have Jenkins test the pull request with one of the pull request
  18. builders that doesn't run automatically, such as Oracle or Selenium. See the
  19. `Jenkins wiki page`_ for instructions.
  20. .. _Jenkins wiki page: https://code.djangoproject.com/wiki/Jenkins
  21. An easy way to checkout a pull request locally is to add an alias to your
  22. ``~/.gitconfig`` (``upstream`` is assumed to be ``django/django``)::
  23. [alias]
  24. pr = !sh -c \"git fetch upstream pull/${1}/head:pr/${1} && git checkout pr/${1}\"
  25. Now you can simply run ``git pr ####`` to checkout the corresponding pull
  26. request.
  27. At this point, you can work on the code. Use ``git rebase -i`` and ``git
  28. commit --amend`` to make sure the commits have the expected level of quality.
  29. Once you're ready:
  30. .. code-block:: console
  31. $ # Pull in the latest changes from master.
  32. $ git checkout master
  33. $ git pull upstream master
  34. $ # Rebase the pull request on master.
  35. $ git checkout pr/####
  36. $ git rebase master
  37. $ git checkout master
  38. $ # Merge the work as "fast-forward" to master to avoid a merge commit.
  39. $ # (in practice, you can omit "--ff-only" since you just rebased)
  40. $ git merge --ff-only pr/XXXX
  41. $ # If you're not sure if you did things correctly, check that only the
  42. $ # changes you expect will be pushed to upstream.
  43. $ git push --dry-run upstream master
  44. $ # Push!
  45. $ git push upstream master
  46. $ # Delete the pull request branch.
  47. $ git branch -d pr/xxxx
  48. For changes on your own branches, force push to your fork after rebasing on
  49. master but before merging and pushing to upstream. This allows the commit
  50. hashes on master and your branch to match which automatically closes the pull
  51. request. Since you can't push to other contributors' branches, comment on the
  52. pull request "Merged in XXXXXXX" (replacing with the commit hash) after you
  53. merge it. Trac checks for this message format to indicate on the ticket page
  54. whether or not a pull request is merged.
  55. If a pull request doesn't need to be merged as multiple commits, you can use
  56. GitHub's "Squash and merge" button on the website. Edit the commit message as
  57. needed to conform to :ref:`the guidelines <committing-guidelines>` and remove
  58. the pull request number that's automatically appended to the message's first
  59. line.
  60. When rewriting the commit history of a pull request, the goal is to make
  61. Django's commit history as usable as possible:
  62. * If a patch contains back-and-forth commits, then rewrite those into one.
  63. For example, if a commit adds some code and a second commit fixes stylistic
  64. issues introduced in the first commit, those commits should be squashed
  65. before merging.
  66. * Separate changes to different commits by logical grouping: if you do a
  67. stylistic cleanup at the same time as you do other changes to a file,
  68. separating the changes into two different commits will make reviewing
  69. history easier.
  70. * Beware of merges of upstream branches in the pull requests.
  71. * Tests should pass and docs should build after each commit. Neither the
  72. tests nor the docs should emit warnings.
  73. * Trivial and small patches usually are best done in one commit. Medium to
  74. large work may be split into multiple commits if it makes sense.
  75. Practicality beats purity, so it is up to each committer to decide how much
  76. history mangling to do for a pull request. The main points are engaging the
  77. community, getting work done, and having a usable commit history.
  78. .. _committing-guidelines:
  79. Committing guidelines
  80. =====================
  81. In addition, please follow the following guidelines when committing code to
  82. Django's Git repository:
  83. * Never change the published history of ``django/django`` branches by force
  84. pushing. If you absolutely must (for security reasons for example), first
  85. discuss the situation with the team.
  86. * For any medium-to-big changes, where "medium-to-big" is according to
  87. your judgment, please bring things up on the |django-developers|
  88. mailing list before making the change.
  89. If you bring something up on |django-developers| and nobody responds,
  90. please don't take that to mean your idea is great and should be
  91. implemented immediately because nobody contested it. Everyone doesn't always
  92. have a lot of time to read mailing list discussions immediately, so you may
  93. have to wait a couple of days before getting a 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. .. code-block:: none
  103. Fixed #18307 -- Added git workflow guidelines
  104. Refactored the Django's documentation to remove mentions of SVN
  105. specific tasks. Added guidelines of how to use Git, GitHub, and
  106. how to use pull request together with Trac instead.
  107. If the patch wasn't a pull request, you should credit the contributors in
  108. the commit message: "Thanks A for report, B for the patch and C for the
  109. review."
  110. * For commits to a branch, prefix the commit message with the branch name.
  111. For example: "[1.4.x] Fixed #xxxxx -- Added support for mind reading."
  112. * Limit commits to the most granular change that makes sense. This means,
  113. use frequent small commits rather than infrequent large commits. For
  114. example, if implementing feature X requires a small change to library Y,
  115. first commit the change to library Y, then commit feature X in a separate
  116. commit. This goes a *long way* in helping everyone 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/trac-hacks/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. .. code-block:: none
  142. [<Django version>] Fixed <ticket> -- <description>
  143. Backport of <revision> from <branch>.
  144. For example:
  145. .. code-block:: none
  146. [1.3.x] Fixed #17028 -- Changed diveintopython.org -> diveintopython.net.
  147. Backport of 80c0cbf1c97047daed2c5b41b296bbc56fe1d7e3 from master.
  148. There's a `script on the wiki
  149. <https://code.djangoproject.com/wiki/CommitterTips#AutomatingBackports>`_
  150. to automate this.
  151. Reverting commits
  152. =================
  153. Nobody's perfect; mistakes will be committed.
  154. But try very hard to ensure that mistakes don't happen. Just because we have a
  155. reversion policy doesn't relax your responsibility to aim for the highest
  156. quality possible. Really: double-check your work, or have it checked by
  157. another committer, **before** you commit it in the first place!
  158. When a mistaken commit is discovered, please follow these guidelines:
  159. * If possible, have the original author revert their own commit.
  160. * Don't revert another author's changes without permission from the
  161. original author.
  162. * Use git revert -- this will make a reverse commit, but the original
  163. commit will still be part of the commit history.
  164. * If the original author can't be reached (within a reasonable amount
  165. of time -- a day or so) and the problem is severe -- crashing bug,
  166. major test failures, etc. -- then ask for objections on the
  167. |django-developers| mailing list then revert if there are none.
  168. * If the problem is small (a feature commit after feature freeze,
  169. say), wait it out.
  170. * If there's a disagreement between the committer and the
  171. reverter-to-be then try to work it out on the |django-developers|
  172. mailing list. If an agreement can't be reached then it should
  173. be put to a vote.
  174. * If the commit introduced a confirmed, disclosed security
  175. vulnerability then the commit may be reverted immediately without
  176. permission from anyone.
  177. * The release branch maintainer may back out commits to the release
  178. branch without permission if the commit breaks the release branch.
  179. * If you mistakenly push a topic branch to ``django/django``, just delete it.
  180. For instance, if you did: ``git push upstream feature_antigravity``,
  181. just do a reverse push: ``git push upstream :feature_antigravity``.
  182. .. _ticket tracker: https://code.djangoproject.com/