upleb.uk

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

summaryrefslogtreecommitdiff
path: root/src/bin/ngit/sub_commands/init.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/bin/ngit/sub_commands/init.rs')
-rw-r--r--src/bin/ngit/sub_commands/init.rs90
1 files changed, 88 insertions, 2 deletions
diff --git a/src/bin/ngit/sub_commands/init.rs b/src/bin/ngit/sub_commands/init.rs
index 32af619..ffee9bd 100644
--- a/src/bin/ngit/sub_commands/init.rs
+++ b/src/bin/ngit/sub_commands/init.rs
@@ -1,7 +1,7 @@
1use std::collections::HashMap; 1use std::{collections::HashMap, str::FromStr};
2 2
3use anyhow::{Context, Result}; 3use anyhow::{Context, Result};
4use ngit::cli_interactor::PromptConfirmParms; 4use ngit::{cli_interactor::PromptConfirmParms, git::nostr_url::NostrUrlDecoded};
5use nostr::{nips::nip01::Coordinate, FromBech32, PublicKey, ToBech32}; 5use nostr::{nips::nip01::Coordinate, FromBech32, PublicKey, ToBech32};
6use nostr_sdk::{Kind, RelayUrl}; 6use nostr_sdk::{Kind, RelayUrl};
7 7
@@ -413,6 +413,7 @@ pub async fn launch(cli_args: &Cli, args: &SubCommandArgs) -> Result<()> {
413 git_server, 413 git_server,
414 web, 414 web,
415 relays: relays.clone(), 415 relays: relays.clone(),
416 trusted_maintainer: user_ref.public_key,
416 maintainers: maintainers.clone(), 417 maintainers: maintainers.clone(),
417 events: HashMap::new(), 418 events: HashMap::new(),
418 }; 419 };
@@ -431,6 +432,7 @@ pub async fn launch(cli_args: &Cli, args: &SubCommandArgs) -> Result<()> {
431 ) 432 )
432 .await?; 433 .await?;
433 434
435 // TODO - does this git config item do more harm than good?
434 git_repo.save_git_config_item( 436 git_repo.save_git_config_item(
435 "nostr.repo", 437 "nostr.repo",
436 &Coordinate { 438 &Coordinate {
@@ -448,6 +450,11 @@ pub async fn launch(cli_args: &Cli, args: &SubCommandArgs) -> Result<()> {
448 .map(std::string::ToString::to_string) 450 .map(std::string::ToString::to_string)
449 .collect::<Vec<String>>(); 451 .collect::<Vec<String>>();
450 452
453 prompt_to_set_nostr_url_as_origin(&repo_ref, &git_repo)?;
454
455 // TODO: if no state event exists and there is currently a remote called
456 // "origin", automtically push rather than waiting for the next commit
457
451 // no longer create a new maintainers.yaml file - its too confusing for users 458 // no longer create a new maintainers.yaml file - its too confusing for users
452 // as it falls out of sync with data in nostr event . update if it already 459 // as it falls out of sync with data in nostr event . update if it already
453 // exists 460 // exists
@@ -481,3 +488,82 @@ pub async fn launch(cli_args: &Cli, args: &SubCommandArgs) -> Result<()> {
481 } 488 }
482 Ok(()) 489 Ok(())
483} 490}
491
492fn prompt_to_set_nostr_url_as_origin(repo_ref: &RepoRef, git_repo: &Repo) -> Result<()> {
493 println!(
494 "starting from your next commit, when you `git push` to a remote that uses your nostr url, it will store your repository state on nostr and update the state of the git server(s) you just listed."
495 );
496 println!(
497 "in addition, any remote branches beginning with `pr/` are open PRs from contributors. they can submit these by simply pushing a branch with this `pr/` prefix."
498 );
499
500 if let Ok(origin_remote) = git_repo.git_repo.find_remote("origin") {
501 if let Some(origin_url) = origin_remote.url() {
502 if let Ok(nostr_url) = NostrUrlDecoded::from_str(origin_url) {
503 if let Some(c) = &nostr_url.coordinates.iter().next() {
504 if c.identifier == repo_ref.identifier {
505 if nostr_url
506 .coordinates
507 .iter()
508 .next()
509 .context(
510 "a decoded nostr url will always have at least one coordinate",
511 )?
512 .public_key
513 == repo_ref.trusted_maintainer
514 {
515 return Ok(());
516 }
517 // origin is set to a different trusted maintainer
518 println!(
519 "warning: currently git remote 'origin' is set to a different trusted maintainer with the same identifier"
520 );
521 ask_to_set_origin_remote(repo_ref, git_repo)?;
522 } else {
523 // origin is linked to a different identifier
524 println!(
525 "warning: currently git remote 'origin' is set to a different repository identifier"
526 );
527 ask_to_set_origin_remote(repo_ref, git_repo)?;
528 }
529 }
530 } else {
531 // remote is non-nostr url
532 ask_to_set_origin_remote(repo_ref, git_repo)?;
533 }
534 } else {
535 // no origin remote
536 ask_to_create_new_origin_remote(repo_ref, git_repo)?;
537 }
538 }
539 println!("contributors can clone your repository by installing ngit and using this clone url:");
540 println!("{}", repo_ref.to_nostr_git_url());
541
542 Ok(())
543}
544
545fn ask_to_set_origin_remote(repo_ref: &RepoRef, git_repo: &Repo) -> Result<()> {
546 if Interactor::default().confirm(
547 PromptConfirmParms::default()
548 .with_default(true)
549 .with_prompt("set remote \"origin\" to the nostr url of your repository?"),
550 )? {
551 git_repo
552 .git_repo
553 .remote_set_url("origin", &repo_ref.to_nostr_git_url())?;
554 }
555 Ok(())
556}
557
558fn ask_to_create_new_origin_remote(repo_ref: &RepoRef, git_repo: &Repo) -> Result<()> {
559 if Interactor::default().confirm(
560 PromptConfirmParms::default()
561 .with_default(true)
562 .with_prompt("set remote \"origin\" to the nostr url of your repository?"),
563 )? {
564 git_repo
565 .git_repo
566 .remote("origin", &repo_ref.to_nostr_git_url())?;
567 }
568 Ok(())
569}