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