diff mbox series

[v2,3/4] bitbake-setup: print colored diffs

Message ID 20260218-improve-bbsetup-readability-v2-3-351584e5df9c@bootlin.com
State New
Headers show
Series bitbake-setup: Improve readability | expand

Commit Message

Antonin Godard Feb. 18, 2026, 1:15 p.m. UTC
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(-)

Comments

Antonin Godard Feb. 18, 2026, 3:27 p.m. UTC | #1
Hi,

On Wed Feb 18, 2026 at 2:15 PM CET, Antonin Godard wrote:
[...]
> @@ -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))

Oops, this slipped through, but shouldn't be changed at all since it is removed
in the next commit. I will resend the series.

Antonin
diff mbox series

Patch

diff --git a/bin/bitbake-setup b/bin/bitbake-setup
index 9b0ecc8d0e4..e456aa93bf3 100755
--- a/bin/bitbake-setup
+++ b/bin/bitbake-setup
@@ -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))