.
This commit is contained in:
@@ -309,6 +309,23 @@ pub fn worktree_all_commits(workdir: &Path) -> Result<Vec<FileCommit>> {
|
||||
all_commits(&gix::open(workdir)?)
|
||||
}
|
||||
|
||||
/// The commit HEAD points to, like `git log -1`. Returns `Ok(None)` for a
|
||||
/// repository without commits yet (unborn HEAD).
|
||||
pub fn head_commit(repo: &gix::Repository) -> Result<Option<FileCommit>> {
|
||||
let Some(head) = repo.head_id().ok() else {
|
||||
return Ok(None);
|
||||
};
|
||||
let commit = head.object()?.into_commit();
|
||||
let author = commit.author()?;
|
||||
let message = commit.message()?;
|
||||
Ok(Some(FileCommit {
|
||||
id: commit.id().shorten_or_id().to_string(),
|
||||
summary: String::from_utf8_lossy(message.title).trim().to_string(),
|
||||
author: String::from_utf8_lossy(author.name).trim().to_string(),
|
||||
time: author.time()?.seconds,
|
||||
}))
|
||||
}
|
||||
|
||||
/// Short names of local branches (`refs/heads/*`), sorted alphabetically.
|
||||
pub fn worktree_branches(workdir: &Path) -> Result<Vec<String>> {
|
||||
let repo = gix::open(workdir)?;
|
||||
@@ -353,10 +370,12 @@ pub struct WorktreeSnapshot {
|
||||
pub readme: Option<Vec<u8>>,
|
||||
/// Branch HEAD points to (`None` when detached, e.g. on a tag).
|
||||
pub current_branch: Option<String>,
|
||||
/// Commit HEAD points to, if any (see [`head_commit`]).
|
||||
pub head_commit: Option<FileCommit>,
|
||||
}
|
||||
|
||||
/// Snapshot the worktree after a branch/tag switch: entries, README and the
|
||||
/// branch HEAD points to, opening the repository once.
|
||||
/// Snapshot the worktree after a branch/tag switch: entries, README, the
|
||||
/// branch HEAD points to and its commit, opening the repository once.
|
||||
pub fn worktree_snapshot(workdir: &Path) -> Result<WorktreeSnapshot> {
|
||||
let repo = gix::open(workdir)?;
|
||||
let readme_path = find_readme(&repo)?;
|
||||
@@ -369,6 +388,7 @@ pub fn worktree_snapshot(workdir: &Path) -> Result<WorktreeSnapshot> {
|
||||
readme_path,
|
||||
readme,
|
||||
current_branch: current_branch(&repo)?,
|
||||
head_commit: head_commit(&repo)?,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -672,6 +692,23 @@ mod tests {
|
||||
assert!(find_readme(&repo).expect("find").is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn head_commit_reports_head() {
|
||||
let (_dir, repo) = fixture(&[("a.txt", b"one")]);
|
||||
|
||||
// Unborn HEAD: no commit yet.
|
||||
assert!(head_commit(&repo).expect("head").is_none());
|
||||
|
||||
commit_all(&repo, "initial");
|
||||
let head = head_commit(&repo).expect("head").expect("commit");
|
||||
assert_eq!(
|
||||
head.id,
|
||||
repo.head_id().expect("head id").shorten_or_id().to_string()
|
||||
);
|
||||
assert_eq!(head.summary, "initial");
|
||||
assert_eq!(head.author, "Test Author");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn worktree_branches_and_tags_list_short_names() {
|
||||
let (dir, repo) = fixture(&[("a.txt", b"one")]);
|
||||
@@ -743,6 +780,10 @@ mod tests {
|
||||
|
||||
let snapshot = worktree_snapshot(dir).expect("snapshot");
|
||||
assert_eq!(snapshot.current_branch.as_deref(), Some("feature"));
|
||||
assert_eq!(
|
||||
snapshot.head_commit.as_ref().expect("head commit").summary,
|
||||
"feature work"
|
||||
);
|
||||
assert_eq!(
|
||||
String::from_utf8(snapshot.readme.expect("readme")).expect("utf8"),
|
||||
"# feature"
|
||||
@@ -763,6 +804,10 @@ mod tests {
|
||||
|
||||
let snapshot = worktree_snapshot(dir).expect("snapshot");
|
||||
assert_eq!(snapshot.current_branch.as_deref(), Some(default.as_str()));
|
||||
assert_eq!(
|
||||
snapshot.head_commit.as_ref().expect("head commit").summary,
|
||||
"initial"
|
||||
);
|
||||
assert_eq!(
|
||||
String::from_utf8(snapshot.readme.expect("readme")).expect("utf8"),
|
||||
"# main"
|
||||
|
||||
Reference in New Issue
Block a user