upleb.uk

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

summaryrefslogtreecommitdiff
path: root/src/git_remote_helper.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/git_remote_helper.rs')
-rw-r--r--src/git_remote_helper.rs239
1 files changed, 199 insertions, 40 deletions
diff --git a/src/git_remote_helper.rs b/src/git_remote_helper.rs
index ba8ab61..68aa681 100644
--- a/src/git_remote_helper.rs
+++ b/src/git_remote_helper.rs
@@ -16,12 +16,17 @@ use anyhow::{bail, Context, Result};
16use auth_git2::GitAuthenticator; 16use auth_git2::GitAuthenticator;
17#[cfg(not(test))] 17#[cfg(not(test))]
18use client::Connect; 18use client::Connect;
19use client::{fetching_with_report, get_repo_ref_from_cache}; 19use client::{
20 fetching_with_report, get_repo_ref_from_cache, get_state_from_cache, sign_event, STATE_KIND,
21};
20use git::RepoActions; 22use git::RepoActions;
21use git2::{Remote, Repository}; 23use git2::{Oid, Repository};
22use nostr::nips::nip01::Coordinate; 24use nostr::nips::nip01::Coordinate;
23use nostr_sdk::Url; 25use nostr_sdk::{EventBuilder, Tag, Url};
26use nostr_signer::NostrSigner;
24use repo_ref::RepoRef; 27use repo_ref::RepoRef;
28use repo_state::RepoState;
29use sub_commands::send::send_events;
25 30
26#[cfg(not(test))] 31#[cfg(not(test))]
27use crate::client::Client; 32use crate::client::Client;
@@ -93,12 +98,14 @@ async fn main() -> Result<()> {
93 } 98 }
94 ["push", refspec] => { 99 ["push", refspec] => {
95 push( 100 push(
96 &git_repo.git_repo, 101 &git_repo,
97 &repo_ref, 102 &repo_ref,
98 nostr_remote_url, 103 nostr_remote_url,
99 &stdin, 104 &stdin,
100 refspec, 105 refspec,
101 )?; 106 &client,
107 )
108 .await?;
102 } 109 }
103 ["list"] => { 110 ["list"] => {
104 list(&git_repo.git_repo, &repo_ref, false)?; 111 list(&git_repo.git_repo, &repo_ref, false)?;
@@ -171,12 +178,14 @@ fn fetch(git_repo: &Repository, repo_ref: &RepoRef, stdin: &Stdin, refstr: &str)
171 Ok(()) 178 Ok(())
172} 179}
173 180
174fn push( 181async fn push(
175 git_repo: &Repository, 182 git_repo: &Repo,
176 repo_ref: &RepoRef, 183 repo_ref: &RepoRef,
177 nostr_remote_url: &str, 184 nostr_remote_url: &str,
178 stdin: &Stdin, 185 stdin: &Stdin,
179 initial_refspec: &str, 186 initial_refspec: &str,
187 #[cfg(test)] client: &crate::client::MockConnect,
188 #[cfg(not(test))] client: &Client,
180) -> Result<()> { 189) -> Result<()> {
181 // if no state events - create from first git server listed 190 // if no state events - create from first git server listed
182 let refspecs = get_refspecs_from_push_batch(stdin, initial_refspec)?; 191 let refspecs = get_refspecs_from_push_batch(stdin, initial_refspec)?;
@@ -184,9 +193,10 @@ fn push(
184 .git_server 193 .git_server
185 .first() 194 .first()
186 .context("no git server listed in nostr repository announcement")?; 195 .context("no git server listed in nostr repository announcement")?;
187 let mut git_server_remote = git_repo.remote_anonymous(git_server_url)?; 196 let mut git_server_remote = git_repo.git_repo.remote_anonymous(git_server_url)?;
197
188 let auth = GitAuthenticator::default(); 198 let auth = GitAuthenticator::default();
189 let git_config = git_repo.config()?; 199 let git_config = git_repo.git_repo.config()?;
190 let mut push_options = git2::PushOptions::new(); 200 let mut push_options = git2::PushOptions::new();
191 let mut remote_callbacks = git2::RemoteCallbacks::new(); 201 let mut remote_callbacks = git2::RemoteCallbacks::new();
192 remote_callbacks.credentials(auth.credentials(&git_config)); 202 remote_callbacks.credentials(auth.credentials(&git_config));
@@ -198,8 +208,9 @@ fn push(
198 .iter() 208 .iter()
199 .find(|r| r.contains(format!(":{name}").as_str())) 209 .find(|r| r.contains(format!(":{name}").as_str()))
200 { 210 {
201 if let Err(e) = update_remote_refs_pushed(git_repo, refspec, nostr_remote_url) 211 if let Err(e) =
202 .context("could not update remote_ref locally") 212 update_remote_refs_pushed(&git_repo.git_repo, refspec, nostr_remote_url)
213 .context("could not update remote_ref locally")
203 { 214 {
204 return Err(git2::Error::from_str(e.to_string().as_str())); 215 return Err(git2::Error::from_str(e.to_string().as_str()));
205 } 216 }
@@ -211,49 +222,125 @@ fn push(
211 push_options.remote_callbacks(remote_callbacks); 222 push_options.remote_callbacks(remote_callbacks);
212 git_server_remote.push(&refspecs, Some(&mut push_options))?; 223 git_server_remote.push(&refspecs, Some(&mut push_options))?;
213 git_server_remote.disconnect()?; 224 git_server_remote.disconnect()?;
225
226 // TODO check whether push was succesful before proceeding - geting outcome from
227 // callback isn't straightforward
228
229 let new_state = generate_updated_state(git_repo, repo_ref, &refspecs).await?;
230
231 // TODO enable interactive login
232 let (signer, user_ref) = login::launch(
233 git_repo,
234 &None,
235 &None,
236 &None,
237 &None,
238 Some(client),
239 false,
240 true,
241 )
242 .await?;
243 let new_repo_state = RepoState::build(repo_ref.identifier.clone(), new_state, &signer).await?;
244
245 send_events(
246 client,
247 git_repo.get_path()?,
248 vec![new_repo_state.event],
249 user_ref.relays.write(),
250 repo_ref.relays.clone(),
251 false,
252 true,
253 )
254 .await?;
255
214 println!(); 256 println!();
215 Ok(()) 257 Ok(())
216} 258}
217 259
260async fn generate_updated_state(
261 git_repo: &Repo,
262 repo_ref: &RepoRef,
263 refspecs: &Vec<String>,
264) -> Result<Vec<(String, String)>> {
265 let new_state = {
266 if let Ok(mut repo_state) = get_state_from_cache(git_repo.get_path()?, repo_ref).await {
267 for refspec in refspecs {
268 let (from, to) = refspec_to_from_to(refspec)?;
269 if to.is_empty() {
270 // delete
271 repo_state.state.retain(|(name, _)| !name.eq(to));
272 } else if repo_state.state.iter().any(|(name, _)| name.eq(from)) {
273 // update
274 repo_state.state = repo_state
275 .state
276 .iter()
277 .map(|(name, value)| {
278 (
279 name.clone(),
280 if name.eq(to) {
281 reference_to_ref_value(&git_repo.git_repo, to).unwrap()
282 } else {
283 value.to_string()
284 },
285 )
286 })
287 .collect();
288 } else {
289 // add
290 repo_state.state.push((
291 to.to_string(),
292 reference_to_ref_value(&git_repo.git_repo, to).unwrap(),
293 ));
294 }
295 }
296 repo_state.state
297 } else {
298 let mut state = vec![];
299 let git_server_url = repo_ref
300 .git_server
301 .first()
302 .context("no git server listed in nostr repository announcement")?;
303 let mut git_server_remote = git_repo.git_repo.remote_anonymous(git_server_url)?;
304 git_server_remote.connect(git2::Direction::Fetch)?;
305 for head in git_server_remote.list()? {
306 state.push((
307 head.name().to_string(),
308 if let Some(symbolic_ref) = head.symref_target() {
309 format!("ref: {}", symbolic_ref)
310 } else {
311 head.oid().to_string()
312 },
313 ));
314 }
315 git_server_remote.disconnect()?;
316 state
317 }
318 };
319 Ok(new_state)
320}
321
218fn update_remote_refs_pushed( 322fn update_remote_refs_pushed(
219 git_repo: &Repository, 323 git_repo: &Repository,
220 refspec: &str, 324 refspec: &str,
221 nostr_remote_url: &str, 325 nostr_remote_url: &str,
222) -> Result<()> { 326) -> Result<()> {
223 if !refspec.contains(':') { 327 let (from, _) = refspec_to_from_to(refspec)?;
224 bail!(
225 "refspec should contain a colon (:) but consists of: {}",
226 refspec
227 );
228 }
229 let parts = refspec.split(':').collect::<Vec<&str>>();
230 let from = parts.first().unwrap();
231 let to = parts.get(1).unwrap();
232 328
233 let nostr_remote = get_remote_by_url(git_repo, nostr_remote_url)?; 329 let target_ref_name = refspec_remote_ref_name(git_repo, refspec, nostr_remote_url)?;
234 330
235 let target_ref_name = format!(
236 "refs/remotes/{}/{}",
237 nostr_remote.name().context("remote should have a name")?,
238 to.replace("refs/heads/", ""), // TODO only replace if it begins with this
239 );
240 if from.is_empty() { 331 if from.is_empty() {
241 if let Ok(mut remote_ref) = git_repo.find_reference(&target_ref_name) { 332 if let Ok(mut remote_ref) = git_repo.find_reference(&target_ref_name) {
242 remote_ref.delete()?; 333 remote_ref.delete()?;
243 } 334 }
244 } else { 335 } else {
245 let local_ref = git_repo 336 let commit = reference_to_commit(git_repo, from)
246 .find_reference(from) 337 .context(format!("cannot get commit of reference {from}"))?;
247 .context(format!("from ref in refspec should exist: {from}"))?;
248 let commit = local_ref
249 .peel_to_commit()
250 .context(format!("from ref in refspec should peel to commit: {from}"))?;
251 if let Ok(mut remote_ref) = git_repo.find_reference(&target_ref_name) { 338 if let Ok(mut remote_ref) = git_repo.find_reference(&target_ref_name) {
252 remote_ref.set_target(commit.id(), "updated by nostr remote helper")?; 339 remote_ref.set_target(commit, "updated by nostr remote helper")?;
253 } else { 340 } else {
254 git_repo.reference( 341 git_repo.reference(
255 &target_ref_name, 342 &target_ref_name,
256 commit.id(), 343 commit,
257 false, 344 false,
258 "created by nostr remote helper", 345 "created by nostr remote helper",
259 )?; 346 )?;
@@ -262,9 +349,61 @@ fn update_remote_refs_pushed(
262 Ok(()) 349 Ok(())
263} 350}
264 351
265fn get_remote_by_url<'a>(git_repo: &'a Repository, url: &'a str) -> Result<Remote<'a>> { 352fn refspec_to_from_to(refspec: &str) -> Result<(&str, &str)> {
353 if !refspec.contains(':') {
354 bail!(
355 "refspec should contain a colon (:) but consists of: {}",
356 refspec
357 );
358 }
359 let parts = refspec.split(':').collect::<Vec<&str>>();
360 Ok((parts.first().unwrap(), parts.get(1).unwrap()))
361}
362
363fn refspec_remote_ref_name(
364 git_repo: &Repository,
365 refspec: &str,
366 nostr_remote_url: &str,
367) -> Result<String> {
368 let (_, to) = refspec_to_from_to(refspec)?;
369 let nostr_remote = git_repo
370 .find_remote(&get_remote_name_by_url(git_repo, nostr_remote_url)?)
371 .context("we should have just located this remote")?;
372 Ok(format!(
373 "refs/remotes/{}/{}",
374 nostr_remote.name().context("remote should have a name")?,
375 to.replace("refs/heads/", ""), // TODO only replace if it begins with this
376 ))
377}
378
379fn reference_to_commit(git_repo: &Repository, reference: &str) -> Result<Oid> {
380 Ok(git_repo
381 .find_reference(reference)
382 .context(format!("cannot find reference: {reference}"))?
383 .peel_to_commit()
384 .context(format!("cannot get commit from reference: {reference}"))?
385 .id())
386}
387
388// this maybe a commit id or a ref: pointer
389fn reference_to_ref_value(git_repo: &Repository, reference: &str) -> Result<String> {
390 let reference_obj = git_repo
391 .find_reference(reference)
392 .context(format!("cannot find reference: {reference}"))?;
393 if let Some(symref) = reference_obj.symbolic_target() {
394 Ok(symref.to_string())
395 } else {
396 Ok(reference_obj
397 .peel_to_commit()
398 .context(format!("cannot get commit from reference: {reference}"))?
399 .id()
400 .to_string())
401 }
402}
403
404fn get_remote_name_by_url(git_repo: &Repository, url: &str) -> Result<String> {
266 let remotes = git_repo.remotes()?; 405 let remotes = git_repo.remotes()?;
267 let remote_name = remotes 406 Ok(remotes
268 .iter() 407 .iter()
269 .find(|r| { 408 .find(|r| {
270 if let Some(name) = r { 409 if let Some(name) = r {
@@ -278,10 +417,8 @@ fn get_remote_by_url<'a>(git_repo: &'a Repository, url: &'a str) -> Result<Remot
278 } 417 }
279 }) 418 })
280 .context("could not find remote with matching url")? 419 .context("could not find remote with matching url")?
281 .context("remote with matching url must be named")?; 420 .context("remote with matching url must be named")?
282 git_repo 421 .to_string())
283 .find_remote(remote_name)
284 .context("we should have just located this remote")
285} 422}
286 423
287fn get_refstrs_from_fetch_batch(stdin: &Stdin, initial_refstr: &str) -> Result<Vec<String>> { 424fn get_refstrs_from_fetch_batch(stdin: &Stdin, initial_refstr: &str) -> Result<Vec<String>> {
@@ -319,3 +456,25 @@ fn get_refspecs_from_push_batch(stdin: &Stdin, initial_refspec: &str) -> Result<
319 } 456 }
320 Ok(refspecs) 457 Ok(refspecs)
321} 458}
459
460impl RepoState {
461 pub async fn build(
462 identifier: String,
463 state: Vec<(String, String)>,
464 signer: &NostrSigner,
465 ) -> Result<RepoState> {
466 let mut tags = vec![Tag::identifier(identifier.clone())];
467 for (name, value) in &state {
468 tags.push(Tag::custom(
469 nostr_sdk::TagKind::Custom(name.into()),
470 vec![value.clone()],
471 ));
472 }
473 let event = sign_event(EventBuilder::new(STATE_KIND, "", tags), signer).await?;
474 Ok(RepoState {
475 identifier,
476 state,
477 event,
478 })
479 }
480}