Confirmando código¶
Esta sección está dirigida a los fusionados y a cualquiera interesado en saber cómo se compromete el código en Django. Si eres un miembro de la comunidad que quiere contribuir código a Django, mira en Working with Git and GitHub en su lugar.
Gestionando las pull requests¶
Dado que Django está alojado en GitHub, los parches se proporcionan en forma de pull requests.
Cuando envíe una solicitud de extracción, asegúrese de que cada una de ellas se ajusta a las directrices que se describen a continuación. Se espera que los colaboradores proporcionen los mejores pull requests posibles. En la práctica, las fusiones, que probablemente estarán más familiarizadas con las directrices de confirmación, pueden decidir actualizar ellas mismas una confirmación.
Es posible que desee que las acciones de Jenkins o GitHub prueben el pull request con uno de los constructores de pull request que no se ejecuta automáticamente, como Oracle o Selenium. Consulte la página wiki CI para obtener instrucciones.
Si te encuentras revisando pull requests localmente más a menudo, este alias de git te será útil:
[alias]
pr = !sh -c \"git fetch upstream pull/${1}/head:pr/${1} && git checkout pr/${1}\"
Añádelo a tu ~/.gitconfig
, y establece upstream
como django/django
. Luego puedes ejecutar git pr ####
para obtener el pull request correspondiente.
En este punto, puedes trabajar en el código. Utiliza git rebase -i
y git commit --amend
para asegurarte de que los commits tienen el nivel de calidad esperado. Una vez que estés listo:
$ # Pull in the latest changes from main.
$ git checkout main
$ git pull upstream main
$ # Rebase the pull request on main.
$ git checkout pr/####
$ git rebase main
$ git checkout main
$ # Merge the work as "fast-forward" to main to avoid a merge commit.
$ # (in practice, you can omit "--ff-only" since you just rebased)
$ git merge --ff-only pr/XXXX
$ # If you're not sure if you did things correctly, check that only the
$ # changes you expect will be pushed to upstream.
$ git push --dry-run upstream main
$ # Push!
$ git push upstream main
$ # Delete the pull request branch.
$ git branch -d pr/xxxx
...\> REM Pull in the latest changes from main.
...\> git checkout main
...\> git pull upstream main
...\> REM Rebase the pull request on main.
...\> git checkout pr/####
...\> git rebase main
...\> git checkout main
...\> REM Merge the work as "fast-forward" to main to avoid a merge commit.
...\> REM (in practice, you can omit "--ff-only" since you just rebased)
...\> git merge --ff-only pr/XXXX
...\> REM If you're not sure if you did things correctly, check that only the
...\> REM changes you expect will be pushed to upstream.
...\> git push --dry-run upstream main
...\> REM Push!
...\> git push upstream main
...\> REM Delete the pull request branch.
...\> git branch -d pr/xxxx
Fuerza el push a la rama después de hacer el rebasing en main pero antes de hacer el merging y el push a upstream. Esto permite que los hashes de commit en main y en la rama coincidan, lo que cierra automáticamente la solicitud de pull.
Si no es necesario fusionar una pull request en varias confirmaciones, puede utilizar el botón «Squash and merge» de GitHub en el sitio web. Edite el mensaje de confirmación según sea necesario para que se ajuste a las directrices y elimine el número de solicitud de extracción que se añade automáticamente a la primera línea del mensaje.
Al reescribir el historial de commits de un pull request, el objetivo es hacer que el historial de commits de Django sea lo más usable posible:
Si un parche contiene confirmaciones de ida y vuelta, reescríbalas en una sola. Por ejemplo, si una confirmación añade algo de código y una segunda corrige problemas estilísticos introducidos en la primera, esas confirmaciones deben aplastarse antes de fusionarse.
Separe los cambios en diferentes commits mediante una agrupación lógica: si realiza una limpieza estilística al mismo tiempo que realiza otros cambios en un archivo, separar los cambios en dos commits diferentes facilitará la revisión del historial.
Cuidado con las fusiones de ramas ascendentes en los pull requests.
Las pruebas deben pasar y los documentos deben compilarse después de cada confirmación. Ni las pruebas ni los documentos deben emitir advertencias.
Los parches pequeños y triviales suelen realizarse mejor en una sola confirmación. Los trabajos medianos y grandes pueden dividirse en varias confirmaciones si tiene sentido.
La practicidad vence a la pureza, así que depende de cada fusión decidir cuánta manipulación de la historia hacer para un pull request. Los puntos principales son involucrar a la comunidad, hacer el trabajo y tener un historial de commit utilizable.
Directrices de compromiso¶
Además, sigue las siguientes directrices cuando envíes código al repositorio Git de Django:
Nunca cambies el historial publicado de las ramas
django/django
forzando el push. Si es absolutamente necesario (por razones de seguridad, por ejemplo), primero discute la situación con el equipo.Para cualquier cambio de mediano a gran escala, donde «mediano a gran escala» queda a su criterio, por favor, coméntelo en el Foro Django antes de realizar el cambio.
Si plantea algo y nadie responde, por favor, no asuma que su idea es excelente y debe implementarse de inmediato solo por que nadie la cuestiono. No todo el mundo tiene mucho tiempo para leer las discuciones de inmediato, así que es posible que tenga que esperar un par de días antes de recibir una respuesta.
Escriba los mensajes de confirmación detallados en pasado, no en presente.
Bueno: «Error de Unicode corregido en la API RSS».
Bad: «Fixes Unicode bug in RSS API.»
Bad: «Fixing Unicode bug in RSS API.»
The commit message should be in lines of 72 chars maximum. There should be a subject line, separated by a blank line and then paragraphs of 72 char lines. The limits are soft. For the subject line, shorter is better. In the body of the commit message more detail is better than less:
Fixed #18307 -- Added git workflow guidelines. Refactored the Django's documentation to remove mentions of SVN specific tasks. Added guidelines of how to use Git, GitHub, and how to use pull request together with Trac instead.
Credit the contributors in the commit message: «Thanks A for the report and B for review.» Use git’s Co-Authored-By as appropriate.
For commits to a branch, prefix the commit message with the branch name. For example: «[1.4.x] Fixed #xxxxx – Added support for mind reading.»
Limit commits to the most granular change that makes sense. This means, use frequent small commits rather than infrequent large commits. For example, if implementing feature X requires a small change to library Y, first commit the change to library Y, then commit feature X in a separate commit. This goes a long way in helping everyone follow your changes.
Separate bug fixes from feature changes. Bugfixes may need to be backported to the stable branch, according to Supported versions.
If your commit closes a ticket in the Django ticket tracker, begin your commit message with the text «Fixed #xxxxx», where «xxxxx» is the number of the ticket your commit fixes. Example: «Fixed #123 – Added whizbang feature.». We’ve rigged Trac so that any commit message in that format will automatically close the referenced ticket and post a comment to it with the full commit message.
For the curious, we’re using a Trac plugin for this.
Nota
Note that the Trac integration doesn’t know anything about pull requests. So if you try to close a pull request with the phrase «closes #400» in your commit message, GitHub will close the pull request, but the Trac plugin will not close the same numbered ticket in Trac.
If your commit references a ticket in the Django ticket tracker but does not close the ticket, include the phrase «Refs #xxxxx», where «xxxxx» is the number of the ticket your commit references. This will automatically post a comment to the appropriate ticket.
Write commit messages for backports using this pattern:
[<Django version>] Fixed <ticket> -- <description> Backport of <revision> from <branch>.
For example:
[1.3.x] Fixed #17028 -- Changed diveintopython.org -> diveintopython.net. Backport of 80c0cbf1c97047daed2c5b41b296bbc56fe1d7e3 from main.
There’s a script on the wiki to automate this.
If the commit fixes a regression, include this in the commit message:
Regression in 6ecccad711b52f9273b1acb07a57d3f806e93928.
(use the commit hash where the regression was introduced).
Reverting commits¶
Nobody’s perfect; mistakes will be committed.
But try very hard to ensure that mistakes don’t happen. Just because we have a reversion policy doesn’t relax your responsibility to aim for the highest quality possible. Really: double-check your work, or have it checked by another merger before you commit it in the first place!
When a mistaken commit is discovered, please follow these guidelines:
If possible, have the original author revert their own commit.
Don’t revert another author’s changes without permission from the original author.
Use git revert – this will make a reverse commit, but the original commit will still be part of the commit history.
If the original author can’t be reached (within a reasonable amount of time – a day or so) and the problem is severe – crashing bug, major test failures, etc. – then ask for objections on the Django Forum then revert if there are none.
If the problem is small (a feature commit after feature freeze, say), wait it out.
If there’s a disagreement between the merger and the reverter-to-be then try to work it out on the Django Forum . If an agreement can’t be reached then it should be put to a vote.
If the commit introduced a confirmed, disclosed security vulnerability then the commit may be reverted immediately without permission from anyone.
The release branch maintainer may back out commits to the release branch without permission if the commit breaks the release branch.
If you mistakenly push a topic branch to
django/django
, delete it. For instance, if you did:git push upstream feature_antigravity
, do a reverse push:git push upstream :feature_antigravity
.