Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions doc/cli/clone.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/git_nested/commands/clone.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions src/git_nested/commands/pull.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
50 changes: 50 additions & 0 deletions src/git_nested/content.py
Original file line number Diff line number Diff line change
Expand Up @@ -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`.

Expand Down
66 changes: 66 additions & 0 deletions tests/e2e/test_attributes.py
Original file line number Diff line number Diff line change
@@ -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 == ''
Loading