Skip to contentSkip to conversation

bitbake-setup: print colored diffs

PATCH New Antonin Godard 1 participant 1 message Feb 20

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 bitbake-setup: print colored diffs

Define a get_diff_color_param() function that returns the --color value for diff commands. It function also uses color_enabled() to force showing color or not in the spawned subprocess (--color=auto would never show color).

---
Changes in v3:
- Rebase on master
- Drop patch 1/2 (merged)
- Drop patch 4
- Define a get_diff_color_param() helper function for diff commands
- Link to v2: https://patch.msgid.link/20260218-improve-bbsetup-readability-v2-0-351584e5df9c@bootlin.com

Changes in v2:
- Drop the --quiet patch
- Define a run_git_diff() helper function used for printing colored
diffs. Drop get_diff() in favor of it.
- Link to v1: https://patch.msgid.link/20260213-improve-bbsetup-readability-v1-0-41cec3b06992@bootlin.com

---
 bin/bitbake-setup | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
bin/bitbake-setup +6 -3
index 9b0ecc8d0e4..553164b575d 100755
@@ -42,6 +42,9 @@ def color_enabled() -> bool:
     """
     return logger.handlers[0].formatter.color_enabled
 
+def get_diff_color_param() -> str:
+    return "--color=always" if color_enabled() else "--color=never"
+
 def print_configs(prompt: str, choices: list[str], descriptions: list[str] = []):
     """
     Helper function to print a list of choices and align the output.
@@ -202,7 +205,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 = bb.process.run('git -C {} diff {} {}'.format(r_path, get_diff_color_param(), rev))[0]
         if diff:
             return True
         return False
@@ -728,7 +731,7 @@ 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 {} -uNr {} {}'.format(get_diff_color_param(), file1, file2))
     except bb.process.ExecutionError as e:
         if e.exitcode == 1:
             return e.stdout
@@ -786,7 +789,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 = bb.process.run('git -C {} diff {}'.format(confdir, get_diff_color_param()))[0]
 
     if config_diff:
         logger.plain('\nConfiguration in {} has changed:\n{}'.format(setupdir, config_diff))

---
base-commit: d9700632bd6b627d1124fdc83ddf7bfb4199228d
change-id: 20260123-improve-bbsetup-readability-ff6a0ee8ee25