@@ -281,3 +281,123 @@ Additional tips:
281281 separate ``west global `` command since ``global `` already searches for the
282282 ``GTAGS `` file starting from your current working directory. This is why you
283283 need to run ``global `` from inside the workspace.
284+
285+ .. _west-patch :
286+
287+ Working with patches: ``west patch ``
288+ ************************************
289+
290+ The ``patch `` command allows users to apply patches to Zephyr or Zephyr modules
291+ in a controlled manner that makes automation and tracking easier for external applications that
292+ use the :ref: `T2 star topology <west-t2 >`. The :ref: `patches.yml <patches-yml >` file stores
293+ metadata about patch files and fills-in the gaps between official Zephyr releases, so that users
294+ can easily see the status of any upstreaming efforts, and determine which patches to drop before
295+ upgrading to the next Zephyr release.
296+
297+ There are several sub-commands available to manage patches for Zephyr or other modules in the
298+ workspace:
299+
300+ * ``apply ``: apply patches listed in ``patches.yml ``
301+ * ``clean ``: remove all patches that have been applied, and reset to the manifest checkout state
302+ * ``list ``: list all patches in ``patches.yml ``
303+ * ``gh-fetch ``: fetch patches from a GitHub pull request
304+
305+ .. code-block :: none
306+
307+ west-workspace/
308+ └── application/
309+ ...
310+ ├── west.yml
311+ └── zephyr
312+ ├── module.yml
313+ ├── patches
314+ │ ├── bootloader
315+ │ │ └── mcuboot
316+ │ │ └── my-tweak-for-mcuboot.patch
317+ │ └── zephyr
318+ │ └── my-zephyr-change.patch
319+ └── patches.yml
320+
321+ In this example, the :ref: `west manifest <west-manifests >` file, ``west.yml ``, would pin to a
322+ specific Zephyr revision (e.g. ``v4.1.0 ``) and apply patches against that revision of Zephyr and
323+ the specific revisions of other modules used in the application. However, this application needs
324+ two changes in order to meet requirements; one for Zephyr and another for MCUBoot.
325+
326+ .. _patches-yml :
327+
328+ .. code-block :: yaml
329+
330+ patches :
331+ - path : zephyr/my-zephyr-change.patch
332+ sha256sum : c676cd376a4d19dc95ac4e44e179c253853d422b758688a583bb55c3c9137035
333+ module : zephyr
334+ author : Obi-Wan Kenobi
335+ 336+ date : 2025-05-04
337+ upstreamable : false
338+ comments : |
339+ An application-specific change we need for Zephyr.
340+ - path : bootloader/mcuboot/my-tweak-for-mcuboot.patch
341+ sha256sum : e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
342+ module : mcuboot
343+ author : Darth Sidious
344+ 345+ date : 2025-05-04
346+ merge-pr : https://github.com/zephyrproject-rtos/zephyr/pull/<pr-number>
347+ issue : https://github.com/zephyrproject-rtos/zephyr/issues/<issue-number>
348+ merge-status : true
349+ merge-commit : 1234567890abcdef1234567890abcdef12345678
350+ merge-date : 2025-05-06
351+ apply-command : git apply
352+ comments : |
353+ A change to mcuboot that has been merged already. We can remove this
354+ patch when we are ready to upgrade to the next Zephyr release.
355+
356+ Patches can easily be applied in an automated manner. For example:
357+
358+ .. code-block :: bash
359+
360+ west init -m < manifest repo> < workspace>
361+ cd < workspace>
362+ west update
363+ west patch apply
364+
365+ When it is time to update to a newer version of Zephyr, the ``west.yml `` file can be updated to
366+ point at the next Zephyr release, e.g. ``v4.2.0 ``. Patches that are no longer needed, like
367+ ``my-tweak-for-mcuboot.patch `` in the example above, can be removed from ``patches.yml `` and from
368+ the external application repository, and then the following commands can be run.
369+
370+ .. code-block :: bash
371+
372+ west patch clean
373+ west update
374+ west patch apply --roll-back # roll-back all patches if one does not apply cleanly
375+
376+ If a patch needs to be reworked, remember to update the ``patches.yml `` file with the new SHA256
377+ checksum.
378+
379+ .. code-block :: bash
380+
381+ sha256sum zephyr/patches/zephyr/my-zephyr-change.patch
382+ 7d57ca78d5214f422172cc47fed9d0faa6d97a0796c02485bff0bf29455765e9
383+
384+ It is also possible to use ``west patch gh-fetch `` to fetch patches from a GitHub pull request and
385+ automatically create or update the ``patches.yml `` file. This can be useful when the author already
386+ has a number of changes captured in existing upstream pull requests.
387+
388+ .. code-block :: bash
389+
390+ west patch gh-fetch --owner zephyrproject-rtos --repo zephyr --pull-request < pr-number> \
391+ --module zephyr --split-commits
392+
393+ The above command will create the directory and file structure below, which includes patches for
394+ each individual commit associated with the given pull request.
395+
396+ .. code-block :: none
397+
398+ zephyr
399+ ├── patches
400+ │ ├── first-commit-from-pr.patch
401+ │ ├── second-commit-from-pr.patch
402+ │ └── third-commit-from-pr.patch
403+ └── patches.yml
0 commit comments