Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change behavior of --base-commit and --outdated-since options #22

Merged
merged 7 commits into from
Dec 29, 2023
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
55 changes: 35 additions & 20 deletions tests/test_check_outdated_articles.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,8 @@ def test__list_modified_translations(self, root):

utils.create_files(root, *((path, '# Article') for path in article_paths))
utils.stage_all_and_commit("add article title")
commit_hash = utils.get_last_commit_hash()

# note that at least two existing commits are necessary to get a diff using `revision^`
modified_translations = outdater.list_modified_translations(commit_hash)
modified_translations = outdater.list_modified_translations("HEAD^")

assert multiset(modified_translations) == multiset(utils.remove(article_paths, "en.md", "TEMPLATE.md"))

Expand All @@ -49,9 +47,8 @@ def test__list_modified_translations(self, root):
utils.take(article_paths, "fr.md"))
)
utils.stage_all_and_commit("add article content")
commit_hash = utils.get_last_commit_hash()

modified_translations = outdater.list_modified_translations(commit_hash)
modified_translations = outdater.list_modified_translations("HEAD^")

assert multiset(modified_translations) == multiset(utils.take(article_paths, "fr.md"))

Expand All @@ -70,16 +67,14 @@ def test__list_modified_originals(self, root):

utils.create_files(root, *((path, '# Article') for path in article_paths))
utils.stage_all_and_commit("add some articles")
commit_hash = utils.get_last_commit_hash()

utils.create_files(root, *zip(article_paths[0:2], [
'# Article\n\nThis is an article in English.',
'# Article\n\nThis is another article in English.',
]))
utils.stage_all_and_commit("add article content")
commit_hash = utils.get_last_commit_hash()

modified_originals = outdater.list_modified_originals(commit_hash)
modified_originals = outdater.list_modified_originals("HEAD^")
assert multiset(modified_originals) == multiset(utils.take(article_paths, "en.md"))

def test__list_outdated_translations(self, root):
Expand Down Expand Up @@ -223,7 +218,7 @@ def test__full_autofix_flow(self, root):
utils.stage_all_and_commit("modify english articles")
commit_hash_2 = utils.get_last_commit_hash()

exit_code = outdater.main("--base-commit", commit_hash_2, f"{outdater.AUTOFIX_FLAG}")
exit_code = outdater.main("--base-commit", "HEAD^", "--outdated-since", commit_hash_2, f"{outdater.AUTOFIX_FLAG}")

assert exit_code == 0

Expand Down Expand Up @@ -300,17 +295,15 @@ def test__full_autocommit_flow(self, root):
utils.stage_all_and_commit("modify english articles")
commit_hash_2 = utils.get_last_commit_hash()

exit_code = outdater.main("--base-commit", commit_hash_2, f"{outdater.AUTOFIX_FLAG}", f"{outdater.AUTOCOMMIT_FLAG}")
exit_code = outdater.main("--base-commit", "HEAD^", "--outdated-since", commit_hash_2, f"{outdater.AUTOFIX_FLAG}", f"{outdater.AUTOCOMMIT_FLAG}")

assert exit_code == 0

commit_hash_3 = utils.get_last_commit_hash()

assert commit_hash_3 != commit_hash_2
assert commit_hash_2 != utils.get_last_commit_hash()

assert utils.get_changed_files() == []

outdated_translations = outdater.list_modified_translations(commit_hash_3)
outdated_translations = outdater.list_modified_translations("HEAD^")

non_chinese_translations = utils.remove(article_paths, "en.md", "zh-tw.md")

Expand Down Expand Up @@ -384,7 +377,7 @@ def test__full_autofix_flow_with_changed_root(self, root):
commit_hash_2 = utils.get_last_commit_hash()

cd = file_utils.ChangeDirectory("wiki")
exit_code = outdater.main("--root", "..", "--base-commit", commit_hash_2, f"{outdater.AUTOFIX_FLAG}")
exit_code = outdater.main("--root", "..", "--base-commit", "HEAD^", "--outdated-since", commit_hash_2, f"{outdater.AUTOFIX_FLAG}")
del cd

assert exit_code == 0
Expand Down Expand Up @@ -463,18 +456,16 @@ def test__full_autocommit_flow_with_changed_root(self, root):
commit_hash_2 = utils.get_last_commit_hash()

