Skip to content

Commit b26be22

Browse files
authored
Merge pull request #143 from devchat-ai/ignore_exception_without_git
handle git missing error
2 parents ebbf9a6 + 66cd18d commit b26be22

3 files changed

Lines changed: 58 additions & 6 deletions

File tree

devchat/_cli/run.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,17 @@
44
import sys
55
from typing import List
66
import rich_click as click
7-
from git import Repo, GitCommandError
7+
try:
8+
from git import Repo, GitCommandError
9+
except Exception:
10+
pass
811
from devchat._cli.utils import init_dir, handle_errors, valid_git_repo, clone_git_repo
12+
from devchat._cli.utils import download_and_extract_workflow
913
from devchat.engine import Namespace, CommandParser, RecursivePrompter
1014
from devchat.utils import get_logger
1115

1216
logger = get_logger(__name__)
1317

14-
1518
@click.command(
1619
help="The 'command' argument is the name of the command to run or get information about.")
1720
@click.argument('command', required=False, default='')
@@ -44,7 +47,11 @@ def run(command: str, list_flag: bool, recursive_flag: bool, update_sys_flag: bo
4447
'https://gitee.com/devchat-ai/workflows.git',
4548
'https://github.com/devchat-ai/workflows.git'
4649
]
47-
_clone_or_pull_git_repo(sys_dir, git_urls)
50+
zip_urls = [
51+
'https://gitlab.com/devchat-ai/workflows/-/archive/main/workflows-main.zip',
52+
'https://codeload.github.com/devchat-ai/workflows/zip/refs/heads/main'
53+
]
54+
_clone_or_pull_git_repo(sys_dir, git_urls, zip_urls)
4855
return
4956

5057
if list_flag:
@@ -72,13 +79,23 @@ def run(command: str, list_flag: bool, recursive_flag: bool, update_sys_flag: bo
7279
return
7380

7481

75-
def _clone_or_pull_git_repo(target_dir: str, repo_urls: List[str]):
82+
def _clone_or_pull_git_repo(target_dir: str, repo_urls: List[str], zip_urls: List[str]):
7683
"""
7784
Clone a Git repository to a specified location, or pull it if it already exists.
7885
7986
:param target_dir: The path where the repository should be cloned.
8087
:param repo_urls: A list of possible Git repository URLs.
8188
"""
89+
if shutil.which('git') is None:
90+
# If Git is not installed, download and extract the workflow
91+
for url in zip_urls:
92+
try:
93+
download_and_extract_workflow(url, target_dir)
94+
break
95+
except Exception as err:
96+
logger.exception("Failed to download and extract workflow: %s", err)
97+
return
98+
8299
if os.path.exists(target_dir):
83100
if valid_git_repo(target_dir, repo_urls):
84101
try:

devchat/_cli/utils.py

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,14 @@
22
import os
33
import sys
44
import json
5+
import shutil
56
from typing import Tuple, List, Optional, Any
6-
from git import Repo, InvalidGitRepositoryError, GitCommandError
7+
import zipfile
8+
import requests
9+
try:
10+
from git import Repo, InvalidGitRepositoryError, GitCommandError
11+
except Exception:
12+
pass
713
import rich_click as click
814
from devchat.config import ConfigManager, OpenAIModelConfig
915
from devchat.utils import find_root_dir, add_gitignore, setup_logger, get_logger
@@ -12,6 +18,31 @@
1218
logger = get_logger(__name__)
1319

1420

21+
def download_and_extract_workflow(workflow_url, target_dir):
22+
# Download the workflow zip file
23+
response = requests.get(workflow_url, stream=True, timeout=10)
24+
# Downaload file to temp dir
25+
os.makedirs(target_dir, exist_ok=True)
26+
zip_path = os.path.join(target_dir, 'workflow.zip')
27+
with open(zip_path, 'wb') as file_handle:
28+
for chunk in response.iter_content(chunk_size=8192):
29+
if chunk:
30+
file_handle.write(chunk)
31+
32+
# Extract the zip file
33+
parent_dir = os.path.dirname(target_dir)
34+
with zipfile.ZipFile(zip_path, 'r') as zip_ref:
35+
zip_ref.extractall(parent_dir)
36+
37+
# Delete target directory if exists
38+
if os.path.exists(target_dir):
39+
shutil.rmtree(target_dir)
40+
41+
# Rename extracted directory to target directory
42+
extracted_dir = os.path.join(parent_dir, 'workflows-main')
43+
os.rename(extracted_dir, target_dir)
44+
45+
1546
@contextmanager
1647
def handle_errors():
1748
"""Handle errors in the CLI."""

devchat/utils.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,11 @@ def get_user_info() -> Tuple[str, str]:
119119
cmd = ['git', 'config', 'user.name']
120120
user_name = subprocess.check_output(cmd, encoding='utf-8').strip()
121121
except Exception:
122-
user_name = getpass.getuser()
122+
try:
123+
user_name = getpass.getuser()
124+
except Exception:
125+
user_dir = os.path.expanduser("~")
126+
user_name = user_dir.split(os.sep)[-1]
123127

124128
try:
125129
cmd = ['git', 'config', 'user.email']

0 commit comments

Comments
 (0)