upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/src/lib
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/mod.rs1
-rw-r--r--src/lib/push.rs310
2 files changed, 311 insertions, 0 deletions
diff --git a/src/lib/mod.rs b/src/lib/mod.rs
index 076c2d2..265dd6b 100644
--- a/src/lib/mod.rs
+++ b/src/lib/mod.rs
@@ -4,6 +4,7 @@ pub mod git;
4pub mod git_events; 4pub mod git_events;
5pub mod list; 5pub mod list;
6pub mod login; 6pub mod login;
7pub mod push;
7pub mod repo_ref; 8pub mod repo_ref;
8pub mod repo_state; 9pub mod repo_state;
9pub mod utils; 10pub mod utils;
diff --git a/src/lib/push.rs b/src/lib/push.rs
new file mode 100644
index 0000000..0d0ec93
--- /dev/null
+++ b/src/lib/push.rs
@@ -0,0 +1,310 @@
1use std::{
2 sync::{Arc, Mutex},
3 time::Instant,
4};
5
6use anyhow::{Result, anyhow};
7use auth_git2::GitAuthenticator;
8use console::Term;
9
10use crate::{
11 cli_interactor::count_lines_per_msg_vec,
12 git::{
13 Repo,
14 nostr_url::{CloneUrl, NostrUrlDecoded},
15 oid_to_shorthand_string,
16 },
17 utils::{
18 Direction, get_short_git_server_name, get_write_protocols_to_try, join_with_and,
19 set_protocol_preference,
20 },
21};
22
23pub fn push_to_remote(
24 git_repo: &Repo,
25 git_server_url: &str,
26 decoded_nostr_url: &NostrUrlDecoded,
27 remote_refspecs: &[String],
28 term: &Term,
29 is_grasp_server: bool,
30) -> Result<()> {
31 let server_url = git_server_url.parse::<CloneUrl>()?;
32 let protocols_to_attempt =
33 get_write_protocols_to_try(git_repo, &server_url, decoded_nostr_url, is_grasp_server);
34
35 let mut failed_protocols = vec![];
36 let mut success = false;
37
38 for protocol in &protocols_to_attempt {
39 term.write_line(format!("push: {} over {protocol}...", server_url.short_name(),).as_str())?;
40
41 let formatted_url = server_url.format_as(protocol, &decoded_nostr_url.user)?;
42
43 if let Err(error) = push_to_remote_url(git_repo, &formatted_url, remote_refspecs, term) {
44 term.write_line(
45 format!("push: {formatted_url} failed over {protocol}: {error}").as_str(),
46 )?;
47 failed_protocols.push(protocol);
48 } else {
49 success = true;
50 if !failed_protocols.is_empty() {
51 term.write_line(format!("push: succeeded over {protocol}").as_str())?;
52 let _ = set_protocol_preference(git_repo, protocol, &server_url, &Direction::Push);
53 }
54 break;
55 }
56 }
57 if success {
58 Ok(())
59 } else {
60 let error = anyhow!(
61 "{} failed over {}{}",
62 server_url.short_name(),
63 join_with_and(&failed_protocols),
64 if decoded_nostr_url.protocol.is_some() {
65 " and nostr url contains protocol override so no other protocols were attempted"
66 } else {
67 ""
68 },
69 );
70 term.write_line(format!("push: {error}").as_str())?;
71 Err(error)
72 }
73}
74
75pub fn push_to_remote_url(
76 git_repo: &Repo,
77 git_server_url: &str,
78 remote_refspecs: &[String],
79 term: &Term,
80) -> Result<()> {
81 let git_config = git_repo.git_repo.config()?;
82 let mut git_server_remote = git_repo.git_repo.remote_anonymous(git_server_url)?;
83 let auth = GitAuthenticator::default();
84 let mut push_options = git2::PushOptions::new();
85 let mut remote_callbacks = git2::RemoteCallbacks::new();
86 let push_reporter = Arc::new(Mutex::new(PushReporter::new(term)));
87
88 remote_callbacks.credentials(auth.credentials(&git_config));
89
90 remote_callbacks.push_update_reference({
91 let push_reporter = Arc::clone(&push_reporter);
92 move |name, error| {
93 let mut reporter = push_reporter.lock().unwrap();
94 if let Some(error) = error {
95 let existing_lines = reporter.count_all_existing_lines();
96 reporter.update_reference_errors.push(format!(
97 "WARNING: {} failed to push {name} error: {error}",
98 get_short_git_server_name(git_repo, git_server_url),
99 ));
100 reporter.write_all(existing_lines);
101 }
102 Ok(())
103 }
104 });
105
106 remote_callbacks.push_negotiation({
107 let push_reporter = Arc::clone(&push_reporter);
108 move |updates| {
109 let mut reporter = push_reporter.lock().unwrap();
110 let existing_lines = reporter.count_all_existing_lines();
111
112 for update in updates {
113 let dst_refname = update
114 .dst_refname()
115 .unwrap_or("")
116 .replace("refs/heads/", "")
117 .replace("refs/tags/", "tags/");
118 let msg = if update.dst().is_zero() {
119 format!("push: - [delete] {dst_refname}")
120 } else if update.src().is_zero() {
121 if update.dst_refname().unwrap_or("").contains("refs/tags") {
122 format!("push: * [new tag] {dst_refname}")
123 } else {
124 format!("push: * [new branch] {dst_refname}")
125 }
126 } else {
127 let force = remote_refspecs
128 .iter()
129 .any(|r| r.contains(&dst_refname) && r.contains('+'));
130 format!(
131 "push: {} {}..{} {} -> {dst_refname}",
132 if force { "+" } else { " " },
133 oid_to_shorthand_string(update.src()).unwrap(),
134 oid_to_shorthand_string(update.dst()).unwrap(),
135 update
136 .src_refname()
137 .unwrap_or("")
138 .replace("refs/heads/", "")
139 .replace("refs/tags/", "tags/"),
140 )
141 };
142 // other possibilities will result in push to fail but better reporting is
143 // needed:
144 // deleting a non-existant branch:
145 // ! [remote rejected] <old-branch-name> -> <old-branch-name> (not found)
146 // adding a branch that already exists:
147 // ! [remote rejected] <new-branch-name> -> <new-branch-name> (already exists)
148 // pushing without non-fast-forward without force:
149 // ! [rejected] <branch-name> -> <branch-name> (non-fast-forward)
150 reporter.negotiation.push(msg);
151 }
152 reporter.write_all(existing_lines);
153 Ok(())
154 }
155 });
156
157 remote_callbacks.push_transfer_progress({
158 let push_reporter = Arc::clone(&push_reporter);
159 #[allow(clippy::cast_precision_loss)]
160 move |current, total, bytes| {
161 let mut reporter = push_reporter.lock().unwrap();
162 reporter.process_transfer_progress_update(current, total, bytes);
163 }
164 });
165
166 remote_callbacks.sideband_progress({
167 let push_reporter = Arc::clone(&push_reporter);
168 move |data| {
169 let mut reporter = push_reporter.lock().unwrap();
170 reporter.process_remote_msg(data);
171 true
172 }
173 });
174 push_options.remote_callbacks(remote_callbacks);
175 git_server_remote.push(remote_refspecs, Some(&mut push_options))?;
176 let _ = git_server_remote.disconnect();
177 Ok(())
178}
179
180#[allow(clippy::cast_precision_loss)]
181#[allow(clippy::float_cmp)]
182#[allow(clippy::needless_pass_by_value)]
183fn report_on_transfer_progress(
184 current: usize,
185 total: usize,
186 bytes: usize,
187 start_time: &Instant,
188 end_time: Option<&Instant>,
189) -> Option<String> {
190 if total == 0 {
191 return None;
192 }
193 let percentage = ((current as f64 / total as f64) * 100.0)
194 // always round down because 100% complete is misleading when its not complete
195 .floor();
196 let (size, unit) = if bytes as f64 >= (1024.0 * 1024.0) {
197 (bytes as f64 / (1024.0 * 1024.0), "MiB")
198 } else {
199 (bytes as f64 / 1024.0, "KiB")
200 };
201 let speed = {
202 let duration = if let Some(end_time) = end_time {
203 (*end_time - *start_time).as_millis() as f64
204 } else {
205 start_time.elapsed().as_millis() as f64
206 };
207
208 if duration > 0.0 {
209 (bytes as f64 / (1024.0 * 1024.0)) / (duration / 1000.0) // Convert bytes to MiB and milliseconds to seconds
210 } else {
211 0.0
212 }
213 };
214
215 Some(format!(
216 "push: Writing objects: {percentage}% ({current}/{total}) {size:.2} {unit} | {speed:.2} MiB/s{}",
217 if current == total { ", done." } else { "" },
218 ))
219}
220
221pub struct PushReporter<'a> {
222 remote_msgs: Vec<String>,
223 negotiation: Vec<String>,
224 transfer_progress_msgs: Vec<String>,
225 update_reference_errors: Vec<String>,
226 term: &'a console::Term,
227 start_time: Option<Instant>,
228 end_time: Option<Instant>,
229}
230impl<'a> PushReporter<'a> {
231 fn new(term: &'a console::Term) -> Self {
232 Self {
233 remote_msgs: vec![],
234 negotiation: vec![],
235 transfer_progress_msgs: vec![],
236 update_reference_errors: vec![],
237 term,
238 start_time: None,
239 end_time: None,
240 }
241 }
242 fn write_all(&self, lines_to_clear: usize) {
243 let _ = self.term.clear_last_lines(lines_to_clear);
244 for msg in &self.remote_msgs {
245 let _ = self.term.write_line(format!("remote: {msg}").as_str());
246 }
247 for msg in &self.negotiation {
248 let _ = self.term.write_line(msg);
249 }
250 for msg in &self.transfer_progress_msgs {
251 let _ = self.term.write_line(msg);
252 }
253 for msg in &self.update_reference_errors {
254 let _ = self.term.write_line(msg);
255 }
256 }
257
258 fn count_all_existing_lines(&self) -> usize {
259 let width = self.term.size().1;
260 count_lines_per_msg_vec(width, &self.remote_msgs, "remote: ".len())
261 + count_lines_per_msg_vec(width, &self.negotiation, 0)
262 + count_lines_per_msg_vec(width, &self.transfer_progress_msgs, 0)
263 + count_lines_per_msg_vec(width, &self.update_reference_errors, 0)
264 }
265 fn process_remote_msg(&mut self, data: &[u8]) {
266 if let Ok(data) = str::from_utf8(data) {
267 let data = data
268 .split(['\n', '\r'])
269 .map(str::trim)
270 .filter(|line| !line.trim().is_empty())
271 .collect::<Vec<&str>>();
272 for data in data {
273 let existing_lines = self.count_all_existing_lines();
274 let msg = data.to_string();
275 if let Some(last) = self.remote_msgs.last() {
276 if (last.contains('%') && !last.contains("100%"))
277 || last == &msg.replace(", done.", "")
278 {
279 self.remote_msgs.pop();
280 self.remote_msgs.push(msg);
281 } else {
282 self.remote_msgs.push(msg);
283 }
284 } else {
285 self.remote_msgs.push(msg);
286 }
287 self.write_all(existing_lines);
288 }
289 }
290 }
291 fn process_transfer_progress_update(&mut self, current: usize, total: usize, bytes: usize) {
292 if self.start_time.is_none() {
293 self.start_time = Some(Instant::now());
294 }
295 if let Some(report) = report_on_transfer_progress(
296 current,
297 total,
298 bytes,
299 &self.start_time.unwrap(),
300 self.end_time.as_ref(),
301 ) {
302 let existing_lines = self.count_all_existing_lines();
303 if report.contains("100%") {
304 self.end_time = Some(Instant::now());
305 }
306 self.transfer_progress_msgs = vec![report];
307 self.write_all(existing_lines);
308 }
309 }
310}