1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
//! Name validation utilities.
//!
//! This module provides [`validate_name`], the single point of truth for
//! checking whether a string can be used as a name in the `libvctrl`
//! ecosystem.
//!
//! # What makes a valid name?
//!
//! A valid name must:
//! - Not be empty.
//! - Not exceed [`MAX_NAME_LENGTH`] bytes (255 by default).
//! - Not contain the path separator `/`.
//! - Not be exactly `.` or `..` (to prevent directory traversal).
//!
//! These rules are deliberately strict and conservative. A name that
//! passes `validate_name` is safe to use as a file name, a tree entry
//! name, a reference name, or a tag name without risking path traversal
//! or filesystem corruption.
//!
//! # Why these restrictions?
//!
//! - **No `/`** – prevents path injection. A name like `"../../etc/passwd"`
//! could trick a naive backend into writing files outside the
//! repository.
//! - **No `.` or `..`** – prevents ambiguity in directory traversal.
//! These are special directory entries on all major operating systems.
//! - **Length limit** – prevents denial‑of‑service via memory exhaustion.
//!
//! # When to use
//!
//! Call `validate_name` whenever you have a raw string that will be
//! used as a name in any object. The constructors in `libvctrl_handler`
//! already call this function (or equivalent validation), so if you are
//! using those constructors you do not need to call it separately.
//!
//! This function is exposed for cases where you need to validate names
//! in custom code, or when building components that accept names as
//! raw strings before passing them to constructors.
//!
//! # Example
//!
//! ```rust
//! use libvctrl_core::validate::name::validate_name;
//!
//! // Valid names
//! assert!(validate_name("hello").is_ok());
//! assert!(validate_name("README.md").is_ok());
//! assert!(validate_name("refs-heads-main").is_ok()); // '/' is not allowed
//!
//! // Invalid names
//! assert!(validate_name("").is_err()); // empty
//! assert!(validate_name(&"a".repeat(300)).is_err()); // too long
//! assert!(validate_name("src/main.rs").is_err()); // contains '/'
//! assert!(validate_name("..").is_err()); // is '..'
//! assert!(validate_name(".").is_err()); // is '.'
//! ```
use ;
/// Validates a name according to the fundamental contracts.
///
/// A valid name:
/// - Is not empty.
/// - Does not exceed `MAX_NAME_LENGTH` bytes.
/// - Does not contain the path separator `/`.
/// - Is not `.` or `..`.
///
/// This function is the single point of truth for name validation in
/// `libvctrl_core`. All higher-level modules (builders, stores) call it
/// before constructing objects that carry a name.
///
/// # Errors
/// Returns [`VctrlError::InvalidName`] with a descriptive message.
///
/// # Examples
/// ```
/// use libvctrl_core::validate::name::validate_name;
/// assert!(validate_name("hello").is_ok());
/// assert!(validate_name("").is_err());
/// assert!(validate_name(&"a".repeat(300)).is_err());
/// assert!(validate_name("src/main.rs").is_err()); // contains '/'
/// assert!(validate_name("..").is_err());
/// ```