Add option to force update of database records

This commit is contained in:
Avram Lubkin 2023-07-12 11:00:46 -04:00 коммит произвёл Avram Lubkin
Родитель bd590c15c9
Коммит 922d010da8
4 изменённых файлов: 44 добавлений и 10 удалений

Просмотреть файл

@ -92,7 +92,7 @@ class Session:
if options.upstream:
LOGGER.info("Begin monitoring upstream")
Upstream(self.config, self.database, repo).process_commits()
Upstream(self.config, self.database, repo).process_commits(options.force_update)
LOGGER.info("Finishing monitoring upstream")
if options.downstream:

Просмотреть файл

@ -89,6 +89,11 @@ def get_run_parser():
metavar="APPROXIDATE",
help="Passed to underlying git commands. By default, the history is not limited",
)
parser.add_argument(
"--force-update",
action="store_true",
help="Force update to existing upstream patch records",
)
return parser

Просмотреть файл

@ -22,13 +22,14 @@ class Upstream:
self.database = database
self.repo = repo
def process_commits(self):
def process_commits(self, force_update=False):
"""
Generate patches for commits affecting tracked paths
"""
paths = self.repo.get_tracked_paths(self.config.upstream.sections)
added = 0
updated = 0
total = 0
# We use `--min-parents=1 --max-parents=1` to avoid both merges and graft commits.
@ -42,14 +43,42 @@ class Upstream:
):
total += 1
with self.database.get_session() as session:
# See if commit ID is in database
if (
session.query(PatchData.commitID)
.filter_by(commitID=commit.hexsha)
.one_or_none()
is None
):
# Query database for commit
if force_update:
query = session.query(PatchData)
else:
query = session.query(PatchData.commitID)
patch = query.filter_by(commitID=commit.hexsha).one_or_none()
# If commit is missing, add it
if patch is None:
session.add(PatchData.create(commit, paths))
added += 1
# If commit is present, optionally update
elif force_update:
# Get a local patch object
patch_data = PatchData.create(commit, paths)
# Iterate through the columns
record_updated = False
for column in (
col.name for col in patch_data.__table__.columns if not col.primary_key
):
# Skip commit ID
if column == "commitID":
continue
# If the new value is different, update it
new_value = getattr(patch_data, column)
if getattr(patch, column) != new_value:
LOGGER.info("Updating %s for %s", column, commit.hexsha)
setattr(patch, column, new_value)
record_updated = True
if record_updated:
updated += 1
LOGGER.info("%d of %d patches added to database.", added, total)
if force_update:
LOGGER.info("%d of %d patches updated in database.", updated, total)

Просмотреть файл

@ -26,7 +26,7 @@ def get_filenames(commit: git.Commit) -> List[str]:
return []
diffs = commit.tree.diff(commit.parents[0])
# Sometimes a path is in A and not B but we want all filenames.
return list(
return sorted(
{diff.a_path for diff in diffs if diff.a_path is not None}
| {diff.b_path for diff in diffs if diff.b_path is not None}
)