|
| 1 | +# Contribution workflow and the review process |
| 2 | + |
| 3 | +This document outlines the best practice, using git and CI, which **must** be followed |
| 4 | +for all contributions to `aiida-mlip`. Also contained are instructions and tips for |
| 5 | +managing your fork of the project, which will help keep merges clean and avoid many |
| 6 | +headaches. |
| 7 | + |
| 8 | +Please read our |
| 9 | +[developer documentation](https://stfc.github.io/aiida-mlip/developer_guide) |
| 10 | +for more technical details relating to contributions. |
| 11 | + |
| 12 | +## Golden rules |
| 13 | + |
| 14 | +In brief the rules for contribution are as follows: |
| 15 | + |
| 16 | +- Follow the branch, fix, merge model, from your own fork or fork/branch model |
| 17 | +- An issue should be created for every piece of work |
| 18 | +- Pull/merge requests will not be accepted without a review |
| 19 | +- New features must have a test |
| 20 | +- All tests must pass, no regressions may be merged |
| 21 | + |
| 22 | +## Issues |
| 23 | + |
| 24 | +### Using issues |
| 25 | + |
| 26 | +- Open an issue for each piece of work done, using the appropriate issue template |
| 27 | +- Open issues for design discussions. The answers may be important for newer members |
| 28 | +- When adding features, use comments to provide succinct reports on progress |
| 29 | + |
| 30 | +### Labels |
| 31 | + |
| 32 | +Labels may be assigned to issues to help classify them. Examples include: |
| 33 | + |
| 34 | +- `bug`: something is not working as expected. We typically aim to fix these ASAP |
| 35 | +- `enhancement`: adding or improving on features |
| 36 | +- `testing`: adding or enhancing unit testing or CI |
| 37 | +- `documentation`: adding or enhancing documentation |
| 38 | +- `question`: Something is unclear |
| 39 | + |
| 40 | +## Pull requests |
| 41 | + |
| 42 | +### Creating pull requests |
| 43 | + |
| 44 | +- Each pull request should aim to resolve an open issue |
| 45 | +- Relevant questions and discussions should be noted in the pull request, even if |
| 46 | + discussed elsewhere, as these may be useful for reference or other developers |
| 47 | +- Creating draft pull requests as early as possible is encouraged, to track progress, |
| 48 | + and so early feedback can be given |
| 49 | + |
| 50 | +### Labels |
| 51 | + |
| 52 | +As with issues, labels may be assigned to issues to help classify them. |
| 53 | + |
| 54 | +## Review |
| 55 | + |
| 56 | +All merge requests will be reviewed to ensure the integrity of the code. |
| 57 | + |
| 58 | +The reviewer(s) have the following responsibilities: |
| 59 | + |
| 60 | +- Ensuring all contribution rules have been followed |
| 61 | +- Ensuring the [coding style](./coding_style.md) is adhered to |
| 62 | +- Only accepting a merge if all tests have passed |
| 63 | +- Using the comments system to request changes for the submitter to make |
| 64 | + |
| 65 | +### Enforcing style |
| 66 | + |
| 67 | +GitHub Actions will automatically run a pre-commit and enforce the coding style. To |
| 68 | +reduce the number of CI failures, please run the pre-commit locally before you push to |
| 69 | +your repository. |
| 70 | + |
| 71 | +```sh |
| 72 | + pre-commit run --all-files |
| 73 | +``` |
| 74 | + |
| 75 | +## Using git for development |
| 76 | + |
| 77 | +The GitHub instance hosts an *upstream* repository, which we will refer to as |
| 78 | +*upstream*. Contributors will work on their own personal copies of the repository by |
| 79 | +creating *forks*. This allows us to keep *upstream* clean, while users may work on |
| 80 | +their own *fork*, creating commits and changing the code as they see fit. Only in |
| 81 | +exceptional circumstances are branches allowed in *upstream*. |
| 82 | + |
| 83 | +The *upstream* repository may be cloned as follows, |
| 84 | + |
| 85 | +``` sh |
| 86 | +git clone [email protected]:stfc/aiida-mlip.git |
| 87 | +``` |
| 88 | + |
| 89 | +A *fork* is created using the web UI. It may then be cloned for a user called |
| 90 | +'username' as follows: |
| 91 | + |
| 92 | +``` sh |
| 93 | +git clone [email protected]:username/aiida-mlip.git aiida-mlip-username |
| 94 | +``` |
| 95 | + |
| 96 | +or added as an alternative origin in the *upstream* cloned repository: |
| 97 | + |
| 98 | +``` sh |
| 99 | +git remote add username [email protected]:username/aiida-mlip.git |
| 100 | +``` |
| 101 | + |
| 102 | +### Branch, fix, merge model: |
| 103 | + |
| 104 | +All work should follow the workflow of branch, fix, merge. Let us assume you have an |
| 105 | +issue with your code which needs to be fixed. |
| 106 | + |
| 107 | +#### Step 1: Branch from your fork |
| 108 | + |
| 109 | +Create a new branch for the issue on the dashboard of your fork, we will assume the |
| 110 | +branch is called 'fix-xyz'. Clone the branch, |
| 111 | + |
| 112 | +``` sh |
| 113 | +$ git clone -b fix-xyz --single-branch [email protected]:username/aiida-mlip.git aiida-mlip-fix-xyz |
| 114 | +``` |
| 115 | + |
| 116 | +Alternatively you can create the branch in the CLI using |
| 117 | + |
| 118 | +``` sh |
| 119 | +# clone the repository, if you already have a local repository this is not nessecary |
| 120 | +$ git clone [email protected]:username/aiida-mlip.git aiida-mlip-fix-xyz |
| 121 | +$ pushd aiida-mlip-fix-xyz |
| 122 | +# create and checkout a new branch (this is equivalent to git branch followed by git checkout) |
| 123 | +$ git checkout -b fix-xyz |
| 124 | +# create a remote tracking branch for you to push your changes to |
| 125 | +$ git push -u origin fix-xyz |
| 126 | +``` |
| 127 | + |
| 128 | +#### Step 2: Fix the issue and commit your changes |
| 129 | + |
| 130 | +Fix whatever is wrong. Use `git status` to see which files you have |
| 131 | +changed and prepare a commit. |
| 132 | + |
| 133 | +``` sh |
| 134 | +# stage changes |
| 135 | +$ git add <filename|folder> # to add the new things |
| 136 | +# commit the changes with a clear and brief message |
| 137 | +$ git commit -m "<commit message>" |
| 138 | +# push the commit to origin |
| 139 | +$ git push |
| 140 | +``` |
| 141 | + |
| 142 | +#### Step 3a: Merge your branch into upstream |
| 143 | + |
| 144 | +Follow the provided link after the push and continue to the web interface. Add any |
| 145 | +relevant labels or milestones and assign a reviewer. Compare the code and if you are |
| 146 | +happy click to submit your pull request. |
| 147 | + |
| 148 | +After the pull request has been submitted, tests will be run and your reviewer will be |
| 149 | +notified. |
| 150 | + |
| 151 | +#### Step 3b: Finalising the merge |
| 152 | + |
| 153 | +If all is OK with the commit your reviewer may set the request to be merged once all |
| 154 | +tests pass. Otherwise, the reviewer may open discussions using the GitHub comment |
| 155 | +system to point out issues that may need to be addressed before the commit can be |
| 156 | +merged. |
| 157 | + |
| 158 | +If changes need to be made you may make more commits onto your branch. When you push |
| 159 | +your branch to your *fork* the pull request will be automatically updated to use the |
| 160 | +latest commit. Reply to the discussions to indicate when and how they have been |
| 161 | +addressed. |
| 162 | + |
| 163 | +If your branch has become out of sync with *upstream* then conflicts may arise. |
| 164 | +Sometimes these cannot be automatically resolved and you will need to resolve them by |
| 165 | +hand. GitHub provides instructions for this, or you can follow this routine, |
| 166 | + |
| 167 | +``` sh |
| 168 | +# add upstream as a remote if you have not already |
| 169 | +$ git remote add upstream [email protected]:stfc/aiida-mlip.git |
| 170 | +# get the changes to upstream since you started working on your issue |
| 171 | +$ git fetch upstream |
| 172 | +# merge these changes into your branch (assuming you want to merge into the main branch on upstream) |
| 173 | +$ git merge upstream/main |
| 174 | +# resolve any conflicts |
| 175 | +# push to your fork |
| 176 | +$ git push |
| 177 | +``` |
| 178 | + |
| 179 | +Alternatively you may use rebase which will replay the changes you made in your branch |
| 180 | +on top of *upstream/main*. However, be sure you understand the differences between |
| 181 | +merge and rebase. |
| 182 | + |
| 183 | +``` sh |
| 184 | +# add upstream as a remote if you have not already |
| 185 | +$ git remote add upstream [email protected]:stfc/aiida-mlip.git |
| 186 | +# get the changes to upstream since you started working on your issue |
| 187 | +$ git fetch upstream |
| 188 | +# merge these changes into your branch (assuming you want to merge into the main branch on upstream) |
| 189 | +$ git rebase -i upstream/main |
| 190 | +# resolve any conflicts |
| 191 | +# push to your fork |
| 192 | +$ git push |
| 193 | +``` |
| 194 | + |
| 195 | + |
| 196 | +### Advanced git |
| 197 | + |
| 198 | +#### Keeping your fork in sync with project |
| 199 | + |
| 200 | +By adding two remotes, one for *upstream* and one for your *fork* it is possible to |
| 201 | +keep your *fork* in sync with *upstream*. This will greatly simplify merge requests. |
| 202 | +GitHub also offers a sync functionality in their web UI that achieves the same. |
| 203 | + |
| 204 | +``` sh |
| 205 | +# clone your fork |
| 206 | +$ git clone [email protected]:username/aiida-mlip.git aiida-mlip-username |
| 207 | +pushd aiida-mlip-username |
| 208 | +# add a remote for upstream |
| 209 | +$ git remote add upstream [email protected]:stfc/aiida-mlip.git |
| 210 | +``` |
| 211 | + |
| 212 | +These commands need to be done only once. `git remote -v` shall show you |
| 213 | +the origin and project fetch and push links |
| 214 | + |
| 215 | +``` sh |
| 216 | +$ git remote -v |
| 217 | +origin [email protected]:username/aiida-mlip.git (fetch) |
| 218 | +origin [email protected]:username/aiida-mlip.git (push) |
| 219 | +upstream [email protected]:stfc/aiida-mlip.git (fetch) |
| 220 | +upstream [email protected]:stfc/aiida-mlip.git (push) |
| 221 | +``` |
| 222 | + |
| 223 | +When you need to sync your *fork* with *upstream*, do the following, |
| 224 | + |
| 225 | +``` sh |
| 226 | +# get the latest commits from upstream |
| 227 | +$ git fetch upstream |
| 228 | +# ensure you are in the main branch of your fork |
| 229 | +$ git checkout main |
| 230 | +# merge your main branch into the main branch of upstream |
| 231 | +$ git merge upstream/main |
| 232 | +# push these changes back to the remote of your fork (origin) |
| 233 | +$ git push |
| 234 | +``` |
| 235 | + |
| 236 | +Of course, one can use a similar process to merge any other branch or available |
| 237 | +projects. |
| 238 | + |
| 239 | +#### Rebasing commits |
| 240 | + |
| 241 | +When working on an issue you may use multiple commits. When you are ready to create a |
| 242 | +merge request, you should squash your changes into one commit in order to keep |
| 243 | +*upstream* clean. This is most easily achieved with an interactive rebase. |
| 244 | + |
| 245 | +Assuming you have made five commits, |
| 246 | + |
| 247 | +``` sh |
| 248 | +# rebase your branch five commits before HEAD i.e. where your branch originally diverged |
| 249 | +$ git rebase -i HEAD~5 |
| 250 | +# follow the instructions. 'pick' the first commit then 'sqaush' or 'fixup' the rest. |
| 251 | +# You should now be left with a single commit containing all your changes |
| 252 | +# Push your commmit to the remote, use --force-with-lease if you have already pushed this branch to |
| 253 | +# 'rewrite history' |
| 254 | +$ git push origin branchname --force-with-lease |
| 255 | +``` |
| 256 | + |
| 257 | +Using force is a powerful and dangerous option, use it only if you know 150% nobody |
| 258 | +else touched that branch. |
| 259 | + |
| 260 | +#### Cleaning stale branches |
| 261 | + |
| 262 | +Deleting branches from the web interface will get rid of the remotes and not of your |
| 263 | +local copies. The local branches left behind are called stale branches. To get rid of |
| 264 | +them: |
| 265 | + |
| 266 | +``` sh |
| 267 | +$ git remote prune origin |
| 268 | +``` |
| 269 | + |
| 270 | +To delete a local branch: |
| 271 | + |
| 272 | +``` sh |
| 273 | +$ git branch -d localBranch |
| 274 | +``` |
| 275 | + |
| 276 | +If unmerged commits exists, but you still want to delete the branch, use: |
| 277 | + |
| 278 | +``` sh |
| 279 | +$ git branch -D localBranch |
| 280 | +``` |
| 281 | + |
| 282 | +To delete a remote branch on the remote *origin* use: |
| 283 | + |
| 284 | +``` sh |
| 285 | +$ git push -d origin remoteBranch |
| 286 | +``` |
| 287 | + |
| 288 | +## Code Coverage |
| 289 | + |
| 290 | +Ensure that any code you add does not reduce the code coverage in a meaningful way. |
| 291 | +Reviewers may insist on new test to be added. Please cooperate. |
0 commit comments