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 | |
Josip Sokcevic | 00eefc5 | 2023-08-27 22:11:27 | [diff] [blame] | 11 | IMPORTANT NOTE: Due to a bug in fsmonitor, we encourage you to disable it until |
| 12 | the underlying bug is fixed. More details in https://crbug.com/1475405. |
| 13 | |
Josip Sokcevic | 79a4a54 | 2023-08-15 00:35:08 | [diff] [blame] | 14 | [TOC] |
| 15 | |
| 16 | ## A quick introduction to Git submoduldes |
| 17 | |
| 18 | [Git submodules](https://git-scm.com/docs/gitsubmodules) are managed via the |
| 19 | combination of `.gitmodules` files and gitlinks. `.gitmodules` is a text file |
| 20 | that configures submodules, and each submodule entry contains the path to the |
| 21 | submodule's worktree and the URL of the submodule. Gitlink is a special type of |
| 22 | file in the Git database that tracks a submodule commit. |
| 23 | |
Josip Sokcevic | 703849e | 2023-08-16 00:13:34 | [diff] [blame] | 24 | You can find an example of Git dependency below. Note that gclient-condition is |
| 25 | a custom property used by gclient and not git. It's identical to `condition` in |
| 26 | `DEPS` and the allowed variables are defined in `vars = {` section of `DEPS`. |
Josip Sokcevic | 79a4a54 | 2023-08-15 00:35:08 | [diff] [blame] | 27 | |
| 28 | `.gitmodules`: |
| 29 | |
| 30 | ``` |
| 31 | [submodule "third_party/catapult"] |
| 32 | path = third_party/catapult |
| 33 | url = https://chromium.googlesource.com/catapult.git |
Josip Sokcevic | 703849e | 2023-08-16 00:13:34 | [diff] [blame] | 34 | gclient-condition = checkout_linux |
Josip Sokcevic | 79a4a54 | 2023-08-15 00:35:08 | [diff] [blame] | 35 | ``` |
| 36 | |
| 37 | gitlink entry, retrieved using `git ls-files -s -- third_party/catapult`: |
| 38 | |
| 39 | ``` |
| 40 | 160000 0b39a694c0b61392d1180520ed1c13e390029c41 0 third_party/catapult |
| 41 | ``` |
| 42 | |
| 43 | Corresponding DEPS entry would look like: |
| 44 | |
| 45 | ``` |
| 46 | 'third_party/catapult': { |
Josip Sokcevic | 703849e | 2023-08-16 00:13:34 | [diff] [blame] | 47 | 'url': 'https://chromium.googlesource.com/catapult.git@0b39a694c0b61392d1180520ed1c13e390029c41', |
| 48 | 'condition': 'checkout_linux', |
Josip Sokcevic | 79a4a54 | 2023-08-15 00:35:08 | [diff] [blame] | 49 | } |
| 50 | ``` |
| 51 | |
| 52 | ## How to avoid accidental Git submodule updates? |
| 53 | |
| 54 | The simplest approach is to always run gclient sync after updated chromium |
| 55 | checkout (e.g. after `git pull`, or `git checkout`). You can automate that by |
| 56 | adding post-checkout hook (example below). To confirm there are no changes, run |
| 57 | `git status`. If you use `git commit -a`, check the "Changes to be committed" |
| 58 | section that shows up in the edit commit message. |
| 59 | |
| 60 | ### Automatically run gclient sync after git pull / git checkout |
| 61 | |
| 62 | We need to have Git two hooks: post-checkout and post-merge. In chromium/src |
| 63 | directory, edit `.git/hooks/post-checkout`: |
| 64 | |
| 65 | ``` |
| 66 | #!/bin/sh |
| 67 | |
| 68 | set -u |
| 69 | gclient sync |
| 70 | ``` |
| 71 | |
| 72 | and set it to be executable: `chmod +x .git/hooks/post-checkout`. Repeat the |
| 73 | same for `.git/hooks/post-merge`. |
| 74 | |
| 75 | More information about githooks can be found |
| 76 | [here](https://git-scm.com/docs/githooks). |
| 77 | |
| 78 | ### Git status shows modified dependencies. What does that mean? |
| 79 | |
| 80 | If a submodule is checked out at a different commit than one tracked by its |
| 81 | parent, `git status` in the parent repo will show unstaged changes with "new |
| 82 | commits" in parenthesis, such as: |
| 83 | |
| 84 | ``` |
| 85 | modified: <git deps name> (new commits) |
| 86 | ``` |
| 87 | |
| 88 | Commands like `git commit -a` or `git add *|.|-A|u` WILL include this in your |
| 89 | commit and your CL (which is likely NOT what you want). |
| 90 | |
| 91 | Instead you can: |
| 92 | |
| 93 | ``` |
| 94 | # Run gclient sync to sync dependencies |
| 95 | gclient sync |
| 96 | # check git status again |
| 97 | |
| 98 | # OR |
| 99 | git add <file> # for each file you want to stage |
| 100 | # Then commit your staged files |
| 101 | git commit -v -m "Fix foo/bar" |
| 102 | ``` |
| 103 | |
Josip Sokcevic | 15a0575 | 2023-08-24 17:02:20 | [diff] [blame] | 104 | NOTE: due to a bug in gclient (crbug.com/1475448), it's possible that gclient |
| 105 | left unmanaged git repository. You may need to manually remove those unmanaged |
| 106 | repositories. |
| 107 | |
| 108 | ``` |
| 109 | # Inside chromium/src checkout: |
| 110 | # This ensures that all managed dependencies are in sync: |
| 111 | gclient sync -D |
| 112 | # This moves all unused dependencies to ../unused directory in gclient root |
| 113 | # (just outside of src directory). It then tells git to restore gitlink. |
| 114 | for f in $( git status | grep '(new commits)' | awk '{print $2}' ); do mkdir -p "../unused/`dirname $f`" && mv $f "../unused/$f" && git checkout -- $f; done |
| 115 | # inspect ../unused/ if you'd like, and remove it there's nothing useful there, |
| 116 | # e.g. no non-uploaded commits. |
| 117 | ``` |
| 118 | |
Josip Sokcevic | 79a4a54 | 2023-08-15 00:35:08 | [diff] [blame] | 119 | If a submodule has uncommitted changes (i.e. you made some manual changes to the |
| 120 | affected submodule), running `git status` in its parent repo will show them as |
| 121 | unstaged changes: |
| 122 | |
| 123 | ``` |
| 124 | modified: <git deps name> (modified content) |
| 125 | |
| 126 | # or |
| 127 | |
| 128 | modified: <git deps name> (untracked content) |
| 129 | ``` |
| 130 | |
| 131 | It's not possible to add those changes to the parent repository. You can ignore |
| 132 | such status, or you can cd into submodule and address it. E.g. you may delete |
| 133 | untracked files (content) or reset modified content to match HEAD. |
| 134 | |
| 135 | ## I accidentally staged Git submodule (not yet committed) |
| 136 | |
| 137 | If you accidentally stage a Git submodule, you can unstage it by running `git |
| 138 | restore --staged <path to submodule>`. |
| 139 | |
| 140 | ## I accidentally committed Git submodule |
| 141 | |
| 142 | We will need to create either a commit that sets it back to old value, or amend |
| 143 | the commit that added it. You can try to run `gclient sync` to bring the commit |
| 144 | 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] | 145 | <path>@<old hash>`, run `gclient gitmodules` to sync all submodules commits back |
Josip Sokcevic | 79a4a54 | 2023-08-15 00:35:08 | [diff] [blame] | 146 | to what is in DEPS, or check detailed instructions in [Managing |
| 147 | dependencies](dependencies.md). |
| 148 | |
Josip Sokcevic | 00eefc5 | 2023-08-27 22:11:27 | [diff] [blame] | 149 | NOTE: setdep for chromium/src is always prefixed with src/. For example, if you |
| 150 | are updating v8, the command would be `gclient setdep -r src/v8@<hash>. |
| 151 | |
Joanna Wang | 394548e78 | 2023-09-25 19:11:46 | [diff] [blame^] | 152 | ## Workflows with submodules |
| 153 | |
| 154 | ### Submodules during `git status` and `git commit` |
| 155 | Submodules that show up under `Changes not staged for commit` when you run |
| 156 | `git status` can be hidden with `git -c diff.ignoreSubmodules=all status` |
| 157 | |
| 158 | You can also `git commit -a` your changes while excluding all submodules with |
| 159 | `git -c diff.ignoreSubmodules=all commit -a`. |
| 160 | |
| 161 | To make these commands shorter, you can create git aliases for them by adding |
| 162 | the following to your src/.git/commit file: |
| 163 | ``` |
| 164 | [alias] |
| 165 | # 's', 'c', or whatever alias you want for each command |
| 166 | s = -c diff.ignoreSubmodules=all status |
| 167 | c = -c diff.ignoreSubmodules=all commit -a |
| 168 | d = -c diff.ignoreSubmodules=all difftool --dir-diff |
| 169 | ``` |
| 170 | With the above, you can execute these commands by running `git s` and `git c` |
| 171 | |
| 172 | NOTE: `diff.ignoreSubmodules` is not supported with `git add`. If you are hiding |
| 173 | subodules from your view with something like `git s`, running |
| 174 | `git add .|--all|-A` will still stage any submodules you do not see for commit. |
| 175 | Then running `git c` will still include these submodules in your commit. |
| 176 | |
| 177 | We recommend you use the pre-commit git hook detailed below. |
| 178 | |
| 179 | ### Submodules during a `git rebase-update` |
| 180 | While resolving merge conflicts during a `git rebase-update` you may see |
| 181 | submodules show up in unexpected places. |
| 182 | |
| 183 | #### Submodules under "Changes not staged for commit" |
| 184 | Submodules under this section can be safely ignored. This simply shows that the |
| 185 | local commits of these submodules do not match the latest pinned commits fetched |
| 186 | from remote. In other words, these submodules have been rolled since your last |
| 187 | `git rebase-update`. |
| 188 | |
| 189 | If you use a diff tool like meld you can run: |
| 190 | `git -c diff.ignoreSubmodules=all difftool --dir-diff` |
| 191 | to prevent these submodules from showing up in your diff tool. |
| 192 | |
| 193 | #### Submodules under "Unmerged paths" |
| 194 | If Submodules show up under this section it means that new revisions were |
| 195 | committed for those submodules (either intentional or unintentionally) and these |
| 196 | submodules were also rolled at remote. So now there is a conflict. |
| 197 | |
| 198 | If you DID NOT intentionally make any submdoules changes, you should run: |
| 199 | `gclient gitmodules`. This will update the submdoules for you, to match whatever |
| 200 | commits are listed in DEPS (which you have just pulled from remote). |
| 201 | |
| 202 | If you DID intentionally roll submodules, you can resolve this conflict just by |
| 203 | resetting it: |
| 204 | `gclient setdep -r {path}@{hash}` |
| 205 | |
Joanna Wang | eda00e4 | 2023-09-20 18:51:47 | [diff] [blame] | 206 | ## BETA: Install a hook to help detect unintentional submodule commits |
| 207 | |
| 208 | depot_tools provides an opt-in pre-commit hook to detect unintentional submodule |
| 209 | changes during `git commit` and remove them from the commit. |
| 210 | |
| 211 | To install the hook: `gclient installhooks` |
| 212 | |
| 213 | If there is an existing pre-commit hook, gclient will instruct you how to update |
| 214 | it. If you have already installed this hook, gclient will do nothing. |
| 215 | |
| 216 | To uninstall the hook, in `chromium/src` `rm .git/hooks/pre-commit` if you have |
| 217 | no other hooks. Otherwise update `.git/hooks/pre-commit` to remove the gclient |
| 218 | provided hook. |
| 219 | |
| 220 | To bypass this hook run `git commit --no-verify` (which bypasses all hooks you |
| 221 | may have) OR set the following environment variable: `SKIP_GITLINK_PRECOMMIT=1` |
| 222 | (which bypasses this specific hook). |
| 223 | |
| 224 | Note that this is currently and best effort solution and does not guarantee |
| 225 | that unintentional commits will always be detected. The team will iterate |
| 226 | quickly on this hook to fill in other gaps and behavior is subject to change. |
| 227 | 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. |
| 228 | |
Josip Sokcevic | 79a4a54 | 2023-08-15 00:35:08 | [diff] [blame] | 229 | ## FAQ |
| 230 | |
| 231 | ### Why do we have Git dependencies in both DEPS and Git submodules? |
| 232 | |
| 233 | Lots of Chromium infrastructure already parse DEPS file directly. Instead of a |
| 234 | massive switch, it's easier to transition to Git submodules this way. Moreover, |
| 235 | unwanted Git submodule updates can be detected and developers can be warned. |
| 236 | |
| 237 | ### How do I manually roll Git submodule? |
Josip Sokcevic | 00eefc5 | 2023-08-27 22:11:27 | [diff] [blame] | 238 | |
Josip Sokcevic | 79a4a54 | 2023-08-15 00:35:08 | [diff] [blame] | 239 | See the [dependencies](dependencies.md) page. |
| 240 | |
Josip Sokcevic | 00eefc5 | 2023-08-27 22:11:27 | [diff] [blame] | 241 | ### I got a conflict on a submodule, how do I resolve it? |
| 242 | |
| 243 | First, you will need to determine what is the right commit hash. If you |
| 244 | accidentally committed a gitlink, which got in the meantime updated, you most |
| 245 | likely want to restore the original updated gitlink. You can run `gclient |
| 246 | gitmodules`, which will take care of all unmerged submodule paths, and set it to |
| 247 | match DEPS file. |
| 248 | |
| 249 | If you prefer to manually resolve it, under git status, you will see "Unmerged |
| 250 | paths". If those are submodules, you want to restore them by running the |
| 251 | following command: |
| 252 | |
| 253 | ``` |
| 254 | git restore --staging <affected path> |
| 255 | ``` |
| 256 | |
Joanna Wang | b812e22 | 2023-08-31 17:43:53 | [diff] [blame] | 257 | ### How do I see what revision is pinned? |
| 258 | |
| 259 | `gclient getdep` will return whatever commit is pinned for the deps in `DEPS` |
| 260 | (unstaged, staged, or committed). If the repo is using git submodules only |
| 261 | (and has no git deps in `DEPS`) it will return the whatever pinned commit is |
| 262 | staged or committed. |
| 263 | |
| 264 | ``` |
| 265 | gclient getdep -r <path> |
| 266 | ``` |
| 267 | |
| 268 | |
Josip Sokcevic | 00eefc5 | 2023-08-27 22:11:27 | [diff] [blame] | 269 | If you want to keep your gitlink, then run `git add <affected path>`. |
| 270 | |
Josip Sokcevic | 79a4a54 | 2023-08-15 00:35:08 | [diff] [blame] | 271 | ### How can I provide feedback? |
| 272 | |
| 273 | Please file [a bug under Infra>SDK |
| 274 | component](https://bugs.chromium.org/p/chromium/issues/entry?components=Infra%3ESDK). |
| 275 | |