upleb.uk

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

summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/repo_ref.rs190
1 files changed, 104 insertions, 86 deletions
diff --git a/src/repo_ref.rs b/src/repo_ref.rs
index ca196d9..fa8ff4e 100644
--- a/src/repo_ref.rs
+++ b/src/repo_ref.rs
@@ -227,8 +227,27 @@ pub async fn try_and_get_repo_coordinates(
227 #[cfg(not(test))] client: &Client, 227 #[cfg(not(test))] client: &Client,
228 prompt_user: bool, 228 prompt_user: bool,
229) -> Result<HashSet<Coordinate>> { 229) -> Result<HashSet<Coordinate>> {
230 let mut repo_coordinates = HashSet::new(); 230 let mut repo_coordinates = get_repo_coordinates_from_git_config(git_repo)?;
231
232 // TODO: when nostr remotes functionality is added, iterate on each remote and
233 // extract coordinates
234
235 if repo_coordinates.is_empty() {
236 repo_coordinates = get_repo_coordinates_from_maintainers_yaml(git_repo, client).await?;
237 }
231 238
239 if repo_coordinates.is_empty() {
240 if prompt_user {
241 repo_coordinates = get_repo_coordinates_from_user_prompt(git_repo)?;
242 } else {
243 bail!("couldn't find repo coordinates in git config nostr.repo or in maintainers.yaml");
244 }
245 }
246 Ok(repo_coordinates)
247}
248
249fn get_repo_coordinates_from_git_config(git_repo: &Repo) -> Result<HashSet<Coordinate>> {
250 let mut repo_coordinates = HashSet::new();
232 if let Some(repo_override) = git_repo.get_git_config_item("nostr.repo", Some(false))? { 251 if let Some(repo_override) = git_repo.get_git_config_item("nostr.repo", Some(false))? {
233 for s in repo_override.split(',') { 252 for s in repo_override.split(',') {
234 if let Ok(c) = Coordinate::parse(s) { 253 if let Ok(c) = Coordinate::parse(s) {
@@ -236,104 +255,103 @@ pub async fn try_and_get_repo_coordinates(
236 } 255 }
237 } 256 }
238 } 257 }
258 Ok(repo_coordinates)
259}
239 260
240 // TODO: when nostr remotes functionality is added, iterate on each remote and 261async fn get_repo_coordinates_from_maintainers_yaml(
241 // extract coordinates 262 git_repo: &Repo,
242 263 #[cfg(test)] client: &crate::client::MockConnect,
243 if repo_coordinates.is_empty() { 264 #[cfg(not(test))] client: &Client,
244 if let Ok(repo_config) = get_repo_config_from_yaml(git_repo) { 265) -> Result<HashSet<Coordinate>> {
245 let maintainers = { 266 let mut repo_coordinates = HashSet::new();
246 let mut maintainers = HashSet::new(); 267 if let Ok(repo_config) = get_repo_config_from_yaml(git_repo) {
247 for m in &repo_config.maintainers { 268 let maintainers = {
248 if let Ok(maintainer) = PublicKey::parse(m) { 269 let mut maintainers = HashSet::new();
249 maintainers.insert(maintainer); 270 for m in &repo_config.maintainers {
250 } 271 if let Ok(maintainer) = PublicKey::parse(m) {
251 } 272 maintainers.insert(maintainer);
252 maintainers
253 };
254 if let Some(identifier) = repo_config.identifier {
255 for public_key in maintainers {
256 repo_coordinates.insert(Coordinate {
257 kind: Kind::GitRepoAnnouncement,
258 public_key,
259 identifier: identifier.clone(),
260 relays: vec![],
261 });
262 } 273 }
263 } else { 274 }
264 // if repo_config.identifier.is_empty() { 275 maintainers
265 // this will only apply for a few repositories created before ngit v1.3 276 };
266 // that haven't updated their maintainers.yaml 277 if let Some(identifier) = repo_config.identifier {
267 if let Ok(Some(current_user_npub)) = 278 for public_key in maintainers {
268 git_repo.get_git_config_item("nostr.npub", None) 279 repo_coordinates.insert(Coordinate {
269 { 280 kind: Kind::GitRepoAnnouncement,
270 if let Ok(current_user) = PublicKey::parse(current_user_npub) { 281 public_key,
271 for m in &repo_config.maintainers { 282 identifier: identifier.clone(),
272 if let Ok(maintainer) = PublicKey::parse(m) { 283 relays: vec![],
273 if current_user.eq(&maintainer) { 284 });
274 println!( 285 }
275 "please run `ngit init` to add the repo identifier to maintainers.yaml" 286 } else {
276 ); 287 // if repo_config.identifier.is_empty() {
277 } 288 // this will only apply for a few repositories created before ngit v1.3
289 // that haven't updated their maintainers.yaml
290 if let Ok(Some(current_user_npub)) = git_repo.get_git_config_item("nostr.npub", None) {
291 if let Ok(current_user) = PublicKey::parse(current_user_npub) {
292 for m in &repo_config.maintainers {
293 if let Ok(maintainer) = PublicKey::parse(m) {
294 if current_user.eq(&maintainer) {
295 println!(
296 "please run `ngit init` to add the repo identifier to maintainers.yaml"
297 );
278 } 298 }
279 } 299 }
280 } 300 }
281 } 301 }
282 // look find all repo refs with root_commit. for identifier 302 }
283 let filter = nostr::Filter::default() 303 // look find all repo refs with root_commit. for identifier
284 .kind(nostr::Kind::GitRepoAnnouncement) 304 let filter = nostr::Filter::default()
285 .reference(git_repo.get_root_commit()?.to_string()) 305 .kind(nostr::Kind::GitRepoAnnouncement)
286 .authors(maintainers.clone()); 306 .reference(git_repo.get_root_commit()?.to_string())
287 let mut events = 307 .authors(maintainers.clone());
288 get_events_from_cache(git_repo.get_path()?, vec![filter.clone()]).await?; 308 let mut events =
289 if events.is_empty() { 309 get_events_from_cache(git_repo.get_path()?, vec![filter.clone()]).await?;
290 events = 310 if events.is_empty() {
291 get_event_from_global_cache(git_repo.get_path()?, vec![filter.clone()]) 311 events =
292 .await?; 312 get_event_from_global_cache(git_repo.get_path()?, vec![filter.clone()]).await?;
293 } 313 }
294 if events.is_empty() { 314 if events.is_empty() {
295 println!( 315 println!(
296 "finding repository events for this repository for npubs in maintainers.yaml" 316 "finding repository events for this repository for npubs in maintainers.yaml"
297 ); 317 );
298 events = client 318 events = client
299 .get_events(client.get_fallback_relays().clone(), vec![filter.clone()]) 319 .get_events(client.get_fallback_relays().clone(), vec![filter.clone()])
300 .await?; 320 .await?;
301 } 321 }
302 if let Some(e) = events.first() { 322 if let Some(e) = events.first() {
303 if let Some(identifier) = e.identifier() { 323 if let Some(identifier) = e.identifier() {
304 for m in &repo_config.maintainers { 324 for m in &repo_config.maintainers {
305 if let Ok(maintainer) = PublicKey::parse(m) { 325 if let Ok(maintainer) = PublicKey::parse(m) {
306 repo_coordinates.insert(Coordinate { 326 repo_coordinates.insert(Coordinate {
307 kind: Kind::GitRepoAnnouncement, 327 kind: Kind::GitRepoAnnouncement,
308 public_key: maintainer, 328 public_key: maintainer,
309 identifier: identifier.to_string(), 329 identifier: identifier.to_string(),
310 relays: vec![], 330 relays: vec![],
311 }); 331 });
312 }
313 } 332 }
314 } 333 }
315 } else {
316 let c = ask_for_naddr()?;
317 git_repo.save_git_config_item("nostr.repo", &c.to_bech32()?, false)?;
318 repo_coordinates.insert(c);
319 } 334 }
335 } else {
336 let c = ask_for_naddr()?;
337 git_repo.save_git_config_item("nostr.repo", &c.to_bech32()?, false)?;
338 repo_coordinates.insert(c);
320 } 339 }
321 } 340 }
322 } 341 }
342 Ok(repo_coordinates)
343}
323 344
324 if repo_coordinates.is_empty() { 345fn get_repo_coordinates_from_user_prompt(git_repo: &Repo) -> Result<HashSet<Coordinate>> {
325 if !prompt_user { 346 let mut repo_coordinates = HashSet::new();
326 bail!("couldn't find repo coordinates in git config nostr.repo or in maintainers.yaml"); 347 // TODO: present list of events filter by root_commit
327 } 348 // TODO: fallback to search based on identifier
328 // TODO: present list of events filter by root_commit 349 let c = ask_for_naddr()?;
329 // TODO: fallback to search based on identifier 350 // PROBLEM: we are saving this before checking whether it actually exists, which
330 let c = ask_for_naddr()?; 351 // means next time the user won't be prompted and may not know how to
331 // PROBLEM: we are saving this before checking whether it actually exists, which 352 // change the selected repo
332 // means next time the user won't be prompted and may not know how to 353 git_repo.save_git_config_item("nostr.repo", &c.to_bech32()?, false)?;
333 // change the selected repo 354 repo_coordinates.insert(c);
334 git_repo.save_git_config_item("nostr.repo", &c.to_bech32()?, false)?;
335 repo_coordinates.insert(c);
336 }
337 Ok(repo_coordinates) 355 Ok(repo_coordinates)
338} 356}
339 357