1.. _west-workspaces:
2
3Workspaces
4##########
5
6This page describes the *west workspace* concept introduced in
7:ref:`west-basics` in more detail.
8
9.. _west-manifest-rev:
10
11The ``manifest-rev`` branch
12***************************
13
14West creates and controls a Git branch named ``manifest-rev`` in each
15project. This branch points to the revision that the manifest file
16specified for the project at the time :ref:`west-update` was last run.
17Other workspace management commands may use ``manifest-rev`` as a reference
18point for the upstream revision as of this latest update. Among other
19purposes, the ``manifest-rev`` branch allows the manifest file to use SHAs
20as project revisions.
21
22Although ``manifest-rev`` is a normal Git branch, west will recreate and/or
23reset it on the next update. For this reason, it is **dangerous**
24to check it out or otherwise modify it yourself. For instance, any commits
25you manually add to this branch may be lost the next time you run ``west
26update``. Instead, check out a local branch with another name, and either
27rebase it on top of a new ``manifest-rev``, or merge ``manifest-rev`` into
28it.
29
30.. note::
31
32   West does not create a ``manifest-rev`` branch in the manifest repository,
33   since west does not manage the manifest repository's branches or revisions.
34
35The ``refs/west/*`` Git refs
36****************************
37
38West also reserves all Git refs that begin with ``refs/west/`` (such as
39``refs/west/foo``) for itself in local project repositories. Unlike
40``manifest-rev``, these refs are not regular branches. West's behavior here is
41an implementation detail; users should not rely on these refs' existence or
42behavior.
43
44Private repositories
45********************
46
47You can use west to fetch from private repositories. There is nothing
48west-specific about this.
49
50The ``west update`` command essentially runs ``git fetch YOUR_PROJECT_URL``
51when a project's ``manifest-rev`` branch must be updated to a newly fetched
52commit. It's up to your environment to make sure the fetch succeeds.
53
54You can either enter the password manually or use any of the `credential
55helpers built in to Git`_. Since Git has credential storage built in, there is
56no need for a west-specific feature.
57
58The following sections cover common cases for running ``west update`` without
59having to enter your password, as well as how to troubleshoot issues.
60
61.. _credential helpers built in to Git:
62   https://git-scm.com/docs/gitcredentials
63
64Fetching via HTTPS
65==================
66
67On Windows when fetching from GitHub, recent versions of Git prompt you for
68your GitHub password in a graphical window once, then store it for future use
69(in a default installation). Passwordless fetching from GitHub should therefore
70work "out of the box" on Windows after you have done it once.
71
72In general, you can store your credentials on disk using the "store" git
73credential helper. See the `git-credential-store`_ manual page for details.
74
75To use this helper for all the repositories in your workspace, run:
76
77.. code-block:: shell
78
79   west forall -c "git config credential.helper store"
80
81To use this helper on just the projects ``foo`` and ``bar``, run:
82
83.. code-block:: shell
84
85   west forall -c "git config credential.helper store" foo bar
86
87To use this helper by default on your computer, run:
88
89.. code-block:: shell
90
91   git config --global credential.helper store
92
93On GitHub, you can set up a `personal access token`_ to use in place of your
94account password. (This may be required if your account has two-factor
95authentication enabled, and may be preferable to storing your account password
96in plain text even if two-factor authentication is disabled.)
97
98You can use the Git credential store to authenticate with a GitHub PAT
99(Personal Access Token) like so:
100
101.. code-block:: shell
102
103   echo "https://x-access-token:$GH_TOKEN@github.com" >> ~/.git-credentials
104
105If you don't want to store any credentials on the file system, you can store
106them in memory temporarily using `git-credential-cache`_ instead.
107
108If you setup fetching via SSH, you can use Git URL rewrite feature. The following
109command instructs Git to use SSH URLs for GitHub instead of HTTPS ones:
110
111.. code-block:: shell
112
113   git config --global url."git@github.com:".insteadOf "https://github.com/"
114
115.. _git-credential-store:
116   https://git-scm.com/docs/git-credential-store#_examples
117.. _git-credential-cache:
118   https://git-scm.com/docs/git-credential-cache
119.. _personal access token:
120   https://docs.github.com/en/github/authenticating-to-github/creating-a-personal-access-token
121
122Fetching via SSH
123================
124
125If your SSH key has no password, fetching should just work. If it does have a
126password, you can avoid entering it manually every time using `ssh-agent`_.
127
128On GitHub, see `Connecting to GitHub with SSH`_ for details on configuration
129and key creation.
130
131.. _ssh-agent:
132   https://www.ssh.com/ssh/agent
133.. _Connecting to GitHub with SSH:
134   https://docs.github.com/en/github/authenticating-to-github/connecting-to-github-with-ssh
135
136Project locations
137*****************
138
139Projects can be located anywhere inside the workspace, but they may not
140"escape" it.
141
142In other words, project repositories need not be located in subdirectories of
143the manifest repository or as immediate subdirectories of the topdir. However,
144projects must have paths inside the workspace.
145
146You may replace a project's repository directory within the workspace with a
147symbolic link to elsewhere on your computer, but west will not do this for you.
148
149.. _west-topologies:
150
151Topologies supported
152********************
153
154The following are example source code topologies supported by west.
155
156- T1: star topology, zephyr is the manifest repository
157- T2: star topology, a Zephyr application is the manifest repository
158- T3: forest topology, freestanding manifest repository
159
160T1: Star topology, zephyr is the manifest repository
161====================================================
162
163- The zephyr repository acts as the central repository and specifies
164  its :ref:`modules` in its :file:`west.yml`
165- Analogy with existing mechanisms: Git submodules with zephyr as the
166  super-project
167
168This is the default. See :ref:`west-workspace` for how mainline Zephyr is an
169example of this topology.
170
171.. _west-t2:
172
173T2: Star topology, application is the manifest repository
174=========================================================
175
176- Useful for those focused on a single application
177- A repository containing a Zephyr application acts as the central repository
178  and names other projects required to build it in its :file:`west.yml`. This
179  includes the zephyr repository and any modules.
180- Analogy with existing mechanisms: Git submodules with the application as
181  the super-project, zephyr and other projects as submodules
182
183A workspace using this topology looks like this:
184
185.. code-block:: none
186
187   west-workspace/
188189   ├── application/         # .git/     │
190   │   ├── CMakeLists.txt191   │   ├── prj.conf                     │  never modified by west
192   │   ├── src/                         │
193   │   │   └── main.c194   │   └── west.yml         # main manifest with optional import(s) and override(s)
195   │                                    │
196   ├── modules/
197   │   └── lib/
198   │       └── zcbor/       # .git/ project from either the main manifest or some import.
199200   └── zephyr/              # .git/ project
201       └── west.yml         # This can be partially imported with lower precedence or ignored.
202                            # Only the 'manifest-rev' version can be imported.
203
204
205Here is an example :file:`application/west.yml` which uses
206:ref:`west-manifest-import`, available since west 0.7, to import Zephyr v2.5.0
207and its modules into the application manifest file:
208
209.. code-block:: yaml
210
211   # Example T2 west.yml, using manifest imports.
212   manifest:
213     remotes:
214       - name: zephyrproject-rtos
215         url-base: https://github.com/zephyrproject-rtos
216     projects:
217       - name: zephyr
218         remote: zephyrproject-rtos
219         revision: v2.5.0
220         import: true
221     self:
222       path: application
223
224You can still selectively "override" individual Zephyr modules if you use
225``import:`` in this way; see :ref:`west-manifest-ex1.3` for an example.
226
227Another way to do the same thing is to copy/paste :file:`zephyr/west.yml`
228to :file:`application/west.yml`, adding an entry for the zephyr
229project itself, like this:
230
231.. code-block:: yaml
232
233   # Equivalent to the above, but with manually maintained Zephyr modules.
234   manifest:
235     remotes:
236       - name: zephyrproject-rtos
237         url-base: https://github.com/zephyrproject-rtos
238     defaults:
239       remote: zephyrproject-rtos
240     projects:
241       - name: zephyr
242         revision: v2.5.0
243         west-commands: scripts/west-commands.yml
244       - name: net-tools
245         revision: some-sha-goes-here
246         path: tools/net-tools
247       # ... other Zephyr modules go here ...
248     self:
249       path: application
250
251(The ``west-commands`` is there for :ref:`west-build-flash-debug` and other
252Zephyr-specific :ref:`west-extensions`. It's not necessary when using
253``import``.)
254
255The main advantage to using ``import`` is not having to track the revisions of
256imported projects separately. In the above example, using ``import`` means
257Zephyr's :ref:`module <modules>` versions are automatically determined from the
258:file:`zephyr/west.yml` revision, instead of having to be copy/pasted (and
259maintained) on their own.
260
261T3: Forest topology
262===================
263
264- Useful for those supporting multiple independent applications or downstream
265  distributions with no "central" repository
266- A dedicated manifest repository which contains no Zephyr source code,
267  and specifies a list of projects all at the same "level"
268- Analogy with existing mechanisms: Google repo-based source distribution
269
270A workspace using this topology looks like this:
271
272.. code-block:: none
273
274   west-workspace/
275   ├── app1/               # .git/ project
276   │   ├── CMakeLists.txt
277   │   ├── prj.conf
278   │   └── src/
279   │       └── main.c
280   ├── app2/               # .git/ project
281   │   ├── CMakeLists.txt
282   │   ├── prj.conf
283   │   └── src/
284   │       └── main.c
285   ├── manifest-repo/      # .git/ never modified by west
286   │   └── west.yml        # main manifest with optional import(s) and override(s)
287   ├── modules/
288   │   └── lib/
289   │       └── zcbor/      # .git/ project from either the main manifest or
290   │                       #       from some import
291292   └── zephyr/             # .git/ project
293       └── west.yml        # This can be partially imported with lower precedence or ignored.
294                           # Only the 'manifest-rev' version can be imported.
295
296Here is an example T3 :file:`manifest-repo/west.yml` which uses
297:ref:`west-manifest-import`, available since west 0.7, to import Zephyr
298v2.5.0 and its modules, then add the ``app1`` and ``app2`` projects:
299
300.. code-block:: yaml
301
302   manifest:
303     remotes:
304       - name: zephyrproject-rtos
305         url-base: https://github.com/zephyrproject-rtos
306       - name: your-git-server
307         url-base: https://git.example.com/your-company
308     defaults:
309       remote: your-git-server
310     projects:
311       - name: zephyr
312         remote: zephyrproject-rtos
313         revision: v2.5.0
314         import: true
315       - name: app1
316         revision: SOME_SHA_OR_BRANCH_OR_TAG
317       - name: app2
318         revision: ANOTHER_SHA_OR_BRANCH_OR_TAG
319     self:
320       path: manifest-repo
321
322You can also do this "by hand" by copy/pasting :file:`zephyr/west.yml`
323as shown :ref:`above <west-t2>` for the T2 topology, with the same caveats.
324