@@ -69,6 +69,39 @@ def print_configs(prompt: str, choices: list[str], descriptions: list[str] = [])
msg += f" {descriptions[n]}"
logger.plain(msg)
+def run_git_diff(revision: str = "", path1: str = "", path2: str = "", repo_path: str = "") -> str:
+ """
+ Run git diff from either the current working directory, or in the path
+ specified by repo_path.
+ Then, run git diff with either:
+ - no argument,
+ - or a revision,
+ - or two files / two directories.
+ """
+ assert not (revision and (path1 or path2)), \
+ "run_git_diff can only be called with a revision or two paths, not both"
+ assert not (path1 and not path2), \
+ "run_git_diff only received one path, two expected"
+
+ path = f"-C {repo_path}" if repo_path else ""
+ color = "--color=always" if color_enabled() else "--color=never"
+ git_cmd = f"git {path} diff {color}"
+ if revision:
+ git_cmd += f" {revision}"
+ elif path1:
+ git_cmd += f" {path1} {path2}"
+
+ diff = ""
+ try:
+ diff = bb.process.run(f"{git_cmd}")[0].strip()
+ except bb.process.ExecutionError as e:
+ if e.exitcode == 1:
+ diff = e.stdout
+ else:
+ raise e
+
+ return diff
+
# If bitbake is from a release tarball or somewhere like pypi where
# updates may not be straightforward, prefer to use the git repo as the
# default registry
@@ -202,7 +235,7 @@ be preserved in a backup directory.""".format(r_name, r_path))
status = bb.process.run('git -C {} status --porcelain'.format(r_path))[0]
if status:
return True
- diff = bb.process.run('git -C {} diff {}'.format(r_path, rev))[0]
+ diff = run_git_diff(revision=rev, repo_path=r_path)
if diff:
return True
return False
@@ -728,7 +761,9 @@ def init_config(top_dir, settings, args):
def get_diff(file1, file2):
try:
- bb.process.run('diff -uNr {} {}'.format(file1, file2))
+ bb.process.run('diff --color={} -uNr {} {}'.format("always" if BBSETUP_COLOR else "never",
+ file1,
+ file2))
except bb.process.ExecutionError as e:
if e.exitcode == 1:
return e.stdout
@@ -786,7 +821,7 @@ def build_status(top_dir, settings, args, d, update=False):
new_upstream_config = obtain_config(top_dir, registry, args, source_overrides, d)
write_upstream_config(confdir, new_upstream_config)
- config_diff = bb.process.run('git -C {} diff'.format(confdir))[0]
+ config_diff = run_git_diff(repo_path=confdir)
if config_diff:
logger.plain('\nConfiguration in {} has changed:\n{}'.format(setupdir, config_diff))
Define a new run_git_diff() function that can be used to print diffs between two paths (files or directories), or show a diff in a Git repository. This function also uses color_enabled() to force showing color or not in the subprocess. Replace the different calls of `diff` and `git diff` by this helper function. Signed-off-by: Antonin Godard <antonin.godard@bootlin.com> --- bin/bitbake-setup | 41 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 38 insertions(+), 3 deletions(-)