Skip to content

Commit 1c4a82a

Browse files
Fix gitprof when running on Linux
1 parent 49f9511 commit 1c4a82a

8 files changed

Lines changed: 97 additions & 31 deletions

File tree

gitprof/cli/clone.py

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,26 +27,37 @@
2727
import click
2828

2929
from gitprof import command_utils
30+
from gitprof import os_utils
3031
from gitprof import ssh
3132
from gitprof import ux
3233
from gitprof.cli import root
3334
from gitprof.files import Config, Profile
3435
from gitprof.vcs import services
3536

3637

38+
def _create_clone_command(ssh_command, repo, dest) -> str:
39+
if os_utils.is_windows():
40+
return (
41+
f"powershell -c "
42+
+ f'"git -c core.sshCommand="""{ssh_command}""" clone {repo}" "{dest}"'
43+
)
44+
45+
return f'git -c core.sshCommand="{ssh_command}" clone "{repo}" "{dest}"'
46+
47+
3748
def do_clone(
3849
ssh_command: str, repo: str, profile: Profile, dest: str, add_to_known_hosts=False
3950
):
4051
if add_to_known_hosts:
4152
ssh_command = f"{ssh_command} -o StrictHostKeyChecking=no"
4253

43-
cmd = (
44-
f"powershell -c "
45-
f'"git -c core.sshCommand="""{ssh_command}""" clone {repo}" "{dest}"'
46-
)
54+
cmd = _create_clone_command(ssh_command, repo, dest)
4755

4856
pipes = subprocess.Popen(
49-
cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE
57+
cmd,
58+
stdout=subprocess.PIPE,
59+
stderr=subprocess.PIPE,
60+
shell=True,
5061
)
5162
stdout, stderr = pipes.communicate()
5263

@@ -128,7 +139,9 @@ def advice():
128139
@click.argument("repo")
129140
@click.option("-p", "--profile", help="Which profile to clone the repo with")
130141
def clone(repo: str, profile: str):
131-
profile = command_utils.create_profile_interactive(profile)
142+
profile = command_utils.choose_profile_interactive(
143+
profile, title="Choose a profile to clone with"
144+
)
132145

133146
click.echo(f"Cloning '{repo}' with profile: {profile}")
134147
profile: Profile = Config().get_profile(name=profile)

gitprof/cli/config.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424

2525
import click
2626

27+
from gitprof import os_utils
28+
from gitprof import ux
2729
from gitprof import files
2830
from gitprof.cli import root
2931

@@ -43,7 +45,13 @@ def edit_config(editor: str):
4345
if editor:
4446
return os.system(fr'"{editor}" {files.config_file}')
4547

46-
os.system(f'start "" "{files.config_file}"')
48+
if os_utils.is_windows():
49+
return os.system(f'start "" "{files.config_file}"')
50+
51+
editor = ux.get_simple_input(
52+
question="Please enter the command for your text editor (e.g. 'vi')"
53+
)
54+
return os.system(f'"{editor}" "{files.config_file}"')
4755

4856

4957
@config.command("rm", help="Delete the config file")

gitprof/cli/profile.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ def create_profile(name: str, username: str = None):
9898
print(f"Trying to find your Git committer name and email...")
9999
result = github_utils.get_name_and_email(username)
100100

101-
if len(result) == 2:
101+
if result and len(result) == 2:
102102
name, email = result
103103

104104
profile.git_name = name
@@ -107,6 +107,10 @@ def create_profile(name: str, username: str = None):
107107
click.echo(
108108
f"\nYour name and email were found and are set as the defaults for the next questions."
109109
)
110+
else:
111+
click.echo(
112+
f"Failed to find name and email. You'll need to enter them manually.\n"
113+
)
110114

