Skip to content

Commit 116bcbb

Browse files
Write live output from subprocess
1 parent fb8f503 commit 116bcbb

File tree

1 file changed

+15
-75
lines changed

1 file changed

+15
-75
lines changed

gitprof/cli/clone.py

Lines changed: 15 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -46,93 +46,33 @@ def _create_clone_command(ssh_command, repo, dest) -> str:
4646

4747

4848
def do_clone(
49-
ssh_command: str, repo: str, profile: Profile, dest: str, add_to_known_hosts=False
50-
):
49+
ssh_command: str,
50+
repo: str,
51+
dest: str,
52+
add_to_known_hosts=False,
53+
) -> None:
5154
if add_to_known_hosts:
5255
ssh_command = f"{ssh_command} -o StrictHostKeyChecking=no"
5356

5457
cmd = _create_clone_command(ssh_command, repo, dest)
5558

56-
pipes = subprocess.Popen(
59+
process = subprocess.Popen(
5760
cmd,
5861
stdout=subprocess.PIPE,
59-
stderr=subprocess.PIPE,
6062
shell=True,
6163
)
62-
stdout, stderr = pipes.communicate()
6364

64-
if pipes.returncode != 0:
65-
ux.print_header("CLONE FAILED", newlines_before=2)
65+
print()
6666

67-
stdout, stderr = (
68-
i.decode("utf-8").replace("\r", "").strip() for i in (stdout, stderr)
69-
)
70-
stdcombined = stdout + stderr
71-
72-
print(stdout)
73-
print(stderr)
74-
75-
def advice():
76-
ux.print_header("ADVICE", newlines_before=2)
67+
for c in iter(lambda: process.stdout.read(1), b""):
68+
sys.stdout.buffer.write(c)
7769

78-
not_in_known_hosts = re.findall(
79-
r"Host key verification failed",
80-
stdcombined,
81-
re.MULTILINE,
70+
process.wait()
71+
if process.returncode != 0:
72+
print(
73+
f"\nError: clone failed. Please view the error message above for details."
8274
)
83-
84-
if re.match("Identity file (.*) not accessible", stdcombined):
85-
advice()
86-
print("Your SSH key seems to be missing.")
87-
elif "Permission denied" in stdcombined:
88-
advice()
89-
90-
pub = ssh.get_public_key(profile.ssh_key)
91-
print(f"\nYour SSH public key is:\n\n{pub}")
92-
93-
ux.sleep(1)
94-
95-
url = services.get_ssh_url_for(profile.service)
96-
if url:
97-
print(
98-
f"You may need to add the SSH key to "
99-
f"your account on '{profile.service}'."
100-
)
101-
yes = (
102-
ux.get_simple_input(
103-
question="Would you like to open the settings page for this [Y/n]",
104-
default="Y",
105-
show_default=False,
106-
).lower()
107-
== "y"
108-
)
109-
110-
if yes:
111-
import webbrowser
112-
113-
webbrowser.open_new_tab(url)
114-
115-
sys.exit(1)
116-
elif not_in_known_hosts:
117-
print(stdcombined)
118-
119-
if (
120-
ux.get_simple_input(
121-
question="The remote server is not in 'known_hosts'. "
122-
"Would you like to add it? [Y/n]",
123-
default="y",
124-
show_default=False,
125-
newlines_before=2,
126-
).lower()
127-
== "y"
128-
):
129-
print(f"Cloning with '-o StrictHostKeyChecking=no' in SSH command...")
130-
do_clone(ssh_command, repo, profile, dest, add_to_known_hosts=True)
131-
else:
132-
print(f"Sorry, cannot clone when the host key is not accepted.")
133-
sys.exit(1)
134-
else:
135-
sys.exit(1)
75+
sys.exit(1)
13676

13777

13878
@root.command("clone", help="Clone a Git repository")
@@ -173,7 +113,7 @@ def clone(repo: str, profile: str):
173113
dest = re.findall(r".*/(.*?)\.git", repo)[0]
174114
ssh_command = ssh.get_ssh_command(ssh_key)
175115

176-
do_clone(ssh_command, repo, profile, dest)
116+
do_clone(ssh_command, repo, dest)
177117
cwd = os.getcwd()
178118

179119
try:

0 commit comments

Comments
 (0)