hyprstream_p2p/git/
protocol.rs1use crate::types::GitRef;
4use std::collections::HashMap;
5
6pub const CAPABILITIES: &[&str] = &["fetch"];
8
9#[derive(Debug, Clone)]
11pub enum GitCommand {
12 Capabilities,
13 List,
14 Fetch {
15 commit_hash: String,
16 refname: String,
17 },
18}
19
20pub fn parse_command(line: &str) -> Option<GitCommand> {
22 let line = line.trim();
23
24 if line == "capabilities" {
25 Some(GitCommand::Capabilities)
26 } else if line == "list" {
27 Some(GitCommand::List)
28 } else if line.starts_with("fetch ") {
29 let parts: Vec<&str> = line.split_whitespace().collect();
30 if parts.len() >= 3 {
31 Some(GitCommand::Fetch {
32 commit_hash: parts[1].to_owned(),
33 refname: parts[2].to_owned(),
34 })
35 } else {
36 None
37 }
38 } else {
39 None
40 }
41}
42
43pub struct GitRemoteHelper {
45 refs: HashMap<String, String>,
46}
47
48impl GitRemoteHelper {
49 pub fn new(refs: Vec<GitRef>) -> Self {
51 let refs = refs
52 .into_iter()
53 .map(|git_ref| (git_ref.name, git_ref.hash.to_hex()))
54 .collect();
55
56 GitRemoteHelper { refs }
57 }
58
59 pub fn handle_command(&self, command: &GitCommand) -> String {
61 match command {
62 GitCommand::Capabilities => {
63 let mut response = String::new();
64 for capability in CAPABILITIES {
65 response.push_str(capability);
66 response.push('\n');
67 }
68 response.push('\n');
69 response
70 }
71 GitCommand::List => {
72 let mut response = String::new();
73 for (refname, commit_hash) in &self.refs {
74 response.push_str(&format!("{commit_hash} {refname}\n"));
75 }
76 response.push('\n');
77 response
78 }
79 GitCommand::Fetch { .. } => {
80 String::new()
83 }
84 }
85 }
86
87 pub fn get_ref(&self, refname: &str) -> Option<&String> {
89 self.refs.get(refname)
90 }
91
92 pub fn refs(&self) -> &HashMap<String, String> {
94 &self.refs
95 }
96}
97
98#[cfg(test)]
99mod tests {
100 use super::*;
101 use crate::types::GitHash;
102
103 #[test]
104 fn test_parse_command() {
105 assert!(matches!(
106 parse_command("capabilities"),
107 Some(GitCommand::Capabilities)
108 ));
109
110 assert!(matches!(parse_command("list"), Some(GitCommand::List)));
111
112 if let Some(GitCommand::Fetch {
113 commit_hash,
114 refname,
115 }) = parse_command("fetch abc123 refs/heads/main")
116 {
117 assert_eq!(commit_hash, "abc123");
118 assert_eq!(refname, "refs/heads/main");
119 } else {
120 panic!("Failed to parse fetch command");
121 }
122
123 assert!(parse_command("invalid").is_none());
124 }
125
126 #[test]
127 fn test_git_remote_helper() -> crate::error::Result<()> {
128 let refs = vec![
130 GitRef {
131 name: "refs/heads/main".to_owned(),
132 hash: GitHash::from_hex("0123456789abcdef0123456789abcdef01234567")?,
133 },
134 GitRef {
135 name: "HEAD".to_owned(),
136 hash: GitHash::from_hex("0123456789abcdef0123456789abcdef01234567")?,
137 },
138 ];
139
140 let helper = GitRemoteHelper::new(refs);
141
142 let caps_response = helper.handle_command(&GitCommand::Capabilities);
144 assert!(caps_response.contains("fetch"));
145
146 let list_response = helper.handle_command(&GitCommand::List);
148 assert!(list_response.contains("0123456789abcdef0123456789abcdef01234567 refs/heads/main"));
149 assert!(list_response.contains("0123456789abcdef0123456789abcdef01234567 HEAD"));
150
151 assert!(helper.get_ref("refs/heads/main").is_some());
153 assert!(helper.get_ref("nonexistent").is_none());
154 Ok(())
155 }
156}