1use std::collections::{BTreeSet, HashMap, HashSet};
17use std::fs;
18use std::io;
19use std::path::{Path, PathBuf};
20
21use crate::config::ConfigSet;
22use crate::error::{Error, Result};
23use crate::objects::ObjectId;
24use crate::pack;
25
26#[derive(Debug, Clone, PartialEq, Eq)]
28pub enum Ref {
29 Direct(ObjectId),
31 Symbolic(String),
33}
34
35pub fn read_ref_file(path: &Path) -> Result<Ref> {
42 if let Ok(target) = fs::read_link(path) {
43 return Ok(Ref::Symbolic(target.to_string_lossy().into_owned()));
44 }
45
46 let content = match fs::read_to_string(path) {
47 Ok(c) => c,
48 Err(e)
52 if e.kind() == io::ErrorKind::IsADirectory
53 || e.kind() == io::ErrorKind::NotADirectory
54 || e.raw_os_error() == Some(libc::EISDIR)
55 || e.raw_os_error() == Some(libc::ENOTDIR) =>
56 {
57 return Err(Error::Io(io::Error::new(io::ErrorKind::NotFound, e)));
58 }
59 Err(e) => return Err(Error::Io(e)),
60 };
61 let content = content.trim_end_matches('\n');
62 parse_ref_content(content)
63}
64
65pub(crate) fn parse_ref_content(content: &str) -> Result<Ref> {
67 if let Some(target) = content.strip_prefix("ref: ") {
68 Ok(Ref::Symbolic(target.trim().to_owned()))
69 } else if content.len() == 40 && content.chars().all(|c| c.is_ascii_hexdigit()) {
70 let oid: ObjectId = content.parse()?;
71 Ok(Ref::Direct(oid))
72 } else if content == "unknown-oid" {
73 const PLACEHOLDER: &[u8; 20] = b"GritUnknownOidPlc!X!";
77 let oid = ObjectId::from_bytes(PLACEHOLDER)?;
78 Ok(Ref::Direct(oid))
79 } else {
80 Err(Error::InvalidRef(content.to_owned()))
81 }
82}
83
84pub fn resolve_ref(git_dir: &Path, refname: &str) -> Result<ObjectId> {
98 if crate::reftable::is_reftable_repo(git_dir) {
99 return crate::reftable::reftable_resolve_ref(git_dir, refname);
100 }
101 let common = common_dir(git_dir);
102 resolve_ref_depth(git_dir, common.as_deref(), refname, 0)
103}
104
105pub fn common_dir(git_dir: &Path) -> Option<PathBuf> {
110 let commondir_file = git_dir.join("commondir");
111 let raw = fs::read_to_string(commondir_file).ok()?;
112 let rel = raw.trim();
113 let path = if Path::new(rel).is_absolute() {
116 PathBuf::from(rel)
117 } else {
118 git_dir.join(rel)
119 };
120 path.canonicalize().ok()
121}
122
123fn resolve_ref_depth(
129 git_dir: &Path,
130 _common: Option<&Path>,
131 refname: &str,
132 depth: usize,
133) -> Result<ObjectId> {
134 if depth > 10 {
135 return Err(Error::InvalidRef(format!(
136 "ref symlink too deep: {refname}"
137 )));
138 }
139
140 let (store, stor_name) = crate::worktree_ref::resolve_ref_storage(git_dir, refname);
141 let storage_owned = crate::ref_namespace::storage_ref_name(&stor_name);
142 let try_names: Vec<&str> =
143 if stor_name == "HEAD" && crate::ref_namespace::ref_storage_prefix().is_some() {
144 vec![storage_owned.as_str()]
145 } else if storage_owned != stor_name {
146 vec![storage_owned.as_str(), stor_name.as_str()]
147 } else {
148 vec![stor_name.as_str()]
149 };
150
151 for name in try_names {
152 let path = store.join(name);
153 match read_ref_file(&path) {
154 Ok(Ref::Direct(oid)) => return Ok(oid),
155 Ok(Ref::Symbolic(target)) => {
156 return resolve_ref_depth(git_dir, None, &target, depth + 1);
157 }
158 Err(Error::Io(ref e)) if e.kind() == io::ErrorKind::NotFound => {}
159 Err(e) => return Err(e),
160 }
161
162 if let Some(oid) = lookup_packed_ref(&store, name)? {
163 return Ok(oid);
164 }
165 }
166
167 Err(Error::InvalidRef(format!("ref not found: {refname}")))
168}
169
170#[derive(Debug, Clone, Copy, PartialEq, Eq)]
176pub enum RawRefLookup {
177 Exists,
179 NotFound,
181 IsDirectory,
183}
184
185pub fn read_raw_ref(git_dir: &Path, refname: &str) -> Result<RawRefLookup> {
200 if crate::reftable::is_reftable_repo(git_dir) {
201 read_raw_ref_reftable(git_dir, refname)
202 } else {
203 read_raw_ref_files(git_dir, refname)
204 }
205}
206
207fn read_raw_ref_files(git_dir: &Path, refname: &str) -> Result<RawRefLookup> {
208 let (store, stor_name) = crate::worktree_ref::resolve_ref_storage(git_dir, refname);
209 let storage_owned = crate::ref_namespace::storage_ref_name(&stor_name);
210 let (names, n): ([&str; 2], usize) = if storage_owned != stor_name {
211 ([storage_owned.as_str(), stor_name.as_str()], 2)
212 } else {
213 ([stor_name.as_str(), stor_name.as_str()], 1)
214 };
215
216 for name in names.iter().take(n) {
217 if let Some(lookup) = read_raw_ref_at(store.join(name))? {
218 return Ok(lookup);
219 }
220
221 if packed_ref_name_exists(&store, name)? {
222 return Ok(RawRefLookup::Exists);
223 }
224 }
225
226 Ok(RawRefLookup::NotFound)
227}
228
229#[must_use]
231pub fn lock_path_for_ref(path: &Path) -> PathBuf {
232 let mut s = path.as_os_str().to_owned();
233 s.push(".lock");
234 PathBuf::from(s)
235}
236
237fn read_raw_ref_at(path: PathBuf) -> Result<Option<RawRefLookup>> {
238 match fs::symlink_metadata(&path) {
239 Ok(meta) => {
240 if meta.is_dir() {
241 return Ok(Some(RawRefLookup::IsDirectory));
242 }
243 Ok(Some(RawRefLookup::Exists))
244 }
245 Err(e)
246 if e.kind() == io::ErrorKind::NotFound
247 || e.kind() == io::ErrorKind::NotADirectory
248 || e.raw_os_error() == Some(libc::ENOTDIR) =>
249 {
250 Ok(None)
251 }
252 Err(e) => Err(Error::Io(e)),
253 }
254}
255
256fn packed_ref_with_prefix(git_dir: &Path, prefix_with_slash: &str) -> Result<Option<String>> {
257 let packed = git_dir.join("packed-refs");
258 let content = match fs::read_to_string(&packed) {
259 Ok(c) => c,
260 Err(e) if e.kind() == io::ErrorKind::NotFound => return Ok(None),
261 Err(e) => return Err(Error::Io(e)),
262 };
263 let mut best: Option<String> = None;
264 for line in content.lines() {
265 if line.is_empty() || line.starts_with('#') || line.starts_with('^') {
266 continue;
267 }
268 let mut parts = line.split_whitespace();
269 let _oid = parts.next();
270 let Some(name) = parts.next() else {
271 continue;
272 };
273 let name = name.trim();
274 if name.starts_with(prefix_with_slash) {
275 let take = match &best {
276 None => true,
277 Some(b) => name < b.as_str(),
278 };
279 if take {
280 best = Some(name.to_owned());
281 }
282 }
283 }
284 Ok(best)
285}
286
287fn packed_ref_name_exists(git_dir: &Path, refname: &str) -> Result<bool> {
288 let packed = git_dir.join("packed-refs");
289 let content = match fs::read_to_string(&packed) {
290 Ok(c) => c,
291 Err(e) if e.kind() == io::ErrorKind::NotFound => return Ok(false),
292 Err(e) => return Err(Error::Io(e)),
293 };
294 for line in content.lines() {
295 if line.is_empty() || line.starts_with('#') || line.starts_with('^') {
296 continue;
297 }
298 let mut parts = line.split_whitespace();
299 let _oid = parts.next();
300 if let Some(name) = parts.next() {
301 if name == refname {
302 return Ok(true);
303 }
304 }
305 }
306 Ok(false)
307}
308
309fn refname_namespace_conflicts(existing: &str, candidate: &str) -> bool {
310 if existing == candidate {
311 return false;
312 }
313 existing
314 .strip_prefix(candidate)
315 .is_some_and(|rest| rest.starts_with('/'))
316 || candidate
317 .strip_prefix(existing)
318 .is_some_and(|rest| rest.starts_with('/'))
319}
320
321fn packed_ref_namespace_conflict(git_dir: &Path, refname: &str) -> Result<bool> {
322 let packed = git_dir.join("packed-refs");
323 let content = match fs::read_to_string(&packed) {
324 Ok(c) => c,
325 Err(e) if e.kind() == io::ErrorKind::NotFound => return Ok(false),
326 Err(e) => return Err(Error::Io(e)),
327 };
328 for line in content.lines() {
329 if line.is_empty() || line.starts_with('#') || line.starts_with('^') {
330 continue;
331 }
332 let mut parts = line.split_whitespace();
333 let _oid = parts.next();
334 if let Some(name) = parts.next() {
335 if refname_namespace_conflicts(name, refname) {
336 return Ok(true);
337 }
338 }
339 }
340 Ok(false)
341}
342
343pub fn packed_refs_entry_exists(git_dir: &Path, refname: &str) -> Result<bool> {
355 if crate::reftable::is_reftable_repo(git_dir) || refname == "HEAD" {
356 return Ok(false);
357 }
358 let storage_dir = ref_storage_dir(git_dir, refname);
359 packed_ref_name_exists(&storage_dir, refname)
360}
361
362#[derive(Debug, Clone, PartialEq, Eq)]
364pub enum RefnameUnavailable {
365 AncestorExists {
367 blocking: String,
369 new_ref: String,
371 },
372 DescendantExists {
374 blocking: String,
376 new_ref: String,
378 },
379 SameBatch {
381 refname: String,
383 other: String,
385 },
386}
387
388impl RefnameUnavailable {
389 #[must_use]
391 pub fn lock_message_suffix(&self) -> String {
392 match self {
393 RefnameUnavailable::AncestorExists { blocking, new_ref } => {
394 format!("'{blocking}' exists; cannot create '{new_ref}'")
395 }
396 RefnameUnavailable::DescendantExists { blocking, new_ref } => {
397 format!("'{blocking}' exists; cannot create '{new_ref}'")
398 }
399 RefnameUnavailable::SameBatch { refname, other } => {
400 format!("cannot process '{refname}' and '{other}' at the same time")
401 }
402 }
403 }
404}
405
406fn find_descendant_in_sorted_extras(
407 dirname_with_slash: &str,
408 extras: &BTreeSet<String>,
409) -> Option<String> {
410 let start = extras
411 .range(dirname_with_slash.to_string()..)
412 .next()
413 .cloned()?;
414 if start.starts_with(dirname_with_slash) {
415 Some(start)
416 } else {
417 None
418 }
419}
420
421pub fn verify_refname_available_for_create(
434 git_dir: &Path,
435 refname: &str,
436 extras: &BTreeSet<String>,
437 skip: &HashSet<String>,
438) -> std::result::Result<(), RefnameUnavailable> {
439 let git_dir = fs::canonicalize(git_dir).unwrap_or_else(|_| git_dir.to_path_buf());
442 let mut seen_dirnames: HashSet<String> = HashSet::new();
443 let segments: Vec<&str> = refname.split('/').filter(|s| !s.is_empty()).collect();
444 if segments.len() <= 1 {
445 } else {
447 let mut dirname = String::new();
448 for part in &segments[..segments.len() - 1] {
449 if !dirname.is_empty() {
450 dirname.push('/');
451 }
452 dirname.push_str(part);
453
454 if !seen_dirnames.insert(dirname.clone()) {
455 continue;
456 }
457
458 if skip.contains(&dirname) {
459 continue;
460 }
461
462 match read_raw_ref(&git_dir, &dirname) {
463 Ok(RawRefLookup::Exists) => {
464 return Err(RefnameUnavailable::AncestorExists {
465 blocking: dirname.clone(),
466 new_ref: refname.to_owned(),
467 });
468 }
469 Ok(RawRefLookup::NotFound | RawRefLookup::IsDirectory) => {}
472 Err(_) => {}
473 }
474
475 if extras.contains(&dirname) {
476 return Err(RefnameUnavailable::SameBatch {
477 refname: refname.to_owned(),
478 other: dirname.clone(),
479 });
480 }
481 }
482 }
483
484 let mut leaf_dir = String::with_capacity(refname.len() + 1);
485 leaf_dir.push_str(refname);
486 leaf_dir.push('/');
487
488 let under = list_refs(&git_dir, &leaf_dir).unwrap_or_default();
489 if under.is_empty() {
490 let packed_dir = common_dir(&git_dir).unwrap_or_else(|| git_dir.clone());
491 if let Ok(Some(name)) = packed_ref_with_prefix(&packed_dir, &leaf_dir) {
492 if !skip.contains(&name) {
493 return Err(RefnameUnavailable::DescendantExists {
494 blocking: name,
495 new_ref: refname.to_owned(),
496 });
497 }
498 }
499 if packed_dir != git_dir {
500 if let Ok(Some(name)) = packed_ref_with_prefix(&git_dir, &leaf_dir) {
501 if !skip.contains(&name) {
502 return Err(RefnameUnavailable::DescendantExists {
503 blocking: name,
504 new_ref: refname.to_owned(),
505 });
506 }
507 }
508 }
509 }
510 if under.is_empty()
511 && fs::symlink_metadata(git_dir.join(refname))
512 .map(|m| m.is_dir())
513 .unwrap_or(false)
514 {
515 let mut blocking: Option<String> = None;
516 let dir_path = git_dir.join(refname);
517 if let Ok(read) = fs::read_dir(&dir_path) {
518 for entry in read.flatten() {
519 let path = entry.path();
520 let Ok(meta) = fs::metadata(&path) else {
521 continue;
522 };
523 if !meta.is_file() {
524 continue;
525 }
526 let name = entry.file_name().to_string_lossy().into_owned();
527 let full = format!("{refname}/{name}");
528 blocking = Some(full);
529 break;
530 }
531 }
532 if let Some(b) = blocking {
533 if !skip.contains(&b) {
534 return Err(RefnameUnavailable::DescendantExists {
535 blocking: b,
536 new_ref: refname.to_owned(),
537 });
538 }
539 }
540 }
541
542 for (existing, _) in under {
543 if skip.contains(&existing) {
544 continue;
545 }
546 return Err(RefnameUnavailable::DescendantExists {
547 blocking: existing,
548 new_ref: refname.to_owned(),
549 });
550 }
551
552 if let Some(extra) = find_descendant_in_sorted_extras(&leaf_dir, extras) {
553 if !skip.contains(&extra) {
554 return Err(RefnameUnavailable::SameBatch {
555 refname: refname.to_owned(),
556 other: extra,
557 });
558 }
559 }
560
561 Ok(())
562}
563
564fn read_raw_ref_reftable(git_dir: &Path, refname: &str) -> Result<RawRefLookup> {
565 if refname == "HEAD" {
566 let head_path = git_dir.join("HEAD");
567 match fs::symlink_metadata(&head_path) {
568 Ok(meta) => {
569 if meta.is_dir() {
570 return Ok(RawRefLookup::IsDirectory);
571 }
572 return Ok(RawRefLookup::Exists);
573 }
574 Err(e) if e.kind() == io::ErrorKind::NotFound => return Ok(RawRefLookup::NotFound),
575 Err(e) => return Err(Error::Io(e)),
576 }
577 }
578
579 if let Some(lookup) = read_raw_ref_at(git_dir.join(refname))? {
580 return Ok(lookup);
581 }
582
583 let stack = crate::reftable::ReftableStack::open(git_dir)?;
584 match stack.lookup_ref(refname)? {
585 Some(rec) => match rec.value {
586 crate::reftable::RefValue::Deletion => Ok(RawRefLookup::NotFound),
587 _ => Ok(RawRefLookup::Exists),
588 },
589 None => Ok(RawRefLookup::NotFound),
590 }
591}
592
593fn lookup_packed_ref(git_dir: &Path, refname: &str) -> Result<Option<ObjectId>> {
595 let packed = git_dir.join("packed-refs");
596 let content = match fs::read_to_string(&packed) {
597 Ok(c) => c,
598 Err(e) if e.kind() == io::ErrorKind::NotFound => return Ok(None),
599 Err(e) => return Err(Error::Io(e)),
600 };
601
602 for line in content.lines() {
603 if line.starts_with('#') || line.starts_with('^') {
604 continue;
605 }
606 let mut parts = line.splitn(2, ' ');
607 let hash = parts.next().unwrap_or("");
608 let name = parts.next().unwrap_or("").trim();
609 if name == refname && hash.len() == 40 {
610 let oid: ObjectId = hash.parse()?;
611 return Ok(Some(oid));
612 }
613 }
614 Ok(None)
615}
616
617pub fn write_symbolic_ref(git_dir: &Path, refname: &str, target: &str) -> Result<()> {
634 if crate::reftable::is_reftable_repo(git_dir) {
635 return crate::reftable::reftable_write_symref(git_dir, refname, target, None, None);
636 }
637 let storage_dir = ref_storage_dir(git_dir, refname);
638 if packed_ref_namespace_conflict(&storage_dir, refname)? {
639 return Err(Error::InvalidRef(format!(
640 "cannot update ref '{refname}': reference namespace conflict"
641 )));
642 }
643 let stor = crate::ref_namespace::storage_ref_name(refname);
644 let path = storage_dir.join(stor);
645 if let Some(parent) = path.parent() {
646 fs::create_dir_all(parent)?;
647 }
648 let content = format!("ref: {target}\n");
649 let lock = lock_path_for_ref(&path);
650 fs::write(&lock, &content)?;
651 fs::rename(&lock, &path)?;
652 Ok(())
653}
654
655pub fn write_ref(git_dir: &Path, refname: &str, oid: &ObjectId) -> Result<()> {
656 if crate::reftable::is_reftable_repo(git_dir) {
657 return crate::reftable::reftable_write_ref(git_dir, refname, oid, None, None);
658 }
659 let storage_dir = ref_storage_dir(git_dir, refname);
660 if packed_ref_namespace_conflict(&storage_dir, refname)? {
661 return Err(Error::InvalidRef(format!(
662 "cannot update ref '{refname}': reference namespace conflict"
663 )));
664 }
665 let stor = crate::ref_namespace::storage_ref_name(refname);
666 let path = storage_dir.join(stor);
667 remove_empty_ref_directory(&path);
674 if fs::symlink_metadata(&path)
681 .map(|m| m.file_type().is_dir())
682 .unwrap_or(false)
683 {
684 let display = ref_path_for_display(&path);
685 return Err(Error::Message(format!(
686 "fatal: cannot lock ref '{refname}': there is a non-empty directory '{display}' blocking reference '{refname}'"
687 )));
688 }
689 if let Some(parent) = path.parent() {
690 fs::create_dir_all(parent)?;
691 }
692 let content = format!("{oid}\n");
693 let lock = lock_path_for_ref(&path);
695 fs::write(&lock, &content)?;
696 fs::rename(&lock, &path)?;
697 Ok(())
698}
699
700fn ref_path_for_display(path: &Path) -> String {
705 if let Ok(cwd) = std::env::current_dir() {
706 if let Ok(rel) = path.strip_prefix(&cwd) {
707 return rel.to_string_lossy().into_owned();
708 }
709 if let (Ok(cwd_c), Ok(path_c)) = (cwd.canonicalize(), path.canonicalize()) {
713 if let Ok(rel) = path_c.strip_prefix(&cwd_c) {
714 return rel.to_string_lossy().into_owned();
715 }
716 }
717 }
718 path.to_string_lossy().into_owned()
719}
720
721fn remove_empty_ref_directory(path: &Path) {
727 match fs::symlink_metadata(path) {
728 Ok(meta) if meta.file_type().is_dir() => {}
729 _ => return,
730 }
731 if dir_tree_has_files(path) {
734 return;
735 }
736 let _ = remove_dir_tree(path);
737}
738
739fn dir_tree_has_files(dir: &Path) -> bool {
741 let Ok(entries) = fs::read_dir(dir) else {
742 return true;
744 };
745 for entry in entries.flatten() {
746 match entry.file_type() {
747 Ok(ft) if ft.is_dir() => {
748 if dir_tree_has_files(&entry.path()) {
749 return true;
750 }
751 }
752 Ok(_) => return true, Err(_) => return true,
754 }
755 }
756 false
757}
758
759fn remove_dir_tree(dir: &Path) -> io::Result<()> {
761 for entry in fs::read_dir(dir)? {
762 let entry = entry?;
763 if entry.file_type()?.is_dir() {
764 remove_dir_tree(&entry.path())?;
765 }
766 }
767 fs::remove_dir(dir)
768}
769
770pub fn delete_ref(git_dir: &Path, refname: &str) -> Result<()> {
778 if crate::reftable::is_reftable_repo(git_dir) {
779 return crate::reftable::reftable_delete_ref(git_dir, refname);
780 }
781 let storage_dir = ref_storage_dir(git_dir, refname);
782 let stor = crate::ref_namespace::storage_ref_name(refname);
783 let path = storage_dir.join(&stor);
784
785 remove_packed_ref(&storage_dir, &stor)?;
791
792 remove_empty_ref_directory(&path);
796 match fs::remove_file(&path) {
797 Ok(()) => {}
798 Err(e) if e.kind() == io::ErrorKind::NotFound => {}
799 Err(e)
800 if e.kind() == io::ErrorKind::NotADirectory
801 || e.raw_os_error() == Some(libc::ENOTDIR) => {}
802 Err(e)
805 if e.raw_os_error() == Some(libc::EISDIR) || e.raw_os_error() == Some(libc::EPERM) => {}
806 Err(e) => return Err(Error::Io(e)),
807 }
808
809 let log_path = storage_dir.join("logs").join(&stor);
810
811 let _ = fs::remove_file(&log_path);
817
818 let logs_root = storage_dir.join("logs");
819 let mut parent = log_path.parent();
820 while let Some(p) = parent {
821 if p == logs_root.as_path() || !p.starts_with(&logs_root) {
822 break;
823 }
824 if fs::remove_dir(p).is_err() {
825 break; }
827 parent = p.parent();
828 }
829
830 Ok(())
831}
832
833fn remove_packed_ref(git_dir: &Path, refname: &str) -> Result<()> {
835 let packed_path = git_dir.join("packed-refs");
836 let content = match fs::read_to_string(&packed_path) {
837 Ok(c) => c,
838 Err(e)
839 if e.kind() == io::ErrorKind::NotFound
840 || e.kind() == io::ErrorKind::NotADirectory
841 || e.raw_os_error() == Some(libc::ENOTDIR) =>
842 {
843 return Ok(());
844 }
845 Err(e) => return Err(Error::Io(e)),
846 };
847
848 let mut out = String::new();
849 let mut skip_peeled = false;
850 let mut changed = false;
851 let mut header_written = false;
854
855 for line in content.lines() {
856 if skip_peeled {
857 if line.starts_with('^') {
858 changed = true;
859 continue;
860 }
861 skip_peeled = false;
862 }
863
864 if line.starts_with('#') {
865 continue;
867 }
868 if line.starts_with('^') {
869 out.push_str(line);
870 out.push('\n');
871 continue;
872 }
873
874 if !header_written {
876 out.insert_str(0, "# pack-refs with: peeled fully-peeled sorted\n");
877 header_written = true;
878 }
879
880 let mut parts = line.splitn(2, ' ');
882 let _hash = parts.next().unwrap_or("");
883 let name = parts.next().unwrap_or("").trim();
884 if name == refname {
885 changed = true;
886 skip_peeled = true;
887 continue;
888 }
889
890 out.push_str(line);
891 out.push('\n');
892 }
893
894 if changed {
895 let lock = lock_path_for_ref(&packed_path); let abs_lock = fs::canonicalize(git_dir)
903 .map(|d| d.join("packed-refs.lock"))
904 .unwrap_or_else(|_| lock.clone());
905 let timeout_ms = ConfigSet::load(Some(git_dir), true)
909 .ok()
910 .and_then(|cfg| cfg.get("core.packedrefstimeout"))
911 .and_then(|v| v.trim().parse::<i64>().ok())
912 .unwrap_or(0);
913 let deadline =
914 std::time::Instant::now() + std::time::Duration::from_millis(timeout_ms.max(0) as u64);
915 loop {
916 match std::fs::OpenOptions::new()
917 .write(true)
918 .create_new(true)
919 .open(&lock)
920 {
921 Ok(_) => break,
922 Err(e) if e.kind() == io::ErrorKind::AlreadyExists => {
923 if timeout_ms > 0 && std::time::Instant::now() < deadline {
924 std::thread::sleep(std::time::Duration::from_millis(50));
925 continue;
926 }
927 return Err(Error::Message(format!(
928 "Unable to create '{}': File exists.",
929 abs_lock.display()
930 )));
931 }
932 Err(e) => return Err(Error::Io(e)),
933 }
934 }
935
936 let tmp = packed_path.with_extension("new");
937 let mut created_tmp = false;
938 let write_result = (|| -> Result<()> {
939 let mut file = std::fs::OpenOptions::new()
940 .write(true)
941 .create_new(true)
942 .open(&tmp)
943 .map_err(Error::Io)?;
944 created_tmp = true;
945 use std::io::Write as _;
946 file.write_all(out.as_bytes()).map_err(Error::Io)?;
947 drop(file);
948 fs::rename(&tmp, &packed_path).map_err(Error::Io)?;
949 created_tmp = false; Ok(())
951 })();
952
953 let _ = fs::remove_file(&lock);
956 if write_result.is_err() && created_tmp {
957 let _ = fs::remove_file(&tmp);
958 }
959 write_result?;
960 }
961
962 Ok(())
963}
964
965pub fn read_head(git_dir: &Path) -> Result<Option<String>> {
973 match read_ref_file(&git_dir.join("HEAD"))? {
974 Ref::Symbolic(target) => Ok(Some(target)),
975 Ref::Direct(_) => Ok(None),
976 }
977}
978
979pub fn read_symbolic_ref(git_dir: &Path, refname: &str) -> Result<Option<String>> {
986 if crate::reftable::is_reftable_repo(git_dir) {
987 return crate::reftable::reftable_read_symbolic_ref(git_dir, refname);
988 }
989 let (store, stor_name) = crate::worktree_ref::resolve_ref_storage(git_dir, refname);
990 let storage_owned = crate::ref_namespace::storage_ref_name(&stor_name);
991 let try_names: Vec<&str> =
992 if stor_name == "HEAD" && crate::ref_namespace::ref_storage_prefix().is_some() {
993 vec![storage_owned.as_str()]
994 } else if storage_owned != stor_name {
995 vec![storage_owned.as_str(), stor_name.as_str()]
996 } else {
997 vec![stor_name.as_str()]
998 };
999
1000 for name in try_names {
1001 let path = store.join(name);
1002 match read_ref_file(&path) {
1003 Ok(Ref::Symbolic(target)) => return Ok(Some(target)),
1004 Ok(Ref::Direct(_)) => return Ok(None),
1005 Err(Error::Io(ref e))
1006 if e.kind() == io::ErrorKind::NotFound
1007 || e.kind() == io::ErrorKind::NotADirectory
1008 || e.kind() == io::ErrorKind::IsADirectory => {}
1009 Err(e) => return Err(e),
1010 }
1011 }
1012
1013 Ok(None)
1014}
1015
1016#[derive(Clone, Copy, Debug, PartialEq, Eq)]
1018pub enum LogRefsConfig {
1019 Unset,
1021 None,
1023 Normal,
1025 Always,
1027}
1028
1029pub fn read_log_refs_config(git_dir: &Path) -> LogRefsConfig {
1033 let config_dir = common_dir(git_dir).unwrap_or_else(|| git_dir.to_path_buf());
1034 let config_path = config_dir.join("config");
1035 let content = match fs::read_to_string(config_path) {
1036 Ok(c) => c,
1037 Err(_) => return LogRefsConfig::Unset,
1038 };
1039
1040 let mut in_core = false;
1041 for line in content.lines() {
1042 let trimmed = line.trim();
1043 if trimmed.starts_with('[') {
1044 in_core = trimmed.to_ascii_lowercase().starts_with("[core]");
1045 continue;
1046 }
1047 if !in_core {
1048 continue;
1049 }
1050 let Some((key, value)) = trimmed.split_once('=') else {
1051 continue;
1052 };
1053 if !key.trim().eq_ignore_ascii_case("logallrefupdates") {
1054 continue;
1055 }
1056 let v = value.trim();
1057 let lower = v.to_ascii_lowercase();
1058 return match lower.as_str() {
1059 "always" => LogRefsConfig::Always,
1060 "1" | "true" | "yes" | "on" => LogRefsConfig::Normal,
1061 "0" | "false" | "no" | "off" | "never" => LogRefsConfig::None,
1062 _ => LogRefsConfig::Unset,
1063 };
1064 }
1065 LogRefsConfig::Unset
1066}
1067
1068fn read_core_bare(git_dir: &Path) -> bool {
1069 let config_dir = common_dir(git_dir).unwrap_or_else(|| git_dir.to_path_buf());
1070 let config_path = config_dir.join("config");
1071 let Ok(content) = fs::read_to_string(config_path) else {
1072 return false;
1073 };
1074 let mut in_core = false;
1075 for line in content.lines() {
1076 let trimmed = line.trim();
1077 if trimmed.starts_with('[') {
1078 in_core = trimmed.to_ascii_lowercase().starts_with("[core]");
1079 continue;
1080 }
1081 if !in_core {
1082 continue;
1083 }
1084 let Some((key, value)) = trimmed.split_once('=') else {
1085 continue;
1086 };
1087 if key.trim().eq_ignore_ascii_case("bare") {
1088 let v = value.trim().to_ascii_lowercase();
1089 return matches!(v.as_str(), "1" | "true" | "yes" | "on");
1090 }
1091 }
1092 false
1093}
1094
1095pub fn effective_log_refs_config(git_dir: &Path) -> LogRefsConfig {
1097 match read_log_refs_config(git_dir) {
1098 LogRefsConfig::Unset => {
1099 if read_core_bare(git_dir) {
1100 LogRefsConfig::None
1101 } else {
1102 LogRefsConfig::Normal
1103 }
1104 }
1105 other => other,
1106 }
1107}
1108
1109#[must_use]
1112pub fn should_autocreate_reflog_for_mode(refname: &str, mode: LogRefsConfig) -> bool {
1113 match mode {
1114 LogRefsConfig::Always => true,
1115 LogRefsConfig::Normal => {
1116 refname == "HEAD"
1117 || refname.starts_with("refs/heads/")
1118 || refname.starts_with("refs/remotes/")
1119 || refname.starts_with("refs/notes/")
1120 }
1121 LogRefsConfig::None | LogRefsConfig::Unset => false,
1122 }
1123}
1124
1125#[must_use]
1127pub fn should_autocreate_reflog(git_dir: &Path, refname: &str) -> bool {
1128 should_autocreate_reflog_for_mode(refname, effective_log_refs_config(git_dir))
1129}
1130
1131fn clear_conflicting_reflog_files(logs_root: &Path, target: &Path) {
1157 let Ok(rel) = target.strip_prefix(logs_root) else {
1158 return;
1159 };
1160 let mut cur = logs_root.to_path_buf();
1161 for component in rel.components() {
1162 cur.push(component);
1163 match fs::symlink_metadata(&cur) {
1164 Ok(meta) if meta.file_type().is_dir() => {}
1165 Ok(_) => {
1166 let _ = fs::remove_file(&cur);
1169 }
1170 Err(_) => break, }
1172 }
1173}
1174
1175pub fn append_reflog(
1176 git_dir: &Path,
1177 refname: &str,
1178 old_oid: &ObjectId,
1179 new_oid: &ObjectId,
1180 identity: &str,
1181 message: &str,
1182 force_create: bool,
1183) -> Result<()> {
1184 if crate::reftable::is_reftable_repo(git_dir) {
1185 return crate::reftable::reftable_append_reflog(
1186 git_dir,
1187 refname,
1188 old_oid,
1189 new_oid,
1190 identity,
1191 message,
1192 force_create,
1193 );
1194 }
1195 let storage_dir = ref_storage_dir(git_dir, refname);
1196 let stor = crate::ref_namespace::storage_ref_name(refname);
1197 let log_path = storage_dir.join("logs").join(&stor);
1198 let may_create = force_create || should_autocreate_reflog(git_dir, refname);
1199 if !may_create && !log_path.exists() {
1200 return Ok(());
1201 }
1202 if let Some(parent) = log_path.parent() {
1203 let logs_root = storage_dir.join("logs");
1208 clear_conflicting_reflog_files(&logs_root, parent);
1209 fs::create_dir_all(parent)?;
1210 }
1211 let line = if message.is_empty() {
1212 format!("{old_oid} {new_oid} {identity}\n")
1213 } else {
1214 format!("{old_oid} {new_oid} {identity}\t{message}\n")
1215 };
1216 let mut file = fs::OpenOptions::new()
1217 .create(true)
1218 .append(true)
1219 .open(&log_path)?;
1220 use io::Write;
1221 file.write_all(line.as_bytes())?;
1222 Ok(())
1223}
1224
1225#[must_use]
1231pub fn reflog_file_path(git_dir: &Path, refname: &str) -> PathBuf {
1232 let (store, stor_name) = crate::worktree_ref::resolve_ref_storage(git_dir, refname);
1233 store.join("logs").join(stor_name)
1234}
1235
1236fn ref_storage_dir(git_dir: &Path, refname: &str) -> PathBuf {
1237 crate::worktree_ref::resolve_ref_storage(git_dir, refname).0
1238}
1239
1240fn normalize_list_refs_prefix(git_dir: &Path, prefix: &str) -> String {
1249 if prefix.is_empty() {
1250 return String::new();
1251 }
1252 if prefix.ends_with('/') {
1253 return prefix.to_string();
1254 }
1255 let candidate = ref_storage_dir(git_dir, prefix).join(prefix);
1256 if candidate.is_file() {
1257 prefix.to_string()
1258 } else {
1259 format!("{prefix}/")
1260 }
1261}
1262
1263pub fn list_refs(git_dir: &Path, prefix: &str) -> Result<Vec<(String, ObjectId)>> {
1273 let prefix_norm = normalize_list_refs_prefix(git_dir, prefix);
1274 let prefix = prefix_norm.as_str();
1275 if crate::reftable::is_reftable_repo(git_dir) {
1276 return crate::reftable::reftable_list_refs(git_dir, prefix);
1277 }
1278 let mut by_name: HashMap<String, ObjectId> = HashMap::new();
1282
1283 let stored_prefixes: Vec<String> = if let Some(ns) = crate::ref_namespace::ref_storage_prefix()
1284 {
1285 if prefix.starts_with("refs/namespaces/") {
1286 vec![prefix.to_owned()]
1287 } else if prefix.starts_with("refs/") {
1288 vec![format!("{ns}{prefix}")]
1289 } else {
1290 vec![prefix.to_owned()]
1291 }
1292 } else {
1293 vec![prefix.to_owned()]
1294 };
1295
1296 for stored_prefix in stored_prefixes {
1297 if let Some(cdir) = common_dir(git_dir) {
1298 if cdir != git_dir {
1299 collect_packed_refs_into_map(&cdir, &stored_prefix, false, &mut by_name)?;
1300 let cbase = cdir.join(&stored_prefix);
1301 collect_loose_refs_into_map(&cbase, &stored_prefix, &cdir, false, &mut by_name)?;
1302 }
1303 }
1304
1305 collect_packed_refs_into_map(git_dir, &stored_prefix, false, &mut by_name)?;
1306 let base = git_dir.join(&stored_prefix);
1307 collect_loose_refs_into_map(&base, &stored_prefix, git_dir, false, &mut by_name)?;
1308 }
1309
1310 let mut results: Vec<(String, ObjectId)> = by_name.into_iter().collect();
1311 if crate::worktree_ref::is_linked_worktree_git_dir(git_dir) {
1312 results.retain(|(name, _)| crate::worktree_ref::ref_visible_from_worktree(git_dir, name));
1313 }
1314 results.sort_by(|a, b| a.0.cmp(&b.0));
1315 Ok(results)
1316}
1317
1318pub fn resolve_ref_dwim(git_dir: &Path, spec: &str) -> (usize, Option<ObjectId>) {
1320 crate::worktree_ref::resolve_ref_dwim(|candidate| resolve_ref(git_dir, candidate).ok(), spec)
1321}
1322
1323pub fn list_refs_physical(git_dir: &Path, prefix: &str) -> Result<Vec<(String, ObjectId)>> {
1328 if crate::reftable::is_reftable_repo(git_dir) {
1329 return crate::reftable::reftable_list_refs(git_dir, prefix);
1330 }
1331 let mut by_name: HashMap<String, ObjectId> = HashMap::new();
1332 let stored_prefix = prefix.to_owned();
1333
1334 if let Some(cdir) = common_dir(git_dir) {
1335 if cdir != git_dir {
1336 collect_packed_refs_into_map(&cdir, &stored_prefix, true, &mut by_name)?;
1337 let cbase = cdir.join(&stored_prefix);
1338 collect_loose_refs_into_map(&cbase, &stored_prefix, &cdir, true, &mut by_name)?;
1339 }
1340 }
1341
1342 collect_packed_refs_into_map(git_dir, &stored_prefix, true, &mut by_name)?;
1343 let base = git_dir.join(&stored_prefix);
1344 collect_loose_refs_into_map(&base, &stored_prefix, git_dir, true, &mut by_name)?;
1345
1346 let mut results: Vec<(String, ObjectId)> = by_name.into_iter().collect();
1347 results.sort_by(|a, b| a.0.cmp(&b.0));
1348 Ok(results)
1349}
1350
1351pub fn collect_alternate_ref_oids(receiving_git_dir: &Path) -> Result<Vec<ObjectId>> {
1360 let config = ConfigSet::load(Some(receiving_git_dir), true)?;
1361 let objects_dir = receiving_git_dir.join("objects");
1362 let alternates = pack::read_alternates_recursive(&objects_dir).unwrap_or_default();
1363 let mut out = Vec::new();
1364 let mut seen = std::collections::HashSet::new();
1365 for alt_objects in alternates {
1366 let Some(alt_git_dir) = alt_objects.parent().map(PathBuf::from) else {
1367 continue;
1368 };
1369 if !alt_git_dir.join("refs").is_dir() {
1370 continue;
1371 }
1372 if let Some(prefixes) = config
1373 .get("core.alternaterefsprefixes")
1374 .or_else(|| config.get("core.alternateRefsPrefixes"))
1375 {
1376 for part in prefixes.split_whitespace() {
1377 if let Ok(oid) = resolve_ref(&alt_git_dir, part) {
1378 if seen.insert(oid) {
1379 out.push(oid);
1380 }
1381 continue;
1382 }
1383 for (_, oid) in list_refs(&alt_git_dir, part)? {
1384 if seen.insert(oid) {
1385 out.push(oid);
1386 }
1387 }
1388 }
1389 } else {
1390 for (_, oid) in list_refs(&alt_git_dir, "refs/")? {
1391 if seen.insert(oid) {
1392 out.push(oid);
1393 }
1394 }
1395 }
1396 }
1397 Ok(out)
1398}
1399
1400pub fn list_refs_glob(git_dir: &Path, pattern: &str) -> Result<Vec<(String, ObjectId)>> {
1402 let glob_pos = pattern.find(['*', '?', '[']);
1403 let prefix_owned: String = match glob_pos {
1404 Some(pos) => match pattern[..pos].rfind('/') {
1405 Some(slash) => pattern[..=slash].to_owned(),
1406 None => String::new(),
1407 },
1408 None => {
1409 let mut p = pattern.trim_end_matches('/').to_owned();
1410 if !p.is_empty() {
1411 p.push('/');
1412 }
1413 p
1414 }
1415 };
1416 let prefix = prefix_owned.as_str();
1417 let all = list_refs(git_dir, prefix)?;
1418 let mut results = Vec::new();
1419 for (refname, oid) in all {
1420 if ref_matches_glob(&refname, pattern) {
1421 results.push((refname, oid));
1422 }
1423 }
1424 Ok(results)
1425}
1426
1427pub fn ref_matches_glob(refname: &str, pattern: &str) -> bool {
1431 if !pattern.contains('*') && !pattern.contains('?') && !pattern.contains('[') {
1433 return refname == pattern
1434 || refname.ends_with(&format!("/{pattern}"))
1435 || refname.starts_with(&format!("{pattern}/"));
1436 }
1437 glob_match(pattern, refname)
1438}
1439
1440fn glob_match(pattern: &str, text: &str) -> bool {
1441 let pat = pattern.as_bytes();
1442 let txt = text.as_bytes();
1443 let (mut pi, mut ti) = (0, 0);
1444 let (mut star_pi, mut star_ti) = (usize::MAX, 0);
1445 while ti < txt.len() {
1446 if pi < pat.len() && (pat[pi] == b'?' || pat[pi] == txt[ti]) {
1447 pi += 1;
1448 ti += 1;
1449 } else if pi < pat.len() && pat[pi] == b'*' {
1450 star_pi = pi;
1451 star_ti = ti;
1452 pi += 1;
1453 } else if star_pi != usize::MAX {
1454 pi = star_pi + 1;
1455 star_ti += 1;
1456 ti = star_ti;
1457 } else {
1458 return false;
1459 }
1460 }
1461 while pi < pat.len() && pat[pi] == b'*' {
1462 pi += 1;
1463 }
1464 pi == pat.len()
1465}
1466
1467fn loose_ref_file_direct_oid(path: &Path) -> Option<ObjectId> {
1469 let content = fs::read_to_string(path).ok()?;
1470 let content = content.trim_end_matches('\n').trim();
1471 if content.len() == 40 && content.chars().all(|c| c.is_ascii_hexdigit()) {
1472 content.parse().ok()
1473 } else {
1474 None
1475 }
1476}
1477
1478fn collect_loose_refs_into_map(
1479 dir: &Path,
1480 prefix: &str,
1481 resolve_git_dir: &Path,
1482 physical_keys: bool,
1483 out: &mut HashMap<String, ObjectId>,
1484) -> Result<()> {
1485 let read = match fs::read_dir(dir) {
1486 Ok(r) => r,
1487 Err(e)
1488 if e.kind() == io::ErrorKind::NotFound
1489 || e.kind() == io::ErrorKind::NotADirectory
1490 || e.raw_os_error() == Some(libc::ENOTDIR) =>
1491 {
1492 return Ok(());
1493 }
1494 Err(e) => return Err(Error::Io(e)),
1495 };
1496
1497 for entry in read {
1498 let entry = entry?;
1499 let name = entry.file_name();
1500 let name_str = name.to_string_lossy();
1501 let refname = format!("{prefix}{name_str}");
1502 let path = entry.path();
1503 let meta = match fs::metadata(&path) {
1504 Ok(m) => m,
1505 Err(_) => continue,
1506 };
1507
1508 if meta.is_dir() {
1509 collect_loose_refs_into_map(
1510 &path,
1511 &format!("{refname}/"),
1512 resolve_git_dir,
1513 physical_keys,
1514 out,
1515 )?;
1516 } else if meta.is_file() {
1517 if physical_keys {
1518 if let Some(oid) = loose_ref_file_direct_oid(&path) {
1519 out.insert(refname, oid);
1520 } else if let Ok(Ref::Symbolic(target)) = read_ref_file(&path) {
1521 if let Ok(oid) = resolve_ref(resolve_git_dir, target.trim()) {
1522 out.insert(refname, oid);
1523 }
1524 }
1525 } else {
1526 let logical = crate::ref_namespace::logical_ref_name_from_storage(&refname)
1527 .unwrap_or_else(|| refname.clone());
1528 if let Ok(oid) = resolve_ref(resolve_git_dir, &logical) {
1529 out.insert(logical, oid);
1530 }
1531 }
1532 }
1533 }
1534 Ok(())
1535}
1536
1537pub fn resolve_at_n_branch(git_dir: &Path, spec: &str) -> Result<String> {
1540 let inner = spec
1542 .strip_prefix("@{-")
1543 .and_then(|s| s.strip_suffix('}'))
1544 .ok_or_else(|| Error::InvalidRef(format!("not an @{{-N}} ref: {spec}")))?;
1545 let n: usize = inner
1546 .parse()
1547 .map_err(|_| Error::InvalidRef(format!("invalid N in {spec}")))?;
1548 if n == 0 {
1549 return Err(Error::InvalidRef("@{-0} is not valid".to_string()));
1550 }
1551 let entries = crate::reflog::read_reflog(git_dir, "HEAD")?;
1552 let mut count = 0usize;
1553 for entry in entries.iter().rev() {
1554 let msg = &entry.message;
1555 if let Some(rest) = msg.strip_prefix("checkout: moving from ") {
1556 count += 1;
1557 if count == n {
1558 if let Some(to_pos) = rest.find(" to ") {
1559 return Ok(rest[..to_pos].to_string());
1560 }
1561 }
1562 }
1563 }
1564 Err(Error::InvalidRef(format!(
1565 "{spec}: only {count} checkout(s) in reflog"
1566 )))
1567}
1568
1569fn ref_name_matches_list_prefix(refname: &str, prefix: &str) -> bool {
1570 if refname.starts_with(prefix) {
1571 return true;
1572 }
1573 if prefix.ends_with('/') {
1574 let trimmed = prefix.trim_end_matches('/');
1575 if refname == trimmed {
1576 return true;
1577 }
1578 }
1579 false
1580}
1581
1582fn collect_packed_refs_into_map(
1583 git_dir: &Path,
1584 prefix: &str,
1585 physical_keys: bool,
1586 out: &mut HashMap<String, ObjectId>,
1587) -> Result<()> {
1588 let packed_path = git_dir.join("packed-refs");
1589 let content = match fs::read_to_string(&packed_path) {
1590 Ok(c) => c,
1591 Err(e) if e.kind() == io::ErrorKind::NotFound => return Ok(()),
1592 Err(e) => return Err(Error::Io(e)),
1593 };
1594
1595 for line in content.lines() {
1596 if line.starts_with('#') || line.starts_with('^') || line.is_empty() {
1597 continue;
1598 }
1599 let mut parts = line.splitn(2, ' ');
1600 let hash = parts.next().unwrap_or("");
1601 let refname = parts.next().unwrap_or("").trim();
1602 if !ref_name_matches_list_prefix(refname, prefix) || hash.len() != 40 {
1603 continue;
1604 }
1605 let oid: ObjectId = hash.parse()?;
1606 let key = if physical_keys {
1607 refname.to_owned()
1608 } else {
1609 crate::ref_namespace::logical_ref_name_from_storage(refname)
1610 .unwrap_or_else(|| refname.to_owned())
1611 };
1612 out.insert(key, oid);
1613 }
1614 Ok(())
1615}
1616
1617#[cfg(test)]
1618mod refname_available_tests {
1619 use super::*;
1620 use std::collections::{BTreeSet, HashSet};
1621 use tempfile::tempdir;
1622
1623 #[test]
1624 fn loose_parent_blocks_child_create() {
1625 let dir = tempdir().unwrap();
1626 let git_dir = dir.path();
1627 fs::create_dir_all(git_dir.join("refs/1l")).unwrap();
1628 fs::write(
1629 git_dir.join("refs/1l/c"),
1630 "67bf698f3ab735e92fb011a99cff3497c44d30c1\n",
1631 )
1632 .unwrap();
1633 assert_eq!(
1634 read_raw_ref(git_dir, "refs/1l/c").unwrap(),
1635 RawRefLookup::Exists
1636 );
1637 let extras = BTreeSet::from([
1638 "refs/1l/b".to_string(),
1639 "refs/1l/c/x".to_string(),
1640 "refs/1l/d".to_string(),
1641 ]);
1642 let skip = HashSet::new();
1643 let err = verify_refname_available_for_create(git_dir, "refs/1l/c/x", &extras, &skip)
1644 .unwrap_err();
1645 assert!(matches!(
1646 err,
1647 RefnameUnavailable::AncestorExists { ref blocking, .. } if blocking == "refs/1l/c"
1648 ));
1649 }
1650
1651 #[test]
1652 fn verify_sees_loose_ref_after_canonical_git_dir() {
1653 let dir = tempdir().unwrap();
1654 let git_dir = dir.path().join(".git");
1655 fs::create_dir_all(git_dir.join("refs/1l")).unwrap();
1656 fs::write(
1657 git_dir.join("refs/1l/c"),
1658 "67bf698f3ab735e92fb011a99cff3497c44d30c1\n",
1659 )
1660 .unwrap();
1661 let skip = HashSet::new();
1662 let extras = BTreeSet::new();
1663 let err = verify_refname_available_for_create(&git_dir, "refs/1l/c/x", &extras, &skip)
1664 .unwrap_err();
1665 assert!(matches!(
1666 err,
1667 RefnameUnavailable::AncestorExists { ref blocking, .. } if blocking == "refs/1l/c"
1668 ));
1669 }
1670
1671 #[test]
1672 fn list_refs_finds_sibling_under_parent_directory() {
1673 let dir = tempdir().unwrap();
1674 let git_dir = dir.path();
1675 fs::create_dir_all(git_dir.join("refs/ns/p")).unwrap();
1676 fs::write(
1677 git_dir.join("refs/ns/p/x"),
1678 "67bf698f3ab735e92fb011a99cff3497c44d30c1\n",
1679 )
1680 .unwrap();
1681 let listed = list_refs(git_dir, "refs/ns/p/").unwrap();
1682 assert!(
1683 listed.iter().any(|(n, _)| n == "refs/ns/p/x"),
1684 "got {listed:?}"
1685 );
1686 }
1687
1688 #[test]
1689 fn verify_blocks_parent_when_child_ref_exists() {
1690 let dir = tempdir().unwrap();
1691 let git_dir = dir.path();
1692 fs::create_dir_all(git_dir.join("refs/ns/p")).unwrap();
1693 fs::write(
1694 git_dir.join("refs/ns/p/x"),
1695 "67bf698f3ab735e92fb011a99cff3497c44d30c1\n",
1696 )
1697 .unwrap();
1698 let extras = BTreeSet::from(["refs/ns/p".to_string()]);
1699 let skip = HashSet::new();
1700 let err =
1701 verify_refname_available_for_create(git_dir, "refs/ns/p", &extras, &skip).unwrap_err();
1702 assert!(matches!(
1703 err,
1704 RefnameUnavailable::DescendantExists { ref blocking, .. }
1705 if blocking == "refs/ns/p/x"
1706 ));
1707 }
1708
1709 #[test]
1710 fn verify_blocks_parent_git_style_nested_path() {
1711 let dir = tempdir().unwrap();
1712 let git_dir = dir.path();
1713 fs::create_dir_all(git_dir.join("refs/3l/c")).unwrap();
1714 fs::write(
1715 git_dir.join("refs/3l/c/x"),
1716 "67bf698f3ab735e92fb011a99cff3497c44d30c1\n",
1717 )
1718 .unwrap();
1719 let extras = BTreeSet::from(["refs/3l/c".to_string()]);
1720 let skip = HashSet::new();
1721 let err =
1722 verify_refname_available_for_create(git_dir, "refs/3l/c", &extras, &skip).unwrap_err();
1723 assert!(matches!(
1724 err,
1725 RefnameUnavailable::DescendantExists { ref blocking, .. }
1726 if blocking == "refs/3l/c/x"
1727 ));
1728 }
1729
1730 #[test]
1731 fn intermediate_directory_does_not_block_nested_create() {
1732 let dir = tempdir().unwrap();
1733 let git_dir = dir.path();
1734 fs::create_dir_all(git_dir.join("refs/ns")).unwrap();
1735 fs::write(
1736 git_dir.join("refs/ns/existing"),
1737 "67bf698f3ab735e92fb011a99cff3497c44d30c1\n",
1738 )
1739 .unwrap();
1740 assert_eq!(
1741 read_raw_ref(git_dir, "refs/ns").unwrap(),
1742 RawRefLookup::IsDirectory
1743 );
1744 let extras = BTreeSet::from(["refs/ns/newchild".to_string()]);
1745 let skip = HashSet::new();
1746 verify_refname_available_for_create(git_dir, "refs/ns/newchild", &extras, &skip).unwrap();
1747 }
1748}
1749
1750#[cfg(test)]
1751mod read_raw_ref_tests {
1752 use super::*;
1753 use tempfile::tempdir;
1754
1755 #[test]
1756 fn loose_ref_file_is_exists() {
1757 let dir = tempdir().unwrap();
1758 let git_dir = dir.path();
1759 fs::create_dir_all(git_dir.join("refs/heads")).unwrap();
1760 fs::write(
1761 git_dir.join("refs/heads/side"),
1762 "0000000000000000000000000000000000000000\n",
1763 )
1764 .unwrap();
1765 assert_eq!(
1766 read_raw_ref(git_dir, "refs/heads/side").unwrap(),
1767 RawRefLookup::Exists
1768 );
1769 }
1770
1771 #[test]
1772 fn missing_ref_is_not_found() {
1773 let dir = tempdir().unwrap();
1774 let git_dir = dir.path();
1775 fs::create_dir_all(git_dir.join("refs/heads")).unwrap();
1776 assert_eq!(
1777 read_raw_ref(git_dir, "refs/heads/nope").unwrap(),
1778 RawRefLookup::NotFound
1779 );
1780 }
1781
1782 #[test]
1783 fn directory_where_ref_expected_is_is_directory() {
1784 let dir = tempdir().unwrap();
1785 let git_dir = dir.path();
1786 fs::create_dir_all(git_dir.join("refs/heads")).unwrap();
1787 assert_eq!(
1788 read_raw_ref(git_dir, "refs/heads").unwrap(),
1789 RawRefLookup::IsDirectory
1790 );
1791 }
1792
1793 #[test]
1794 fn packed_ref_name_is_exists() {
1795 let dir = tempdir().unwrap();
1796 let git_dir = dir.path();
1797 fs::write(
1798 git_dir.join("packed-refs"),
1799 "# pack-refs with: peeled fully-peeled \n\
1800 0000000000000000000000000000000000000000 refs/heads/packed\n",
1801 )
1802 .unwrap();
1803 assert_eq!(
1804 read_raw_ref(git_dir, "refs/heads/packed").unwrap(),
1805 RawRefLookup::Exists
1806 );
1807 }
1808}