From 9f4d2e8930e1f2ef304b34ec2b7200fd3bd672ff Mon Sep 17 00:00:00 2001 From: clayton Date: Wed, 6 Dec 2023 22:28:13 -0800 Subject: [PATCH 1/7] Default `base_commit` to master, change diff to not select parent --- wikitools/git_utils.py | 2 +- .../commands/check_outdated_articles.py | 30 +++++-------------- 2 files changed, 9 insertions(+), 23 deletions(-) diff --git a/wikitools/git_utils.py b/wikitools/git_utils.py index b97d3cb..7591d72 100644 --- a/wikitools/git_utils.py +++ b/wikitools/git_utils.py @@ -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() diff --git a/wikitools_cli/commands/check_outdated_articles.py b/wikitools_cli/commands/check_outdated_articles.py index bc8ad14..cd182ae 100644 --- a/wikitools_cli/commands/check_outdated_articles.py +++ b/wikitools_cli/commands/check_outdated_articles.py @@ -71,7 +71,7 @@ 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("-b", "--base-commit", default="master", 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("-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") @@ -151,41 +151,27 @@ 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)) if with_bad_hashes: - print_bad_hash_error(*with_bad_hashes, outdated_hash=args.outdated_since or base_commit) + print_bad_hash_error(*with_bad_hashes, outdated_hash=args.outdated_since or args.base_commit) 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 = args.outdated_since or args.base_commit should_autofix = getattr(args, AUTOFIX_FLAG[2:], False) should_autocommit = getattr(args, AUTOCOMMIT_FLAG[2:], False) From 11a727e1259b2305d25f3023790634cbec0ea61c Mon Sep 17 00:00:00 2001 From: clayton Date: Wed, 6 Dec 2023 22:28:14 -0800 Subject: [PATCH 2/7] Default `outdated_since` to previous value --- wikitools_cli/commands/check_outdated_articles.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/wikitools_cli/commands/check_outdated_articles.py b/wikitools_cli/commands/check_outdated_articles.py index cd182ae..dd8e304 100644 --- a/wikitools_cli/commands/check_outdated_articles.py +++ b/wikitools_cli/commands/check_outdated_articles.py @@ -72,7 +72,7 @@ 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", default="master", 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("-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") @@ -161,8 +161,11 @@ def main(*args): 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 args.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 @@ -171,7 +174,7 @@ def main(*args): 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 args.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) From ad890f30b1a94194e039f7a39892f50178c63d99 Mon Sep 17 00:00:00 2001 From: clayton Date: Wed, 6 Dec 2023 22:28:14 -0800 Subject: [PATCH 3/7] Update tests --- tests/test_check_outdated_articles.py | 31 ++++++++++----------------- 1 file changed, 11 insertions(+), 20 deletions(-) diff --git a/tests/test_check_outdated_articles.py b/tests/test_check_outdated_articles.py index dfa5b68..73efe2b 100644 --- a/tests/test_check_outdated_articles.py +++ b/tests/test_check_outdated_articles.py @@ -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")) @@ -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")) @@ -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): @@ -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 @@ -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") @@ -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 @@ -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") From 915c32007a47025f1471e82f574c04f77e3521e8 Mon Sep 17 00:00:00 2001 From: clayton Date: Thu, 7 Dec 2023 18:02:22 -0800 Subject: [PATCH 4/7] Fix visual tests --- tests/visual/test_check_outdated_articles.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/visual/test_check_outdated_articles.py b/tests/visual/test_check_outdated_articles.py index d625640..136f758 100644 --- a/tests/visual/test_check_outdated_articles.py +++ b/tests/visual/test_check_outdated_articles.py @@ -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(): @@ -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( From a73e61035f351f30c7d5db2a00bebcec6ea070f6 Mon Sep 17 00:00:00 2001 From: clayton Date: Thu, 7 Dec 2023 18:02:22 -0800 Subject: [PATCH 5/7] Error when trying to write null outdated_hash, add test --- tests/test_check_outdated_articles.py | 24 +++++++++++++ .../commands/check_outdated_articles.py | 35 +++++++++++-------- 2 files changed, 44 insertions(+), 15 deletions(-) diff --git a/tests/test_check_outdated_articles.py b/tests/test_check_outdated_articles.py index 73efe2b..3b74592 100644 --- a/tests/test_check_outdated_articles.py +++ b/tests/test_check_outdated_articles.py @@ -501,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() == [] diff --git a/wikitools_cli/commands/check_outdated_articles.py b/wikitools_cli/commands/check_outdated_articles.py index dd8e304..fd26d6b 100644 --- a/wikitools_cli/commands/check_outdated_articles.py +++ b/wikitools_cli/commands/check_outdated_articles.py @@ -180,22 +180,27 @@ def main(*args): 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 From 2217ab58d96caec1f95968941c5449e226d359e7 Mon Sep 17 00:00:00 2001 From: clayton Date: Thu, 7 Dec 2023 18:02:22 -0800 Subject: [PATCH 6/7] Ensure master branch is created for dummy repo --- tests/utils.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/utils.py b/tests/utils.py index ee98bf9..ba229e6 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -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", "john.smith@example.com") git_utils.git("config", "commit.gpgsign", "false") From 2f8f7c7e98f52b292d2f1f6ddbd942179f36a2c3 Mon Sep 17 00:00:00 2001 From: clayton Date: Thu, 7 Dec 2023 18:04:51 -0800 Subject: [PATCH 7/7] No default for outdated_hash in outdate_translations --- wikitools_cli/commands/check_outdated_articles.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wikitools_cli/commands/check_outdated_articles.py b/wikitools_cli/commands/check_outdated_articles.py index fd26d6b..9f8c6bc 100644 --- a/wikitools_cli/commands/check_outdated_articles.py +++ b/wikitools_cli/commands/check_outdated_articles.py @@ -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. """