Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 50 additions & 62 deletions codemcp/tools/chmod.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,74 +64,62 @@ async def chmod(
# Get the directory containing the file for git operations
directory = os.path.dirname(absolute_path)

try:
# Check current file permissions
current_mode = os.stat(absolute_path).st_mode
is_executable = bool(current_mode & stat.S_IXUSR)

if mode == "a+x" and is_executable:
message = f"File '{path}' is already executable"
return {
"output": message,
"resultForAssistant": message,
}
elif mode == "a-x" and not is_executable:
message = f"File '{path}' is already non-executable"
return {
"output": message,
"resultForAssistant": message,
}

# Execute chmod command
cmd = ["chmod", mode, absolute_path]
await run_command(
cmd=cmd,
cwd=directory,
capture_output=True,
text=True,
check=True,
)
# Check current file permissions
current_mode = os.stat(absolute_path).st_mode
is_executable = bool(current_mode & stat.S_IXUSR)

if mode == "a+x" and is_executable:
message = f"File '{path}' is already executable"
return {
"output": message,
"resultForAssistant": message,
}
elif mode == "a-x" and not is_executable:
message = f"File '{path}' is already non-executable"
return {
"output": message,
"resultForAssistant": message,
}

# Prepare success message
if mode == "a+x":
description = f"Make '{os.path.basename(absolute_path)}' executable"
action_msg = f"Made file '{path}' executable"
else:
description = (
f"Remove executable permission from '{os.path.basename(absolute_path)}'"
)
action_msg = f"Removed executable permission from file '{path}'"

# Commit the changes
success, commit_message = await commit_changes(
directory,
description,
chat_id,
# Execute chmod command
cmd = ["chmod", mode, absolute_path]
await run_command(
cmd=cmd,
cwd=directory,
capture_output=True,
text=True,
check=True,
)

# Prepare success message
if mode == "a+x":
description = f"Make '{os.path.basename(absolute_path)}' executable"
action_msg = f"Made file '{path}' executable"
else:
description = (
f"Remove executable permission from '{os.path.basename(absolute_path)}'"
)
action_msg = f"Removed executable permission from file '{path}'"

if not success:
logging.warning(f"Failed to commit chmod changes: {commit_message}")
return {
"output": f"{action_msg}, but failed to commit changes: {commit_message}",
"resultForAssistant": f"{action_msg}, but failed to commit changes: {commit_message}",
}
# Commit the changes
success, commit_message = await commit_changes(
directory,
description,
chat_id,
)

# Prepare output
output = {
"output": f"{action_msg} and committed changes",
}
if not success:
raise RuntimeError(f"Failed to commit chmod changes: {commit_message}")

# Add formatted result for assistant
output["resultForAssistant"] = render_result_for_assistant(output)
# Prepare output
output = {
"output": f"{action_msg} and committed changes",
}

return output
except Exception as e:
logging.exception(f"Error executing chmod: {e!s}")
error_message = f"Error executing chmod: {e!s}"
return {
"output": error_message,
"resultForAssistant": error_message,
}
# Add formatted result for assistant
output["resultForAssistant"] = render_result_for_assistant(output)

return output


def render_result_for_assistant(output: dict[str, Any]) -> str:
Expand Down
55 changes: 18 additions & 37 deletions codemcp/tools/git_blame.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,43 +68,24 @@ async def git_blame(

logging.debug(f"Executing git blame command: {' '.join(cmd)}")

try:
# Execute git blame command asynchronously
result = await run_command(
cmd=cmd,
cwd=absolute_path,
capture_output=True,
text=True,
check=False, # Don't raise exception if git blame fails
)

# Process results
if result.returncode != 0:
logging.error(
f"git blame failed with exit code {result.returncode}: {result.stderr}"
)
error_message = f"Error: {result.stderr}"
return {
"output": error_message,
"resultForAssistant": error_message,
}

# Prepare output
output = {
"output": result.stdout,
}

# Add formatted result for assistant
output["resultForAssistant"] = render_result_for_assistant(output)

return output
except Exception as e:
logging.exception(f"Error executing git blame: {e!s}")
error_message = f"Error executing git blame: {e!s}"
return {
"output": error_message,
"resultForAssistant": error_message,
}
# Execute git blame command asynchronously
result = await run_command(
cmd=cmd,
cwd=absolute_path,
capture_output=True,
text=True,
check=True, # Allow exception if git blame fails to propagate up
)

# Prepare output
output = {
"output": result.stdout,
}

# Add formatted result for assistant
output["resultForAssistant"] = render_result_for_assistant(output)

return output


def render_result_for_assistant(output: dict[str, Any]) -> str:
Expand Down
55 changes: 18 additions & 37 deletions codemcp/tools/git_diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,43 +69,24 @@ async def git_diff(

logging.debug(f"Executing git diff command: {' '.join(cmd)}")

try:
# Execute git diff command asynchronously
result = await run_command(
cmd=cmd,
cwd=absolute_path,
capture_output=True,
text=True,
check=False, # Don't raise exception if git diff fails
)

# Process results
if result.returncode != 0:
logging.error(
f"git diff failed with exit code {result.returncode}: {result.stderr}"
)
error_message = f"Error: {result.stderr}"
return {
"output": error_message,
"resultForAssistant": error_message,
}

# Prepare output
output = {
"output": result.stdout,
}

# Add formatted result for assistant
output["resultForAssistant"] = render_result_for_assistant(output)

return output
except Exception as e:
logging.exception(f"Error executing git diff: {e!s}")
error_message = f"Error executing git diff: {e!s}"
return {
"output": error_message,
"resultForAssistant": error_message,
}
# Execute git diff command asynchronously
result = await run_command(
cmd=cmd,
cwd=absolute_path,
capture_output=True,
text=True,
check=True, # Allow exception if git diff fails to propagate up
)

# Prepare output
output = {
"output": result.stdout,
}

# Add formatted result for assistant
output["resultForAssistant"] = render_result_for_assistant(output)

return output


def render_result_for_assistant(output: dict[str, Any]) -> str:
Expand Down
55 changes: 18 additions & 37 deletions codemcp/tools/git_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,43 +68,24 @@ async def git_log(

logging.debug(f"Executing git log command: {' '.join(cmd)}")

try:
# Execute git log command asynchronously
result = await run_command(
cmd=cmd,
cwd=absolute_path,
capture_output=True,
text=True,
check=False, # Don't raise exception if git log fails
)

# Process results
if result.returncode != 0:
logging.error(
f"git log failed with exit code {result.returncode}: {result.stderr}"
)
error_message = f"Error: {result.stderr}"
return {
"output": error_message,
"resultForAssistant": error_message,
}

# Prepare output
output = {
"output": result.stdout,
}

# Add formatted result for assistant
output["resultForAssistant"] = render_result_for_assistant(output)

return output
except Exception as e:
logging.exception(f"Error executing git log: {e!s}")
error_message = f"Error executing git log: {e!s}"
return {
"output": error_message,
"resultForAssistant": error_message,
}
# Execute git log command asynchronously
result = await run_command(
cmd=cmd,
cwd=absolute_path,
capture_output=True,
text=True,
check=True, # Allow exception if git log fails to propagate up
)

# Prepare output
output = {
"output": result.stdout,
}

# Add formatted result for assistant
output["resultForAssistant"] = render_result_for_assistant(output)

return output


def render_result_for_assistant(output: dict[str, Any]) -> str:
Expand Down
55 changes: 18 additions & 37 deletions codemcp/tools/git_show.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,43 +70,24 @@ async def git_show(

logging.debug(f"Executing git show command: {' '.join(cmd)}")

try:
# Execute git show command asynchronously
result = await run_command(
cmd=cmd,
cwd=absolute_path,
capture_output=True,
text=True,
check=False, # Don't raise exception if git show fails
)

# Process results
if result.returncode != 0:
logging.error(
f"git show failed with exit code {result.returncode}: {result.stderr}"
)
error_message = f"Error: {result.stderr}"
return {
"output": error_message,
"resultForAssistant": error_message,
}

# Prepare output
output = {
"output": result.stdout,
}

# Add formatted result for assistant
output["resultForAssistant"] = render_result_for_assistant(output)

return output
except Exception as e:
logging.exception(f"Error executing git show: {e!s}")
error_message = f"Error executing git show: {e!s}"
return {
"output": error_message,
"resultForAssistant": error_message,
}
# Execute git show command asynchronously
result = await run_command(
cmd=cmd,
cwd=absolute_path,
capture_output=True,
text=True,
check=True, # Allow exception if git show fails to propagate up
)

# Prepare output
output = {
"output": result.stdout,
}

# Add formatted result for assistant
output["resultForAssistant"] = render_result_for_assistant(output)

return output


def render_result_for_assistant(output: dict[str, Any]) -> str:
Expand Down
Loading
Loading