Skip to content

Commit 0837be8

Browse files
cliffhallclaude
andcommitted
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: cliffhall <cliff@futurescale.com>
1 parent e1d154e commit 0837be8

3 files changed

Lines changed: 371 additions & 0 deletions

File tree

‎src/git/README.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,8 @@ A repository's configuration and hooks travel with it (an unpacked archive, a sh
187187
- Each filter driver (`filter.<name>.clean`, `smudge`, `process`) and merge driver (`merge.<name>.driver`) that the repository's own configuration defines is emptied: files are staged and checked out unfiltered, and a file a merge driver would have merged is a conflict, which `git_merge` aborts. Drivers from your global or system configuration, such as Git LFS's, keep working.
188188
- `commit.gpgSign` and `merge.verifySignatures` are `false`, and `git_merge` passes `--no-gpg-sign` and `--no-verify-signatures`. Each `branch.<name>.mergeOptions` the repository's own configuration sets is emptied, since Git reads it as command-line options. `submodule.recurse` and `merge.autoStash` are `false`, and `core.pager` is `cat`.
189189
- `git_status`, `git_diff_unstaged` and `git_diff` pass `--ignore-submodules=dirty`, because looking for changes inside a submodule runs Git under the submodule's own configuration. A submodule whose checked-out commit moved is still reported; changes inside one are not, so run the tool on the submodule's path to see them.
190+
- `diff.submodule` is `short`, so a diff reports a submodule whose commit moved as a `Subproject commit` line rather than running a nested `git diff` inside it, which would not carry the flags above.
191+
- `git_add` and `git_checkout` let Git check whether a checked-out submodule changed, which runs Git inside it under its own configuration. The filter and merge drivers each checked-out submodule (and each submodule inside one) defines are emptied too, the same way as the repository's own.
190192

191193
This needs Git 2.31 or later; with an older Git every tool call fails with an error saying so, rather than run unprotected.
192194

‎src/git/src/mcp_server_git/server.py‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,9 @@ def _git_text(repo: git.Repo, command: str, *args: str) -> str:
130130
# - `submodule.recurse` would carry checkout and merge into submodules, whose
131131
# own configuration this override cannot neutralize (see `_LISTED_KEYS`).
132132
# - `merge.autoStash` keeps git_merge from stashing behind the tool's back.
133+
# - `diff.submodule` set to `diff` makes git diff run a nested git diff inside
134+
# each changed submodule, without `_DIFF_FLAGS`, so the submodule's own
135+
# `diff.external` and textconv drivers would run; `short` starts nothing.
133136
#
134137
# External diff programs and textconv filters are turned off per command
135138
# (`_DIFF_FLAGS`), and filter and merge drivers per repository (`_LISTED_KEYS`).
@@ -142,6 +145,7 @@ def _git_text(repo: git.Repo, command: str, *args: str) -> str:
142145
("submodule.recurse", "false"),
143146
("status.submoduleSummary", "false"),
144147
("merge.autoStash", "false"),
148+
("diff.submodule", "short"),
145149
)
146150

147151
# A filter driver (`filter.<name>.clean`/`smudge`/`process`, applied by
@@ -187,6 +191,54 @@ def _config_env(entries: list[tuple[str, str]]) -> dict[str, str]:
187191
return env
188192

189193

194+
def _submodule_drivers(repo: git.Repo) -> set[str]:
195+
"""The filter and merge driver keys each checked-out submodule defines.
196+
197+
`git_add ["."]` and `git_checkout` make git run `git status` inside a
198+
checked-out submodule, under the submodule's own configuration, where a
199+
filter driver its `.gitattributes` selects would run. The overrides are
200+
inherited by that nested git, so emptying the drivers by name works there
201+
too once their names are known. A submodule is found at each gitlink in
202+
the index, then inside it at each of its own, the way git descends.
203+
Listing them runs only `git config` and `git ls-files`, which start no
204+
program. A gitlink whose directory git cannot open as a repository is
205+
skipped: the nested git cannot open it either, so it runs nothing there.
206+
"""
207+
drivers: set[str] = set()
208+
if repo.working_tree_dir is None:
209+
return drivers
210+
pending = [Path(repo.working_tree_dir)]
211+
seen: set[Path] = set()
212+
while pending:
213+
top = pending.pop()
214+
# -z prints `<mode> <object> <stage>\t<path>\0` for each entry.
215+
listing = repo.git(C=str(top)).ls_files("-z", "--stage")
216+
for entry in listing.split("\0"):
217+
info, _, path = entry.partition("\t")
218+
if not info.startswith("160000 "):
219+
continue
220+
submodule = top / path
221+
if not (submodule / ".git").exists():
222+
continue # not checked out: git does not look inside
223+
resolved = submodule.resolve()
224+
if resolved in seen:
225+
continue
226+
seen.add(resolved)
227+
try:
228+
listed = repo.git(C=str(submodule)).config(
229+
"--show-scope", "-z", "--get-regexp", _LISTED_KEYS
230+
)
231+
except git.GitCommandError:
232+
continue # not a repository git can open, or nothing listed
233+
fields = listed.split("\0")
234+
for scope, field in zip(fields[0::2], fields[1::2]):
235+
key = field.partition("\n")[0]
236+
if key != "core.hookspath" and scope not in _TRUSTED_SCOPES:
237+
drivers.add(key)
238+
pending.append(submodule)
239+
return drivers
240+
241+
190242
def harden_repo(repo: git.Repo) -> None:
191243
"""Keep every git command run for `repo` from running a program it names.
192244
@@ -223,6 +275,7 @@ def harden_repo(repo: git.Repo) -> None:
223275
"overrides that keep a repository's own configuration from running "
224276
"programs"
225277
)
278+
drivers |= _submodule_drivers(repo)
226279
if drivers:
227280
entries += [(key, "") for key in sorted(drivers)]
228281
repo.git.update_environment(**_config_env(entries))

0 commit comments

Comments
 (0)