1use std::{
2 fs::{self},
3 sync::{Arc, Mutex},
4};
5
6use anyhow::Error;
7use fluentci_core::deps::{Graph, GraphCommand};
8use fluentci_ext::git::Git as GitExt;
9use fluentci_ext::git_checkout::GitCheckout as GitCheckoutExt;
10use fluentci_ext::git_last_commit::GitLastCommit as GitLastCommitExt;
11use fluentci_ext::runner::Runner as RunnerExt;
12use fluentci_types::{directory::Directory, git::Git};
13use uuid::Uuid;
14
15use crate::util::{extract_git_repo, validate_git_url};
16
17pub fn git(graph: Arc<Mutex<Graph>>, url: String, reset: bool) -> Result<Git, Error> {
18 let mut graph = graph.lock().unwrap();
19
20 if reset {
21 graph.reset();
22 }
23
24 graph.runner = Arc::new(Box::new(GitExt::default()));
25 graph.runner.setup()?;
26 graph.work_dir = format!(
27 "{}/.fluentci/cache",
28 dirs::home_dir().unwrap().to_str().unwrap()
29 );
30
31 if !validate_git_url(&url) {
32 return Err(Error::msg("Invalid git url"));
33 }
34 let repo = extract_git_repo(&url);
35 graph.work_dir = format!("{}/{}", graph.work_dir, repo);
36
37 fs::create_dir_all(&graph.work_dir)?;
38
39 let id = Uuid::new_v4().to_string();
40 graph.execute(GraphCommand::AddVertex(
41 id.clone(),
42 "git".into(),
43 url.clone(),
44 vec![],
45 Arc::new(Box::new(GitExt::default())),
46 ))?;
47 graph.execute_vertex(&id)?;
48 graph.work_dir = format!(
49 "{}/{}",
50 graph.work_dir,
51 url.split("/").last().unwrap().replace(".git", "")
52 );
53 let git = Git { id };
54 Ok(git)
55}
56
57pub fn branch(graph: Arc<Mutex<Graph>>, name: String) -> Result<(), Error> {
58 let mut graph = graph.lock().unwrap();
59 graph.runner = Arc::new(Box::new(GitCheckoutExt::default()));
60 graph.runner.setup()?;
61
62 let id = Uuid::new_v4().to_string();
63
64 let dep_id = graph.vertices[graph.size() - 1].id.clone();
65 let deps = match graph.size() {
66 1 => vec![],
67 _ => vec![dep_id],
68 };
69 graph.execute(GraphCommand::AddVertex(
70 id.clone(),
71 "git-checkout".into(),
72 name,
73 deps,
74 Arc::new(Box::new(GitCheckoutExt::default())),
75 ))?;
76 graph.execute_vertex(&id)?;
77
78 if graph.size() > 2 {
79 let x = graph.size() - 2;
80 let y = graph.size() - 1;
81 graph.execute(GraphCommand::AddEdge(x, y))?;
82 }
83 Ok(())
84}
85
86pub fn commit(graph: Arc<Mutex<Graph>>) -> Result<String, Error> {
87 let mut graph = graph.lock().unwrap();
88 graph.runner = Arc::new(Box::new(GitLastCommitExt::default()));
89 graph.runner.setup()?;
90
91 let id = Uuid::new_v4().to_string();
92
93 let dep_id = graph.vertices[graph.size() - 1].id.clone();
94 let deps = match graph.size() {
95 1 => vec![],
96 _ => vec![dep_id],
97 };
98 graph.execute(GraphCommand::AddVertex(
99 id.clone(),
100 "git-last-commit".into(),
101 "".into(),
102 deps,
103 Arc::new(Box::new(GitLastCommitExt::default())),
104 ))?;
105
106 if graph.size() > 2 {
107 let x = graph.size() - 2;
108 let y = graph.size() - 1;
109 graph.execute(GraphCommand::AddEdge(x, y))?;
110 }
111
112 graph.execute_vertex(&id)
113}
114
115pub fn tree(graph: Arc<Mutex<Graph>>) -> Result<Directory, Error> {
116 let id = Uuid::new_v4().to_string();
117 let mut graph = graph.lock().unwrap();
118
119 let dep_id = graph.vertices[graph.size() - 1].id.clone();
120
121 graph.execute(GraphCommand::AddVertex(
122 id.clone(),
123 "tree".into(),
124 "".into(),
125 vec![dep_id],
126 Arc::new(Box::new(RunnerExt::default())),
127 ))?;
128
129 let x = graph.size() - 2;
130 let y = graph.size() - 1;
131 graph.execute(GraphCommand::AddEdge(x, y))?;
132 graph.runner = Arc::new(Box::new(RunnerExt::default()));
133
134 let path = graph.work_dir.clone();
135
136 graph.execute(GraphCommand::AddVolume(
137 id.clone(),
138 "directory".into(),
139 path.clone(),
140 ))?;
141
142 let directory = Directory { id, path };
143
144 Ok(directory)
145}