cd = file_utils.ChangeDirectory("wiki")
exit_code = outdater.main("--root", "..", "--base-commit", commit_hash_2, f"{outdater.AUTOFIX_FLAG}", f"{outdater.AUTOCOMMIT_FLAG}")
exit_code = outdater.main("--root", "..", "--base-commit", "HEAD^", "--outdated-since", commit_hash_2, f"{outdater.AUTOFIX_FLAG}", f"{outdater.AUTOCOMMIT_FLAG}")
del cd

assert exit_code == 0

commit_hash_3 = utils.get_last_commit_hash()

assert commit_hash_3 != commit_hash_2
assert commit_hash_2 != utils.get_last_commit_hash()

assert utils.get_changed_files() == []

outdated_translations = outdater.list_modified_translations(commit_hash_3)
outdated_translations = outdater.list_modified_translations("HEAD^")

non_chinese_translations = utils.remove(article_paths, "en.md", "zh-tw.md")

Expand Down Expand Up @@ -510,3 +501,27 @@ def test__full_autocommit_flow_with_changed_root(self, root):
log = git_utils.git("--no-pager", "log", "--pretty=oneline").splitlines()

assert len(log) == 4

def test__full_autofix_flow_with_invalid_outdated_since(self, root):
utils.set_up_dummy_repo()
article_paths = [
'wiki/Article/en.md',
'wiki/Article/fr.md',
'wiki/Article/pt-br.md',
'wiki/Article/zh-tw.md',
]

utils.create_files(root, *((path, '# Article') for path in article_paths))
utils.stage_all_and_commit("add articles")

utils.create_files(root, *(
(article_path, '# Article\n\nThis is an article in English.') for article_path in
utils.take(article_paths, "en.md")
))
utils.stage_all_and_commit("modify english articles")

exit_code = outdater.main("--base-commit", "HEAD^", f"{outdater.AUTOFIX_FLAG}")

assert exit_code == 1

assert utils.get_changed_files() == []
1 change: 1 addition & 0 deletions tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def stage_all_and_commit(commit_message):

def set_up_dummy_repo():
git_utils.git("init")
git_utils.git("branch", "-m", "master")
git_utils.git("config", "user.name", "John Smith")
git_utils.git("config", "user.email", "[email protected]")
git_utils.git("config", "commit.gpgsign", "false")
Expand Down
4 changes: 2 additions & 2 deletions tests/visual/test_check_outdated_articles.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def check_outdated_articles_test():
utils.stage_all_and_commit("modify english article")
commit_hash = utils.get_last_commit_hash()

outdater.main("--base-commit", commit_hash)
outdater.main("--base-commit", "HEAD^", "--outdated-since", commit_hash)


def check_outdated_articles_test_no_recommend_autofix():
Expand All @@ -52,7 +52,7 @@ def check_outdated_articles_test_no_recommend_autofix():
utils.stage_all_and_commit("modify english article")
commit_hash = utils.get_last_commit_hash()

outdater.main("--no-recommend-autofix", "--base-commit", commit_hash)
outdater.main("--no-recommend-autofix", "--base-commit", "HEAD^", "--outdated-since", commit_hash)


test = VisualTest(
Expand Down
2 changes: 1 addition & 1 deletion wikitools/git_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def git(*args, expected_code=0):


def git_diff(*file_paths, base_commit=""):
res = git("diff", "--diff-filter=d", "--name-only", f"{base_commit}^", "--", *file_paths)
res = git("diff", "--diff-filter=d", "--name-only", base_commit, "--", *file_paths)
return res.splitlines()


Expand Down
72 changes: 33 additions & 39 deletions wikitools_cli/commands/check_outdated_articles.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ def print_bad_hash_error(*filenames, outdated_hash=None):

def parse_args(args):
parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawTextHelpFormatter, usage="%(prog)s check-outdated-articles [options]")
parser.add_argument("-b", "--base-commit", help="commit since which to look for changes")
parser.add_argument("-o", "--outdated-since", default="", help=f"commit hash for the {OUTDATED_HASH_TAG} tag, uses the value of --base-commit if unspecified")
parser.add_argument("-b", "--base-commit", default="master", help="commit since which to look for changes")
parser.add_argument("-o", "--outdated-since", help=f"commit hash for the {OUTDATED_HASH_TAG} tag, uses the first commit where HEAD diverged from master if unspecified")
parser.add_argument("-a", "--all", default=False, action="store_true", help="look for incorrect hashes in all outdated articles")
parser.add_argument(f"{AUTOFIX_FLAG_SHORT}", f"{AUTOFIX_FLAG}", default=False, action="store_true", help=f"automatically add `{OUTDATED_HASH_TAG}: {{hash}}` to outdated articles")
parser.add_argument(f"{AUTOCOMMIT_FLAG_SHORT}", f"{AUTOCOMMIT_FLAG}", default=False, action="store_true", help=f"automatically commit changes")
Expand Down Expand Up @@ -106,7 +106,7 @@ def list_modified_originals(base_commit):
return git_utils.git_diff('wiki/**/en.md', base_commit=base_commit)


