pr_workflow.rst 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  1. .. _doc_pr_workflow:
  2. Pull request workflow
  3. =====================
  4. .. highlight:: shell
  5. The so-called "PR workflow" used by Godot is common to many projects using
  6. Git, and should be familiar to veteran free software contributors. The idea
  7. is that only a small number (if any) commit directly to the *master* branch.
  8. Instead, contributors *fork* the project (i.e. create a copy of it, which
  9. they can modify as they wish), and then use the GitHub interface to request
  10. a *pull* from one of their fork's branches to one branch of the original
  11. (often named *upstream*) repository.
  12. The resulting *pull request* (PR) can then be reviewed by other contributors,
  13. which might approve it, reject it, or most often request that modifications
  14. be done. Once approved, the PR can then be merged by one of the core
  15. developers, and its commit(s) will become part of the target branch (usually
  16. the *master* branch).
  17. We will go together through an example to show the typical workflow and
  18. associated Git commands. But first, let's have a quick look at the
  19. organization of Godot's Git repository.
  20. Git source repository
  21. ---------------------
  22. The `repository on GitHub <https://github.com/godotengine/godot>`_ is a
  23. `Git <https://git-scm.com>`_ code repository together with an embedded
  24. issue tracker and PR system.
  25. .. note:: If you are contributing to the documentation, its repository can
  26. be found `here <https://github.com/godotengine/godot-docs>`_.
  27. The Git version control system is the tool used to keep track of successive
  28. edits to the source code - to contribute efficiently to Godot, learning the
  29. basics of the Git command line is *highly* recommended. There exist some
  30. graphical interfaces for Git, but they usually encourage users to take bad
  31. habits regarding the Git and PR workflow, and we therefore recommend not to
  32. use them. In particular, we advise not to use GitHub's online editor for code
  33. contributions (although it's tolerated for small fixes or documentation changes)
  34. as it enforces one commit per file and per modification,
  35. which quickly leads to PRs with an unreadable Git history (especially after peer review).
  36. .. seealso:: The first sections of Git's "Book" are a good introduction to
  37. the tool's philosophy and the various commands you need to
  38. master in your daily workflow. You can read them online on the
  39. `Git SCM <https://git-scm.com/book/en/v2>`_ website.
  40. The branches on the Git repository are organized as follows:
  41. - The ``master`` branch is where the development of the next major version
  42. occurs. As a development branch, it can be unstable
  43. and is not meant for use in production. This is where PRs should be done
  44. in priority.
  45. - The stable branches are named after their version, e.g. ``3.1`` and ``2.1``.
  46. They are used to backport bugfixes and enhancements from the ``master``
  47. branch to the currently maintained stable release (e.g. 3.1.2 or 2.1.6).
  48. As a rule of thumb, the last stable branch is maintained until the next
  49. major version (e.g. the ``3.0`` branch was maintained until the release of
  50. Godot 3.1).
  51. If you want to make PRs against a maintained stable branch, please check
  52. first if your changes are also relevant for the ``master`` branch, and if so
  53. make the PR for the ``master`` branch in priority. Release managers can then
  54. cherry-pick the fix to a stable branch if relevant.
  55. - There might occasionally be feature branches, usually meant to be merged into
  56. the ``master`` branch at some time.
  57. Forking and cloning
  58. -------------------
  59. The first step is to *fork* the `godotengine/godot <https://github.com/godotengine/godot>`_
  60. repository on GitHub. To do so, you will need to have a GitHub account and to
  61. be logged in. In the top right corner of the repository's GitHub page, you
  62. should see the "Fork" button as shown below:
  63. .. image:: img/github_fork_button.png
  64. Click it, and after a while you should be redirected to your own fork of the
  65. Godot repo, with your GitHub username as namespace:
  66. .. image:: img/github_fork_url.png
  67. You can then *clone* your fork, i.e. create a local copy of the online
  68. repository (in Git speak, the *origin remote*). If you haven't already,
  69. download Git from `its website <https://git-scm.com>`_ if you're using Windows or
  70. macOS, or install it through your package manager if you're using Linux.
  71. .. note:: If you are on Windows, open Git Bash to type commands. macOS and Linux users
  72. can use their respective terminals.
  73. To clone your fork from GitHub, use the following command:
  74. ::
  75. $ git clone https://github.com/USERNAME/godot
  76. .. note:: In our examples, the "$" character denotes the command line prompt
  77. on typical UNIX shells. It is not part of the command and should
  78. not be typed.
  79. After a little while, you should have a ``godot`` directory in your current
  80. working directory. Move into it using the ``cd`` command:
  81. ::
  82. $ cd godot
  83. We will start by setting up a reference to the original repository that we forked:
  84. ::
  85. $ git remote add upstream https://github.com/godotengine/godot
  86. $ git fetch upstream
  87. This will create a reference named ``upstream`` pointing to the original
  88. ``godotengine/godot`` repository. This will be useful when you want to pull new
  89. commits from its ``master`` branch to update your fork. You have another
  90. remote reference named ``origin``, which points to your fork (``USERNAME/godot``).
  91. You only need to do the above steps once, as long as you keep that local
  92. ``godot`` folder (which you can move around if you want, the relevant
  93. metadata is hidden in its ``.git`` subfolder).
  94. .. note:: *Branch it, pull it, code it, stage it, commit, push it, rebase
  95. it... technologic.*
  96. This bad take on Daft Punk's *Technologic* shows the general
  97. conception Git beginners have of its workflow: lots of strange
  98. commands to learn by copy and paste, hoping they will work as
  99. expected. And that's actually not a bad way to learn, as long as
  100. you're curious and don't hesitate to question your search engine
  101. when lost, so we will give you the basic commands to know when
  102. working in Git.
  103. In the following, we will assume as an example that you want to implement a feature in
  104. Godot's project manager, which is coded in the ``editor/project_manager.cpp``
  105. file.
  106. Branching
  107. ---------
  108. By default, the ``git clone`` should have put you on the ``master`` branch of
  109. your fork (``origin``). To start your own feature development, we will create
  110. a feature branch:
  111. ::
  112. # Create the branch based on the current branch (master)
  113. $ git branch better-project-manager
  114. # Change the current branch to the new one
  115. $ git checkout better-project-manager
  116. This command is equivalent:
  117. ::
  118. # Change the current branch to a new named one, based on the current branch
  119. $ git checkout -b better-project-manager
  120. If you want to go back to the ``master`` branch, you'd use:
  121. ::
  122. $ git checkout master
  123. You can see which branch you are currently on with the ``git branch``
  124. command:
  125. ::
  126. $ git branch
  127. 2.1
  128. * better-project-manager
  129. master
  130. Be sure to always go back to the ``master`` branch before creating a new branch,
  131. as your current branch will be used as the base for the new one. Alternatively,
  132. you can specify a custom base branch after the new branch's name:
  133. ::
  134. $ git checkout -b my-new-feature master
  135. Updating your branch
  136. --------------------
  137. This would not be needed the first time (just after you forked the upstream
  138. repository). However, the next time you want to work on something, you will
  139. notice that your fork's ``master`` is several commits behind the upstream
  140. ``master`` branch: pull requests from other contributors would have been merged
  141. in the meantime.
  142. To ensure there won't be conflicts between the feature you develop and the
  143. current upstream ``master`` branch, you will have to update your branch by
  144. *pulling* the upstream branch.
  145. ::
  146. $ git pull --rebase upstream master
  147. The ``--rebase`` argument will ensure that any local changes that you committed
  148. will be re-applied *on top* of the pulled branch, which is usually what we want
  149. in our PR workflow. This way, when you open a pull request, your own commits will
  150. be the only difference with the upstream ``master`` branch.
  151. While rebasing, conflicts may arise if your commits modified code that has been
  152. changed in the upstream branch in the meantime. If that happens, Git will stop at
  153. the conflicting commit and will ask you to resolve the conflicts. You can do so
  154. with any text editor, then stage the changes (more on that later), and proceed with
  155. ``git rebase --continue``. Repeat the operation if later commits have conflicts too,
  156. until the rebase operation completes.
  157. If you're unsure about what is going on during a rebase and you panic (no worry,
  158. we all do the first few times), you can abort the rebase with ``git rebase --abort``.
  159. You will then be back to the original state of your branch before calling
  160. ``git pull --rebase``.
  161. .. note:: If you omit the ``--rebase`` argument, you will instead create a merge
  162. commit which tells Git what to make of the two distinct branches. If any
  163. conflicts arise, they would be resolved all at once via this merge commit.
  164. While this is a valid workflow and the default behavior of ``git pull``,
  165. merge commits within PRs are frowned upon in our PR workflow. We only use
  166. them when merging PRs into the upstream branch.
  167. The philosophy is that a PR should represent the final stage of the changes
  168. made to the codebase, and we are not interested in mistakes and fixes that
  169. would have been done in intermediate stages before merging.
  170. Git gives us great tools to "rewrite the history" and make it as if we got
  171. things right the first time, and we're happy to use it to ensure that
  172. changes are easy to review and understand long after they have been merged.
  173. If you have already created a merge commit without using ``rebase``, or
  174. have made any other changes that have resulted in undesired history, the best option
  175. is to use an *interactive rebase* on the upstream branch. See the :ref:`dedicated
  176. section <doc_pr_workflow_rebase>` for instructions.
  177. .. tip:: If at any time you want to *reset* a local branch to a given commit or branch,
  178. you can do so with ``git reset --hard <commit ID>`` or
  179. ``git reset --hard <remote>/<branch>`` (e.g. ``git reset --hard upstream/master``).
  180. Be warned that this will remove any changes that you might have committed in
  181. this branch. If you ever lose commits by mistake, use the ``git reflog`` command
  182. to find the commit ID of the previous state that you would like to restore, and
  183. use it as argument of ``git reset --hard`` to go back to that state.
  184. Making changes
  185. --------------
  186. You would then do your changes to our example's
  187. ``editor/project_manager.cpp`` file with your usual development environment
  188. (text editor, IDE, etc.).
  189. By default, those changes are *unstaged*. The staging area is a layer between
  190. your working directory (where you make your modifications) and the local Git
  191. repository (the commits and all the metadata in the ``.git`` folder). To
  192. bring changes from the working directory to the Git repository, you need to
  193. *stage* them with the ``git add`` command, and then to commit them with the
  194. ``git commit`` command.
  195. There are various commands you should know to review your current work,
  196. before staging it, while it is staged, and after it has been committed.
  197. - ``git diff`` will show you the current unstaged changes, i.e. the
  198. differences between your working directory and the staging area.
  199. - ``git checkout -- <files>`` will undo the unstaged changes to the given
  200. files.
  201. - ``git add <files>`` will *stage* the changes on the listed files.
  202. - ``git diff --staged`` will show the current staged changes, i.e. the
  203. differences between the staging area and the last commit.
  204. - ``git reset HEAD <files>`` will *unstage* changes to the listed files.
  205. - ``git status`` will show you what are the currently staged and unstaged
  206. modifications.
  207. - ``git commit`` will commit the staged files. It will open a text editor
  208. (you can define the one you want to use with the ``GIT_EDITOR`` environment
  209. variable or the ``core.editor`` setting in your Git configuration) to let you
  210. write a commit log. You can use ``git commit -m "Cool commit log"`` to
  211. write the log directly.
  212. - ``git commit --amend`` lets you amend the last commit with your currently
  213. staged changes (added with ``git add``). This is the best option if you
  214. want to fix a mistake in the last commit (bug, typo, style issue, etc.).
  215. - ``git log`` will show you the last commits of your current branch. If you
  216. did local commits, they should be shown at the top.
  217. - ``git show`` will show you the changes of the last commit. You can also
  218. specify a commit hash to see the changes for that commit.
  219. That's a lot to memorize! Don't worry, just check this cheat sheet when you
  220. need to make changes, and learn by doing.
  221. Here's how the shell history could look like on our example:
  222. ::
  223. # It's nice to know where you're starting from
  224. $ git log
  225. # Do changes to the project manager with the nano text editor
  226. $ nano editor/project_manager.cpp
  227. # Find an unrelated bug in Control and fix it
  228. $ nano scene/gui/control.cpp
  229. # Review changes
  230. $ git status
  231. $ git diff
  232. # We'll do two commits for our unrelated changes,
  233. # starting by the Control changes necessary for the PM enhancements
  234. $ git add scene/gui/control.cpp
  235. $ git commit -m "Fix handling of margins in Control"
  236. # Check we did good
  237. $ git log
  238. $ git show
  239. $ git status
  240. # Make our second commit
  241. $ git add editor/project_manager.cpp
  242. $ git commit -m "Add a pretty banner to the project manager"
  243. $ git log
  244. With this, we should have two new commits in our ``better-project-manager``
  245. branch which were not in the ``master`` branch. They are still only local
  246. though, the remote fork does not know about them, nor does the upstream repo.
  247. Pushing changes to a remote
  248. ---------------------------
  249. That's where ``git push`` will come into play. In Git, a commit is always
  250. done in the local repository (unlike Subversion where a commit will modify
  251. the remote repository directly). You need to *push* the new commits to a
  252. remote branch to share them with the world. The syntax for this is:
  253. ::
  254. $ git push <remote> <local branch>[:<remote branch>]
  255. The part about the remote branch can be omitted if you want it to have the
  256. same name as the local branch, which is our case in this example, so we will
  257. do:
  258. ::
  259. $ git push origin better-project-manager
  260. Git will ask you for your username and password, and the changes will be sent
  261. to your remote. If you check the fork's page on GitHub, you should see a new
  262. branch with your added commits.
  263. Issuing a pull request
  264. ----------------------
  265. When you load your fork's branch on GitHub, you should see a line saying
  266. *"This branch is 2 commits ahead of godotengine:master."* (and potentially some
  267. commits behind, if your ``master`` branch was out of sync with the upstream
  268. ``master`` branch).
  269. .. image:: img/github_fork_make_pr.png
  270. On that line, there is a "Pull request" link. Clicking it will open a form
  271. that will let you issue a pull request on the ``godotengine/godot`` upstream
  272. repository. It should show you your two commits, and state "Able to merge".
  273. If not (e.g. it has way more commits, or says there are merge conflicts),
  274. don't create the PR, something went wrong. Go to IRC and ask for support :)
  275. Use an explicit title for the PR and put the necessary details in the comment
  276. area. You can drag and drop screenshots, GIFs or zipped projects if relevant,
  277. to showcase what your work implements. Click "Create a pull request", and
  278. tadaa!
  279. Modifying a pull request
  280. ------------------------
  281. While it is reviewed by other contributors, you will often need to make
  282. changes to your yet-unmerged PR, either because contributors requested them,
  283. or because you found issues yourself while testing.
  284. The good news is that you can modify a pull request simply by acting on the
  285. branch you made the pull request from. You can e.g. make a new commit on that
  286. branch, push it to your fork, and the PR will be updated automatically:
  287. ::
  288. # Check out your branch again if you had changed in the meantime
  289. $ git checkout better-project-manager
  290. # Fix a mistake
  291. $ nano editor/project_manager.cpp
  292. $ git add editor/project_manager.cpp
  293. $ git commit -m "Fix a typo in the banner's title"
  294. $ git push origin better-project-manager
  295. However, be aware that in our PR workflow, we favor commits that bring the
  296. codebase from one functional state to another functional state, without having
  297. intermediate commits fixing up bugs in your own code or style issues. Most of
  298. the time, we will prefer a single commit in a given PR (unless there's a good
  299. reason to keep the changes separate), so instead of authoring a new commit,
  300. considering using ``git commit --amend`` to amend the previous commit with your
  301. fixes. The above example would then become:
  302. ::
  303. # Check out your branch again if you had changed in the meantime
  304. $ git checkout better-project-manager
  305. # Fix a mistake
  306. $ nano editor/project_manager.cpp
  307. $ git add editor/project_manager.cpp
  308. # --amend will change the previous commit, so you will have the opportunity
  309. # to edit its commit message if relevant.
  310. $ git commit --amend
  311. # As we modified the last commit, it no longer matches the one from your
  312. # remote branch, so we need to force push to overwrite that branch.
  313. $ git push --force origin better-project-manager
  314. .. Kept for compatibility with the previous title, linked in many PRs.
  315. .. _mastering-the-pr-workflow-the-rebase:
  316. .. _doc_pr_workflow_rebase:
  317. The interactive rebase
  318. ----------------------
  319. If you didn't follow the above steps closely to *amend* changes into a commit
  320. instead of creating fixup commits, or if you authored your changes without being
  321. aware of our workflow and Git usage tips, reviewers might request of your to
  322. *rebase* your branch to *squash* some or all of the commits into one.
  323. Indeed, if some commits have been made following reviews to fix bugs, typos, etc.
  324. in the original commit, they are not relevant to a future changelog reader who
  325. would want to know what happened in the Godot codebase, or when and how a given
  326. file was last modified.
  327. To squash those extraneous commits into the main one, we will have to *rewrite
  328. history*. Right, we have that power. You may read that it's a bad practice, and
  329. it's true when it comes to branches of the upstream repo. But in your fork, you
  330. can do whatever you want, and everything is allowed to get neat PRs :)
  331. We will use the *interactive rebase* ``git rebase -i`` to do this. This
  332. command takes a commit ID or a branch name as argument, and will let you modify
  333. all commits between that commit/branch and the last one in your working branch,
  334. the so-called ``HEAD``.
  335. While you can give any commit ID to ``git rebase -i`` and review everything in
  336. between, the most common and convenient workflow involves rebasing on the
  337. *upstream ``master`` branch*, which you can do with:
  338. ::
  339. $ git rebase -i upstream/master
  340. .. note:: Referencing branches in Git is a bit tricky due to the distinction
  341. between remote and local branches. Here, ``upstream/master`` (with a
  342. `/`) is a local branch which has been pulled from the ``upstream``
  343. remote's ``master`` branch.
  344. Interactive rebases can only be done on local branches, so the `/`
  345. is important here. As the upstream remote changes frequently, your
  346. local ``upstream/master`` branch may become outdated, so you can
  347. update it with ``git fetch upstream master``. Contrarily to
  348. ``git pull --rebase upstream master`` which would update your
  349. currently checked out branch, ``fetch`` will only update the
  350. ``upstream/master`` reference (which is distinct from your local
  351. ``master`` branch... yes it's confusing, but you'll become familiar
  352. with this little by little).
  353. This will open a text editor (``vi`` by default, see
  354. `Git docs <https://git-scm.com/book/en/v2/Customizing-Git-Git-Configuration#_core_editor>__`
  355. to configure your favorite one) with something which may look like this:
  356. .. code-block:: text
  357. pick 1b4aad7 Add a pretty banner to the project manager
  358. pick e07077e Fix a typo in the banner's title
  359. The editor will also show instructions regarding how you can act on those
  360. commits. In particular, it should tell you that "pick" means to use that
  361. commit (do nothing), and that "squash" and "fixup" can be used to *meld* the
  362. commit in its parent commit. The difference between "squash" and "fixup" is
  363. that "fixup" will discard the commit log from the squashed commit. In our
  364. example, we are not interested in keeping the log of the "Fix a typo" commit,
  365. so we use:
  366. .. code-block:: text
  367. pick 1b4aad7 Add a pretty banner to the project manager
  368. fixup e07077e Fix a typo in the banner's title
  369. Upon saving and quitting the editor, the rebase will occur. The second commit
  370. will be melded into the first one, and ``git log`` and ``git show`` should
  371. now confirm that you have only one commit with the changes from both previous
  372. commits.
  373. But! You rewrote the history, and now your local and remote branches have
  374. diverged. Indeed, commit 1b4aad7 in the above example will have changed, and
  375. therefore got a new commit hash. If you try to push to your remote branch, it
  376. will raise an error:
  377. ::
  378. $ git push origin better-project-manager
  379. To https://github.com/akien-mga/godot
  380. ! [rejected] better-project-manager -> better-project-manager (non-fast-forward)
  381. error: failed to push some refs to 'https://akien-mga@github.com/akien-mga/godot'
  382. hint: Updates were rejected because the tip of your current branch is behind
  383. hint: its remote counterpart.
  384. This is a sane behavior, Git will not let you push changes that would
  385. override remote content. But that's actually what we want to do here, so we
  386. will have to *force* it:
  387. ::
  388. $ git push --force origin better-project-manager
  389. And tadaa! Git will happily *replace* your remote branch with what you had
  390. locally (so make sure that's what you wanted, using ``git log``). This will
  391. also update the PR accordingly.
  392. Deleting a Git branch
  393. ---------------------
  394. After your pull request gets merged, there's one last thing you should do: delete your
  395. Git branch for the PR. There won't be issues if you don't delete your branch, but it's
  396. good practice to do so. You'll need to do this twice, once for the local branch and another
  397. for the remote branch on GitHub.
  398. To delete our better project manager branch locally, use this command:
  399. ::
  400. $ git branch -d better-project-manager
  401. Alternatively, if the branch hadn't been merged yet and we wanted to delete it anyway, instead
  402. of ``-d`` you would use ``-D``.
  403. Next, to delete the remote branch on GitHub use this command:
  404. ::
  405. $ git push origin -d better-project-manager
  406. You can also delete the remote branch from the GitHub PR itself, a button should appear once
  407. it has been merged or closed.