Skip to content

Commit 8597b8d

Browse files
committed
cmd-diff: support cd strategy for diff_cmd_outputs
This strategy simply changes directory into the given path before running the provided command rather than replacing a templated `{}` with the path. Useful for commands that operate more cleanly when operated on in the directory where you want the operation to occur.
1 parent 539ed6a commit 8597b8d

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

src/cmd-diff

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -401,16 +401,21 @@ def diff_metal(diff_from, diff_to):
401401
shutdown_process(p_to)
402402

403403

404-
def diff_cmd_outputs(cmd, path_from, path_to):
404+
def diff_cmd_outputs(cmd, path_from, path_to, strategy='template'):
405+
workingdir = os.getcwd()
405406
with tempfile.NamedTemporaryFile(prefix=cmd[0] + '-') as from_output, \
406407
tempfile.NamedTemporaryFile(prefix=cmd[0] + '-') as to_output:
407408
for path, output in (path_from, from_output), (path_to, to_output):
408409
c = list(cmd)
409-
if '{}' not in c:
410-
c += ['{}']
411-
idx = c.index('{}')
412-
c[idx] = path
413-
subprocess.run(c, check=True, stdout=output).stdout
410+
if strategy == 'template':
411+
if '{}' not in c:
412+
c += ['{}']
413+
idx = c.index('{}')
414+
c[idx] = path
415+
else:
416+
assert strategy == 'cd'
417+
workingdir = path
418+
subprocess.run(c, cwd=workingdir, check=True, stdout=output).stdout
414419
git_diff(from_output.name, to_output.name)
415420

416421

0 commit comments

Comments
 (0)