-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add project baseconfig #147
base: main
Are you sure you want to change the base?
Changes from all commits
2edd2e9
46ef8b6
aa360fa
0f312c9
85e02c1
38a5340
2fe99b0
4b7c684
8d7b929
069d928
bddbc6d
e06c84d
ee54165
c18a3b7
111144a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
# Configure Your Project | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 可以改成 Project based configuration |
||
|
||
GCOP supports project-level configuration, allowing you to set different configuration options for different projects. This guide will help you understand how to configure your project. | ||
|
||
## Initialize Project Configuration | ||
|
||
To initialize GCOP configuration in your project, simply run in the project root directory: | ||
|
||
```bash | ||
gcop init-project | ||
``` | ||
|
||
This command creates a `.gcop/config.yaml` file in your project root directory. If the configuration file already exists, the command will indicate that it has been initialized. | ||
|
||
## Configuration File Structure | ||
|
||
The default content of the project configuration file `.gcop/config.yaml` is as follows: | ||
|
||
```yaml | ||
commit_template: null # Commit message template | ||
enable_data_improvement: false # Whether to enable data improvement | ||
include_git_history: false # Whether to include git history in the prompt | ||
model: | ||
api_base: eg:https://api.openai.com/v1 | ||
api_key: sk-xxx | ||
model_name: provider/name,eg openai/gpt-4o | ||
``` | ||
|
||
## Configuration Priority | ||
|
||
GCOP's configuration priority is as follows (from highest to lowest): | ||
|
||
1. Project-level configuration (`.gcop/config.yaml`) | ||
2. Global configuration (`~/.gcop/config.yaml`) | ||
|
||
This means that project-level configuration will override global configuration, allowing you to set different configurations for different projects. | ||
|
||
## View Current Configuration | ||
|
||
To view the current effective configuration, use: | ||
|
||
```bash | ||
gcop show-config | ||
``` | ||
|
||
This command displays all currently effective GCOP configuration items, including model configuration, commit templates, etc. | ||
|
||
## Important Notes | ||
|
||
1. Make sure to add `.gcop/` to your `.gitignore` file to avoid committing sensitive information like API keys to version control. | ||
|
||
2. It's recommended to use environment variables for storing API keys rather than writing them directly in the configuration file. | ||
|
||
3. Changes to the project configuration file take effect immediately, no need to restart GCOP. | ||
|
||
## Troubleshooting | ||
|
||
If you encounter configuration-related issues, you can: | ||
|
||
1. Use `gcop show-config` to check the current configuration | ||
2. Ensure the configuration file format is correct (valid YAML format) | ||
3. Verify that the API key and model name are correct | ||
4. Check file permissions | ||
|
||
For more help, refer to the [Configuration Guide](/guide/configuration) or submit a GitHub issue. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,11 +10,12 @@ | |
import questionary | ||
import requests | ||
import typer | ||
import yaml | ||
from dotenv import load_dotenv | ||
from pydantic import BaseModel, Field | ||
|
||
from gcop import prompt, version | ||
from gcop.config import ModelConfig, get_config | ||
from gcop.config import EXAMPLE_CONFIG, ModelConfig, get_config | ||
from gcop.utils import check_version_update, migrate_config_if_needed | ||
from gcop.utils.logger import Color, logger | ||
|
||
|
@@ -55,8 +56,27 @@ def get_git_diff(diff_type: Literal["--staged", "--cached"]) -> str: | |
raise ValueError(f"Error getting git diff: {e}") | ||
|
||
|
||
def get_git_history(log_type: Literal["--oneline", "--stat"]) -> str: | ||
"""Get git history | ||
|
||
Args: | ||
log_type(str): log type, --oneline or --stat | ||
|
||
Returns: | ||
str: git history | ||
""" | ||
try: | ||
result = subprocess.check_output( | ||
["git", "log", log_type], text=True, encoding="utf-8" | ||
) | ||
return result | ||
except subprocess.CalledProcessError as e: | ||
raise ValueError(f"Error getting git history: {e}") | ||
|
||
|
||
def generate_commit_message( | ||
diff: str, | ||
commit_message_history: Optional[str] = None, | ||
instruction: Optional[str] = None, | ||
previous_commit_message: Optional[str] = None, | ||
) -> CommitMessage: | ||
|
@@ -73,14 +93,14 @@ def generate_commit_message( | |
str: git commit message with ai generated. | ||
""" | ||
gcop_config = get_config() | ||
|
||
commit_template = gcop_config.commit_template | ||
instruction: str = prompt.get_commit_instrcution( | ||
diff=diff, | ||
commit_template=gcop_config.commit_template, | ||
commmit_message_history=commit_message_history, | ||
commit_template=commit_template, | ||
instruction=instruction, | ||
previous_commit_message=previous_commit_message, | ||
) | ||
|
||
model_config: ModelConfig = gcop_config.model_config | ||
return pne.chat( | ||
messages=instruction, | ||
|
@@ -453,6 +473,7 @@ def commit_command( | |
process, please select "exit". | ||
""" | ||
diff: str = get_git_diff("--staged") | ||
commit_message_history: str = get_git_history("--staged") | ||
|
||
if not diff: | ||
logger.color_info("No staged changes", color=Color.YELLOW) | ||
|
@@ -462,7 +483,7 @@ def commit_command( | |
logger.color_info("[On Ready] Generating commit message...") | ||
|
||
commit_messages: CommitMessage = generate_commit_message( | ||
diff, instruction, previous_commit_message | ||
diff, commit_message_history, instruction, previous_commit_message | ||
) | ||
|
||
logger.color_info(f"[Thought] {commit_messages.thought}") | ||
|
@@ -492,6 +513,35 @@ def commit_command( | |
actions[response]() | ||
|
||
|
||
@app.command(name="init-project") | ||
@check_version_before_command | ||
def init_project_command(): | ||
"""Initialize gcop config""" | ||
project_path = Path.cwd() | ||
config_folder_path = project_path / ".gcop" / "config.yaml" | ||
if config_folder_path.exists(): | ||
logger.color_info( | ||
"Gcop config already exists in the current project.", color=Color.YELLOW | ||
) | ||
return | ||
try: | ||
config_folder_path.parent.mkdir(parents=True, exist_ok=True) | ||
with open(config_folder_path, "w") as f: | ||
yaml.dump(EXAMPLE_CONFIG, f, default_flow_style=False) | ||
logger.color_info("Gcop config initialized successfully.") | ||
except Exception as e: | ||
logger.color_info(f"Failed to initialize gcop config: {e}", color=Color.RED) | ||
return | ||
|
||
|
||
@app.command(name="show-config") | ||
@check_version_before_command | ||
def show_config_command(): | ||
"""command to show the current gcop config""" | ||
config = get_config() | ||
logger.color_info(f"Current gcop config: {config}") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这个输出要格式化一下 |
||
|
||
|
||
@app.command(name="help") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. help 需要加上对应的命令 |
||
@check_version_before_command | ||
def help_command(): | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
from pathlib import Path | ||
|
||
import pytest | ||
|
||
from gcop.config import GcopConfig, get_config | ||
|
||
|
||
def test_get_config(): | ||
"""test get_config function""" | ||
project_config_path = Path.cwd() / ".gcop" / "config.yaml" | ||
if not project_config_path.exists(): | ||
pytest.skip("Configuration file does not exist. Skip the test") | ||
config = get_config() | ||
assert isinstance(config, GcopConfig) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
我理解这个不算第七点,算一个新的 example case。