development.rst 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. .. include:: global.rst.inc
  2. .. highlight:: bash
  3. .. _development:
  4. Development
  5. ===========
  6. This chapter will get you started with Borg development.
  7. Borg is written in Python (with a little bit of Cython and C for
  8. the performance critical parts).
  9. Contributions
  10. -------------
  11. ... are welcome!
  12. Some guidance for contributors:
  13. - Discuss changes on the GitHub issue tracker, on IRC or on the mailing list.
  14. - Make your PRs on the ``master`` branch (see `Branching Model`_ for details and exceptions).
  15. - Do clean changesets:
  16. - Focus on some topic, resist changing anything else.
  17. - Do not do style changes mixed with functional changes.
  18. - Try to avoid refactorings mixed with functional changes.
  19. - If you need to fix something after commit/push:
  20. - If there are ongoing reviews: do a fixup commit you can
  21. squash into the bad commit later.
  22. - If there are no ongoing reviews or you did not push the
  23. bad commit yet: amend the commit to include your fix or
  24. merge the fixup commit before pushing.
  25. - Have a nice, clear, typo-free commit comment.
  26. - If you fixed an issue, refer to it in your commit comment.
  27. - Follow the style guide (see below).
  28. - If you write new code, please add tests and docs for it.
  29. - Run the tests, fix any issues that come up.
  30. - Make a pull request on GitHub.
  31. - Wait for review by other developers.
  32. Branching model
  33. ---------------
  34. Borg development happens on the ``master`` branch and uses GitHub pull
  35. requests (if you don't have GitHub or don't want to use it you can
  36. send smaller patches via the borgbackup mailing list to the maintainers).
  37. Stable releases are maintained on maintenance branches named ``x.y-maint``, eg.
  38. the maintenance branch of the 1.4.x series is ``1.4-maint``.
  39. Most PRs should be filed against the ``master`` branch. Only if an
  40. issue affects **only** a particular maintenance branch a PR should be
  41. filed against it directly.
  42. While discussing / reviewing a PR it will be decided whether the
  43. change should be applied to maintenance branches. Each maintenance
  44. branch has a corresponding *backport/x.y-maint* label, which will then
  45. be applied.
  46. Changes that are typically considered for backporting:
  47. - Data loss, corruption and inaccessibility fixes.
  48. - Security fixes.
  49. - Forward-compatibility improvements.
  50. - Documentation corrections.
  51. .. rubric:: Maintainer part
  52. From time to time a maintainer will backport the changes for a
  53. maintenance branch, typically before a release or if enough changes
  54. were collected:
  55. 1. Notify others that you're doing this to avoid duplicate work.
  56. 2. Branch a backporting branch off the maintenance branch.
  57. 3. Cherry pick and backport the changes from each labelled PR, remove
  58. the label for each PR you've backported.
  59. To preserve authorship metadata, do not follow the ``git cherry-pick``
  60. instructions to use ``git commit`` after resolving conflicts. Instead,
  61. stage conflict resolutions and run ``git cherry-pick --continue``,
  62. much like using ``git rebase``.
  63. To avoid merge issues (a cherry pick is a form of merge), use
  64. these options (similar to the ``git merge`` options used previously,
  65. the ``-x`` option adds a reference to the original commit)::
  66. git cherry-pick --strategy recursive -X rename-threshold=5% -x
  67. 4. Make a PR of the backporting branch against the maintenance branch
  68. for backport review. Mention the backported PRs in this PR, e.g.:
  69. Includes changes from #2055 #2057 #2381
  70. This way GitHub will automatically show in these PRs where they
  71. were backported.
  72. .. rubric:: Historic model
  73. Previously (until release 1.0.10) Borg used a `"merge upwards"
  74. <https://git-scm.com/docs/gitworkflows#_merging_upwards>`_ model where
  75. most minor changes and fixes where committed to a maintenance branch
  76. (eg. 1.0-maint), and the maintenance branch(es) were regularly merged
  77. back into the main development branch. This became more and more
  78. troublesome due to merges growing more conflict-heavy and error-prone.
  79. How to submit a pull request
  80. ----------------------------
  81. In order to contribute to Borg, you will need to fork the ``borgbackup/borg``
  82. main repository to your own Github repository. Then clone your Github repository
  83. to your local machine. The instructions for forking and cloning a repository
  84. can be found there:
  85. `<https://docs.github.com/en/get-started/quickstart/fork-a-repo>`_ .
  86. Make sure you also fetched the git tags, because without them, ``setuptools-scm``
  87. will run into issues determining the correct borg version. Check if ``git tag``
  88. shows a lot of release tags (version numbers).
  89. If it does not, use ``git fetch --tags`` to fetch them.
  90. To work on your contribution, you first need to decide which branch your pull
  91. request should be against. Often, this might be master branch (esp. for big /
  92. risky contributions), but it could be also a maintenance branch like e.g.
  93. 1.4-maint (esp. for small fixes that should go into next maintenance release,
  94. e.g. 1.4.x).
  95. Start by checking out the appropriate branch:
  96. ::
  97. git checkout master
  98. It is best practice for a developer to keep local ``master`` branch as an
  99. uptodate copy of the upstream ``master`` branch and always do own work in a
  100. separate feature or bugfix branch.
  101. This is useful to be able to rebase own branches onto the upstream branches
  102. they were branched from, if necessary.
  103. This also applies to other upstream branches (like e.g. ``1.4-maint``), not
  104. only to ``master``.
  105. Thus, create a new branch now:
  106. ::
  107. git checkout -b MYCONTRIB-master # choose an appropriate own branch name
  108. Now, work on your contribution in that branch. Use these git commands:
  109. ::
  110. git status # is there anything that needs to be added?
  111. git add ... # if so, add it
  112. git commit # finally, commit it. use a descriptive comment.
  113. Then push the changes to your Github repository:
  114. ::
  115. git push --set-upstream origin MYCONTRIB-master
  116. Finally, make a pull request on ``borgbackup/borg`` Github repository against
  117. the appropriate branch (e.g. ``master``) so that your changes can be reviewed.
  118. What to do if work was accidentally started in wrong branch
  119. -----------------------------------------------------------
  120. If you accidentally worked in ``master`` branch, check out the ``master``
  121. branch and make sure there are no uncommitted changes. Then, create a feature
  122. branch from that, so that your contribution is in a feature branch.
  123. ::
  124. git checkout master
  125. git checkout -b MYCONTRIB-master
  126. Next, check out the ``master`` branch again. Find the commit hash of the last
  127. commit that was made before you started working on your contribution and perform
  128. a hard reset.
  129. ::
  130. git checkout master
  131. git log
  132. git reset --hard THATHASH
  133. Then, update the local ``master`` branch with changes made in the upstream
  134. repository.
  135. ::
  136. git pull borg master
  137. Rebase feature branch onto updated master branch
  138. ------------------------------------------------
  139. After updating the local ``master`` branch from upstream, the feature branch
  140. can be checked out and rebased onto (the now uptodate) ``master`` branch.
  141. ::
  142. git checkout MYCONTRIB-master
  143. git rebase -i master
  144. Next, check if there are any commits that exist in the feature branch
  145. but not in the ``master`` branch and vice versa. If there are no
  146. conflicts or after resolving them, push your changes to your Github repository.
  147. ::
  148. git log
  149. git diff master
  150. git push -f
  151. Code and issues
  152. ---------------
  153. Code is stored on GitHub, in the `Borgbackup organization
  154. <https://github.com/borgbackup/borg/>`_. `Issues
  155. <https://github.com/borgbackup/borg/issues>`_ and `pull requests
  156. <https://github.com/borgbackup/borg/pulls>`_ should be sent there as
  157. well. See also the :ref:`support` section for more details.
  158. Style guide / Automated Code Formatting
  159. ---------------------------------------
  160. We use `black`_ for automatically formatting the code.
  161. If you work on the code, it is recommended that you run black **before each commit**
  162. (so that new code is always using the desired formatting and no additional commits
  163. are required to fix the formatting).
  164. ::
  165. pip install -r requirements.d/codestyle.txt # everybody use same black version
  166. black --check . # only check, don't change
  167. black . # reformat the code
  168. The CI workflows will check the code formatting and will fail if it is not formatted correctly.
  169. When (mass-)reformatting existing code, we need to avoid ruining `git blame`, so please
  170. follow their `guide about avoiding ruining git blame`_:
  171. .. _black: https://black.readthedocs.io/
  172. .. _guide about avoiding ruining git blame: https://black.readthedocs.io/en/stable/guides/introducing_black_to_your_project.html#avoiding-ruining-git-blame
  173. Continuous Integration
  174. ----------------------
  175. All pull requests go through `GitHub Actions`_, which runs the tests on misc.
  176. Python versions and on misc. platforms as well as some additional checks.
  177. .. _GitHub Actions: https://github.com/borgbackup/borg/actions
  178. Output and Logging
  179. ------------------
  180. When writing logger calls, always use correct log level (debug only for
  181. debugging, info for informative messages, warning for warnings, error for
  182. errors, critical for critical errors/states).
  183. When directly talking to the user (e.g. Y/N questions), do not use logging,
  184. but directly output to stderr (not: stdout, it could be connected to a pipe).
  185. To control the amount and kinds of messages output emitted at info level, use
  186. flags like ``--stats`` or ``--list``, then create a topic logger for messages
  187. controlled by that flag. See ``_setup_implied_logging()`` in
  188. ``borg/archiver.py`` for the entry point to topic logging.
  189. Building a development environment
  190. ----------------------------------
  191. First, just install borg into a virtual env :ref:`as described before <git-installation>`.
  192. To install some additional packages needed for running the tests, activate your
  193. virtual env and run::
  194. pip install -r requirements.d/development.txt
  195. This project utilizes pre-commit to format and lint code before it is committed.
  196. Although pre-commit is installed when running the command above, the pre-commit hooks
  197. will have to be installed separately. Run this command to install the pre-commit hooks::
  198. pre-commit install
  199. Running the tests
  200. -----------------
  201. The tests are in the borg/testsuite package.
  202. To run all the tests, you need to have fakeroot installed. If you do not have
  203. fakeroot, you still will be able to run most tests, just leave away the
  204. ``fakeroot -u`` from the given command lines.
  205. To run the test suite use the following command::
  206. fakeroot -u tox # run all tests
  207. Some more advanced examples::
  208. # verify a changed tox.ini (run this after any change to tox.ini):
  209. fakeroot -u tox --recreate
  210. fakeroot -u tox -e py313 # run all tests, but only on python 3.13
  211. fakeroot -u tox borg.testsuite.locking # only run 1 test module
  212. fakeroot -u tox borg.testsuite.locking -- -k '"not Timer"' # exclude some tests
  213. fakeroot -u tox borg.testsuite -- -v # verbose py.test
  214. Important notes:
  215. - When using ``--`` to give options to py.test, you MUST also give ``borg.testsuite[.module]``.
  216. Running the tests (using the pypi package)
  217. ------------------------------------------
  218. Since borg 1.4, it is also possible to run the tests without a development
  219. environment, using the borgbackup dist package (downloaded from pypi.org or
  220. github releases page):
  221. ::
  222. # optional: create and use a virtual env:
  223. python3 -m venv env
  224. . env/bin/activate
  225. # install packages
  226. pip install borgbackup
  227. pip install pytest pytest-benchmark
  228. # run the tests
  229. pytest -v -rs --benchmark-skip --pyargs borg.testsuite
  230. Adding a compression algorithm
  231. ------------------------------
  232. If you want to add a new compression algorithm, please refer to :issue:`1633`
  233. and leave a post there in order to discuss about the proposal.
  234. Documentation
  235. -------------
  236. Generated files
  237. ~~~~~~~~~~~~~~~
  238. Usage documentation (found in ``docs/usage/``) and man pages
  239. (``docs/man/``) are generated automatically from the command line
  240. parsers declared in the program and their documentation, which is
  241. embedded in the program (see archiver.py). These are committed to git
  242. for easier use by packagers downstream.
  243. When a command is added, a command line flag changed, added or removed,
  244. the usage docs need to be rebuilt as well::
  245. python scripts/make.py build_usage
  246. python scripts/make.py build_man
  247. However, we prefer to do this as part of our :ref:`releasing`
  248. preparations, so it is generally not necessary to update these when
  249. submitting patches that change something about the command line.
  250. Building the docs with Sphinx
  251. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  252. The documentation (in reStructuredText format, .rst) is in docs/.
  253. To build the html version of it, you need to have Sphinx installed
  254. (in your Borg virtualenv with Python 3)::
  255. pip install -r requirements.d/docs.txt
  256. Now run::
  257. cd docs/
  258. make html
  259. Then point a web browser at docs/_build/html/index.html.
  260. The website is updated automatically by ReadTheDocs through GitHub web hooks on the
  261. main repository.
  262. Using Vagrant
  263. -------------
  264. We use Vagrant for the automated creation of testing environments and borgbackup
  265. standalone binaries for various platforms.
  266. For better security, there is no automatic sync in the VM to host direction.
  267. The plugin `vagrant-scp` is useful to copy stuff from the VMs to the host.
  268. The "windows10" box requires the `reload` plugin (``vagrant plugin install vagrant-reload``).
  269. Usage::
  270. # To create and provision the VM:
  271. vagrant up OS
  272. # same, but use 6 VM cpus and 12 workers for pytest:
  273. VMCPUS=6 XDISTN=12 vagrant up OS
  274. # To create an ssh session to the VM:
  275. vagrant ssh OS
  276. # To execute a command via ssh in the VM:
  277. vagrant ssh OS -c "command args"
  278. # To shut down the VM:
  279. vagrant halt OS
  280. # To shut down and destroy the VM:
  281. vagrant destroy OS
  282. # To copy files from the VM (in this case, the generated binary):
  283. vagrant scp OS:/vagrant/borg/borg.exe .
  284. Creating standalone binaries
  285. ----------------------------
  286. Make sure you have everything built and installed (including fuse stuff).
  287. When using the Vagrant VMs, pyinstaller will already be installed.
  288. With virtual env activated::
  289. pip install pyinstaller # or git checkout master
  290. pyinstaller -F -n borg-PLATFORM borg/__main__.py
  291. for file in dist/borg-*; do gpg --armor --detach-sign $file; done
  292. If you encounter issues, see also our `Vagrantfile` for details.
  293. .. note:: Standalone binaries built with pyinstaller are supposed to
  294. work on same OS, same architecture (x86 32bit, amd64 64bit)
  295. without external dependencies.
  296. .. _releasing:
  297. Creating a new release
  298. ----------------------
  299. Checklist:
  300. - Make sure all issues for this milestone are closed or moved to the
  301. next milestone.
  302. - Check if there are any pending fixes for security issues.
  303. - Find and fix any low hanging fruit left on the issue tracker.
  304. - Check that GitHub Actions CI is happy.
  305. - Update ``CHANGES.rst``, based on ``git log $PREVIOUS_RELEASE..``.
  306. - Check version number of upcoming release in ``CHANGES.rst``.
  307. - Render ``CHANGES.rst`` via ``make html`` and check for markup errors.
  308. - Verify that ``MANIFEST.in``, ``pyproject.toml`` and ``setup.py`` are complete.
  309. - Run these commands and commit::
  310. python scripts/make.py build_usage
  311. python scripts/make.py build_man
  312. - Tag the release::
  313. git tag -s -m "tagged/signed release X.Y.Z" X.Y.Z
  314. - Create a clean repo and use it for the following steps::
  315. git clone borg borg-clean
  316. This makes sure no uncommitted files get into the release archive.
  317. It will also reveal uncommitted required files.
  318. Moreover, it makes sure the vagrant machines only get committed files and
  319. do a fresh start based on that.
  320. - Run tox and/or binary builds on all supported platforms via vagrant,
  321. check for test failures.
  322. - Create sdist, sign it, upload release to (test) PyPi:
  323. ::
  324. scripts/sdist-sign X.Y.Z
  325. scripts/upload-pypi X.Y.Z test
  326. scripts/upload-pypi X.Y.Z
  327. Note: the signature is not uploaded to PyPi any more, but we upload it to
  328. github releases.
  329. - Put binaries into dist/borg-OSNAME and sign them:
  330. ::
  331. scripts/sign-binaries 201912312359
  332. - Close the release milestone on GitHub.
  333. - `Update borgbackup.org
  334. <https://github.com/borgbackup/borgbackup.github.io/pull/53/files>`_ with the
  335. new version number and release date.
  336. - Announce on:
  337. - Mailing list.
  338. - Mastodon / BlueSky / X (aka Twitter).
  339. - IRC channel (change ``/topic``).
  340. - Create a GitHub release, include:
  341. - pypi dist package and signature
  342. - Standalone binaries (see above for how to create them).
  343. - For macOS, document the macFUSE version in the README of the binaries.
  344. macFUSE uses a kernel extension that needs to be compatible with the
  345. code contained in the binary.
  346. - A link to ``CHANGES.rst``.