|
| 1 | +use serde_json::{json, Value}; |
| 2 | +use socketioxide::extract::{AckSender, Data, State}; |
| 3 | +use tracing::info; |
| 4 | +use crate::app_state::{AppState, send_response}; |
| 5 | +use serde::{Deserialize, Serialize}; |
| 6 | +use git2::{Repository, Status, StatusOptions}; |
| 7 | +use std::path::Path; |
| 8 | +use anyhow::{Context, Result}; |
| 9 | + |
| 10 | +#[derive(Debug, Serialize, Deserialize, Clone, Copy, PartialEq, Eq)] |
| 11 | +#[serde(rename_all = "lowercase")] |
| 12 | +pub enum FileStatus { |
| 13 | + Modified, Added, Deleted, Renamed, |
| 14 | +} |
| 15 | + |
| 16 | +#[derive(Debug, Serialize, Deserialize, Clone)] |
| 17 | +pub struct GitFileStatus { |
| 18 | + pub path: String, |
| 19 | + pub status: FileStatus, |
| 20 | +} |
| 21 | + |
| 22 | +#[derive(Debug, Serialize, Deserialize, Clone)] |
| 23 | +pub struct GitFileOriginalRequest { |
| 24 | + pub path: String, |
| 25 | +} |
| 26 | + |
| 27 | +#[derive(Debug, Serialize, Deserialize, Clone)] |
| 28 | +pub struct GitCommitRequest { |
| 29 | + pub files: Vec<String>, |
| 30 | + pub message: String, |
| 31 | +} |
| 32 | + |
| 33 | +fn git_status_impl() -> Result<Value> { |
| 34 | + let workdir = crate::utils::current_dir(); |
| 35 | + |
| 36 | + let repo = Repository::discover(&workdir)?; |
| 37 | + |
| 38 | + let branch = repo.head() |
| 39 | + .map(|h| h.shorthand().unwrap_or("HEAD").to_string()) |
| 40 | + .unwrap_or_else(|_| "HEAD".to_string()); |
| 41 | + |
| 42 | + let mut opts = StatusOptions::new(); |
| 43 | + opts.include_untracked(true) |
| 44 | + .recurse_untracked_dirs(true) |
| 45 | + .include_ignored(false); |
| 46 | + |
| 47 | + let statuses = repo.statuses(Some(&mut opts))?; |
| 48 | + |
| 49 | + let mut files: Vec<GitFileStatus> = Vec::new(); |
| 50 | + |
| 51 | + for entry in statuses.iter() { |
| 52 | + let path = entry.path().unwrap_or("").to_string(); |
| 53 | + let status = entry.status(); |
| 54 | + |
| 55 | + let file_status = if status.contains(Status::WT_NEW) || status.contains(Status::INDEX_NEW) { |
| 56 | + FileStatus::Added |
| 57 | + } else if status.contains(Status::WT_DELETED) || status.contains(Status::INDEX_DELETED) { |
| 58 | + FileStatus::Deleted |
| 59 | + } else if status.contains(Status::WT_MODIFIED) || status.contains(Status::INDEX_MODIFIED) { |
| 60 | + FileStatus::Modified |
| 61 | + } else if status.contains(Status::WT_RENAMED) || status.contains(Status::INDEX_RENAMED) { |
| 62 | + FileStatus::Renamed |
| 63 | + } else { |
| 64 | + continue; |
| 65 | + }; |
| 66 | + |
| 67 | + files.push(GitFileStatus { |
| 68 | + path, status: file_status |
| 69 | + }); |
| 70 | + } |
| 71 | + |
| 72 | + info!("Git status: {} files changed on branch {}", files.len(), branch); |
| 73 | + |
| 74 | + Ok(json!({ "files": files, "branch": branch })) |
| 75 | +} |
| 76 | + |
| 77 | +pub async fn handle_git_status(ack: AckSender, _state: State<AppState>) { |
| 78 | + info!("Received git:status"); |
| 79 | + send_response(ack, git_status_impl()); |
| 80 | +} |
| 81 | + |
| 82 | +fn git_file_original_impl(request: &GitFileOriginalRequest) -> Result<Value> { |
| 83 | + let workdir = crate::utils::current_dir(); |
| 84 | + |
| 85 | + let repo = Repository::discover(&workdir)?; |
| 86 | + let head = repo.head()?; |
| 87 | + let commit = head.peel_to_commit()?; |
| 88 | + let tree = commit.tree()?; |
| 89 | + |
| 90 | + // Convert absolute path to relative path from repo root |
| 91 | + let repo_path = repo.workdir().unwrap_or(Path::new(".")); |
| 92 | + let file_path = Path::new(&request.path); |
| 93 | + |
| 94 | + let relative_path = if file_path.is_absolute() { |
| 95 | + file_path.strip_prefix(repo_path) |
| 96 | + .map(|p| p.to_string_lossy().to_string()) |
| 97 | + .unwrap_or_else(|_| request.path.clone()) |
| 98 | + } else { |
| 99 | + request.path.clone() |
| 100 | + }; |
| 101 | + |
| 102 | + // Get file from tree - if not found, it's a new file |
| 103 | + let entry = match tree.get_path(Path::new(&relative_path)) { |
| 104 | + Ok(e) => e, |
| 105 | + Err(_) => { |
| 106 | + return Ok(json!({ "content": "", "is_new": true })); |
| 107 | + } |
| 108 | + }; |
| 109 | + |
| 110 | + let blob = repo.find_blob(entry.id())?; |
| 111 | + let content = std::str::from_utf8(blob.content())?.to_string(); |
| 112 | + |
| 113 | + info!("Got original content for {}: {} bytes", relative_path, content.len()); |
| 114 | + Ok(json!({ "content": content, "is_new": false })) |
| 115 | +} |
| 116 | + |
| 117 | +pub async fn handle_git_file_original( |
| 118 | + Data(request): Data<GitFileOriginalRequest>, |
| 119 | + ack: AckSender, |
| 120 | + _state: State<AppState>, |
| 121 | +) { |
| 122 | + info!("Received git:file-original: {:?}", request.path); |
| 123 | + send_response(ack, git_file_original_impl(&request)); |
| 124 | +} |
| 125 | + |
| 126 | +fn git_commit_impl(request: &GitCommitRequest) -> Result<Value> { |
| 127 | + let workdir = crate::utils::current_dir(); |
| 128 | + |
| 129 | + let repo = Repository::discover(&workdir)?; |
| 130 | + let mut index = repo.index()?; |
| 131 | + |
| 132 | + // Add files to index |
| 133 | + for path in &request.files { |
| 134 | + let path = Path::new(path); |
| 135 | + let relative_path = if path.is_absolute() { |
| 136 | + path.strip_prefix(repo.workdir().unwrap_or(Path::new("."))) |
| 137 | + .unwrap_or(path) |
| 138 | + } else { |
| 139 | + path |
| 140 | + }; |
| 141 | + |
| 142 | + index.add_path(relative_path)?; |
| 143 | + } |
| 144 | + |
| 145 | + index.write()?; |
| 146 | + |
| 147 | + let tree_id = index.write_tree()?; |
| 148 | + |
| 149 | + let tree = repo.find_tree(tree_id)?; |
| 150 | + |
| 151 | + let sig = repo.signature().or_else(|_| { |
| 152 | + git2::Signature::now("Anycode User", "user@anycode.dev") |
| 153 | + })?; |
| 154 | + |
| 155 | + // Get HEAD as parent |
| 156 | + let parents: Vec<git2::Commit> = repo.head() |
| 157 | + .ok() |
| 158 | + .and_then(|h| h.peel_to_commit().ok()) |
| 159 | + .map(|c| vec![c]) |
| 160 | + .unwrap_or_default(); |
| 161 | + |
| 162 | + let parents_refs: Vec<&git2::Commit> = parents.iter().collect(); |
| 163 | + |
| 164 | + repo.commit(Some("HEAD"), &sig, &sig, &request.message, &tree, &parents_refs) |
| 165 | + .context("Failed to commit")?; |
| 166 | + |
| 167 | + Ok(json!({})) |
| 168 | +} |
| 169 | + |
| 170 | +pub async fn handle_git_commit( |
| 171 | + Data(request): Data<GitCommitRequest>, |
| 172 | + ack: AckSender, |
| 173 | + _state: State<AppState>, |
| 174 | +) { |
| 175 | + info!("Received git:commit: {} files", request.files.len()); |
| 176 | + send_response(ack, git_commit_impl(&request)); |
| 177 | +} |
| 178 | + |
| 179 | +fn git_push_impl() -> Result<Value> { |
| 180 | + let workdir = crate::utils::current_dir(); |
| 181 | + |
| 182 | + let repo = Repository::discover(&workdir)?; |
| 183 | + let mut remote = repo.find_remote("origin")?; |
| 184 | + let head = repo.head()?; |
| 185 | + |
| 186 | + let branch_name = head.shorthand() |
| 187 | + .context("Detached HEAD state")?; |
| 188 | + |
| 189 | + let refspec = format!("refs/heads/{}:refs/heads/{}", branch_name, branch_name); |
| 190 | + |
| 191 | + let mut callbacks = git2::RemoteCallbacks::new(); |
| 192 | + callbacks.credentials(|_url, username_from_url, _allowed_types| { |
| 193 | + git2::Cred::ssh_key_from_agent(username_from_url.unwrap_or("git")) |
| 194 | + }); |
| 195 | + |
| 196 | + let mut push_opts = git2::PushOptions::new(); |
| 197 | + push_opts.remote_callbacks(callbacks); |
| 198 | + |
| 199 | + remote.push(&[&refspec], Some(&mut push_opts))?; |
| 200 | + |
| 201 | + Ok(json!({})) |
| 202 | +} |
| 203 | + |
| 204 | +pub async fn handle_git_push(ack: AckSender, _state: State<AppState>) { |
| 205 | + info!("Received git:push"); |
| 206 | + send_response(ack, git_push_impl()); |
| 207 | +} |
0 commit comments