def outdate_translations(*translations, outdated_hash=""):
def outdate_translations(*translations, outdated_hash):
"""
Write outdated hash and marker to several translations at once.
"""
Expand Down Expand Up @@ -151,62 +151,56 @@ def main(*args):
if args.root:
changed_cwd = file_utils.ChangeDirectory(args.root)

base_commit = args.base_commit or git_utils.get_first_branch_commit()

if not base_commit and not args.all:
print(f"{console.red('Error:')} neither --base-commit (unable to obtain automatically) nor --all were specified; nothing to do.")
exit_code = 1
if args.root:
del changed_cwd
return exit_code

modified_translations = set()
with_bad_hashes = list()

if not args.all and base_commit:
modified_translations = set(list_modified_translations(base_commit))
with_bad_hashes = list(check_commit_hashes(modified_translations))
elif args.all:
if args.all:
all_translations = file_utils.list_all_translations(file_utils.list_all_article_dirs())
with_bad_hashes = list(check_commit_hashes(all_translations))
else:
modified_translations = set(list_modified_translations(args.base_commit))
with_bad_hashes = list(check_commit_hashes(modified_translations))

outdated_hash = None

if with_bad_hashes:
print_bad_hash_error(*with_bad_hashes, outdated_hash=args.outdated_since or base_commit)
outdated_hash = args.outdated_since or git_utils.get_first_branch_commit()
print_bad_hash_error(*with_bad_hashes, outdated_hash=outdated_hash)
print()
exit_code = 1

if not base_commit:
if args.root:
del changed_cwd
return exit_code

modified_originals = list_modified_originals(base_commit)
modified_originals = list_modified_originals(args.base_commit)
if modified_originals:
all_translations = file_utils.list_all_translations(sorted(os.path.dirname(tl) for tl in modified_originals))
translations_to_outdate = list(list_outdated_translations(all_translations, modified_translations))
if translations_to_outdate:
outdated_hash = args.outdated_since or base_commit
outdated_hash = outdated_hash or args.outdated_since or git_utils.get_first_branch_commit()

should_autofix = getattr(args, AUTOFIX_FLAG[2:], False)
should_autocommit = getattr(args, AUTOCOMMIT_FLAG[2:], False)
if should_autofix:
print(console.green('{} specified, outdating translations...'.format(AUTOFIX_FLAG)))
outdate_translations(*translations_to_outdate, outdated_hash=outdated_hash)
if not should_autocommit:
print(console.green('Done! To commit the changes, run:'))
print(console.green('\tgit add {}; git commit -m "outdate translations"'.format(
" ".join(translations_to_outdate)
)))

if outdated_hash:
outdate_translations(*translations_to_outdate, outdated_hash=outdated_hash)
if not should_autocommit:
print(console.green('Done! To commit the changes, run:'))
print(console.green('\tgit add {}; git commit -m "outdate translations"'.format(
" ".join(translations_to_outdate)
)))
else:
print(console.green('{} specified, committing changes...'.format(AUTOCOMMIT_FLAG)))
git_utils.git("add", *translations_to_outdate)
git_utils.git("commit", "-m", "outdate translations")
print(console.green('Done! The changes have been committed for you.'))
print()
print(git_utils.git("show", "HEAD", "--no-patch"))
print("Changed files:")
for file_path in translations_to_outdate:
print(console.green(f"* {file_path}"))
else:
print(console.green('{} specified, committing changes...'.format(AUTOCOMMIT_FLAG)))
git_utils.git("add", *translations_to_outdate)
git_utils.git("commit", "-m", "outdate translations")
print(console.green('Done! The changes have been committed for you.'))
print()
print(git_utils.git("show", "HEAD", "--no-patch"))
print("Changed files:")
for file_path in translations_to_outdate:
print(console.green(f"* {file_path}"))
print(f"{console.red('Error:')} --outdated-since was not specified and HEAD has not diverged from master.")
exit_code = 1
else:
print_translations_to_outdate(*translations_to_outdate, outdated_hash=outdated_hash, no_recommend_autofix=args.no_recommend_autofix)
exit_code = 1
Expand Down
Loading