fix(gitea-mcp): use project directory for branch detection (#231)

The _get_current_branch() method was running git commands from the
installed plugin directory instead of the user's project directory.
This caused incorrect branch detection (always seeing 'main' from
the marketplace repo instead of the user's actual branch).

Fix: Use CLAUDE_PROJECT_DIR environment variable to get the correct
project directory and pass it as cwd to subprocess.run().

Fixes #231

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
2026-01-28 10:38:17 -05:00
parent 5b3da8da85
commit 8234683bc3
2 changed files with 36 additions and 4 deletions

View File

@@ -7,6 +7,7 @@ Provides async wrappers for issue CRUD operations with:
- Comprehensive error handling - Comprehensive error handling
""" """
import asyncio import asyncio
import os
import subprocess import subprocess
import logging import logging
from typing import List, Dict, Optional from typing import List, Dict, Optional
@@ -27,19 +28,34 @@ class IssueTools:
""" """
self.gitea = gitea_client self.gitea = gitea_client
def _get_project_directory(self) -> Optional[str]:
"""
Get the user's project directory from environment.
Returns:
Project directory path or None if not set
"""
return os.environ.get('CLAUDE_PROJECT_DIR')
def _get_current_branch(self) -> str: def _get_current_branch(self) -> str:
""" """
Get current git branch. Get current git branch from user's project directory.
Uses CLAUDE_PROJECT_DIR environment variable to determine the correct
directory for git operations, avoiding the bug where git runs from
the installed plugin directory instead of the user's project.
Returns: Returns:
Current branch name or 'unknown' if not in a git repo Current branch name or 'unknown' if not in a git repo
""" """
try: try:
project_dir = self._get_project_directory()
result = subprocess.run( result = subprocess.run(
['git', 'rev-parse', '--abbrev-ref', 'HEAD'], ['git', 'rev-parse', '--abbrev-ref', 'HEAD'],
capture_output=True, capture_output=True,
text=True, text=True,
check=True check=True,
cwd=project_dir # Run git in project directory, not plugin directory
) )
return result.stdout.strip() return result.stdout.strip()
except subprocess.CalledProcessError: except subprocess.CalledProcessError:

View File

@@ -7,6 +7,7 @@ Provides async wrappers for PR operations with:
- Comprehensive error handling - Comprehensive error handling
""" """
import asyncio import asyncio
import os
import subprocess import subprocess
import logging import logging
from typing import List, Dict, Optional from typing import List, Dict, Optional
@@ -27,19 +28,34 @@ class PullRequestTools:
""" """
self.gitea = gitea_client self.gitea = gitea_client
def _get_project_directory(self) -> Optional[str]:
"""
Get the user's project directory from environment.
Returns:
Project directory path or None if not set
"""
return os.environ.get('CLAUDE_PROJECT_DIR')
def _get_current_branch(self) -> str: def _get_current_branch(self) -> str:
""" """
Get current git branch. Get current git branch from user's project directory.
Uses CLAUDE_PROJECT_DIR environment variable to determine the correct
directory for git operations, avoiding the bug where git runs from
the installed plugin directory instead of the user's project.
Returns: Returns:
Current branch name or 'unknown' if not in a git repo Current branch name or 'unknown' if not in a git repo
""" """
try: try:
project_dir = self._get_project_directory()
result = subprocess.run( result = subprocess.run(
['git', 'rev-parse', '--abbrev-ref', 'HEAD'], ['git', 'rev-parse', '--abbrev-ref', 'HEAD'],
capture_output=True, capture_output=True,
text=True, text=True,
check=True check=True,
cwd=project_dir # Run git in project directory, not plugin directory
) )
return result.stdout.strip() return result.stdout.strip()
except subprocess.CalledProcessError: except subprocess.CalledProcessError: