2
0

committing-code.txt 10 KB

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