Collects all refs.
(repo: &Repository)
| 175 | |
| 176 | /// Collects all refs. |
| 177 | fn collect_refs(repo: &Repository) -> Result<HashMap<String, Vec<GraphRef>>, git2::Error> { |
| 178 | let mut refs_map: HashMap<String, Vec<GraphRef>> = HashMap::new(); |
| 179 | let head = repo.head().ok(); |
| 180 | let current_branch = get_current_branch(repo); |
| 181 | |
| 182 | for reference in repo.references()? { |
| 183 | let reference = reference?; |
| 184 | |
| 185 | let (ref_type, name) = if reference.is_branch() { |
| 186 | ("branch", reference.shorthand().unwrap_or("")) |
| 187 | } else if reference.is_remote() { |
| 188 | ("remote", reference.shorthand().unwrap_or("")) |
| 189 | } else if reference.is_tag() { |
| 190 | ("tag", reference.shorthand().unwrap_or("")) |
| 191 | } else { |
| 192 | continue; |
| 193 | }; |
| 194 | |
| 195 | if let Some(oid) = reference.target() { |
| 196 | let hash = oid.to_string(); |
| 197 | let is_current = current_branch.as_ref().is_some_and(|cb| cb == name); |
| 198 | let is_head = head.as_ref().and_then(|h| h.target()) == Some(oid); |
| 199 | |
| 200 | let graph_ref = GraphRef { |
| 201 | name: name.to_string(), |
| 202 | ref_type: ref_type.to_string(), |
| 203 | is_current, |
| 204 | is_head, |
| 205 | }; |
| 206 | |
| 207 | refs_map.entry(hash).or_default().push(graph_ref); |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | Ok(refs_map) |
| 212 | } |
| 213 | |
| 214 | /// Returns the current branch name. |
| 215 | fn get_current_branch(repo: &Repository) -> Option<String> { |
no test coverage detected