Skip to content

Commit 09363a8

Browse files
Handling switch back
1 parent ca36aa0 commit 09363a8

File tree

1 file changed

+31
-15
lines changed

1 file changed

+31
-15
lines changed

bolt/utils/nfc-check-setup.py

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,23 @@ def get_git_ref_or_rev(dir: str) -> str:
4242
cmd_rev = "git rev-parse --short HEAD"
4343
return subprocess.check_output(shlex.split(cmd_rev), cwd=dir, text=True).strip()
4444

45+
def switch_back(
46+
switch_back: bool, stash: bool, source_dir: str, old_ref: str, new_ref: str
47+
):
48+
# Switch back to the current revision if needed and inform the user of where
49+
# the HEAD is. Must be called after checking out the previous commit on all
50+
# exit paths.
51+
if switch_back:
52+
print("Switching back to current revision..")
53+
if stash:
54+
subprocess.run(shlex.split("git stash pop"), cwd=source_dir)
55+
subprocess.run(shlex.split(f"git checkout {old_ref}"), cwd=source_dir)
56+
else:
57+
print(
58+
f"The repository {source_dir} has been switched from {old_ref} "
59+
f"to {new_ref}. Local changes were stashed. Switch back using\n\t"
60+
f"git checkout {old_ref}\n"
61+
)
4562

4663
def main():
4764
parser = argparse.ArgumentParser(
@@ -87,10 +104,8 @@ def main():
87104
if not args.create_wrapper and len(wrapper_args) > 0:
88105
parser.parse_args()
89106

90-
bolt_path = f"{args.build_dir}/bin/llvm-bolt"
91-
92-
source_dir = None
93107
# find the repo directory
108+
source_dir = None
94109
try:
95110
CMCacheFilename = f"{args.build_dir}/CMakeCache.txt"
96111
with open(CMCacheFilename) as f:
@@ -103,6 +118,11 @@ def main():
103118
except Exception as e:
104119
sys.exit(e)
105120

121+
# clean the previous llvm-bolt if it exists
122+
bolt_path = f"{args.build_dir}/bin/llvm-bolt"
123+
if os.path.exists(bolt_path):
124+
os.remove(bolt_path)
125+
106126
# build the current commit
107127
print("NFC-Setup: Building current revision..")
108128
subprocess.run(
@@ -151,7 +171,9 @@ def main():
151171

152172
# rename llvm-bolt
153173
if not os.path.exists(bolt_path):
154-
sys.exit(f"Failed to build the previous revision: '{bolt_path}'")
174+
print(f"Failed to build the previous revision: '{bolt_path}'")
175+
switch_back(args.switch_back, stash, source_dir, old_ref, new_ref)
176+
sys.exit(1)
155177
os.replace(bolt_path, f"{bolt_path}.old")
156178

157179
# symlink llvm-bolt-wrapper
@@ -170,18 +192,12 @@ def main():
170192
# symlink llvm-bolt-wrapper
171193
os.symlink(wrapper_path, bolt_path)
172194
except Exception as e:
173-
sys.exit("Failed to create a wrapper:\n" + str(e))
195+
print("Failed to create a wrapper:\n" + str(e))
196+
switch_back(args.switch_back, stash, source_dir, old_ref, new_ref)
197+
sys.exit(1)
198+
199+
switch_back(args.switch_back, stash, source_dir, old_ref, new_ref)
174200

175-
if args.switch_back:
176-
if stash:
177-
subprocess.run(shlex.split("git stash pop"), cwd=source_dir)
178-
subprocess.run(shlex.split(f"git checkout {old_ref}"), cwd=source_dir)
179-
else:
180-
print(
181-
f"The repository {source_dir} has been switched from {old_ref} "
182-
f"to {new_ref}. Local changes were stashed. Switch back using\n\t"
183-
f"git checkout {old_ref}\n"
184-
)
185201
print(
186202
f"Build directory {args.build_dir} is ready to run BOLT tests, e.g.\n"
187203
"\tbin/llvm-lit -sv tools/bolt/test\nor\n"

0 commit comments

Comments
 (0)