| 63 | } |
| 64 | |
| 65 | pub fn clone_at_commit<P>( |
| 66 | repository_url: &Url, |
| 67 | commit_id: &str, |
| 68 | into_dir: P, |
| 69 | get_credentials: &impl Fn(&str) -> Vec<(CredentialType, Cred)>, |
| 70 | skip_submodules: bool, |
| 71 | ) -> Result<(), BuildError> |
| 72 | where |
| 73 | P: AsRef<Path>, |
| 74 | { |
| 75 | let repo = fetch(repository_url, into_dir, get_credentials, commit_id).map_err(|error| BuildError::GitError { |
| 76 | application: "".to_string(), |
| 77 | git_cmd: GitCmd::Fetch, |
| 78 | context: format!("url: {repository_url}/ commit id: {commit_id}"), |
| 79 | raw_error: error, |
| 80 | })?; |
| 81 | // position the repo at the correct commit |
| 82 | let _ = checkout(&repo, commit_id).map_err(|error| BuildError::GitError { |
| 83 | application: "".to_string(), |
| 84 | git_cmd: GitCmd::Checkout, |
| 85 | context: commit_id.to_string(), |
| 86 | raw_error: error, |
| 87 | })?; |
| 88 | |
| 89 | // check submodules if needed |
| 90 | if !skip_submodules { |
| 91 | let submodules = repo.submodules().map_err(|error| BuildError::GitError { |
| 92 | application: "".to_string(), |
| 93 | git_cmd: GitCmd::SubmoduleUpdate, |
| 94 | context: "".to_string(), |
| 95 | raw_error: error, |
| 96 | })?; |
| 97 | if !submodules.is_empty() { |
| 98 | // for auth |
| 99 | let mut callbacks = RemoteCallbacks::new(); |
| 100 | callbacks.credentials(authentication_callback(&get_credentials)); |
| 101 | callbacks.certificate_check(|_, _| Ok(CertificateCheckStatus::CertificateOk)); |
| 102 | |
| 103 | let mut fo = FetchOptions::new(); |
| 104 | fo.remote_callbacks(callbacks); |
| 105 | let mut opts = SubmoduleUpdateOptions::new(); |
| 106 | opts.fetch(fo); |
| 107 | |
| 108 | for mut submodule in submodules { |
| 109 | info!("getting submodule {:?} from {:?}", submodule.name(), submodule.url()); |
| 110 | submodule |
| 111 | .update(true, Some(&mut opts)) |
| 112 | .map_err(|error| BuildError::GitError { |
| 113 | application: "".to_string(), |
| 114 | git_cmd: GitCmd::SubmoduleUpdate, |
| 115 | context: submodule.name().unwrap_or("").to_string(), |
| 116 | raw_error: error, |
| 117 | })? |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | Ok(()) |