forge
Explore
Log in

Don't you dare go Hollow

forge

Clone
★ 0

0 watching0 forksRustAGPL-3.09.3 MiB0d793e7Tahinli2 hours ago359 commits

branches.rs45 lines, 1.3 KiBHistoryRawDownload
1use std::path::Path;
2
3use crate::command::GitEnv;
4use crate::error::{Error, Result};
5use crate::history::{git, validate_oid, zero_oid};
6use crate::pulls::{delete_ref_cas, update_ref_cas};
7
8pub async fn ahead_behind(env: &GitEnv, repo: &Path, base: &str, tip: &str) -> Result<(u32, u32)> {
9 validate_oid(base)?;
10 validate_oid(tip)?;
11 let range = format!("{base}...{tip}");
12 let out = git(env, "rev-list")
13 .git_dir(repo)
14 .arg("--left-right")
15 .arg("--count")
16 .user_revs(&[range.as_str()])?
17 .output()
18 .await?;
19 let text = String::from_utf8_lossy(&out);
20 let mut it = text.split_whitespace();
21 let behind = it.next().and_then(|n| n.parse().ok());
22 let ahead = it.next().and_then(|n| n.parse().ok());
23 match (ahead, behind) {
24 (Some(a), Some(b)) => Ok((a, b)),
25 _ => Err(Error::Failed(format!(
26 "unexpected rev-list output: {text:?}"
27 ))),
28 }
29}
30
31pub async fn create_branch(env: &GitEnv, repo: &Path, name: &str, oid: &str) -> Result<()> {
32 validate_oid(oid)?;
33 update_ref_cas(
34 env,
35 repo,
36 &format!("refs/heads/{name}"),
37 oid,
38 &zero_oid(oid),
39 )
40 .await
41}
42
43pub async fn delete_branch(env: &GitEnv, repo: &Path, name: &str, oid: &str) -> Result<()> {
44 delete_ref_cas(env, repo, &format!("refs/heads/{name}"), oid).await
45}