Observe the current tip of `remote_ref` on `remote` via `git ls-remote`. `None` means the remote does not advertise the ref (create-only push territory); a transport failure is a typed error so the caller never mistakes an unreachable remote for an empty one.
(
workdir: &Path,
remote: &str,
remote_ref: &str,
)
| 571 | /// territory); a transport failure is a typed error so the caller never |
| 572 | /// mistakes an unreachable remote for an empty one. |
| 573 | pub fn observe_remote_ref( |
| 574 | workdir: &Path, |
| 575 | remote: &str, |
| 576 | remote_ref: &str, |
| 577 | ) -> CliResult<Option<String>> { |
| 578 | let output = Command::new("git") |
| 579 | .args(["ls-remote", remote, remote_ref]) |
| 580 | .current_dir(workdir) |
| 581 | .output() |
| 582 | .map_err(|error| CliError::GitError { |
| 583 | message: format!("cannot run git ls-remote: {error}"), |
| 584 | })?; |
| 585 | if !output.status.success() { |
| 586 | return Err(CliError::GitError { |
| 587 | message: format!( |
| 588 | "cannot observe remote '{remote}' ref '{remote_ref}' (ls-remote exited {})", |
| 589 | output.status |
| 590 | ), |
| 591 | }); |
| 592 | } |
| 593 | let text = String::from_utf8_lossy(&output.stdout); |
| 594 | for line in text.lines() { |
| 595 | let mut parts = line.split_whitespace(); |
| 596 | if let (Some(oid), Some(name)) = (parts.next(), parts.next()) { |
| 597 | if name == remote_ref { |
| 598 | return Ok(Some(oid.to_string())); |
| 599 | } |
| 600 | } |
| 601 | } |
| 602 | Ok(None) |
| 603 | } |
| 604 | |
| 605 | /// The `--force-with-lease=<ref>:<expected>` spec for one mapped remote |
| 606 | /// update, from the mapping's `last_observed_remote`. |
no test coverage detected