1*4bcd95a3SBarry Smith(ch_developingmr)= 2*4bcd95a3SBarry Smith 3*4bcd95a3SBarry Smith# Developing a Merge Request 4*4bcd95a3SBarry Smith 5*4bcd95a3SBarry Smith(sec_integration_branches)= 6*4bcd95a3SBarry Smith 7*4bcd95a3SBarry Smith## Select the integration branch 8*4bcd95a3SBarry Smith 9*4bcd95a3SBarry Smith**Integration branches** are permanent branches in a repository that developers can contribute to. PETSc has two integration branches: `release` 10*4bcd95a3SBarry Smithand `main`. **Feature branches** are temporary branches created by developers to add or change a feature. A new feature branch is the basis for each 11*4bcd95a3SBarry Smithmerge request. 12*4bcd95a3SBarry Smith 13*4bcd95a3SBarry Smith(sec_release_branch)= 14*4bcd95a3SBarry Smith 15*4bcd95a3SBarry Smith### `release` 16*4bcd95a3SBarry Smith 17*4bcd95a3SBarry SmithThe `release` branch contains the latest PETSc release including bug-fixes. 18*4bcd95a3SBarry Smith 19*4bcd95a3SBarry SmithBug-fixes, along with most documentation fixes, should start from `release`. 20*4bcd95a3SBarry Smith 21*4bcd95a3SBarry Smith```console 22*4bcd95a3SBarry Smith$ git fetch 23*4bcd95a3SBarry Smith$ git checkout -b yourname/fix-component-name origin/release 24*4bcd95a3SBarry Smith``` 25*4bcd95a3SBarry Smith 26*4bcd95a3SBarry SmithBug-fix updates, about every month, (e.g. 3.17.1) are tagged on `release` (e.g. v3.17.1). 27*4bcd95a3SBarry Smith 28*4bcd95a3SBarry Smith(sec_main_branch)= 29*4bcd95a3SBarry Smith 30*4bcd95a3SBarry Smith### `main` 31*4bcd95a3SBarry Smith 32*4bcd95a3SBarry SmithThe `main` branch contains everything in the release branch as well as new features that have passed all testing 33*4bcd95a3SBarry Smithand will be in the next release (e.g. version 3.18). Users developing software based 34*4bcd95a3SBarry Smithon recently-added features in PETSc should follow `main`. 35*4bcd95a3SBarry Smith 36*4bcd95a3SBarry SmithNew features should start from `main`. 37*4bcd95a3SBarry Smith 38*4bcd95a3SBarry Smith```console 39*4bcd95a3SBarry Smith$ git fetch 40*4bcd95a3SBarry Smith$ git checkout -b yourname/fix-component-name origin/main 41*4bcd95a3SBarry Smith``` 42*4bcd95a3SBarry Smith 43*4bcd95a3SBarry Smith(sec_developing_a_new_feature)= 44*4bcd95a3SBarry Smith 45*4bcd95a3SBarry Smith## Start a new feature branch 46*4bcd95a3SBarry Smith 47*4bcd95a3SBarry Smith- Determine the appropriate integration_branch to start from, `main` or `release` (for documentation and bug fixes only). 48*4bcd95a3SBarry Smith 49*4bcd95a3SBarry Smith- Create and switch to a new feature branch: 50*4bcd95a3SBarry Smith 51*4bcd95a3SBarry Smith ```console 52*4bcd95a3SBarry Smith $ git fetch 53*4bcd95a3SBarry Smith $ git checkout -b <loginname>/<affected-package>-<short-description> origin/main # or origin/release 54*4bcd95a3SBarry Smith ``` 55*4bcd95a3SBarry Smith 56*4bcd95a3SBarry Smith For example, Barry’s new feature branch on removing CPP in `snes/` will 57*4bcd95a3SBarry Smith use 58*4bcd95a3SBarry Smith 59*4bcd95a3SBarry Smith ```console 60*4bcd95a3SBarry Smith $ git checkout -b barry/snes-removecpp origin/main 61*4bcd95a3SBarry Smith ``` 62*4bcd95a3SBarry Smith 63*4bcd95a3SBarry Smith Use all lowercase and no additional underscores in the branch name. 64*4bcd95a3SBarry Smith 65*4bcd95a3SBarry Smith## Develop your code 66*4bcd95a3SBarry Smith 67*4bcd95a3SBarry Smith- Write code and tests. 68*4bcd95a3SBarry Smith 69*4bcd95a3SBarry Smith- For any new features or API changes you introduced add information on them to `doc/changes/dev.rst`. 70*4bcd95a3SBarry Smith 71*4bcd95a3SBarry Smith- Inspect changes and stage code using standard Git commands, e.g. 72*4bcd95a3SBarry Smith 73*4bcd95a3SBarry Smith ```console 74*4bcd95a3SBarry Smith $ git status 75*4bcd95a3SBarry Smith $ git add file1 file2 76*4bcd95a3SBarry Smith $ git commit 77*4bcd95a3SBarry Smith ``` 78*4bcd95a3SBarry Smith 79*4bcd95a3SBarry Smith- Commit code with good commit message, for example 80*4bcd95a3SBarry Smith 81*4bcd95a3SBarry Smith ```console 82*4bcd95a3SBarry Smith $ git commit 83*4bcd95a3SBarry Smith ``` 84*4bcd95a3SBarry Smith 85*4bcd95a3SBarry Smith ```none 86*4bcd95a3SBarry Smith ComponentName: one-line explanation of commit 87*4bcd95a3SBarry Smith 88*4bcd95a3SBarry Smith After a blank line, write a more detailed explanation of the commit. Many tools do not auto-wrap this part, so wrap paragraph text at a reasonable length. Commit messages are meant for other people to read, possibly months or years later, so describe the rationale for the change in a manner that will make sense later, and which will be provide helpful search terms. 89*4bcd95a3SBarry Smith 90*4bcd95a3SBarry Smith Use the imperative, e.g. "Fix bug", not "Fixed bug". 91*4bcd95a3SBarry Smith 92*4bcd95a3SBarry Smith If any interfaces have changed, the commit should fix occurrences in PETSc itself and the message should state its impact on users. 93*4bcd95a3SBarry Smith 94*4bcd95a3SBarry Smith We have defined several standard commit message tags you should use; this makes it easy to search for specific types of contributions. Multiple tags may be used in the same commit message. 95*4bcd95a3SBarry Smith 96*4bcd95a3SBarry Smith /spend 1h or 30m 97*4bcd95a3SBarry Smith 98*4bcd95a3SBarry Smith If other people contributed significantly to a commit, perhaps by reporting bugs or by writing an initial version of the patch, acknowledge them using tags at the end of the commit message. 99*4bcd95a3SBarry Smith 100*4bcd95a3SBarry Smith Reported-by: Helpful User <helpful@example.com> 101*4bcd95a3SBarry Smith Based-on-patch-by: Original Idea <original@example.com> 102*4bcd95a3SBarry Smith Thanks-to: Incremental Improver <improver@example.com> 103*4bcd95a3SBarry Smith 104*4bcd95a3SBarry Smith If work is done for a particular well defined funding source or project you should label the commit with one or more of the tags 105*4bcd95a3SBarry Smith 106*4bcd95a3SBarry Smith Funded-by: My funding source 107*4bcd95a3SBarry Smith Project: My project name 108*4bcd95a3SBarry Smith ``` 109*4bcd95a3SBarry Smith 110*4bcd95a3SBarry Smith- Push the feature branch to the remote repository as desired: 111*4bcd95a3SBarry Smith 112*4bcd95a3SBarry Smith ```console 113*4bcd95a3SBarry Smith % git push -u origin barry/snes-removecpp 114*4bcd95a3SBarry Smith ``` 115*4bcd95a3SBarry Smith 116*4bcd95a3SBarry Smith## Test your branch 117*4bcd95a3SBarry Smith 118*4bcd95a3SBarry Smith- Include {doc}`tests </developers/testing>` which cover any changes to the source code. 119*4bcd95a3SBarry Smith 120*4bcd95a3SBarry Smith- {any}`Run the full test suite <sec_runningtests>` on your machine. 121*4bcd95a3SBarry Smith 122*4bcd95a3SBarry Smith ```console 123*4bcd95a3SBarry Smith $ make alltests TIMEOUT=600 124*4bcd95a3SBarry Smith ``` 125*4bcd95a3SBarry Smith 126*4bcd95a3SBarry Smith- Run the source checkers on your machine. 127*4bcd95a3SBarry Smith 128*4bcd95a3SBarry Smith ```console 129*4bcd95a3SBarry Smith $ make checkbadSource 130*4bcd95a3SBarry Smith $ make clangformat 131*4bcd95a3SBarry Smith $ make lint 132*4bcd95a3SBarry Smith ``` 133*4bcd95a3SBarry Smith 134*4bcd95a3SBarry Smith(sec_clean_commit_history)= 135*4bcd95a3SBarry Smith 136*4bcd95a3SBarry Smith## Maintain a clean commit history 137*4bcd95a3SBarry Smith 138*4bcd95a3SBarry SmithIf your contribution can be logically decomposed into 2 or more 139*4bcd95a3SBarry Smithseparate contributions, submit them in sequence with different 140*4bcd95a3SBarry Smithbranches and merge requests instead of all at once. 141*4bcd95a3SBarry Smith 142*4bcd95a3SBarry SmithOften a branch's commit history does not present a logical series of changes. 143*4bcd95a3SBarry SmithExtra commits from bug-fixes or tiny improvements may accumulate. One commit may contain multiple orthogonal changes. 144*4bcd95a3SBarry SmithThe order of changes may be incorrect. Branches without a clean commit history will often break `git bisect`. 145*4bcd95a3SBarry SmithIdeally, each commit in an MR will pass the PETSc CI testing, while presenting a small-as-possible set of very closely related changes. 146*4bcd95a3SBarry Smith 147*4bcd95a3SBarry SmithUse different commits for: 148*4bcd95a3SBarry Smith 149*4bcd95a3SBarry Smith- fixing formatting and spelling mistakes, 150*4bcd95a3SBarry Smith- fixing a bug, 151*4bcd95a3SBarry Smith- adding a new feature, 152*4bcd95a3SBarry Smith- adding another new feature. 153*4bcd95a3SBarry Smith 154*4bcd95a3SBarry SmithRewriting history can be done in [several ways](https://git-scm.com/book/en/v2/Git-Tools-Rewriting-History); the easiest is often with the interactive `rebase` command, which allows one to combine ("squash"), rearrange, and edit commits. 155*4bcd95a3SBarry Smith 156*4bcd95a3SBarry SmithIt is better to clean up your commits regularly than to wait until you have a large number of them. 157*4bcd95a3SBarry Smith 158*4bcd95a3SBarry SmithFor example, if you have made three commits and the most recent two are fixes for the first, you could use 159*4bcd95a3SBarry Smith 160*4bcd95a3SBarry Smith```console 161*4bcd95a3SBarry Smith$ git rebase -i HEAD~3 162*4bcd95a3SBarry Smith``` 163*4bcd95a3SBarry Smith 164*4bcd95a3SBarry SmithIf the branch has already been pushed, the rewritten branch is not compatible with the remote copy of the branch. You must force push your changes with 165*4bcd95a3SBarry Smith 166*4bcd95a3SBarry Smith```console 167*4bcd95a3SBarry Smith$ git push -f origin branch-name 168*4bcd95a3SBarry Smith``` 169*4bcd95a3SBarry Smith 170*4bcd95a3SBarry Smithto update the remote branch with your copy. This must be done with extreme care and only if you know someone else has not changed the remote copy of the branch, 171*4bcd95a3SBarry Smithotherwise you will lose those changes. Never do a `git pull` immediately after you rebase since that will merge the old branch (from GitLab) into your local one and create a mess [^block-ugly-pull-merge]. 172*4bcd95a3SBarry Smith 173*4bcd95a3SBarry SmithYou can use `git log` to see the recent changes to your branch and help determine what commits should be rearranged, combined, or split. 174*4bcd95a3SBarry SmithYou may also find it helpful to use an additional tool such as 175*4bcd95a3SBarry Smith[git-gui](https://git-scm.com/docs/git-gui/), [lazygit](https://github.com/jesseduffield/lazygit), or [various GUI tools](https://git-scm.com/downloads/guis). 176*4bcd95a3SBarry Smith 177*4bcd95a3SBarry Smith(sec_rebasing)= 178*4bcd95a3SBarry Smith 179*4bcd95a3SBarry Smith## Rebase your branch against the integration branch 180*4bcd95a3SBarry Smith 181*4bcd95a3SBarry SmithYou may also need to occasionally [rebase](https://git-scm.com/book/en/v2/Git-Branching-Rebasing) your branch onto to the latest version of your {any}`integration branch <sec_integration_branches>` [^rebase-not-merge-upstream], if the integration branch has had relevant changes since you started working on your feature branch. 182*4bcd95a3SBarry Smith 183*4bcd95a3SBarry Smith```console 184*4bcd95a3SBarry Smith$ git fetch origin # assume origin --> PETSc upstream 185*4bcd95a3SBarry Smith$ git checkout myname/component-feature 186*4bcd95a3SBarry Smith$ git branch myname/component-feature-backup-1 # optional 187*4bcd95a3SBarry Smith$ git rebase origin/main # or origin/release 188*4bcd95a3SBarry Smith``` 189*4bcd95a3SBarry Smith 190*4bcd95a3SBarry SmithNote that this type of rebasing is different than the `rebase -i` process for organizing your commits in a coherent manner. 191*4bcd95a3SBarry Smith 192*4bcd95a3SBarry Smith```{rubric} Footnotes 193*4bcd95a3SBarry Smith``` 194*4bcd95a3SBarry Smith 195*4bcd95a3SBarry Smith[^rebase-not-merge-upstream]: Rebasing is generally preferable to [merging an upstream branch](http://yarchive.net/comp/linux/git_merges_from_upstream.html). 196*4bcd95a3SBarry Smith 197*4bcd95a3SBarry Smith[^block-ugly-pull-merge]: You may wish to [make it impossible to perform these usually-undesired "non fast-forward" merges when pulling](https://git-scm.com/docs/git-config#Documentation/git-config.txt-pullff), with `git config --global pull.ff only`. 198