Skip to content

Commit

Permalink
Fixed bug with --namespace and multiple runs, closes #43
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw committed Dec 8, 2021
1 parent 27e755a commit 1e3e961
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 3 deletions.
17 changes: 14 additions & 3 deletions git_history/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ def file(

namespace_id = db["namespaces"].lookup({"name": namespace})

commits_to_skip = get_commit_hashes(db)
commits_to_skip = get_commit_hashes(db, namespace)
if skip_hashes:
commits_to_skip.update(skip_hashes)

Expand Down Expand Up @@ -516,9 +516,20 @@ def create_views(db, namespace):
)


def get_commit_hashes(db):
def get_commit_hashes(db, namespace):
return (
set(r[0] for r in db.execute("select hash from commits").fetchall())
set(
r[0]
for r in db.execute(
"""
select hash from commits
where namespace = (
select id from namespaces where name = ?
)
""",
[namespace],
).fetchall()
)
if db["commits"].exists()
else set()
)
Expand Down
57 changes: 57 additions & 0 deletions tests/test_git_history.py
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,63 @@ def test_file_with_id_resume(repo, tmpdir, namespace):
]


def test_file_with_id_resume_two_namespaces(repo, tmpdir):
# https://github.com/simonw/git-history/issues/43
runner = CliRunner()
db_path = str(tmpdir / "db.db")

def run(namespace):
result = runner.invoke(
cli,
[
"file",
db_path,
str(repo / "items.json"),
"--repo",
str(repo),
"--id",
"product_id",
"--namespace",
namespace,
],
)
assert result.exit_code == 0

for namespace in ("one", "two"):
run(namespace)

# Now modify items.json, commit and run again
(repo / "items.json").write_text(
json.dumps(
[
{"product_id": 1, "name": "Gin"},
{"product_id": 2, "name": "Tonic 2"},
{"product_id": 3, "name": "Rum Pony"},
]
),
"utf-8",
)
subprocess.call(git_commit + ["-a", "-m", "another"], cwd=str(repo))
for namespace in ("one", "two"):
run(namespace)

db = sqlite_utils.Database(db_path)
assert set(db.table_names()) == {
"namespaces",
"commits",
"one",
"one_version",
"columns",
"one_changed",
"two",
"two_version",
"two_changed",
}
# Should be five versions: Gin, Tonic -> Tonic 2, Rum -> Rum Pony
assert db["one_version"].count == 5
assert db["two_version"].count == 5


@pytest.mark.parametrize("namespace", (None, "custom"))
def test_file_with_id_full_versions(repo, tmpdir, namespace):
runner = CliRunner()
Expand Down

0 comments on commit 1e3e961

Please sign in to comment.