diff options
| author | DanConwayDev <DanConwayDev@protonmail.com> | 2024-09-12 13:36:38 +0100 |
|---|---|---|
| committer | DanConwayDev <DanConwayDev@protonmail.com> | 2024-09-12 14:15:45 +0100 |
| commit | 1a38f98807a3244b77d2525136f3d6976f61dcc4 (patch) | |
| tree | c130b934285f6f20501f2eaa432cc46342c0c348 /src/bin/git_remote_nostr/list.rs | |
| parent | f5bd964718fc063a07af442e93f65fb8239f8228 (diff) | |
fix(remote): improve fetch & list status updates
to bring them more into line to the native git client
Diffstat (limited to 'src/bin/git_remote_nostr/list.rs')
| -rw-r--r-- | src/bin/git_remote_nostr/list.rs | 50 |
1 files changed, 20 insertions, 30 deletions
diff --git a/src/bin/git_remote_nostr/list.rs b/src/bin/git_remote_nostr/list.rs index d3682ec..7dded6b 100644 --- a/src/bin/git_remote_nostr/list.rs +++ b/src/bin/git_remote_nostr/list.rs | |||
| @@ -164,19 +164,25 @@ pub fn list_from_remote( | |||
| 164 | 164 | ||
| 165 | for protocol in &protocols_to_attempt { | 165 | for protocol in &protocols_to_attempt { |
| 166 | term.write_line( | 166 | term.write_line( |
| 167 | format!("fetching {} ref list over {protocol}...", server_url.short_name(),).as_str(), | 167 | format!( |
| 168 | "fetching {} ref list over {protocol}...", | ||
| 169 | server_url.short_name(), | ||
| 170 | ) | ||
| 171 | .as_str(), | ||
| 168 | )?; | 172 | )?; |
| 169 | 173 | ||
| 170 | let formatted_url = server_url.format_as(protocol, &decoded_nostr_url.user)?; | 174 | let formatted_url = server_url.format_as(protocol, &decoded_nostr_url.user)?; |
| 171 | let res = if [ServerProtocol::UnauthHttps, ServerProtocol::UnauthHttp].contains(protocol) { | 175 | let res = list_from_remote_url( |
| 172 | list_from_remote_url_unauthenticated(git_repo, &formatted_url) | 176 | git_repo, |
| 173 | } else { | 177 | &formatted_url, |
| 174 | list_from_remote_url(git_repo, &formatted_url) | 178 | [ServerProtocol::UnauthHttps, ServerProtocol::UnauthHttp].contains(protocol), |
| 175 | }; | 179 | term, |
| 180 | ); | ||
| 176 | 181 | ||
| 177 | match res { | 182 | match res { |
| 178 | Ok(state) => { | 183 | Ok(state) => { |
| 179 | remote_state = Some(state); | 184 | remote_state = Some(state); |
| 185 | term.clear_last_lines(1)?; | ||
| 180 | if !failed_protocols.is_empty() { | 186 | if !failed_protocols.is_empty() { |
| 181 | term.write_line( | 187 | term.write_line( |
| 182 | format!( | 188 | format!( |
| @@ -189,6 +195,7 @@ pub fn list_from_remote( | |||
| 189 | break; | 195 | break; |
| 190 | } | 196 | } |
| 191 | Err(error) => { | 197 | Err(error) => { |
| 198 | term.clear_last_lines(1)?; | ||
| 192 | term.write_line( | 199 | term.write_line( |
| 193 | format!("list: {formatted_url} failed over {protocol}: {error}").as_str(), | 200 | format!("list: {formatted_url} failed over {protocol}: {error}").as_str(), |
| 194 | )?; | 201 | )?; |
| @@ -201,7 +208,6 @@ pub fn list_from_remote( | |||
| 201 | } | 208 | } |
| 202 | } | 209 | } |
| 203 | } | 210 | } |
| 204 | term.clear_last_lines(1)?; | ||
| 205 | } | 211 | } |
| 206 | if let Some(remote_state) = remote_state { | 212 | if let Some(remote_state) = remote_state { |
| 207 | Ok(remote_state) | 213 | Ok(remote_state) |
| @@ -221,31 +227,11 @@ pub fn list_from_remote( | |||
| 221 | } | 227 | } |
| 222 | } | 228 | } |
| 223 | 229 | ||
| 224 | fn list_from_remote_url_unauthenticated( | ||
| 225 | git_repo: &Repo, | ||
| 226 | git_server_remote_url: &str, | ||
| 227 | ) -> Result<HashMap<String, String>> { | ||
| 228 | let mut git_server_remote = git_repo.git_repo.remote_anonymous(git_server_remote_url)?; | ||
| 229 | let remote_callbacks = git2::RemoteCallbacks::new(); | ||
| 230 | git_server_remote.connect_auth(git2::Direction::Fetch, Some(remote_callbacks), None)?; | ||
| 231 | let mut state = HashMap::new(); | ||
| 232 | for head in git_server_remote.list()? { | ||
| 233 | if let Some(symbolic_reference) = head.symref_target() { | ||
| 234 | state.insert( | ||
| 235 | head.name().to_string(), | ||
| 236 | format!("ref: {symbolic_reference}"), | ||
| 237 | ); | ||
| 238 | } else { | ||
| 239 | state.insert(head.name().to_string(), head.oid().to_string()); | ||
| 240 | } | ||
| 241 | } | ||
| 242 | git_server_remote.disconnect()?; | ||
| 243 | Ok(state) | ||
| 244 | } | ||
| 245 | |||
| 246 | fn list_from_remote_url( | 230 | fn list_from_remote_url( |
| 247 | git_repo: &Repo, | 231 | git_repo: &Repo, |
| 248 | git_server_remote_url: &str, | 232 | git_server_remote_url: &str, |
| 233 | dont_authenticate: bool, | ||
| 234 | term: &console::Term, | ||
| 249 | ) -> Result<HashMap<String, String>> { | 235 | ) -> Result<HashMap<String, String>> { |
| 250 | let git_config = git_repo.git_repo.config()?; | 236 | let git_config = git_repo.git_repo.config()?; |
| 251 | 237 | ||
| @@ -253,8 +239,12 @@ fn list_from_remote_url( | |||
| 253 | // authentication may be required | 239 | // authentication may be required |
| 254 | let auth = GitAuthenticator::default(); | 240 | let auth = GitAuthenticator::default(); |
| 255 | let mut remote_callbacks = git2::RemoteCallbacks::new(); | 241 | let mut remote_callbacks = git2::RemoteCallbacks::new(); |
| 256 | remote_callbacks.credentials(auth.credentials(&git_config)); | 242 | if !dont_authenticate { |
| 243 | remote_callbacks.credentials(auth.credentials(&git_config)); | ||
| 244 | } | ||
| 245 | term.write_line("list: connecting...")?; | ||
| 257 | git_server_remote.connect_auth(git2::Direction::Fetch, Some(remote_callbacks), None)?; | 246 | git_server_remote.connect_auth(git2::Direction::Fetch, Some(remote_callbacks), None)?; |
| 247 | term.clear_last_lines(1)?; | ||
| 258 | let mut state = HashMap::new(); | 248 | let mut state = HashMap::new(); |
| 259 | for head in git_server_remote.list()? { | 249 | for head in git_server_remote.list()? { |
| 260 | if let Some(symbolic_reference) = head.symref_target() { | 250 | if let Some(symbolic_reference) = head.symref_target() { |