1.. _github-reviews: 2 3====================== 4LLVM GitHub User Guide 5====================== 6 7Introduction 8============ 9The LLVM Project uses `GitHub <https://github.com/>`_ for 10`Source Code <https://github.com/llvm/llvm-project>`_, 11`Releases <https://github.com/llvm/llvm-project/releases>`_, 12`Issue Tracking <https://github.com/llvm/llvm-project/issues>`_., and 13`Code Reviews <https://github.com/llvm/llvm-project/pulls>`_. 14 15This page describes how the LLVM Project users and developers can 16participate in the project using GitHub. 17 18Branches 19======== 20 21It is possible to create branches that starts with `users/<username>/`, however this is 22intended to be able to support "stacked" pull-request. Do not create any branches in the 23llvm/llvm-project repository otherwise, please use a fork (see below). User branches that 24aren't associated with a pull-request **will be deleted**. 25 26Using Graphite for stacked Pull Requests 27======================================== 28 29`Graphite <https://app.graphite.dev/>`_ is a stacked pull request tool supported 30by the LLVM repo (the other being `reviewable.io <https://reviewable.io>`_). 31 32Graphite will want to create branches under ``llvm/llvm-project`` rather than your 33private fork, so the guidance above, about branch naming, is critical, otherwise 34``gt submit`` (i.e. publish your PRs for review) will fail. 35 36Use ``gt config`` then ``Branch naming settings`` and ``Set a prefix for branch names``. 37Include the last ``/``. 38 39If you didn't do the above and Graphite created non-prefixed branches, a simple way to 40unblock is to rename (``git -m <old name> <new name>``), and then checkout the branch 41and ``gt track``. 42 43Pull Requests 44============= 45The LLVM project is using GitHub Pull Requests for Code Reviews. This document 46describes the typical workflow of creating a Pull Request and getting it reviewed 47and accepted. This is meant as an overview of the GitHub workflow, for complete 48documentation refer to `GitHub's documentation <https://docs.github.com/pull-requests>`_. 49 50.. note:: 51 If you are using a Pull Request for purposes other than review 52 (eg: precommit CI results, convenient web-based reverts, etc) 53 add the `skip-precommit-approval <https://github.com/llvm/llvm-project/labels?q=skip-precommit-approval>`_ 54 label to the PR. 55 56GitHub Tools 57------------ 58You can interact with GitHub in several ways: via git command line tools, 59the web browser, `GitHub Desktop <https://desktop.github.com/>`_, or the 60`GitHub CLI <https://cli.github.com>`_. This guide will cover the git command line 61tools and the GitHub CLI. The GitHub CLI (`gh`) will be most like the `arc` workflow and 62recommended. 63 64Creating Pull Requests 65---------------------- 66Keep in mind that when creating a pull request, it should generally only contain one 67self-contained commit initially. 68This makes it easier for reviewers to understand the introduced changes and 69provide feedback. It also helps maintain a clear and organized commit history 70for the project. If you have multiple changes you want to introduce, it's 71recommended to create separate pull requests for each change. 72 73Create a local branch per commit you want to submit and then push that branch 74to your `fork <https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/working-with-forks>`_ 75of the llvm-project and 76`create a pull request from the fork <https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/creating-a-pull-request-from-a-fork>`_. 77As GitHub uses the first line of the commit message truncated to 72 characters 78as the pull request title, you may have to edit to reword or to undo this 79truncation. 80 81Creating Pull Requests with GitHub CLI 82^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 83With the CLI it's enough to create the branch locally and then run: 84 85:: 86 87 gh pr create 88 89When prompted select to create and use your own fork and follow 90the instructions to add more information needed. 91 92.. note:: 93 94 When you let the GitHub CLI create a fork of llvm-project to 95 your user, it will change the git "remotes" so that "origin" points 96 to your fork and "upstream" points to the main llvm-project repository. 97 98Updating Pull Requests 99---------------------- 100In order to update your pull request, the only thing you need to do is to push 101your new commits to the branch in your fork. That will automatically update 102the pull request. 103 104When updating a pull request, you should push additional "fix up" commits to 105your branch instead of force pushing. This makes it easier for GitHub to 106track the context of previous review comments. Consider using the 107`built-in support for fixups <https://git-scm.com/docs/git-commit#Documentation/git-commit.txt---fixupamendrewordltcommitgt>`_ 108in git. 109 110If you do this, you must squash and merge before landing the PR and 111you must use the pull request title and description as the commit message. 112You can do this manually with an interactive git rebase or with GitHub's 113built-in tool. See the section about landing your fix below. 114 115When pushing to your branch, make sure you push to the correct fork. Check your 116remotes with: 117 118:: 119 120 git remote -v 121 122And make sure you push to the remote that's pointing to your fork. 123 124Rebasing Pull Requests and Force Pushes 125--------------------------------------- 126In general, you should avoid rebasing a Pull Request and force pushing to the 127branch that's the root of the Pull Request during the review. This action will 128make the context of the old changes and comments harder to find and read. 129 130Sometimes, a rebase might be needed to update your branch with a fix for a test 131or in some dependent code. 132 133After your PR is reviewed and accepted, you want to rebase your branch to ensure 134you won't encounter merge conflicts when landing the PR. 135 136.. note:: 137 This guide assumes that the PR branch only has 1 author. If you are 138 collaborating with others on a single branch, be careful how and when you push 139 changes. ``--force-with-lease`` may be useful in this situation. 140 141Approvals 142--------- 143 144Before merging a PR you must have the required approvals. See 145:ref:`lgtm_how_a_patch_is_accepted` for more details. 146 147Landing your change 148------------------- 149 150When your PR has been approved you can merge your changes. 151 152If you do not have write permissions for the repository, the merge button in 153GitHub's web interface will be disabled. If this is the case, continue following 154the steps here but ask one of your reviewers to click the merge button on your 155behalf. 156 157If the PR is a single commit, all you need to do is click the merge button in 158GitHub's web interface. 159 160If your PR contains multiple commits, you need to consolidate those commits into 161one commit. There are three different ways to do this, shown here with the most 162commonly used first: 163 164* Use the button `Squash and merge` in GitHub's web interface, if you do this 165 remember to review the commit message when prompted. 166 167 Afterwards you can select the option `Delete branch` to delete the branch 168 from your fork. 169 170* `Interactive rebase <https://git-scm.com/docs/git-rebase#_interactive_mode>`_ 171 with fixups. This is the recommended method since you can control the final 172 commit message and check that the final commit looks as you expect. When 173 your local state is correct, remember to force-push to your branch and press 174 the merge button in GitHub's web interface afterwards. 175 176* Merge using the GitHub command line interface. Switch to your branch locally 177 and run: 178 179 :: 180 181 gh pr merge --squash --delete-branch 182 183 If you observe an error message from the above informing you that your pull 184 request is not mergeable, then that is likely because upstream has been 185 modified since your pull request was authored in a way that now results in a 186 merge conflict. You must first resolve this merge conflict in order to merge 187 your pull request. In order to do that: 188 189 :: 190 191 git fetch upstream 192 git rebase upstream/main 193 194 Then fix the source files causing merge conflicts and make sure to rebuild and 195 retest the result. Then: 196 197 :: 198 199 git add <files with resolved merge conflicts> 200 git rebase --continue 201 202 Finally, you'll need to force push to your branch one more time before you can 203 merge: 204 205 :: 206 207 git push --force 208 gh pr merge --squash --delete-branch 209 210 This force push may ask if you intend to push hundreds, or potentially 211 thousands of patches (depending on how long it's been since your pull request 212 was initially authored vs. when you intended to merge it). Since you're pushing 213 to a branch in your fork, this is ok and expected. Github's UI for the pull 214 request will understand that you're rebasing just your patches, and display 215 this result correctly with a note that a force push did occur. 216 217 218Pre-merge Continuous Integration (CI) 219------------------------------------- 220 221Multiple checks will be applied on a pull-request, either for linting/formatting 222or some build and tests. None of these are perfect and you will encounter 223false positive, infrastructure failures (unstable or unavailable worker), or 224you will be unlucky and based your change on a broken revision of the main branch. 225 226None of the checks are strictly mandatory: these are tools to help us build a 227better codebase and be more productive (by avoiding issues found post-merge and 228possible reverts). As a developer you're empowered to exercise your judgement 229about bypassing any of the checks when merging code. 230 231The infrastructure can print messages that make it seem like these are mandatory, 232but this is just an artifact of GitHub infrastructure and not a policy of the 233project. 234 235However, please make sure you do not force-merge any changes that have clear 236test failures directly linked to your changes. Our policy is still to keep the 237``main`` branch in a good condition, and introducing failures to be fixed later 238violates that policy. 239 240Problems After Landing Your Change 241================================== 242 243Even though your PR passed the pre-commit checks and is approved by reviewers, it 244may cause problems for some configurations after it lands. You will be notified 245if this happens and the community is ready to help you fix the problems. 246 247This process is described in detail 248:ref:`here <MyFirstTypoFix Issues After Landing Your PR>`. 249 250 251Checking out another PR locally 252------------------------------- 253Sometimes you want to review another person's PR on your local machine to run 254tests or inspect code in your preferred editor. This is easily done with the 255CLI: 256 257:: 258 259 gh pr checkout <PR Number> 260 261This is also possible with the web interface and the normal git command line 262tools, but the process is a bit more complicated. See GitHub's 263`documentation <https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/reviewing-changes-in-pull-requests/checking-out-pull-requests-locally?platform=linux&tool=webui#modifying-an-inactive-pull-request-locally>`_ 264on the topic. 265 266Example Pull Request with GitHub CLI 267==================================== 268Here is an example for creating a Pull Request with the GitHub CLI: 269 270:: 271 272 # Clone the repo 273 gh repo clone llvm/llvm-project 274 275 # Switch to the repo and create a new branch 276 cd llvm-project 277 git switch -c my_change 278 279 # Create your changes 280 $EDITOR file.cpp 281 282 # Don't forget clang-format 283 git clang-format 284 285 # and don't forget running your tests 286 ninja check-llvm 287 288 # Commit, use a good commit message 289 git commit file.cpp 290 291 # Create the PR, select to use your own fork when prompted. 292 # If you don't have a fork, gh will create one for you. 293 gh pr create 294 295 # If you get any review comments, come back to the branch and 296 # adjust them. 297 git switch my_change 298 $EDITOR file.cpp 299 300 # Commit your changes 301 git commit file.cpp -m "Code Review adjustments" 302 303 # Format changes 304 git clang-format HEAD~ 305 306 # Recommit if any formatting changes 307 git commit -a --amend 308 309 # Push your changes to your fork branch, be mindful of 310 # your remotes here, if you don't remember what points to your 311 # fork, use git remote -v to see. Usually origin points to your 312 # fork and upstream to llvm/llvm-project 313 git push origin my_change 314 315Before merging the PR, it is recommended that you rebase locally and re-run test 316checks: 317 318:: 319 320 # Add upstream as a remote (if you don't have it already) 321 git remote add upstream https://github.com/llvm/llvm-project.git 322 323 # Make sure you have all the latest changes 324 git fetch upstream && git rebase -i upstream/main 325 326 # Make sure tests pass with latest changes and your change 327 ninja check 328 329 # Push the rebased changes to your fork. 330 git push origin my_change --force 331 332 # Now merge it 333 gh pr merge --squash --delete-branch 334 335 336See more in-depth information about how to contribute in the following documentation: 337 338* :doc:`Contributing` 339* :doc:`MyFirstTypoFix` 340 341Example Pull Request with git 342==================================== 343 344Instead of using the GitHub CLI to create a PR, you can push your code to a 345remote branch on your fork and create the PR to upstream using the GitHub web 346interface. 347 348Here is an example of making a PR using git and the GitHub web interface: 349 350First follow the instructions to [fork the repository](https://docs.github.com/en/get-started/quickstart/fork-a-repo?tool=webui#forking-a-repository). 351 352Next follow the instructions to [clone your forked repository](https://docs.github.com/en/get-started/quickstart/fork-a-repo?tool=webui#cloning-your-forked-repository). 353 354Once you've cloned your forked repository, 355 356:: 357 358 # Switch to the forked repo 359 cd llvm-project 360 361 # Create a new branch 362 git switch -c my_change 363 364 # Create your changes 365 $EDITOR file.cpp 366 367 # Don't forget clang-format 368 git clang-format 369 370 # and don't forget running your tests 371 ninja check-llvm 372 373 # Commit, use a good commit message 374 git commit file.cpp 375 376 # Push your changes to your fork branch, be mindful of 377 # your remotes here, if you don't remember what points to your 378 # fork, use git remote -v to see. Usually origin points to your 379 # fork and upstream to llvm/llvm-project 380 git push origin my_change 381 382Navigate to the URL printed to the console from the git push command in the last step. 383Create a pull request from your branch to llvm::main. 384 385:: 386 387 # If you get any review comments, come back to the branch and 388 # adjust them. 389 git switch my_change 390 $EDITOR file.cpp 391 392 # Commit your changes 393 git commit file.cpp -m "Code Review adjustments" 394 395 # Format changes 396 git clang-format HEAD~ 397 398 # Recommit if any formatting changes 399 git commit -a --amend 400 401 # Re-run tests and make sure nothing broke. 402 ninja check 403 404 # Push your changes to your fork branch, be mindful of 405 # your remotes here, if you don't remember what points to your 406 # fork, use git remote -v to see. Usually origin points to your 407 # fork and upstream to llvm/llvm-project 408 git push origin my_change 409 410Before merging the PR, it is recommended that you rebase locally and re-run test 411checks: 412 413:: 414 415 # Add upstream as a remote (if you don't have it already) 416 git remote add upstream https://github.com/llvm/llvm-project.git 417 418 # Make sure you have all the latest changes 419 git fetch upstream && git rebase -i upstream/main 420 421 # Make sure tests pass with latest changes and your change 422 ninja check 423 424 # Push the rebased changes to your fork. 425 git push origin my_change --force 426 427Once your PR is approved, rebased, and tests are passing, click `Squash and 428Merge` on your PR in the GitHub web interface. 429 430See more in-depth information about how to contribute in the following documentation: 431 432* :doc:`Contributing` 433* :doc:`MyFirstTypoFix` 434 435Releases 436======== 437 438Backporting Fixes to the Release Branches 439----------------------------------------- 440You can use special comments on issues or pull requests to make backport 441requests for the release branches. This is done by making a comment containing 442the following command on any issue or pull request that has been added to one 443of the "X.Y.Z Release" milestones. 444 445:: 446 447 /cherry-pick <commit> <commit> <...> 448 449This command takes one or more git commit hashes as arguments and will attempt 450to cherry-pick the commit(s) to the release branch. If the commit(s) fail to 451apply cleanly, then a comment with a link to the failing job will be added to 452the issue/pull request. If the commit(s) do apply cleanly, then a pull request 453will be created with the specified commits. 454 455If a commit you want to backport does not apply cleanly, you may resolve 456the conflicts locally and then create a pull request against the release 457branch. Just make sure to add the release milestone to the pull request. 458 459Getting admin access to CI infrastructure 460========================================= 461 462Any individual who is responsible for setting up and/or maintaining CI infrastructure for a LLVM project can 463request to be granted the CI/CD role to the LLVM organization admins. The request can be made by creating 464`a Github issue <https://github.com/llvm/llvm-project/issues/new>`_ and using the ``infrastructure`` label. 465Applicants must include a justification for why the role is being requested. Applications are reviewed on a 466case-by-case basis by the LLVM admins and the role can be revoked at any point as the LLVM admins see fit. 467