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