From ef552c12a6604c4b834f80b68c342d7f9668aa4b Mon Sep 17 00:00:00 2001 From: DanConwayDev Date: Wed, 27 Nov 2024 10:13:19 +0000 Subject: feat: remove `pull` `push` and `fetch` ngit cmds to simplify the api and encourage use of the git remote helper --- src/bin/ngit/cli.rs | 6 -- src/bin/ngit/main.rs | 3 - src/bin/ngit/sub_commands/fetch.rs | 37 ------- src/bin/ngit/sub_commands/mod.rs | 3 - src/bin/ngit/sub_commands/pull.rs | 203 ---------------------------------- src/bin/ngit/sub_commands/push.rs | 215 ------------------------------------- 6 files changed, 467 deletions(-) delete mode 100644 src/bin/ngit/sub_commands/fetch.rs delete mode 100644 src/bin/ngit/sub_commands/pull.rs delete mode 100644 src/bin/ngit/sub_commands/push.rs (limited to 'src/bin/ngit') diff --git a/src/bin/ngit/cli.rs b/src/bin/ngit/cli.rs index 9caf4d7..3a5b4e7 100644 --- a/src/bin/ngit/cli.rs +++ b/src/bin/ngit/cli.rs @@ -53,18 +53,12 @@ pub fn extract_signer_cli_arguments(args: &Cli) -> Result> { #[derive(Subcommand)] pub enum Commands { - /// update cache with latest updates from nostr - Fetch(sub_commands::fetch::SubCommandArgs), /// signal you are this repo's maintainer accepting proposals via nostr Init(sub_commands::init::SubCommandArgs), /// issue commits as a proposal Send(sub_commands::send::SubCommandArgs), /// list proposals; checkout, apply or download selected List, - /// send proposal revision - Push(sub_commands::push::SubCommandArgs), - /// fetch and apply new proposal commits / revisions linked to branch - Pull, /// login, logout or export keys Account(AccountSubCommandArgs), } diff --git a/src/bin/ngit/main.rs b/src/bin/ngit/main.rs index a49267b..a6e3e11 100644 --- a/src/bin/ngit/main.rs +++ b/src/bin/ngit/main.rs @@ -20,11 +20,8 @@ async fn main() -> Result<()> { AccountCommands::Logout => sub_commands::logout::launch().await, AccountCommands::ExportKeys => sub_commands::export_keys::launch().await, }, - Commands::Fetch(args) => sub_commands::fetch::launch(&cli, args).await, Commands::Init(args) => sub_commands::init::launch(&cli, args).await, Commands::List => sub_commands::list::launch().await, - Commands::Pull => sub_commands::pull::launch().await, - Commands::Push(args) => sub_commands::push::launch(&cli, args).await, Commands::Send(args) => sub_commands::send::launch(&cli, args, false).await, } } diff --git a/src/bin/ngit/sub_commands/fetch.rs b/src/bin/ngit/sub_commands/fetch.rs deleted file mode 100644 index dc548fd..0000000 --- a/src/bin/ngit/sub_commands/fetch.rs +++ /dev/null @@ -1,37 +0,0 @@ -use std::collections::HashSet; - -use anyhow::{Context, Result}; -use clap; -use nostr::nips::nip01::Coordinate; - -use crate::{ - cli::Cli, - client::{fetching_with_report, Client, Connect}, - git::{Repo, RepoActions}, - repo_ref::get_repo_coordinates, -}; - -#[derive(clap::Args)] -pub struct SubCommandArgs { - /// address pointer to repo announcement - #[arg(long, action)] - repo: Vec, -} - -pub async fn launch(args: &Cli, command_args: &SubCommandArgs) -> Result<()> { - let _ = args; - let git_repo = Repo::discover().context("failed to find a git repository")?; - let client = Client::default(); - let repo_coordinates = if command_args.repo.is_empty() { - get_repo_coordinates(&git_repo, &client).await? - } else { - let mut repo_coordinates = HashSet::new(); - for repo in &command_args.repo { - repo_coordinates.insert(Coordinate::parse(repo.clone())?); - } - repo_coordinates - }; - fetching_with_report(git_repo.get_path()?, &client, &repo_coordinates).await?; - client.disconnect().await?; - Ok(()) -} diff --git a/src/bin/ngit/sub_commands/mod.rs b/src/bin/ngit/sub_commands/mod.rs index b6b1e3b..b59b88f 100644 --- a/src/bin/ngit/sub_commands/mod.rs +++ b/src/bin/ngit/sub_commands/mod.rs @@ -1,9 +1,6 @@ pub mod export_keys; -pub mod fetch; pub mod init; pub mod list; pub mod login; pub mod logout; -pub mod pull; -pub mod push; pub mod send; diff --git a/src/bin/ngit/sub_commands/pull.rs b/src/bin/ngit/sub_commands/pull.rs deleted file mode 100644 index f459c8e..0000000 --- a/src/bin/ngit/sub_commands/pull.rs +++ /dev/null @@ -1,203 +0,0 @@ -use anyhow::{bail, Context, Result}; -use ngit::git_events::is_event_proposal_root_for_branch; -use nostr_sdk::PublicKey; - -use crate::{ - client::{ - fetching_with_report, get_all_proposal_patch_events_from_cache, - get_proposals_and_revisions_from_cache, get_repo_ref_from_cache, Client, Connect, - }, - git::{str_to_sha1, Repo, RepoActions}, - git_events::{get_commit_id_from_patch, get_most_recent_patch_with_ancestors, tag_value}, - repo_ref::get_repo_coordinates, -}; - -#[allow(clippy::too_many_lines)] -pub async fn launch() -> Result<()> { - let git_repo = Repo::discover().context("failed to find a git repository")?; - let git_repo_path = git_repo.get_path()?; - - let (main_or_master_branch_name, _) = git_repo - .get_main_or_master_branch() - .context("no main or master branch")?; - - let branch_name = git_repo - .get_checked_out_branch_name() - .context("failed to get checked out branch name")?; - - if branch_name == main_or_master_branch_name { - bail!("checkout a branch associated with a proposal first") - } - let client = Client::default(); - - let repo_coordinates = get_repo_coordinates(&git_repo, &client).await?; - fetching_with_report(git_repo_path, &client, &repo_coordinates).await?; - - let repo_ref = get_repo_ref_from_cache(Some(git_repo_path), &repo_coordinates).await?; - - let logged_in_public_key = - if let Ok(Some(npub)) = git_repo.get_git_config_item("nostr.npub", None) { - PublicKey::parse(npub).ok() - } else { - None - }; - - let proposal_root_event = - get_proposals_and_revisions_from_cache(git_repo_path, repo_ref.coordinates()) - .await? - .iter() - .find(|e| { - is_event_proposal_root_for_branch(e, &branch_name, &logged_in_public_key) - .unwrap_or(false) - }) - .context("failed to find proposal that matches the current branch name")? - .clone(); - - let commit_events = - get_all_proposal_patch_events_from_cache(git_repo_path, &repo_ref, &proposal_root_event.id) - .await?; - - let most_recent_proposal_patch_chain = - get_most_recent_patch_with_ancestors(commit_events.clone()) - .context("failed to get most recent patch for proposal")?; - - let local_branch_tip = git_repo.get_tip_of_branch(&branch_name)?; - - let (main_branch_name, master_tip) = git_repo.get_main_or_master_branch()?; - - let (local_ahead_of_main, local_beind_main) = - git_repo.get_commits_ahead_behind(&master_tip, &local_branch_tip)?; - - let proposal_base_commit = str_to_sha1(&tag_value( - most_recent_proposal_patch_chain - .last() - .context("there should be at least one patch as we have already checked for this")?, - "parent-commit", - )?) - .context("failed to get valid parent commit id from patch")?; - - let (_, proposal_behind_main) = - git_repo.get_commits_ahead_behind(&master_tip, &proposal_base_commit)?; - - let proposal_tip = - str_to_sha1( - &get_commit_id_from_patch(most_recent_proposal_patch_chain.first().context( - "there should be at least one patch as we have already checked for this", - )?) - .context("failed to get valid commit_id from patch")?, - ) - .context("failed to get valid commit_id from patch")?; - - // if uptodate - if proposal_tip.eq(&local_branch_tip) { - println!("branch already up-to-date"); - } - // if new appendments - else if most_recent_proposal_patch_chain.iter().any(|patch| { - get_commit_id_from_patch(patch) - .unwrap_or_default() - .eq(&local_branch_tip.to_string()) - }) { - check_clean(&git_repo)?; - let applied = git_repo - .apply_patch_chain(&branch_name, most_recent_proposal_patch_chain) - .context("failed to apply patch chain")?; - println!("applied {} new commits", applied.len(),); - } - // if parent commit doesnt exist - else if !git_repo.does_commit_exist(&proposal_base_commit.to_string())? { - println!( - "a new version of the proposal has a prant commit that doesnt exist in your local repository." - ); - println!("your '{main_branch_name}' branch may not be up-to-date."); - println!("manually run `git pull` on '{main_branch_name}' and try again"); - } - // if new revision and no local changes (tip of local in proposal history) - else if commit_events.iter().any(|patch| { - get_commit_id_from_patch(patch) - .unwrap_or_default() - .eq(&local_branch_tip.to_string()) - }) { - check_clean(&git_repo)?; - - git_repo.create_branch_at_commit(&branch_name, &proposal_base_commit.to_string())?; - let applied = git_repo - .apply_patch_chain(&branch_name, most_recent_proposal_patch_chain) - .context("failed to apply patch chain")?; - - println!( - "pulled new version of proposal ({} ahead {} behind '{main_branch_name}'), replacing old version ({} ahead {} behind '{main_branch_name}')", - applied.len(), - proposal_behind_main.len(), - local_ahead_of_main.len(), - local_beind_main.len(), - ); - } - // if tip of proposal in branch in history (local appendments made to up-to-date - // proposal) - else if git_repo.ancestor_of(&local_branch_tip, &proposal_tip)? { - let (local_ahead_of_proposal, _) = git_repo - .get_commits_ahead_behind(&proposal_tip, &local_branch_tip) - .context("failed to get commits ahead behind for propsal_top and local_branch_tip")?; - println!( - "local proposal branch exists with {} unpublished commits on top of the most up-to-date version of the proposal", - local_ahead_of_proposal.len() - ); - } else { - println!("you have an amended/rebase version the proposal that is unpublished"); - // user probably has a unpublished amended or rebase version of the latest - // proposal version - // if tip of proposal commits exist (were once part of branch but have been - // amended and git clean up job hasn't removed them) - if git_repo.does_commit_exist(&proposal_tip.to_string())? { - println!( - "you have previously applied the latest version of the proposal ({} ahead {} behind '{main_branch_name}') but your local proposal branch has amended or rebased it ({} ahead {} behind '{main_branch_name}')", - most_recent_proposal_patch_chain.len(), - proposal_behind_main.len(), - local_ahead_of_main.len(), - local_beind_main.len(), - ); - } - // user probably has a unpublished amended or rebase version of an older - // proposal version - else { - println!( - "your local proposal branch ({} ahead {} behind '{main_branch_name}') has conflicting changes with the latest published proposal ({} ahead {} behind '{main_branch_name}')", - local_ahead_of_main.len(), - local_beind_main.len(), - most_recent_proposal_patch_chain.len(), - proposal_behind_main.len(), - ); - - println!( - "its likely that you have rebased / amended an old proposal version because git has no record of the latest proposal commit." - ); - println!( - "it is possible that you have been working off the latest version and git has delete this commit as part of a clean up" - ); - } - println!("to view the latest proposal but retain your changes:"); - println!(" 1) create a new branch off the tip commit of this one to store your changes"); - println!(" 2) run `ngit list` and checkout the latest published version of this proposal"); - - println!("if you are confident in your changes consider running `ngit push --force`"); - - // TODO: this copy could be refined further based on this: - // - amended commits in the proposal - // - if local_base eq proposal base - // - amended an older version of proposal - // - if local_base is behind proposal_base - // - rebased the proposal - // - if local_base is ahead of proposal_base - } - Ok(()) -} - -fn check_clean(git_repo: &Repo) -> Result<()> { - if git_repo.has_outstanding_changes()? { - bail!( - "failed to pull proposal branch when repository is not clean. discard or stash (un)staged changes and try again." - ); - } - Ok(()) -} diff --git a/src/bin/ngit/sub_commands/push.rs b/src/bin/ngit/sub_commands/push.rs deleted file mode 100644 index 89b2364..0000000 --- a/src/bin/ngit/sub_commands/push.rs +++ /dev/null @@ -1,215 +0,0 @@ -use anyhow::{bail, Context, Result}; -use ngit::{ - client::send_events, - git_events::{is_event_proposal_root_for_branch, tag_value}, -}; -use nostr_sdk::PublicKey; - -use crate::{ - cli::{extract_signer_cli_arguments, Cli}, - client::{ - fetching_with_report, get_all_proposal_patch_events_from_cache, - get_proposals_and_revisions_from_cache, get_repo_ref_from_cache, Client, Connect, - }, - git::{identify_ahead_behind, str_to_sha1, Repo, RepoActions}, - git_events::{ - generate_patch_event, get_commit_id_from_patch, get_most_recent_patch_with_ancestors, - }, - login, - repo_ref::get_repo_coordinates, - sub_commands, -}; - -#[derive(Debug, clap::Args)] -pub struct SubCommandArgs { - #[arg(long, action)] - /// send proposal revision from checked out proposal branch - force: bool, -} - -#[allow(clippy::too_many_lines)] -pub async fn launch(cli_args: &Cli, args: &SubCommandArgs) -> Result<()> { - let git_repo = Repo::discover().context("failed to find a git repository")?; - let git_repo_path = git_repo.get_path()?; - - let (main_or_master_branch_name, _) = git_repo - .get_main_or_master_branch() - .context("no main or master branch")?; - - let root_commit = git_repo - .get_root_commit() - .context("failed to get root commit of the repository")?; - - let branch_name = git_repo - .get_checked_out_branch_name() - .context("failed to get checked out branch name")?; - - if branch_name == main_or_master_branch_name { - bail!("checkout a branch associated with a proposal first") - } - let mut client = Client::default(); - - let repo_coordinates = get_repo_coordinates(&git_repo, &client).await?; - - fetching_with_report(git_repo_path, &client, &repo_coordinates).await?; - - let repo_ref = get_repo_ref_from_cache(Some(git_repo_path), &repo_coordinates).await?; - - let logged_in_public_key = - if let Ok(Some(npub)) = git_repo.get_git_config_item("nostr.npub", None) { - PublicKey::parse(npub).ok() - } else { - None - }; - - let proposal_root_event = - get_proposals_and_revisions_from_cache(git_repo_path, repo_ref.coordinates()) - .await? - .iter() - .find(|e| { - is_event_proposal_root_for_branch(e, &branch_name, &logged_in_public_key) - .unwrap_or(false) - }) - .context("failed to find proposal that matches the current branch name")? - .clone(); - - let commit_events = - get_all_proposal_patch_events_from_cache(git_repo_path, &repo_ref, &proposal_root_event.id) - .await?; - - let most_recent_proposal_patch_chain = get_most_recent_patch_with_ancestors(commit_events) - .context("failed to get most recent patch for proposal")?; - - let branch_tip = git_repo.get_tip_of_branch(&branch_name)?; - - let most_recent_patch_commit_id = str_to_sha1( - &get_commit_id_from_patch( - most_recent_proposal_patch_chain - .first() - .context("no patches found")?, - ) - .context("latest patch event doesnt have a commit tag")?, - ) - .context("latest patch event commit tag isn't a valid SHA1 hash")?; - - let proposal_base_commit_id = str_to_sha1( - &tag_value( - most_recent_proposal_patch_chain - .last() - .context("no patches found")?, - "parent-commit", - ) - .context("patch is incorrectly formatted")?, - ) - .context("latest patch event parent-commit tag isn't a valid SHA1 hash")?; - - if most_recent_patch_commit_id.eq(&branch_tip) { - bail!("proposal already up-to-date with local branch"); - } - - if args.force { - println!("preparing to force push proposal revision..."); - sub_commands::send::launch( - cli_args, - &sub_commands::send::SubCommandArgs { - // if not ahead of master prompt, otherwise assume proposal revision is all commits - // ahead - since_or_range: if let Ok((_, _, ahead, _)) = - identify_ahead_behind(&git_repo, &None, &None) - { - if ahead.is_empty() { - String::new() - } else { - format!("HEAD~{}", ahead.len()) - } - } else { - String::new() - }, - in_reply_to: vec![proposal_root_event.id.to_string()], - title: None, - description: None, - no_cover_letter: true, - }, - true, - ) - .await?; - println!("force pushed proposal revision"); - return Ok(()); - } - - if most_recent_proposal_patch_chain.iter().any(|e| { - let c = tag_value(e, "parent-commit").unwrap_or_default(); - c.eq(&branch_tip.to_string()) - }) { - bail!("proposal is ahead of local branch"); - } - - let Ok((ahead, behind)) = git_repo - .get_commits_ahead_behind(&most_recent_patch_commit_id, &branch_tip) - .context("the latest patch in proposal doesnt share an ancestor with your branch.") - else { - if git_repo.ancestor_of(&proposal_base_commit_id, &branch_tip)? { - bail!("local unpublished proposal ammendments. consider force pushing."); - } - bail!("local unpublished proposal has been rebased. consider force pushing"); - }; - - if !behind.is_empty() { - bail!( - "your local proposal branch is {} behind patches on nostr. consider rebasing or force pushing", - behind.len() - ) - } - - println!( - "{} commits ahead. preparing to create creating patch events.", - ahead.len() - ); - - let (signer, user_ref, _) = login::login_or_signup( - &Some(&git_repo), - &extract_signer_cli_arguments(cli_args).unwrap_or(None), - &cli_args.password, - Some(&client), - ) - .await?; - - let mut patch_events: Vec = vec![]; - for commit in &ahead { - patch_events.push( - generate_patch_event( - &git_repo, - &root_commit, - commit, - Some(proposal_root_event.id), - &signer, - &repo_ref, - patch_events.last().map(|e| e.id), - None, - None, - &None, - &[], - ) - .await - .context("failed to make patch event from commit")?, - ); - } - println!("pushing {} commits", ahead.len()); - - client.set_signer(signer).await; - - send_events( - &client, - Some(git_repo_path), - patch_events, - user_ref.relays.write(), - repo_ref.relays.clone(), - !cli_args.disable_cli_spinners, - false, - ) - .await?; - - println!("pushed {} commits", ahead.len()); - - Ok(()) -} -- cgit v1.2.3