111115
profile.git_name = ux.get_simple_input(
112116
"Enter your Git committer name", default=profile.git_name
@@ -139,7 +143,9 @@ def create_profile(name: str, username: str = None):
139143
@profile.command("apply", help="Apply profile to current repository")
140144
@click.option("-p", "--profile", help="The profile to apply")
141145
def apply_profile(profile: str):
142-
profile = command_utils.create_profile_interactive(profile)
146+
profile = command_utils.choose_profile_interactive(
147+
profile, title="Choose a profile to apply"
148+
)
143149

144150
profile: Profile = Config().get_profile(name=profile)
145151
command_utils.set_git_configs(profile)

gitprof/command_utils.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,11 @@
3030

3131
def run_command(args: str):
3232
raw = subprocess.check_output(
33-
args, stdin=None, stderr=None, shell=False, universal_newlines=False
33+
args,
34+
stdin=None,
35+
stderr=None,
36+
shell=True,
37+
universal_newlines=False,
3438
).decode("utf-8")
3539
return "".join(raw).strip().replace("\r\n", "")
3640

@@ -49,7 +53,7 @@ def set_git_configs(profile: Profile):
4953
run_command(f'git config --local core.sshCommand "{ssh_command}"')
5054

5155

52-
def create_profile_interactive(profile: str) -> str:
56+
def choose_profile_interactive(profile: str, title: str) -> str:
5357
config = Config()
5458

5559
while not profile:
@@ -65,7 +69,7 @@ def create_profile_interactive(profile: str) -> str:
6569
options[index] = f"{options[index]}{whitespace}({profile.service})"
6670

6771
chosen = ux.get_input_from_list(
68-
title="Choose a profile to clone with",
72+
title=title,
6973
options=options,
7074
fallback="create new profile",
7175
fallback_index=-1,

gitprof/files.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,14 @@
2828
from os.path import join
2929
from typing import List, Any, Dict, Optional
3030

31-
config_dir = os.path.expanduser(r"~\AppData\Local\gitprof")
32-
os.makedirs(config_dir, exist_ok=True)
31+
from gitprof import os_utils
32+
33+
if os_utils.is_windows():
34+
config_dir = os.path.expanduser(r"~\AppData\Local\gitprof")
35+
else:
36+
config_dir = os.path.expanduser(r"~/.config/gitprof")
3337

38+
os.makedirs(config_dir, exist_ok=True)
3439
config_file = join(config_dir, "config.json")
3540

3641

gitprof/os_utils.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# MIT License
2+
#
3+
# Copyright (c) 2020 Sam McCormack
4+
#
5+
# Permission is hereby granted, free of charge, to any person obtaining a copy
6+
# of this software and associated documentation files (the "Software"), to deal
7+
# in the Software without restriction, including without limitation the rights
8+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
# copies of the Software, and to permit persons to whom the Software is
10+
# furnished to do so, subject to the following conditions:
11+
#
12+
# The above copyright notice and this permission notice shall be included in all
13+
# copies or substantial portions of the Software.
14+
#
15+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
# SOFTWARE.
22+
import platform
23+
24+
25+
def is_windows() -> bool:
26+
return platform.system().lower() == "windows"

gitprof/ssh.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
from typing import List
2525

2626
ssh_dir = os.path.expanduser("~/.ssh")
27+
os.makedirs(ssh_dir, exist_ok=True)
2728

2829

2930
def create_ssh_key(name: str) -> str:

gitprof/vcs/github_utils.py

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,34 +19,37 @@
1919
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
2020
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2121
# SOFTWARE.
22-
from typing import List
22+
from typing import List, Optional, Tuple
2323

2424
from github import Github, GitAuthor, GitCommit
2525
from github.Repository import Repository
2626

2727
g = Github()
2828

2929

30-
def get_name_and_email(username: str):
31-
user = g.get_user(username)
32-
repos: List[Repository] = user.get_repos()
30+
def get_name_and_email(username: str) -> Optional[Tuple[str, str]]:
31+
try:
32+
user = g.get_user(username)
33+
repos: List[Repository] = user.get_repos()
3334

34-
name, email = None, None
35+
name, email = None, None
3536

36-
for r in repos:
37-
commits = r.get_commits(author=user)
38-
size = commits.totalCount
37+
for r in repos:
38+
commits = r.get_commits(author=user)
39+
size = commits.totalCount
3940

40-
if size <= 0:
41-
continue
41+
if size <= 0:
42+
continue
4243

43-
last_commit: GitCommit = commits[size - 1].commit
44-
author: GitAuthor = last_commit.author
44+
last_commit: GitCommit = commits[size - 1].commit
45+
author: GitAuthor = last_commit.author
4546

46-
name = author.name
47-
email = author.email
47+
name = author.name
48+
email = author.email
4849

49-
if name and email:
50-
break
50+
if name and email:
51+
break
5152

52-
return name, email
53+
return name, email
54+
except:
55+
return None

0 commit comments

Comments
 (0)