[BOLT][UTILS] Add nfc-check-setup --switch-back option

Add an option to switch repo revision back, handling stashing automatically.

Reviewed By: rafauler

Differential Revision: https://reviews.llvm.org/D128243
This commit is contained in:
Amir Ayupov 2022-08-17 17:37:13 -07:00
parent e3d64ccf9f
commit 330eec139e

View File

@ -26,7 +26,10 @@ def main():
Passes the options through to llvm-bolt-wrapper. Passes the options through to llvm-bolt-wrapper.
''')) '''))
parser.add_argument('build_dir', nargs='?', default=os.getcwd(), parser.add_argument('build_dir', nargs='?', default=os.getcwd(),
help='Path to BOLT build directory, default is current directory') help='Path to BOLT build directory, default is current '
'directory')
parser.add_argument('--switch-back', default=False, action='store_true',
help='Checkout back to the starting revision')
args, wrapper_args = parser.parse_known_args() args, wrapper_args = parser.parse_known_args()
bolt_path = f'{args.build_dir}/bin/llvm-bolt' bolt_path = f'{args.build_dir}/bin/llvm-bolt'
@ -50,6 +53,11 @@ def main():
# memorize the old hash for logging # memorize the old hash for logging
old_ref = get_git_ref_or_rev(source_dir) old_ref = get_git_ref_or_rev(source_dir)
# determine whether a stash is needed
stash = subprocess.run(shlex.split("git status --porcelain"), cwd=source_dir,
stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
text=True).stdout
if stash:
# save local changes before checkout # save local changes before checkout
subprocess.run(shlex.split("git stash"), cwd=source_dir) subprocess.run(shlex.split("git stash"), cwd=source_dir)
# check out the previous commit # check out the previous commit
@ -70,12 +78,17 @@ def main():
f.write(ini) f.write(ini)
# symlink llvm-bolt-wrapper # symlink llvm-bolt-wrapper
os.symlink(wrapper_path, bolt_path) os.symlink(wrapper_path, bolt_path)
print(f"The repository {source_dir} has been switched from rev {old_ref} " if args.switch_back:
if stash:
subprocess.run(shlex.split("git stash pop"), cwd=source_dir)
subprocess.run(shlex.split(f"git checkout {old_ref}"), cwd=source_dir)
else:
print(f"The repository {source_dir} has been switched from {old_ref} "
f"to {new_ref}. Local changes were stashed. Switch back using\n\t" f"to {new_ref}. Local changes were stashed. Switch back using\n\t"
f"git checkout {old_ref}\n" f"git checkout {old_ref}\n")
"Current build directory is ready to run BOLT tests, e.g.\n\t" print(f"Build directory {args.build_dir} is ready to run BOLT tests, e.g.\n"
"bin/llvm-lit -sv tools/bolt/test\nor\n\t" "\tbin/llvm-lit -sv tools/bolt/test\nor\n"
"bin/llvm-lit -sv tools/bolttests") "\tbin/llvm-lit -sv tools/bolttests")
if __name__ == "__main__": if __name__ == "__main__":