|
1 | 1 | #!/usr/bin/python3 |
2 | | -# SPDX-License-Identifier: GPL-2.0-or-later |
3 | 2 | # -*- coding: utf-8 -*- |
4 | 3 | # |
| 4 | +# SPDX-License-Identifier: GPL-2.0-or-later |
| 5 | +# |
5 | 6 | # Copyright (C) 2015-2025 Sérgio Basto <sergio@serjux.com> |
6 | 7 | # |
7 | 8 | # This program is free software; you can redistribute it and/or modify it |
@@ -54,60 +55,52 @@ parser = argparse.ArgumentParser() |
54 | 55 | parser.add_argument('-v', '--debug', |
55 | 56 | help='writes the commands that will be executed', |
56 | 57 | action='store_true') |
57 | | -parser.add_argument('git_args', nargs='*', default=[]) |
| 58 | +parser.add_argument('tool_args', nargs='*', default=[]) |
58 | 59 | parser.add_argument('patchview_args', nargs=argparse.REMAINDER) |
59 | 60 | args, unknown = parser.parse_known_args() |
60 | | -largs = vars(args).get("git_args") |
61 | | -rargs = vars(args).get("patchview_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 |
62 | 70 |
|
63 | | -patchview_cmd = ["patchview"] + rargs + unknown |
64 | | -pipetoview = False |
65 | | -if tool_cmd.endswith("view"): |
66 | | - # pipeline: first_pipe | second_pipe | editor |
67 | | - tool_cmd = tool_cmd[:-4] # remove "view" |
68 | | - patchview_cmd = ["filterdiff"] + rargs + unknown |
69 | | - pipetoview = True |
70 | | -git_cmd = [tool, tool_cmd] + largs |
| 71 | +vcs_cmd = [tool, tool_cmd] + tool_args |
| 72 | +dest_cmd = [editor, "-R", "-"] |
71 | 73 |
|
72 | | -p1 = Popen(git_cmd, stdout=PIPE, env=enviro, cwd=workdir) |
73 | | -p2 = Popen(patchview_cmd, stdin=p1.stdout, stdout=PIPE, env=enviro, cwd=workdir) |
| 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 | +if pipetoview: |
| 83 | + p2 = Popen(patchview_cmd, stdin=p1.stdout, stdout=PIPE, env=enviro, cwd=workdir) |
| 84 | +else: |
| 85 | + p2 = Popen(patchview_cmd, stdin=p1.stdout, env=enviro, cwd=workdir) |
74 | 86 |
|
75 | 87 | p1.wait() |
76 | | -stdout1, stderr1 = p1.communicate() |
77 | | -if p1.returncode != 0: |
| 88 | +ret1 = p1.returncode |
| 89 | +if ret1 != 0: |
| 90 | + stdout1, _ = p1.communicate() |
78 | 91 | if stdout1: |
79 | 92 | sys.stdout.buffer.write(stdout1) |
80 | | - if stderr1: |
81 | | - sys.stderr.buffer.write(stderr1) |
82 | | - sys.exit(p1.returncode) |
| 93 | + sys.exit(ret1) |
83 | 94 |
|
84 | 95 | p2.wait() |
85 | | -stdout2, stderr2 = p2.communicate() |
86 | | -if p2.returncode != 0: |
| 96 | +ret2 = p2.returncode |
| 97 | +if ret2 != 0: |
| 98 | + stdout2, _ = p2.communicate() |
87 | 99 | if stdout2: |
88 | 100 | sys.stdout.buffer.write(stdout2) |
89 | | - if stderr2: |
90 | | - sys.stderr.buffer.write(stderr2) |
91 | | - sys.exit(p2.returncode) |
| 101 | + sys.exit(ret2) |
92 | 102 |
|
93 | 103 | if pipetoview: |
94 | | - dest_cmd = [editor, "-R", "-"] |
95 | | - p3 = Popen(dest_cmd, stdin=PIPE) |
96 | | - if args.debug: |
97 | | - debug_str = "%s | %s | %s\n" % (" ".join(git_cmd), |
98 | | - " ".join(patchview_cmd), |
99 | | - " ".join(dest_cmd)) |
100 | | - p3.stdin.write(debug_str.encode()) |
101 | | - p3.stdin.flush() |
102 | | - |
103 | | - p3.stdin.write(stdout2) |
104 | | - p3.stdin.close() |
| 104 | + p3 = Popen(dest_cmd, stdin=p2.stdout, env=enviro, cwd=workdir) |
105 | 105 | p3.wait() |
106 | | -else: |
107 | | - # debug print |
108 | | - if args.debug: |
109 | | - print("%s | %s" % (" ".join(git_cmd), " ".join(patchview_cmd))) |
110 | | - sys.stdout.flush() |
111 | | - |
112 | | - sys.stdout.buffer.write(stdout2) |
113 | 106 |
|
0 commit comments