Josip Sokcevic | 79a4a54 | 2023-08-15 00:35:08 | [diff] [blame] | 1 | # Git submodules |
| 2 | |
| 3 | A Git submodule is a Git repository inside another Git repository. Chromium |
| 4 | project doesn't rely on Git submodules directly. Instead, gclient sync is used |
| 5 | to manage Git dependencies. |
| 6 | |
| 7 | In 2023Q3, we started to move source of Git dependencies from DEPS files to Git |
| 8 | submodules. While we do our best to hide complexities of submodules, some will |
| 9 | be exposed. |
| 10 | |
| 11 | [TOC] |
| 12 | |
| 13 | ## A quick introduction to Git submoduldes |
| 14 | |
| 15 | [Git submodules](https://git-scm.com/docs/gitsubmodules) are managed via the |
| 16 | combination of `.gitmodules` files and gitlinks. `.gitmodules` is a text file |
| 17 | that configures submodules, and each submodule entry contains the path to the |
| 18 | submodule's worktree and the URL of the submodule. Gitlink is a special type of |
| 19 | file in the Git database that tracks a submodule commit. |
| 20 | |
Josip Sokcevic | 703849e | 2023-08-16 00:13:34 | [diff] [blame] | 21 | You can find an example of Git dependency below. Note that gclient-condition is |
| 22 | a custom property used by gclient and not git. It's identical to `condition` in |
| 23 | `DEPS` and the allowed variables are defined in `vars = {` section of `DEPS`. |
Josip Sokcevic | 79a4a54 | 2023-08-15 00:35:08 | [diff] [blame] | 24 | |
| 25 | `.gitmodules`: |
| 26 | |
| 27 | ``` |
| 28 | [submodule "third_party/catapult"] |
| 29 | path = third_party/catapult |
| 30 | url = https://chromium.googlesource.com/catapult.git |
Josip Sokcevic | 703849e | 2023-08-16 00:13:34 | [diff] [blame] | 31 | gclient-condition = checkout_linux |
Josip Sokcevic | 79a4a54 | 2023-08-15 00:35:08 | [diff] [blame] | 32 | ``` |
| 33 | |
| 34 | gitlink entry, retrieved using `git ls-files -s -- third_party/catapult`: |
| 35 | |
| 36 | ``` |
| 37 | 160000 0b39a694c0b61392d1180520ed1c13e390029c41 0 third_party/catapult |
| 38 | ``` |
| 39 | |
| 40 | Corresponding DEPS entry would look like: |
| 41 | |
| 42 | ``` |
| 43 | 'third_party/catapult': { |
Josip Sokcevic | 703849e | 2023-08-16 00:13:34 | [diff] [blame] | 44 | 'url': 'https://chromium.googlesource.com/catapult.git@0b39a694c0b61392d1180520ed1c13e390029c41', |
| 45 | 'condition': 'checkout_linux', |
Josip Sokcevic | 79a4a54 | 2023-08-15 00:35:08 | [diff] [blame] | 46 | } |
| 47 | ``` |
| 48 | |
| 49 | ## How to avoid accidental Git submodule updates? |
| 50 | |
André Bianchessi | fa2af277 | 2024-10-03 14:50:19 | [diff] [blame] | 51 | The simplest approach is to always run gclient sync after updating chromium |
Josip Sokcevic | 79a4a54 | 2023-08-15 00:35:08 | [diff] [blame] | 52 | checkout (e.g. after `git pull`, or `git checkout`). You can automate that by |
| 53 | adding post-checkout hook (example below). To confirm there are no changes, run |
| 54 | `git status`. If you use `git commit -a`, check the "Changes to be committed" |
| 55 | section that shows up in the edit commit message. |
| 56 | |
| 57 | ### Automatically run gclient sync after git pull / git checkout |
| 58 | |
| 59 | We need to have Git two hooks: post-checkout and post-merge. In chromium/src |
| 60 | directory, edit `.git/hooks/post-checkout`: |
| 61 | |
| 62 | ``` |
| 63 | #!/bin/sh |
| 64 | |
| 65 | set -u |
| 66 | gclient sync |
| 67 | ``` |
| 68 | |
| 69 | and set it to be executable: `chmod +x .git/hooks/post-checkout`. Repeat the |
| 70 | same for `.git/hooks/post-merge`. |
| 71 | |
| 72 | More information about githooks can be found |
| 73 | [here](https://git-scm.com/docs/githooks). |
| 74 | |
| 75 | ### Git status shows modified dependencies. What does that mean? |
| 76 | |
| 77 | If a submodule is checked out at a different commit than one tracked by its |
| 78 | parent, `git status` in the parent repo will show unstaged changes with "new |
| 79 | commits" in parenthesis, such as: |
| 80 | |
| 81 | ``` |
| 82 | modified: <git deps name> (new commits) |
| 83 | ``` |
| 84 | |
| 85 | Commands like `git commit -a` or `git add *|.|-A|u` WILL include this in your |
| 86 | commit and your CL (which is likely NOT what you want). |
| 87 | |
| 88 | Instead you can: |
| 89 | |
| 90 | ``` |
| 91 | # Run gclient sync to sync dependencies |
| 92 | gclient sync |
| 93 | # check git status again |
| 94 | |
| 95 | # OR |
| 96 | git add <file> # for each file you want to stage |
| 97 | # Then commit your staged files |
| 98 | git commit -v -m "Fix foo/bar" |
| 99 | ``` |
| 100 | |
Josip Sokcevic | 79a4a54 | 2023-08-15 00:35:08 | [diff] [blame] | 101 | If a submodule has uncommitted changes (i.e. you made some manual changes to the |
| 102 | affected submodule), running `git status` in its parent repo will show them as |
| 103 | unstaged changes: |
| 104 | |
| 105 | ``` |
| 106 | modified: <git deps name> (modified content) |
| 107 | |
| 108 | # or |
| 109 | |
| 110 | modified: <git deps name> (untracked content) |
| 111 | ``` |
| 112 | |
| 113 | It's not possible to add those changes to the parent repository. You can ignore |
| 114 | such status, or you can cd into submodule and address it. E.g. you may delete |
| 115 | untracked files (content) or reset modified content to match HEAD. |
| 116 | |
| 117 | ## I accidentally staged Git submodule (not yet committed) |
| 118 | |
| 119 | If you accidentally stage a Git submodule, you can unstage it by running `git |
| 120 | restore --staged <path to submodule>`. |
| 121 | |
| 122 | ## I accidentally committed Git submodule |
| 123 | |
| 124 | We will need to create either a commit that sets it back to old value, or amend |
| 125 | the commit that added it. You can try to run `gclient sync` to bring the commit |
| 126 | back to what is expected. If that doesn't work, you can use `gclient setdep -r |
Josip Sokcevic | 00eefc5 | 2023-08-27 22:11:27 | [diff] [blame] | 127 | <path>@<old hash>`, run `gclient gitmodules` to sync all submodules commits back |
Josip Sokcevic | 79a4a54 | 2023-08-15 00:35:08 | [diff] [blame] | 128 | to what is in DEPS, or check detailed instructions in [Managing |
| 129 | dependencies](dependencies.md). |
| 130 | |
Josip Sokcevic | 00eefc5 | 2023-08-27 22:11:27 | [diff] [blame] | 131 | NOTE: setdep for chromium/src is always prefixed with src/. For example, if you |
| 132 | are updating v8, the command would be `gclient setdep -r src/v8@<hash>. |
| 133 | |
Joanna Wang | 394548e78 | 2023-09-25 19:11:46 | [diff] [blame] | 134 | ## Workflows with submodules |
| 135 | |
Joanna Wang | e26842a | 2024-01-19 04:42:57 | [diff] [blame] | 136 | ### Submodules during 'git status', 'git commit', and 'git add' |
Joanna Wang | 394548e78 | 2023-09-25 19:11:46 | [diff] [blame] | 137 | |
Joanna Wang | e26842a | 2024-01-19 04:42:57 | [diff] [blame] | 138 | For `git status`, submodules that show up under `Changes not staged for commit` |
| 139 | can be hidden with `git -c diff.ignoreSubmodules=all status` |
| 140 | |
| 141 | For `git commit -a` you can exclude all submodules with |
Joanna Wang | 394548e78 | 2023-09-25 19:11:46 | [diff] [blame] | 142 | `git -c diff.ignoreSubmodules=all commit -a`. |
| 143 | |
Joanna Wang | e26842a | 2024-01-19 04:42:57 | [diff] [blame] | 144 | `git add` does NOT support `diff.ignoreSubmodules`. Submodules that were |
| 145 | hidden from you with `git -c diff.ignoreSubmodules=all status` would still |
| 146 | be staged with `git add .|--all|-A` and therefore committed with |
| 147 | `git -c diff.ignoreSubmodules=all commit`. |
| 148 | |
| 149 | Instead you can run `git add ':(exclude,attr:builtin_objectmode=160000)'` which |
| 150 | will stage all changes except for submodules. |
| 151 | |
| 152 | (git assigns `160000` as the objectmode submodules. You can read more about |
| 153 | [`builtin_objectmode`](https://kernel.googlesource.com/pub/scm/git/git/+/refs/heads/next/Documentation/gitattributes.txt#110) |
| 154 | and magic [pathspecs](https://git-scm.com/docs/gitglossary#Documentation/gitglossary.txt-aiddefpathspecapathspec)) |
| 155 | |
Joanna Wang | 394548e78 | 2023-09-25 19:11:46 | [diff] [blame] | 156 | To make these commands shorter, you can create git aliases for them by adding |
Josip Sokcevic | 1fc8067 | 2024-01-24 00:19:38 | [diff] [blame] | 157 | the following to your $HOME/.gitconfig (globally) or src/.git/config file (just |
| 158 | chromium/src): |
Joanna Wang | 394548e78 | 2023-09-25 19:11:46 | [diff] [blame] | 159 | ``` |
| 160 | [alias] |
| 161 | # 's', 'c', or whatever alias you want for each command |
| 162 | s = -c diff.ignoreSubmodules=all status |
| 163 | c = -c diff.ignoreSubmodules=all commit -a |
| 164 | d = -c diff.ignoreSubmodules=all difftool --dir-diff |
Joanna Wang | e26842a | 2024-01-19 04:42:57 | [diff] [blame] | 165 | a = add ':(exclude,attr:builtin_objectmode=160000)' |
Joanna Wang | 394548e78 | 2023-09-25 19:11:46 | [diff] [blame] | 166 | ``` |
Joanna Wang | e26842a | 2024-01-19 04:42:57 | [diff] [blame] | 167 | With the above, you can execute these commands by running `git s`, `git c`, etc. |
| 168 | Or you may also use the pre-commit git hook detailed below. |
Joanna Wang | 394548e78 | 2023-09-25 19:11:46 | [diff] [blame] | 169 | |
Helmut Januschka | 5f8cfb7 | 2024-09-09 16:39:26 | [diff] [blame] | 170 | ### Understanding diff.ignoreSubmodules |
| 171 | |
| 172 | `git config diff.ignoreSubmodules` sets a default behavior for `diff`, `status`, |
| 173 | and several other git subcommands, using one of the [supported values of |
| 174 | `--ignore-submodules`](https://www.git-scm.com/docs/git-diff/#Documentation/git-diff.txt---ignore-submodulesltwhengt). |
| 175 | |
| 176 | By default, `gclient sync` sets this to `dirty` as a local config in the |
| 177 | chromium checkout. This elides submodule output for `git status` in a clean |
| 178 | checkout, but will show submodules as modified when developers locally touch |
| 179 | them. |
| 180 | |
| 181 | Manually setting this to `all` elides such output in all cases. This also omits |
| 182 | submodule changes from `git commit -a`, which can decrease the likelihood of |
| 183 | accidental submodule commits. However, it does not omit such changes from |
| 184 | `git add -A`, meaning developers who use this flow are actually _more_ likely to |
| 185 | commit accidental changes, since they'll be invisible beforehand unless |
| 186 | developers manually set `--ignore-submodules=dirty` or use a lower-level command |
| 187 | such as `git diff-tree`. |
| 188 | |
| 189 | Because `all` can result in misleading output and doesn't fully prevent |
| 190 | accidental submodule commits, typical developers are likely better-served by |
| 191 | leaving this configured to `dirty` and installing the |
| 192 | [commit hook described below](#install-hook) to prevent such commits. |
| 193 | Accordingly, `gclient sync` will warn if it detects a different setting locally; |
| 194 | developers who understand the consequences can silence the warning via the |
| 195 | `GCLIENT_SUPPRESS_SUBMODULE_WARNING` environment variable. |
| 196 | |
Joanna Wang | e0c72ee9d | 2023-09-25 20:57:40 | [diff] [blame] | 197 | ### Submodules during a 'git rebase-update' |
Joanna Wang | 394548e78 | 2023-09-25 19:11:46 | [diff] [blame] | 198 | While resolving merge conflicts during a `git rebase-update` you may see |
| 199 | submodules show up in unexpected places. |
| 200 | |
| 201 | #### Submodules under "Changes not staged for commit" |
| 202 | Submodules under this section can be safely ignored. This simply shows that the |
| 203 | local commits of these submodules do not match the latest pinned commits fetched |
| 204 | from remote. In other words, these submodules have been rolled since your last |
| 205 | `git rebase-update`. |
| 206 | |
| 207 | If you use a diff tool like meld you can run: |
| 208 | `git -c diff.ignoreSubmodules=all difftool --dir-diff` |
| 209 | to prevent these submodules from showing up in your diff tool. |
| 210 | |
| 211 | #### Submodules under "Unmerged paths" |
| 212 | If Submodules show up under this section it means that new revisions were |
| 213 | committed for those submodules (either intentional or unintentionally) and these |
| 214 | submodules were also rolled at remote. So now there is a conflict. |
| 215 | |
| 216 | If you DID NOT intentionally make any submdoules changes, you should run: |
| 217 | `gclient gitmodules`. This will update the submdoules for you, to match whatever |
| 218 | commits are listed in DEPS (which you have just pulled from remote). |
| 219 | |
| 220 | If you DID intentionally roll submodules, you can resolve this conflict just by |
| 221 | resetting it: |
| 222 | `gclient setdep -r {path}@{hash}` |
| 223 | |
Helmut Januschka | 5f8cfb7 | 2024-09-09 16:39:26 | [diff] [blame] | 224 | ## Install a hook to help detect unintentional submodule commits {#install-hook} |
Joanna Wang | eda00e4 | 2023-09-20 18:51:47 | [diff] [blame] | 225 | |
| 226 | depot_tools provides an opt-in pre-commit hook to detect unintentional submodule |
| 227 | changes during `git commit` and remove them from the commit. |
| 228 | |
| 229 | To install the hook: `gclient installhooks` |
| 230 | |
| 231 | If there is an existing pre-commit hook, gclient will instruct you how to update |
| 232 | it. If you have already installed this hook, gclient will do nothing. |
| 233 | |
| 234 | To uninstall the hook, in `chromium/src` `rm .git/hooks/pre-commit` if you have |
| 235 | no other hooks. Otherwise update `.git/hooks/pre-commit` to remove the gclient |
| 236 | provided hook. |
| 237 | |
| 238 | To bypass this hook run `git commit --no-verify` (which bypasses all hooks you |
| 239 | may have) OR set the following environment variable: `SKIP_GITLINK_PRECOMMIT=1` |
| 240 | (which bypasses this specific hook). |
| 241 | |
| 242 | Note that this is currently and best effort solution and does not guarantee |
| 243 | that unintentional commits will always be detected. The team will iterate |
| 244 | quickly on this hook to fill in other gaps and behavior is subject to change. |
| 245 | Please file an [issue](https://bugs.chromium.org/p/chromium/issues/entry?components=Infra%3ESDK&labels=submodules-feedback&[email protected],[email protected]&description=Please%20steps%20to%20reproduce%20the%20problem:%0A%0ADo%20you%20have%20any%20custom%20environment%20setups%20like%20git%20hooks%20or%20git%20configs%20that%20you%20have%20set%20yourself%0A%0APlease%20attach%20output%20of:%0Agit%20config%20-l%0Agit%20map-branches%20-vv%0A%0AIf%20this%20is%20an%20issue%20with%20git%20cl%20upload%20please%20include%20the%20git%20trace%20file%20for%20the%20problematic%20run%20found%20in:%0A%3Cdepot_tools_path%3E/traces/%3Clatest%20trace%3E) for any feedback. |
| 246 | |
Josip Sokcevic | 79a4a54 | 2023-08-15 00:35:08 | [diff] [blame] | 247 | ## FAQ |
| 248 | |
| 249 | ### Why do we have Git dependencies in both DEPS and Git submodules? |
| 250 | |
| 251 | Lots of Chromium infrastructure already parse DEPS file directly. Instead of a |
| 252 | massive switch, it's easier to transition to Git submodules this way. Moreover, |
| 253 | unwanted Git submodule updates can be detected and developers can be warned. |
| 254 | |
| 255 | ### How do I manually roll Git submodule? |
Josip Sokcevic | 00eefc5 | 2023-08-27 22:11:27 | [diff] [blame] | 256 | |
Josip Sokcevic | 79a4a54 | 2023-08-15 00:35:08 | [diff] [blame] | 257 | See the [dependencies](dependencies.md) page. |
| 258 | |
Josip Sokcevic | 00eefc5 | 2023-08-27 22:11:27 | [diff] [blame] | 259 | ### I got a conflict on a submodule, how do I resolve it? |
| 260 | |
| 261 | First, you will need to determine what is the right commit hash. If you |
| 262 | accidentally committed a gitlink, which got in the meantime updated, you most |
| 263 | likely want to restore the original updated gitlink. You can run `gclient |
| 264 | gitmodules`, which will take care of all unmerged submodule paths, and set it to |
| 265 | match DEPS file. |
| 266 | |
| 267 | If you prefer to manually resolve it, under git status, you will see "Unmerged |
| 268 | paths". If those are submodules, you want to restore them by running the |
| 269 | following command: |
| 270 | |
| 271 | ``` |
| 272 | git restore --staging <affected path> |
| 273 | ``` |
| 274 | |
Joanna Wang | b812e22 | 2023-08-31 17:43:53 | [diff] [blame] | 275 | ### How do I see what revision is pinned? |
| 276 | |
| 277 | `gclient getdep` will return whatever commit is pinned for the deps in `DEPS` |
| 278 | (unstaged, staged, or committed). If the repo is using git submodules only |
| 279 | (and has no git deps in `DEPS`) it will return the whatever pinned commit is |
| 280 | staged or committed. |
| 281 | |
| 282 | ``` |
| 283 | gclient getdep -r <path> |
| 284 | ``` |
| 285 | |
| 286 | |
Josip Sokcevic | 00eefc5 | 2023-08-27 22:11:27 | [diff] [blame] | 287 | If you want to keep your gitlink, then run `git add <affected path>`. |
| 288 | |
Josip Sokcevic | 79a4a54 | 2023-08-15 00:35:08 | [diff] [blame] | 289 | ### How can I provide feedback? |
| 290 | |
| 291 | Please file [a bug under Infra>SDK |
| 292 | component](https://bugs.chromium.org/p/chromium/issues/entry?components=Infra%3ESDK). |