diff options
| author | DanConwayDev <DanConwayDev@protonmail.com> | 2024-11-11 09:06:19 +0000 |
|---|---|---|
| committer | DanConwayDev <DanConwayDev@protonmail.com> | 2024-11-11 09:06:19 +0000 |
| commit | f08ee98ab7e19d4e42ffa85aa619f012441fbe47 (patch) | |
| tree | e4d2a15ebeb8a7549ce7e233f4690d71ac95c398 /src/bin/ngit/sub_commands/pull.rs | |
| parent | 4331b73fbda4831f09a783732d6710012a4dcf20 (diff) | |
Revert "refactor: remove ngit `pull` `push` `fetch`"
This reverts commit 43b5e9b38bf5dcfbac85637a2d3efc69ddfe77ac.
Diffstat (limited to 'src/bin/ngit/sub_commands/pull.rs')
| -rw-r--r-- | src/bin/ngit/sub_commands/pull.rs | 203 |
1 files changed, 203 insertions, 0 deletions
diff --git a/src/bin/ngit/sub_commands/pull.rs b/src/bin/ngit/sub_commands/pull.rs new file mode 100644 index 0000000..d79b7b1 --- /dev/null +++ b/src/bin/ngit/sub_commands/pull.rs | |||
| @@ -0,0 +1,203 @@ | |||
| 1 | use anyhow::{bail, Context, Result}; | ||
| 2 | use ngit::git_events::is_event_proposal_root_for_branch; | ||
| 3 | use nostr_sdk::PublicKey; | ||
| 4 | |||
| 5 | use crate::{ | ||
| 6 | client::{ | ||
| 7 | fetching_with_report, get_all_proposal_patch_events_from_cache, | ||
| 8 | get_proposals_and_revisions_from_cache, get_repo_ref_from_cache, Client, Connect, | ||
| 9 | }, | ||
| 10 | git::{str_to_sha1, Repo, RepoActions}, | ||
| 11 | git_events::{get_commit_id_from_patch, get_most_recent_patch_with_ancestors, tag_value}, | ||
| 12 | repo_ref::get_repo_coordinates, | ||
| 13 | }; | ||
| 14 | |||
| 15 | #[allow(clippy::too_many_lines)] | ||
| 16 | pub async fn launch() -> Result<()> { | ||
| 17 | let git_repo = Repo::discover().context("cannot find a git repository")?; | ||
| 18 | let git_repo_path = git_repo.get_path()?; | ||
| 19 | |||
| 20 | let (main_or_master_branch_name, _) = git_repo | ||
| 21 | .get_main_or_master_branch() | ||
| 22 | .context("no main or master branch")?; | ||
| 23 | |||
| 24 | let branch_name = git_repo | ||
| 25 | .get_checked_out_branch_name() | ||
| 26 | .context("cannot get checked out branch name")?; | ||
| 27 | |||
| 28 | if branch_name == main_or_master_branch_name { | ||
| 29 | bail!("checkout a branch associated with a proposal first") | ||
| 30 | } | ||
| 31 | let client = Client::default(); | ||
| 32 | |||
| 33 | let repo_coordinates = get_repo_coordinates(&git_repo, &client).await?; | ||
| 34 | fetching_with_report(git_repo_path, &client, &repo_coordinates).await?; | ||
| 35 | |||
| 36 | let repo_ref = get_repo_ref_from_cache(git_repo_path, &repo_coordinates).await?; | ||
| 37 | |||
| 38 | let logged_in_public_key = | ||
| 39 | if let Ok(Some(npub)) = git_repo.get_git_config_item("nostr.npub", None) { | ||
| 40 | PublicKey::parse(npub).ok() | ||
| 41 | } else { | ||
| 42 | None | ||
| 43 | }; | ||
| 44 | |||
| 45 | let proposal_root_event = | ||
| 46 | get_proposals_and_revisions_from_cache(git_repo_path, repo_ref.coordinates()) | ||
| 47 | .await? | ||
| 48 | .iter() | ||
| 49 | .find(|e| { | ||
| 50 | is_event_proposal_root_for_branch(e, &branch_name, &logged_in_public_key) | ||
| 51 | .unwrap_or(false) | ||
| 52 | }) | ||
| 53 | .context("cannot find proposal that matches the current branch name")? | ||
| 54 | .clone(); | ||
| 55 | |||
| 56 | let commit_events = | ||
| 57 | get_all_proposal_patch_events_from_cache(git_repo_path, &repo_ref, &proposal_root_event.id) | ||
| 58 | .await?; | ||
| 59 | |||
| 60 | let most_recent_proposal_patch_chain = | ||
| 61 | get_most_recent_patch_with_ancestors(commit_events.clone()) | ||
| 62 | .context("cannot get most recent patch for proposal")?; | ||
| 63 | |||
| 64 | let local_branch_tip = git_repo.get_tip_of_branch(&branch_name)?; | ||
| 65 | |||
| 66 | let (main_branch_name, master_tip) = git_repo.get_main_or_master_branch()?; | ||
| 67 | |||
| 68 | let (local_ahead_of_main, local_beind_main) = | ||
| 69 | git_repo.get_commits_ahead_behind(&master_tip, &local_branch_tip)?; | ||
| 70 | |||
| 71 | let proposal_base_commit = str_to_sha1(&tag_value( | ||
| 72 | most_recent_proposal_patch_chain | ||
| 73 | .last() | ||
| 74 | .context("there should be at least one patch as we have already checked for this")?, | ||
| 75 | "parent-commit", | ||
| 76 | )?) | ||
| 77 | .context("cannot get valid parent commit id from patch")?; | ||
| 78 | |||
| 79 | let (_, proposal_behind_main) = | ||
| 80 | git_repo.get_commits_ahead_behind(&master_tip, &proposal_base_commit)?; | ||
| 81 | |||
| 82 | let proposal_tip = | ||
| 83 | str_to_sha1( | ||
| 84 | &get_commit_id_from_patch(most_recent_proposal_patch_chain.first().context( | ||
| 85 | "there should be at least one patch as we have already checked for this", | ||
| 86 | )?) | ||
| 87 | .context("cannot get valid commit_id from patch")?, | ||
| 88 | ) | ||
| 89 | .context("cannot get valid commit_id from patch")?; | ||
| 90 | |||
| 91 | // if uptodate | ||
| 92 | if proposal_tip.eq(&local_branch_tip) { | ||
| 93 | println!("branch already up-to-date"); | ||
| 94 | } | ||
| 95 | // if new appendments | ||
| 96 | else if most_recent_proposal_patch_chain.iter().any(|patch| { | ||
| 97 | get_commit_id_from_patch(patch) | ||
| 98 | .unwrap_or_default() | ||
| 99 | .eq(&local_branch_tip.to_string()) | ||
| 100 | }) { | ||
| 101 | check_clean(&git_repo)?; | ||
| 102 | let applied = git_repo | ||
| 103 | .apply_patch_chain(&branch_name, most_recent_proposal_patch_chain) | ||
| 104 | .context("cannot apply patch chain")?; | ||
| 105 | println!("applied {} new commits", applied.len(),); | ||
| 106 | } | ||
| 107 | // if parent commit doesnt exist | ||
| 108 | else if !git_repo.does_commit_exist(&proposal_base_commit.to_string())? { | ||
| 109 | println!( | ||
| 110 | "a new version of the proposal has a prant commit that doesnt exist in your local repository." | ||
| 111 | ); | ||
| 112 | println!("your '{main_branch_name}' branch may not be up-to-date."); | ||
| 113 | println!("manually run `git pull` on '{main_branch_name}' and try again"); | ||
| 114 | } | ||
| 115 | // if new revision and no local changes (tip of local in proposal history) | ||
| 116 | else if commit_events.iter().any(|patch| { | ||
| 117 | get_commit_id_from_patch(patch) | ||
| 118 | .unwrap_or_default() | ||
| 119 | .eq(&local_branch_tip.to_string()) | ||
| 120 | }) { | ||
| 121 | check_clean(&git_repo)?; | ||
| 122 | |||
| 123 | git_repo.create_branch_at_commit(&branch_name, &proposal_base_commit.to_string())?; | ||
| 124 | let applied = git_repo | ||
| 125 | .apply_patch_chain(&branch_name, most_recent_proposal_patch_chain) | ||
| 126 | .context("cannot apply patch chain")?; | ||
| 127 | |||
| 128 | println!( | ||
| 129 | "pulled new version of proposal ({} ahead {} behind '{main_branch_name}'), replacing old version ({} ahead {} behind '{main_branch_name}')", | ||
| 130 | applied.len(), | ||
| 131 | proposal_behind_main.len(), | ||
| 132 | local_ahead_of_main.len(), | ||
| 133 | local_beind_main.len(), | ||
| 134 | ); | ||
| 135 | } | ||
| 136 | // if tip of proposal in branch in history (local appendments made to up-to-date | ||
| 137 | // proposal) | ||
| 138 | else if git_repo.ancestor_of(&local_branch_tip, &proposal_tip)? { | ||
| 139 | let (local_ahead_of_proposal, _) = git_repo | ||
| 140 | .get_commits_ahead_behind(&proposal_tip, &local_branch_tip) | ||
| 141 | .context("cannot get commits ahead behind for propsal_top and local_branch_tip")?; | ||
| 142 | println!( | ||
| 143 | "local proposal branch exists with {} unpublished commits on top of the most up-to-date version of the proposal", | ||
| 144 | local_ahead_of_proposal.len() | ||
| 145 | ); | ||
| 146 | } else { | ||
| 147 | println!("you have an amended/rebase version the proposal that is unpublished"); | ||
| 148 | // user probably has a unpublished amended or rebase version of the latest | ||
| 149 | // proposal version | ||
| 150 | // if tip of proposal commits exist (were once part of branch but have been | ||
| 151 | // amended and git clean up job hasn't removed them) | ||
| 152 | if git_repo.does_commit_exist(&proposal_tip.to_string())? { | ||
| 153 | println!( | ||
| 154 | "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}')", | ||
| 155 | most_recent_proposal_patch_chain.len(), | ||
| 156 | proposal_behind_main.len(), | ||
| 157 | local_ahead_of_main.len(), | ||
| 158 | local_beind_main.len(), | ||
| 159 | ); | ||
| 160 | } | ||
| 161 | // user probably has a unpublished amended or rebase version of an older | ||
| 162 | // proposal version | ||
| 163 | else { | ||
| 164 | println!( | ||
| 165 | "your local proposal branch ({} ahead {} behind '{main_branch_name}') has conflicting changes with the latest published proposal ({} ahead {} behind '{main_branch_name}')", | ||
| 166 | local_ahead_of_main.len(), | ||
| 167 | local_beind_main.len(), | ||
| 168 | most_recent_proposal_patch_chain.len(), | ||
| 169 | proposal_behind_main.len(), | ||
| 170 | ); | ||
| 171 | |||
| 172 | println!( | ||
| 173 | "its likely that you have rebased / amended an old proposal version because git has no record of the latest proposal commit." | ||
| 174 | ); | ||
| 175 | println!( | ||
| 176 | "it is possible that you have been working off the latest version and git has delete this commit as part of a clean up" | ||
| 177 | ); | ||
| 178 | } | ||
| 179 | println!("to view the latest proposal but retain your changes:"); | ||
| 180 | println!(" 1) create a new branch off the tip commit of this one to store your changes"); | ||
| 181 | println!(" 2) run `ngit list` and checkout the latest published version of this proposal"); | ||
| 182 | |||
| 183 | println!("if you are confident in your changes consider running `ngit push --force`"); | ||
| 184 | |||
| 185 | // TODO: this copy could be refined further based on this: | ||
| 186 | // - amended commits in the proposal | ||
| 187 | // - if local_base eq proposal base | ||
| 188 | // - amended an older version of proposal | ||
| 189 | // - if local_base is behind proposal_base | ||
| 190 | // - rebased the proposal | ||
| 191 | // - if local_base is ahead of proposal_base | ||
| 192 | } | ||
| 193 | Ok(()) | ||
| 194 | } | ||
| 195 | |||
| 196 | fn check_clean(git_repo: &Repo) -> Result<()> { | ||
| 197 | if git_repo.has_outstanding_changes()? { | ||
| 198 | bail!( | ||
| 199 | "cannot pull proposal branch when repository is not clean. discard or stash (un)staged changes and try again." | ||
| 200 | ); | ||
| 201 | } | ||
| 202 | Ok(()) | ||
| 203 | } | ||