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