|
| 1 | +#!/usr/bin/python3 |
| 2 | +# SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | +# -*- coding: utf-8 -*- |
| 4 | +# |
| 5 | +# Copyright (C) 2015-2025 Sérgio Basto <sergio@serjux.com> |
| 6 | +# |
| 7 | +# This program is free software; you can redistribute it and/or modify it |
| 8 | +# under the terms of the GNU General Public License as published by the |
| 9 | +# Free Software Foundation; either version 2 of the License, or (at your |
| 10 | +# option) any later version. |
| 11 | +# |
| 12 | +# This program is distributed in the hope that it will be useful, but |
| 13 | +# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
| 14 | +# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 15 | +# for more details. |
| 16 | +# |
| 17 | +# You should have received a copy of the GNU General Public License along |
| 18 | +# with this program; if not, write to the Free Software Foundation, Inc., |
| 19 | +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | +# |
| 21 | +# patchview-wrapper - wrapper for Git/SVN commands (e.g., "git diff", |
| 22 | +# "git show", "svn diff") piped into patchview, optionally also piped into |
| 23 | +# an editor |
| 24 | + |
| 25 | + |
| 26 | +import os |
| 27 | +import sys |
| 28 | +import argparse |
| 29 | +from subprocess import Popen, PIPE |
| 30 | + |
| 31 | +enviro = os.environ |
| 32 | +workdir = '.' |
| 33 | +editor = os.environ.get("EDITOR", "vim") |
| 34 | + |
| 35 | +tools = ["git", "svn"] |
| 36 | +prog = os.path.basename(sys.argv[0]) |
| 37 | + |
| 38 | +tool = None |
| 39 | +for t in tools: |
| 40 | + if prog.startswith(t): |
| 41 | + tool = t |
| 42 | + break |
| 43 | + |
| 44 | +if tool is None: |
| 45 | + sys.stderr.write(f"Error: program name must start with one of {tools} (got '{prog}')\n") |
| 46 | + sys.exit(1) |
| 47 | + |
| 48 | +tool_cmd = prog[len(tool):] |
| 49 | + |
| 50 | +parser = argparse.ArgumentParser() |
| 51 | +parser.add_argument('-v', '--debug', |
| 52 | + help='writes the commands that will be executed', |
| 53 | + action='store_true') |
| 54 | +parser.add_argument('git_args', nargs='*', default=[]) |
| 55 | +parser.add_argument('patchview_args', nargs=argparse.REMAINDER) |
| 56 | +args, unknown = parser.parse_known_args() |
| 57 | +largs = vars(args).get("git_args") |
| 58 | +rargs = vars(args).get("patchview_args") |
| 59 | + |
| 60 | +if tool_cmd.endswith("view"): |
| 61 | + # pipeline: first_pipe | second_pipe | editor |
| 62 | + tool_cmd = tool_cmd[:-4] # remove "view" |
| 63 | + git_cmd = [tool, tool_cmd] + largs |
| 64 | + patchview_cmd = ["patchview"] + rargs + unknown |
| 65 | + |
| 66 | + p1 = Popen(git_cmd, stdout=PIPE, env=enviro, cwd='.') |
| 67 | + p2 = Popen(patchview_cmd, stdin=p1.stdout, stdout=PIPE, env=enviro, cwd=workdir) |
| 68 | + p1.stdout.close() |
| 69 | + |
| 70 | + dest_cmd = [editor, "-R", "-"] |
| 71 | + p3 = Popen(dest_cmd, stdin=PIPE) |
| 72 | + if args.debug: |
| 73 | + debug_str = "%s | %s | %s\n" % (" ".join(git_cmd), |
| 74 | + " ".join(patchview_cmd), |
| 75 | + " ".join(dest_cmd)) |
| 76 | + p3.stdin.write(debug_str.encode()) |
| 77 | + p3.stdin.flush() |
| 78 | + |
| 79 | + for chunk in iter(lambda: p2.stdout.read(4096), b''): |
| 80 | + p3.stdin.write(chunk) |
| 81 | + p3.stdin.flush() |
| 82 | + |
| 83 | + p2.stdout.close() |
| 84 | + p3.stdin.close() |
| 85 | + p3.wait() |
| 86 | +else: |
| 87 | + # pipeline normal: tool cmd | patchview |
| 88 | + git_cmd = [tool, tool_cmd] + largs |
| 89 | + patchview_cmd = ["patchview"] + rargs + unknown |
| 90 | + |
| 91 | + p1 = Popen(git_cmd, stdout=PIPE, env=os.environ, cwd='.') |
| 92 | + p2 = Popen(patchview_cmd, stdin=p1.stdout, stdout=PIPE, env=enviro, cwd=workdir) |
| 93 | + p1.stdout.close() |
| 94 | + |
| 95 | + # debug print |
| 96 | + if args.debug: |
| 97 | + print("%s | %s" % (" ".join(git_cmd), " ".join(patchview_cmd))) |
| 98 | + sys.stdout.flush() |
| 99 | + |
| 100 | + sys.stdout.buffer.write(p2.communicate()[0]) |
| 101 | + |
0 commit comments