workyard_git/
completion.rs1use crate::{Runner, process::args, repository::remote::choose_remote};
3use std::{collections::BTreeSet, path::Path};
4use workyard_core::{Control, Problem, ProblemCode};
5
6pub struct Catalog {
8 remotes: BTreeSet<String>,
9 branches: BTreeSet<String>,
10 remote_branches: BTreeSet<String>,
11 tags: BTreeSet<String>,
12}
13
14impl Catalog {
15 pub async fn read(path: &Path) -> Result<Self, Problem> {
17 let runner = Runner::default();
18 let control = Control::default();
19 let remotes = runner
20 .checked(path, &args(&["--no-optional-locks", "remote"]), &control)
21 .await?;
22 let references = runner
23 .checked(
24 path,
25 &args(&[
26 "--no-optional-locks",
27 "for-each-ref",
28 "--format=%(refname)%00%(symref)",
29 "refs/heads/",
30 "refs/remotes/",
31 "refs/tags/",
32 ]),
33 &control,
34 )
35 .await?;
36
37 let mut catalog = Self {
38 remotes: lines(&remotes)?.map(str::to_owned).collect(),
39 branches: BTreeSet::new(),
40 remote_branches: BTreeSet::new(),
41 tags: BTreeSet::new(),
42 };
43
44 for line in lines(&references)? {
45 let Some((reference, symbolic)) = line.split_once('\0') else {
46 return Err(Problem::new(
47 ProblemCode::InvalidGitOutput,
48 "Malformed local reference metadata.",
49 ));
50 };
51 if !symbolic.is_empty() {
52 continue;
53 }
54
55 for (prefix, values) in [
56 ("refs/heads/", &mut catalog.branches),
57 ("refs/remotes/", &mut catalog.remote_branches),
58 ("refs/tags/", &mut catalog.tags),
59 ] {
60 if let Some(name) = reference.strip_prefix(prefix) {
61 values.insert(name.into());
62 }
63 }
64 }
65
66 Ok(catalog)
67 }
68
69 pub fn remotes(&self) -> BTreeSet<String> {
71 self.remotes.clone()
72 }
73
74 pub fn sources(&self) -> BTreeSet<String> {
76 let mut values = self.remote_branches.clone();
77 for name in &self.branches {
78 let qualified = self.has_remote_prefix(name);
79 values.insert(if qualified {
80 format!("refs/heads/{name}")
81 } else {
82 name.clone()
83 });
84 }
85
86 for name in &self.tags {
87 let qualified = self.branches.contains(name) || self.has_remote_prefix(name);
88 values.insert(if qualified {
89 format!("refs/tags/{name}")
90 } else {
91 name.clone()
92 });
93 }
94
95 values
96 }
97
98 pub fn checkout_branches(
100 &self,
101 requested_remote: Option<&str>,
102 ) -> Result<BTreeSet<String>, Problem> {
103 let remotes: Vec<_> = self.remotes.iter().cloned().collect();
104 let selected = choose_remote(&remotes, requested_remote)?;
105 let mut values = self.branches.clone();
106 if let Some(remote) = selected {
107 let prefix = format!("{remote}/");
108 values.extend(
109 self.remote_branches
110 .iter()
111 .filter_map(|name| name.strip_prefix(&prefix).map(str::to_owned)),
112 );
113 }
114
115 Ok(values)
116 }
117
118 fn has_remote_prefix(&self, name: &str) -> bool {
119 self.remotes
120 .iter()
121 .any(|remote| name.starts_with(&format!("{remote}/")))
122 }
123}
124
125fn lines(bytes: &[u8]) -> Result<impl Iterator<Item = &str>, Problem> {
126 std::str::from_utf8(bytes).map(str::lines).map_err(|_| {
127 Problem::new(
128 ProblemCode::InvalidGitOutput,
129 "Non-Unicode local reference metadata.",
130 )
131 })
132}