Skip to content

Commit

Permalink
'git add' is also required if the output file is modified
Browse files Browse the repository at this point in the history
  • Loading branch information
mwouts committed Jan 21, 2021
1 parent 719f5f2 commit 5ef5f0c
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 17 deletions.
27 changes: 17 additions & 10 deletions jupytext/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -738,18 +738,17 @@ def write_function(path, fmt):
if args.pre_commit:
system("git", "add", path)
if args.alert_untracked and is_untracked(path):
log(
"[jupytext] Output file {nb_dest} is not up-to-date in the git index. "
"Please run 'git add {nb_dest}' to fix this".format(nb_dest=path)
)

nonlocal untracked_paths
untracked_paths.append(path)

formats = prepare_notebook_for_save(notebook, config, nb_file)
write_pair(nb_file, formats, write_function)
if untracked_paths:
log(
"[jupytext] Output file {nb_dest} is not tracked in the git index, "
"add it to the index using 'git add' to fix this.".format(
nb_dest=", ".join(untracked_paths)
)
)
return 1

elif (
Expand All @@ -765,8 +764,8 @@ def write_function(path, fmt):

if args.alert_untracked and is_untracked(nb_dest):
log(
"[jupytext] Output file {nb_dest} is not tracked in the git index, "
"add it to the index using 'git add' to fix this.".format(nb_dest=nb_dest)
"[jupytext] Output file {nb_dest} is not up-to-date in the git index. "
"Please run 'git add {nb_dest}' to fix this".format(nb_dest=nb_dest)
)
return 1
else:
Expand All @@ -791,11 +790,19 @@ def notebooks_in_git_index(fmt):


def is_untracked(filepath):
"""Check whether a file is untracked by the git index"""
"""Check whether a file was created or modified and needs to be added to the git index"""
if not filepath:
return False

output = system("git", "ls-files", filepath).strip()
return output == ""
if output == "":
return True

output = system("git", "diff", filepath).strip()
if output != "":
return True

return False


def print_paired_paths(nb_file, fmt):
Expand Down
18 changes: 14 additions & 4 deletions tests/test_alert_untracked.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ def test_is_untracked(tmpdir, cwd_tmpdir, tmp_repo):
tmp_repo.index.commit("test")
assert not is_untracked(file)

# changed
tmpdir.join(file).write("modified file\n")
assert is_untracked(file)

# added, not committed
tmp_repo.git.add(file)
assert not is_untracked(file)


def test_ignore_unmatched_ignores(tmpdir, cwd_tmpdir):
# Unmatched file
Expand All @@ -45,7 +53,7 @@ def test_alert_untracked_alerts(tmpdir, cwd_tmpdir, tmp_repo, capsys):
assert tmpdir.join("test.ipynb").exists()

out = capsys.readouterr()
assert "Output file test.ipynb is not tracked in the git index" in out.out
assert "Please run 'git add test.ipynb'" in out.out


def test_alert_untracked_alerts_when_using_sync(tmpdir, cwd_tmpdir, tmp_repo, capsys):
Expand All @@ -61,10 +69,10 @@ def test_alert_untracked_alerts_when_using_sync(tmpdir, cwd_tmpdir, tmp_repo, ca
assert tmpdir.join("test.ipynb").exists()

out = capsys.readouterr()
assert "Output file test.ipynb is not tracked in the git index" in out.out
assert "Please run 'git add test.ipynb'" in out.out


def test_alert_untracked_not_alerts_for_tracked(tmpdir, cwd_tmpdir, tmp_repo):
def test_alert_untracked_alerts_for_modified(tmpdir, cwd_tmpdir, tmp_repo, capsys):
# write test notebook
nb = new_notebook(cells=[new_markdown_cell("A short notebook")])
write(nb, "test.ipynb")
Expand All @@ -80,4 +88,6 @@ def test_alert_untracked_not_alerts_for_tracked(tmpdir, cwd_tmpdir, tmp_repo):
["--from", "ipynb", "--to", "py:light", "--alert-untracked", "test.ipynb"]
)

assert status == 0
assert status == 1
out = capsys.readouterr()
assert "Please run 'git add test.py'" in out.out
9 changes: 6 additions & 3 deletions tests/test_pre_commit.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ def test_pre_commit_hook_for_new_file(

# try again, it will still fail because the output hasn't been added
with pytest.raises(
HookExecutionError, match="Output file test.py is not tracked in the git index"
HookExecutionError,
match="Please run 'git add test.py'",
):
tmp_repo.index.commit("still failing")

Expand Down Expand Up @@ -99,8 +100,10 @@ def test_pre_commit_hook_for_existing_changed_file(
with pytest.raises(HookExecutionError, match="files were modified by this hook"):
tmp_repo.index.commit("fails")

# TODO: Not sure this is the expected message?
with pytest.raises(HookExecutionError, match="files were modified by this hook"):
with pytest.raises(
HookExecutionError,
match="Please run 'git add test.py'",
):
tmp_repo.index.commit("fails again")

# once we add the changes, it will pass
Expand Down

0 comments on commit 5ef5f0c

Please sign in to comment.