Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/python.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,10 @@ fn head_sha(path: String) -> PyResult<Option<String>> {
}

#[pyfunction]
fn remote_head_sha(_path: String, _remote_ref: String) -> PyResult<Option<String>> {
todo!("repo::remote_head_sha (resolve remote-tracking ref)")
#[pyo3(signature = (path, remote_ref="origin/main".to_string()))]
fn remote_head_sha(path: String, remote_ref: String) -> PyResult<Option<String>> {
// soft-fail: any error -> None (API.md)
Ok(crate::repo::remote_head_sha(std::path::Path::new(&path), &remote_ref).unwrap_or(None))
}

#[pyfunction]
Expand Down
3 changes: 3 additions & 0 deletions src/repo/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ pub use current_branch::current_branch;
mod tracking_branch;
pub use tracking_branch::tracking_branch;

mod remote_head_sha;
pub use remote_head_sha::remote_head_sha;

/// Temp-dir git fixtures shared by the per-method parity tests.
///
/// Fixtures are built with the real `git` CLI, so each parity test asserts
Expand Down
47 changes: 47 additions & 0 deletions src/repo/remote_head_sha.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
use crate::error::GitxtendError;
use crate::repo::Result;
use std::path::Path;

/// Object id (full hex) that a remote-tracking ref resolves to, e.g.
/// "origin/main"; `Ok(None)` if that ref doesn't exist. Mirrors
/// `git rev-parse <remote_ref>`.
pub fn remote_head_sha(path: &Path, remote_ref: &str) -> Result<Option<String>> {
let repo = gix::open(path).map_err(GitxtendError::from_err)?;
match repo.rev_parse_single(remote_ref) {
Ok(id) => Ok(Some(id.detach().to_string())),
Err(_) => Ok(None),
}
}

#[cfg(test)]
mod tests {
use super::*;
use crate::repo::fixtures;
use tempfile::tempdir;

#[test]
fn no_such_remote_ref() {
let td = fixtures::repo();
let p = td.path();
assert_eq!(remote_head_sha(p, "origin/main").unwrap(), None);
}

#[test]
fn with_a_remote() {
let remote = tempdir().unwrap();
fixtures::git(remote.path(), &["init", "--bare", "-q", "-b", "main"]);
let td = fixtures::repo();
let p = td.path();
fixtures::git(
p,
&["remote", "add", "origin", &remote.path().to_string_lossy()],
);
fixtures::write(p, "file.txt", "content");
fixtures::git(p, &["add", "-A"]);
fixtures::git(p, &["commit", "-m", "initial commit"]);
fixtures::git(p, &["push", "-q", "-u", "origin", "main"]);

let expected = fixtures::git(p, &["rev-parse", "origin/main"]);
assert_eq!(remote_head_sha(p, "origin/main").unwrap(), Some(expected));
}
}
Loading