1
0
mirror of https://github.com/openSUSE/osc.git synced 2025-01-27 07:06:13 +01:00

Merge pull request #1353 from asdil12/diff

Add highlighting for "osc diff" and similar commands
This commit is contained in:
Daniel Mach 2023-07-14 10:55:45 +02:00 committed by GitHub
commit a2e6a3b0be
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 7 deletions

View File

@ -2340,7 +2340,7 @@ Please submit there instead, or use --nodevelproject to force direct submission.
rdiff = b''
if opts.diff:
run_pager(rdiff)
run_pager(highlight_diff(rdiff))
return
if rdiff is not None:
rdiff = decode_it(rdiff)
@ -3399,7 +3399,7 @@ Please submit there instead, or use --nodevelproject to force direct submission.
action.tgt_project.encode(), action.tgt_package.encode())
diff += submit_action_diff(apiurl, action)
diff += b'\n\n'
run_pager(diff, tmp_suffix='')
run_pager(highlight_diff(diff), tmp_suffix="")
# checkout
elif cmd in ('checkout', 'co'):
@ -4620,8 +4620,21 @@ Please submit there instead, or use --nodevelproject to force direct submission.
print("diff working copy against last committed version\n")
else:
print("diff committed package against linked revision %s\n" % baserev)
run_pager(server_diff(self.get_api_url(), linkinfo.get('project'), linkinfo.get('package'), baserev,
args[0], args[1], linkinfo.get('lsrcmd5'), not opts.plain, opts.missingok))
run_pager(
highlight_diff(
server_diff(
self.get_api_url(),
linkinfo.get("project"),
linkinfo.get("package"),
baserev,
args[0],
args[1],
linkinfo.get("lsrcmd5"),
not opts.plain,
opts.missingok,
)
)
)
return
if opts.change:
@ -4655,7 +4668,7 @@ Please submit there instead, or use --nodevelproject to force direct submission.
diff += server_diff_noex(pac.apiurl, pac.prjname, pac.name, rev1,
pac.prjname, pac.name, rev2,
not opts.plain, opts.missingok, opts.meta, not opts.unexpand, files=files)
run_pager(diff)
run_pager(highlight_diff(diff))
@cmdln.option('--issues-only', action='store_true',
help='show only issues in diff')
@ -4746,7 +4759,7 @@ Please submit there instead, or use --nodevelproject to force direct submission.
if opts.issues_only:
print(decode_it(rdiff))
else:
run_pager(rdiff)
run_pager(highlight_diff(rdiff))
def _pdiff_raise_non_existing_package(self, project, package, msg=None):
raise oscerr.PackageMissing(project, package, msg or '%s/%s does not exist.' % (project, package))
@ -4893,7 +4906,7 @@ Please submit there instead, or use --nodevelproject to force direct submission.
rdiff = server_diff(apiurl, parent_project, parent_package, None, project,
package, None, unified=unified, missingok=noparentok)
run_pager(rdiff)
run_pager(highlight_diff(rdiff))
def _get_branch_parent(self, prj):
m = re.match('^home:[^:]+:branches:(.+)', prj)

View File

@ -4368,6 +4368,24 @@ def get_default_pager():
return 'more'
def format_diff_line(line):
if line.startswith(b"+++") or line.startswith(b"---") or line.startswith(b"Index:"):
line = b"\x1b[1m" + line + b"\x1b[0m"
elif line.startswith(b"+"):
line = b"\x1b[32m" + line + b"\x1b[0m"
elif line.startswith(b"-"):
line = b"\x1b[31m" + line + b"\x1b[0m"
elif line.startswith(b"@"):
line = b"\x1b[96m" + line + b"\x1b[0m"
return line
def highlight_diff(diff):
if sys.stdout.isatty():
diff = b"\n".join((format_diff_line(line) for line in diff.split(b"\n")))
return diff
def run_pager(message, tmp_suffix=''):
if not message:
return