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