From 497fbad86481f29d7126357b04b774b4af865223 Mon Sep 17 00:00:00 2001 From: Thorsten Klein Date: Fri, 13 Nov 2026 03:55:31 +0100 Subject: [PATCH] fix: keep files of nested repos as upstream has them For a submodule, the parent's .gitattributes does not count. For a nested repo it does. A rule like `*.bat text eol=crlf` then shows upstream's CRLF files as changed. The tree is never clean, and the next pull refuses. After a clone or pull, git-nested now adds `-text` for these files to the root .gitattributes, in a commit of its own. The content stays as upstream has it. --- doc/cli/clone.md | 4 ++ src/git_nested/commands/clone.py | 1 + src/git_nested/commands/pull.py | 1 + src/git_nested/content.py | 50 ++++++++++++++++++++++++ tests/e2e/test_attributes.py | 66 ++++++++++++++++++++++++++++++++ 5 files changed, 122 insertions(+) create mode 100644 tests/e2e/test_attributes.py diff --git a/doc/cli/clone.md b/doc/cli/clone.md index e010042a..66cec6f4 100644 --- a/doc/cli/clone.md +++ b/doc/cli/clone.md @@ -111,6 +111,10 @@ If nothing changed, it does nothing and says "already up to date". - `--adopt` can not be used with `--filter` or `--recurse-submodules`. - Without `--recurse-submodules`, submodules of upstream stay empty, and git-nested warns about it. See [Upstream submodules](../guides/submodules.md). +- If your `.gitattributes` would change files of the nested repository, + for example `*.bat text eol=crlf`, `clone` and `pull` add `-text` for + these files to `.gitattributes`, in a commit of their own. So the files + stay as upstream has them. - If upstream is empty, `clone` stops. Use [`init`](init.md) and then [`push`](push.md) to fill it. - With [patches](../guides/patches.md), `--force` starts over: it takes diff --git a/src/git_nested/commands/clone.py b/src/git_nested/commands/clone.py index 5fa5f9a3..1e660838 100644 --- a/src/git_nested/commands/clone.py +++ b/src/git_nested/commands/clone.py @@ -178,6 +178,7 @@ def _clone_into( subdir_worktree=None, command='clone', ) + content.keep_upstream_bytes(git, flags, subdir) if flags.recurse_submodules: nest_submodules(git, flags, config, subdir) else: diff --git a/src/git_nested/commands/pull.py b/src/git_nested/commands/pull.py index cd8bf656..877fdedc 100644 --- a/src/git_nested/commands/pull.py +++ b/src/git_nested/commands/pull.py @@ -321,6 +321,7 @@ def cmd_pull(ctx: CommandContext) -> None: branch, subdir_worktree = pulled output.verbose(f"committing the new {branch} content") _commit_pull(ctx, config, subdir, gitnested, subref, branch, subdir_worktree) + content.keep_upstream_bytes(git, flags, subdir) follow_inner(ctx, config, subdir) if flags.recurse_submodules: clone.nest_submodules(git, flags, config, subdir) diff --git a/src/git_nested/content.py b/src/git_nested/content.py index 30867384..8822f1f6 100644 --- a/src/git_nested/content.py +++ b/src/git_nested/content.py @@ -191,6 +191,56 @@ def matches_upstream(git: GitRunner, subdir: Path, upstream_commit: str, ignored ) +_GITATTRIBUTES = Path('.gitattributes') +_KEEP_BYTES_HEADER = "# git-nested: files that stay as upstream has them\n" + + +def keep_upstream_bytes(git: GitRunner, flags: Flags, subdir: Path) -> None: + """Stop the parent's .gitattributes from changing files in subdir. + + For a submodule, the parent's attributes do not count. For a nested + repo they do. A rule like '*.bat text eol=crlf' then shows upstream's + files as changed. This adds '-text' for them to .gitattributes, in a + commit of its own. + """ + changed = [path for path in git.check_output(['diff', '--name-only', '-z', '--', subdir]).split('\0') if path] + paths = [path for path in changed if _only_attributes_differ(git, path)] + if not paths: + return + text = _GITATTRIBUTES.read_text(encoding='utf-8') if _GITATTRIBUTES.is_file() else '' + with _GITATTRIBUTES.open('w', encoding='utf-8', newline='\n') as f: + f.write(_with_rules(text, paths)) + git.run(['add', '--', _GITATTRIBUTES, *paths]) + output.say(f"{subdir.as_posix()}: .gitattributes keeps {len(paths)} files as upstream has them") + message = f"git nested: keep {subdir.as_posix()} files as upstream has them" + own_message = cast('Flags', dataclasses.replace(flags, message_file=None)) + commit_gitnested_update(git, own_message, subdir, message) + + +def _with_rules(text: str, paths: list[str]) -> str: + """`text` of .gitattributes, with '-text' for `paths` at the end.""" + if text and not text.endswith('\n'): + text += '\n' + if _KEEP_BYTES_HEADER not in text: + text += _KEEP_BYTES_HEADER + return text + ''.join(f"{_attribute_pattern(path)} -text\n" for path in paths) + + +def _only_attributes_differ(git: GitRunner, path: str) -> bool: + """Whether the file has the bytes of its blob, and git shows it as changed only because of attributes.""" + blob = git.check_output(['ls-files', '-s', '--', path]).split()[1:2] + return bool(blob) and git.check_output(['hash-object', '--no-filters', '--', path]) == blob[0] + + +def _attribute_pattern(path: str) -> str: + """`path` as a .gitattributes pattern for this one file, from the top.""" + escaped = ''.join(f'\\{char}' if char in '*?[\\' else char for char in path) + if any(char in escaped for char in ' "#') or not escaped.isascii(): + # C-style quotes: a backslash of the pattern is written twice + return '"/' + escaped.replace('\\', '\\\\').replace('"', '\\"') + '"' + return f'/{escaped}' + + def commit_gitnested_update(git: GitRunner, flags: Flags, subdir: Path, msg: str) -> None: """Commit what is staged onto the current branch, with --file's message if given, else `msg`. diff --git a/tests/e2e/test_attributes.py b/tests/e2e/test_attributes.py new file mode 100644 index 00000000..1c6d104b --- /dev/null +++ b/tests/e2e/test_attributes.py @@ -0,0 +1,66 @@ +"""Tests for the parent's .gitattributes and the files of a nested repo""" + +from conftest import cmd_git_nested + + +def _parent_wants_crlf(env): + """foo says: .bat files have CRLF in the working tree and LF in git.""" + foo = env.workspace / 'foo' + # No newline at the end: the new rules must still start on a line of their own + (foo / ".gitattributes").write_text("*.bat text eol=crlf") + env.run(['git', 'add', '.gitattributes'], cwd=foo) + env.run(['git', 'commit', '-qm', 'bat files'], cwd=foo) + return foo + + +def _upstream_bat(env, name, data): + """Commit `data` as it is to upstream bar.""" + bar = env.workspace / 'bar' + (bar / name).write_bytes(data) + env.run(['git', '-c', 'core.autocrlf=false', 'add', name], cwd=bar) + env.run(['git', 'commit', '-qm', name], cwd=bar) + env.run(['git', 'push', '-q'], cwd=bar) + + +def test_clone_keeps_crlf_files_as_upstream_has_them(foo_bar_cloned): + env = foo_bar_cloned + foo = _parent_wants_crlf(env) + _upstream_bat(env, 'make.bat', b"echo hi\r\n") + _upstream_bat(env, 'plain.bat', b"echo plain\n") + + result = cmd_git_nested(['clone', (env.upstream / 'bar').as_posix()], foo) + + assert "bar: .gitattributes keeps 1 files as upstream has them" in result.output + assert env.run(['git', 'status', '--porcelain'], cwd=foo).output == '' + assert "/bar/make.bat -text" in (foo / '.gitattributes').read_text() + assert 'plain.bat' not in (foo / '.gitattributes').read_text() + assert env.run(['git', 'show', 'HEAD:bar/make.bat'], cwd=foo, text=False).stdout == b"echo hi\r\n" + assert cmd_git_nested('verify bar', cwd=foo).returncode == 0 + + +def test_pull_adds_rules_only_for_new_files(foo_bar_cloned): + env = foo_bar_cloned + foo = _parent_wants_crlf(env) + _upstream_bat(env, 'make.bat', b"echo hi\r\n") + cmd_git_nested(['clone', (env.upstream / 'bar').as_posix()], foo) + _upstream_bat(env, 'make.bat', b"echo changed\r\n") + _upstream_bat(env, 'new.bat', b"echo new\r\n") + + cmd_git_nested('pull bar', cwd=foo) + + rules = (foo / '.gitattributes').read_text() + assert rules.count("/bar/make.bat -text") == 1 + assert "/bar/new.bat -text" in rules + assert rules.count("# git-nested") == 1 + assert env.run(['git', 'status', '--porcelain'], cwd=foo).output == '' + + +def test_a_file_name_with_special_characters(foo_bar_cloned): + env = foo_bar_cloned + foo = _parent_wants_crlf(env) + _upstream_bat(env, 'my [x] file.bat', b"echo hi\r\n") + + cmd_git_nested(['clone', (env.upstream / 'bar').as_posix()], foo) + + assert '"/bar/my \\\\[x] file.bat" -text' in (foo / '.gitattributes').read_text() + assert env.run(['git', 'status', '--porcelain'], cwd=foo).output == ''