sources: Set[Path] = set()
root = ctx.obj["root"]
- exclude_is_None = exclude is None
+ using_default_exclude = exclude is None
exclude = re_compile_maybe_verbose(DEFAULT_EXCLUDES) if exclude is None else exclude
- gitignore = None # type: Optional[PathSpec]
+ gitignore: Optional[PathSpec] = None
root_gitignore = get_gitignore(root)
for s in src:
sources.add(p)
elif p.is_dir():
- if exclude_is_None:
- p_gitignore = get_gitignore(p)
- # No need to use p's gitignore if it is identical to root's gitignore
- # (i.e. root and p point to the same directory).
- if root_gitignore == p_gitignore:
- gitignore = root_gitignore
- else:
- gitignore = root_gitignore + p_gitignore
+ if using_default_exclude:
+ gitignore = {
+ root: root_gitignore,
+ root / p: get_gitignore(p),
+ }
sources.update(
gen_python_files(
p.iterdir(),
valid by calling :func:`assert_equivalent` and :func:`assert_stable` on it.
`mode` is passed to :func:`format_str`.
"""
- if not src_contents.strip():
+ if not mode.preview and not src_contents.strip():
raise NothingChanged
if mode.is_ipynb:
Operate cell-by-cell, only on code cells, only for Python notebooks.
If the ``.ipynb`` originally had a trailing newline, it'll be preserved.
"""
+ if mode.preview and not src_contents:
+ raise NothingChanged
+
trailing_newline = src_contents[-1] == "\n"
modified = False
nb = json.loads(src_contents)
dst_contents = []
for block in dst_blocks:
dst_contents.extend(block.all_lines())
+ if mode.preview and not dst_contents:
+ # Use decode_bytes to retrieve the correct source newline (CRLF or LF),
+ # and check if normalized_content has more than one line
+ normalized_content, _, newline = decode_bytes(src_contents.encode("utf-8"))
+ if "\n" in normalized_content:
+ return newline
+ return ""
return "".join(dst_contents)