Spaces:
Running
Running
import gradio as gr | |
import sys | |
from git_diff_utils import get_latest_commit_diff | |
from git_tree_utils import get_repo_tree_structure, print_tree | |
from git_readme_utils import get_repo_readme | |
def get_diff(repo_source, commit_depth=1): | |
""" | |
Get the diff of a specific commit from a git repository. | |
Args: | |
repo_source (str): Path to local repository or URL to remote repository | |
commit_depth (int, optional): How many commits back to check (default=1 for latest commit) | |
Returns: | |
str: The git diff output formatted as text | |
""" | |
try: | |
commit_depth = int(commit_depth) | |
if commit_depth < 1: | |
return "Error: commit_depth must be a positive integer" | |
except ValueError: | |
return "Error: commit_depth must be an integer" | |
print(f"Getting diff for repository: {repo_source} (commit depth: {commit_depth})") | |
diff = get_latest_commit_diff(repo_source, commit_depth) | |
return diff | |
def get_tree(repo_source, commit_depth=1): | |
""" | |
Get the tree structure of a git repository at a specific commit. | |
Args: | |
repo_source (str): Path to local repository or URL to remote repository | |
commit_depth (int, optional): How many commits back to check (default=1 for latest commit) | |
Returns: | |
str: The formatted tree structure as text | |
""" | |
try: | |
commit_depth = int(commit_depth) | |
if commit_depth < 1: | |
return "Error: commit_depth must be a positive integer" | |
except ValueError: | |
return "Error: commit_depth must be an integer" | |
print(f"Getting tree structure for repository: {repo_source} (commit depth: {commit_depth})") | |
tree = get_repo_tree_structure(repo_source, commit_depth) | |
if isinstance(tree, dict): | |
# Capture the output of print_tree to a string | |
import io | |
from contextlib import redirect_stdout | |
f = io.StringIO() | |
with redirect_stdout(f): | |
print("Repository Structure:") | |
print_tree(tree) | |
output = f.getvalue() | |
return output | |
else: | |
return tree # Error message | |
def get_readme(repo_source, commit_depth=1): | |
""" | |
Get the README content from a git repository at a specific commit. | |
Args: | |
repo_source (str): Path to local repository or URL to remote repository | |
commit_depth (int, optional): How many commits back to check (default=1 for latest commit) | |
Returns: | |
str: The content of the README file or an error message | |
""" | |
try: | |
commit_depth = int(commit_depth) | |
if commit_depth < 1: | |
return "Error: commit_depth must be a positive integer" | |
except ValueError: | |
return "Error: commit_depth must be an integer" | |
print(f"Getting README for repository: {repo_source} (commit depth: {commit_depth})") | |
result = get_repo_readme(repo_source, commit_depth) | |
if isinstance(result, tuple): | |
readme_file, readme_content = result | |
return f"README File: {readme_file}\n\n{readme_content}" | |
else: | |
return result # Error message | |
# Create the Gradio interface | |
with gr.Blocks(title="Git Repository Explorer") as demo: | |
gr.Markdown("# Git Repository Explorer") | |
# Feature selection | |
feature = gr.Radio( | |
choices=["View Commit Diff", "View Repository Structure", "View README"], | |
value="View Commit Diff", | |
label="Select Feature" | |
) | |
# Common inputs for all features | |
with gr.Row(): | |
with gr.Column(): | |
repo_input = gr.Textbox( | |
label="Repository Path or URL", | |
placeholder="Enter local path or remote URL (e.g., https://github.com/username/repo.git)", | |
lines=1 | |
) | |
depth_input = gr.Number( | |
label="Commit Depth", | |
value=1, | |
minimum=1, | |
step=1, | |
info="How many commits back to check (1 = latest commit)" | |
) | |
submit_btn = gr.Button("Execute", variant="primary") | |
# Output area | |
output = gr.TextArea( | |
label="Output", | |
lines=20, | |
max_lines=50, | |
show_copy_button=True | |
) | |
# Function to handle feature selection and execution | |
def process_request(feature_selection, repo_source, commit_depth): | |
if feature_selection == "View Commit Diff": | |
return get_diff(repo_source, commit_depth) | |
elif feature_selection == "View Repository Structure": | |
return get_tree(repo_source, commit_depth) | |
elif feature_selection == "View README": | |
return get_readme(repo_source, commit_depth) | |
return "Please select a feature" | |
# Update button text based on feature selection | |
def update_button_text(feature_selection): | |
if feature_selection == "View Commit Diff": | |
return "Get Diff" | |
elif feature_selection == "View Repository Structure": | |
return "Get Tree Structure" | |
elif feature_selection == "View README": | |
return "Get README" | |
return "Execute" | |
# Update output label based on feature selection | |
def update_output_label(feature_selection): | |
if feature_selection == "View Commit Diff": | |
return "Diff Output" | |
elif feature_selection == "View Repository Structure": | |
return "Repository Structure" | |
elif feature_selection == "View README": | |
return "README Content" | |
return "Output" | |
# Set up events | |
feature.change(fn=update_button_text, inputs=feature, outputs=submit_btn) | |
feature.change(fn=update_output_label, inputs=feature, outputs=output) | |
submit_btn.click( | |
fn=process_request, | |
inputs=[feature, repo_input, depth_input], | |
outputs=output | |
) | |
gr.Markdown(""" | |
### Instructions: | |
1. Select the feature you want to use | |
2. Enter a git repository path or URL | |
3. Specify how many commits back to check (default: 1 for the latest commit) | |
4. Click the execute button | |
This tool can handle both local repositories and remote URLs automatically. | |
#### Features: | |
- **View Commit Diff**: Shows the differences made in a specific commit | |
- **View Repository Structure**: Displays the tree structure of the repository at a specific commit | |
- **View README**: Shows the content of the README file if present in the repository | |
""") | |
# Launch the app with MCP server enabled | |
if __name__ == "__main__": | |
#demo.launch(share=True, mcp_server=True) | |
demo.launch(share=False, server_name="0.0.0.0", server_port=7860, mcp_server=True) |