1use collections::HashSet;
2use rand::Rng;
3
4const ADJECTIVES: &[&str] = &[
5 "able", "agate", "airy", "alpine", "amber", "ample", "aqua", "arctic", "arid", "ashen",
6 "astral", "autumn", "avid", "balmy", "birch", "bold", "boreal", "brave", "breezy", "brief",
7 "bright", "brisk", "broad", "bronze", "calm", "cerith", "cheery", "civil", "clean", "clear",
8 "clever", "cobalt", "cool", "copper", "coral", "cozy", "crisp", "cubic", "cyan", "deft",
9 "dense", "dewy", "direct", "dusky", "dusty", "early", "earnest", "earthy", "elder", "elfin",
10 "equal", "even", "exact", "faint", "fair", "fast", "fawn", "ferny", "fiery", "fine", "firm",
11 "fleet", "floral", "focal", "fond", "frank", "fresh", "frosty", "full", "gentle", "gilded",
12 "glacial", "glad", "glossy", "golden", "grand", "green", "gusty", "hale", "happy", "hardy",
13 "hazel", "hearty", "hilly", "humble", "hushed", "icy", "ideal", "inky", "iron", "ivory",
14 "jade", "jovial", "keen", "kind", "lapis", "leafy", "level", "light", "lilac", "limber",
15 "lively", "lofty", "loyal", "lucid", "lunar", "major", "maple", "marshy", "mellow", "merry",
16 "mild", "milky", "misty", "modest", "mossy", "muted", "narrow", "naval", "neat", "nimble",
17 "noble", "north", "novel", "oaken", "ochre", "olive", "onyx", "opal", "optic", "ornate",
18 "oval", "owed", "ozone", "pale", "pastel", "pearl", "pecan", "peppy", "pilot", "placid",
19 "plain", "plucky", "plum", "plush", "poised", "polar", "polished", "poplar", "prime", "proof",
20 "proud", "quartz", "quick", "quiet", "rainy", "rapid", "raspy", "ready", "regal", "roomy",
21 "rooted", "rosy", "round", "royal", "ruddy", "russet", "sage", "salty", "sandy", "satin",
22 "scenic", "sedge", "serene", "sheer", "silky", "silver", "sleek", "smart", "smooth", "snowy",
23 "snug", "solar", "solid", "south", "spry", "stark", "steady", "steel", "steep", "still",
24 "stocky", "stoic", "stony", "stout", "sturdy", "suede", "sunny", "supple", "sure", "tall",
25 "tangy", "tawny", "teal", "terse", "thick", "tidal", "tidy", "timber", "topaz", "total",
26 "trim", "tropic", "tulip", "upper", "urban", "vast", "velvet", "verde", "vivid", "vocal",
27 "warm", "waxen", "west", "whole", "wide", "wild", "wise", "witty", "woven", "young", "zealous",
28 "zephyr", "zesty", "zinc",
29];
30
31const NOUNS: &[&str] = &[
32 "acorn", "almond", "anvil", "apricot", "arbor", "atlas", "badge", "badger", "basin", "bay",
33 "beacon", "beam", "bell", "birch", "blade", "bloom", "bluff", "bobcat", "bolt", "breeze",
34 "bridge", "brook", "bunting", "burrow", "cabin", "cairn", "canyon", "cape", "cedar", "chasm",
35 "cliff", "clover", "coast", "cobble", "colt", "comet", "conch", "condor", "coral", "cove",
36 "coyote", "crane", "crater", "creek", "crest", "curlew", "daisy", "dale", "dawn", "den",
37 "dove", "drake", "drift", "drum", "dune", "dusk", "eagle", "eel", "egret", "elk", "emu",
38 "falcon", "fawn", "fennel", "fern", "ferret", "ferry", "fig", "finch", "fjord", "flicker",
39 "flint", "flower", "fox", "frost", "gale", "garnet", "gate", "gazelle", "geyser", "glade",
40 "glen", "gorge", "granite", "grove", "gull", "harbor", "hare", "haven", "hawk", "hazel",
41 "heath", "hedge", "heron", "hill", "hollow", "horizon", "ibis", "inlet", "isle", "ivy",
42 "jackal", "jasper", "juniper", "kinglet", "kitten", "knoll", "lagoon", "lake", "lantern",
43 "larch", "lark", "laurel", "lava", "leaf", "ledge", "lily", "linden", "lodge", "loft", "loon",
44 "lotus", "mantle", "maple", "marble", "marsh", "marten", "meadow", "merlin", "mill", "minnow",
45 "moon", "moose", "moss", "moth", "newt", "north", "nutmeg", "oak", "oasis", "obsidian",
46 "orbit", "orchid", "oriole", "osprey", "otter", "owl", "palm", "panther", "pass", "peach",
47 "peak", "pebble", "pelican", "peony", "perch", "pier", "pike", "pine", "plover", "plume",
48 "pond", "poppy", "prairie", "prism", "quail", "quarry", "quartz", "rain", "rampart", "raven",
49 "ravine", "reed", "reef", "ridge", "river", "robin", "rook", "rowan", "sage", "salmon",
50 "sequoia", "shore", "shrew", "shrike", "sigma", "sky", "slope", "snipe", "snow", "sparrow",
51 "spruce", "stag", "star", "starling", "stoat", "stone", "stork", "storm", "strand", "summit",
52 "sycamore", "tern", "terrace", "thistle", "thorn", "thrush", "tide", "timber", "toucan",
53 "trail", "trout", "tulip", "tundra", "turtle", "vale", "valley", "veranda", "violet", "viper",
54 "vole", "walrus", "warbler", "willow", "wolf", "wren", "yak", "zenith",
55];
56
57/// Generates a worktree name in `"adjective-noun"` format (e.g. `"calm-river"`).
58///
59/// Tries up to 10 random combinations, skipping any name that already appears
60/// in `existing_names`. Returns `None` if no unused name is found.
61pub fn generate_worktree_name(existing_names: &[&str], rng: &mut impl Rng) -> Option<String> {
62 let existing: HashSet<&str> = existing_names.iter().copied().collect();
63
64 for _ in 0..10 {
65 let adjective = ADJECTIVES[rng.random_range(0..ADJECTIVES.len())];
66 let noun = NOUNS[rng.random_range(0..NOUNS.len())];
67 let name = format!("{adjective}-{noun}");
68
69 if !existing.contains(name.as_str()) {
70 return Some(name);
71 }
72 }
73
74 None
75}
76
77#[cfg(test)]
78mod tests {
79 use super::*;
80 use rand::rngs::StdRng;
81
82 #[gpui::test(iterations = 10)]
83 fn test_generate_worktree_name_format(mut rng: StdRng) {
84 let name = generate_worktree_name(&[], &mut rng).unwrap();
85 let (adjective, noun) = name.split_once('-').expect("name should contain a hyphen");
86 assert!(
87 ADJECTIVES.contains(&adjective),
88 "{adjective:?} is not in ADJECTIVES"
89 );
90 assert!(NOUNS.contains(&noun), "{noun:?} is not in NOUNS");
91 }
92
93 #[gpui::test(iterations = 100)]
94 fn test_generate_worktree_name_avoids_existing(mut rng: StdRng) {
95 let existing = &["swift-falcon", "calm-river", "bold-cedar"];
96 let name = generate_worktree_name(existing, &mut rng).unwrap();
97 for &branch in existing {
98 assert_ne!(
99 name, branch,
100 "generated name should not match an existing branch"
101 );
102 }
103 }
104
105 #[gpui::test]
106 fn test_generate_worktree_name_returns_none_when_stuck(mut rng: StdRng) {
107 let all_names: Vec<String> = ADJECTIVES
108 .iter()
109 .flat_map(|adj| NOUNS.iter().map(move |noun| format!("{adj}-{noun}")))
110 .collect();
111 let refs: Vec<&str> = all_names.iter().map(|s| s.as_str()).collect();
112 let result = generate_worktree_name(&refs, &mut rng);
113 assert!(result.is_none());
114 }
115
116 #[test]
117 fn test_adjectives_are_valid() {
118 let mut seen = HashSet::default();
119 for &word in ADJECTIVES {
120 assert!(seen.insert(word), "duplicate entry in ADJECTIVES: {word:?}");
121 }
122
123 for window in ADJECTIVES.windows(2) {
124 assert!(
125 window[0] < window[1],
126 "ADJECTIVES is not sorted: {0:?} should come before {1:?}",
127 window[0],
128 window[1],
129 );
130 }
131
132 for &word in ADJECTIVES {
133 assert!(
134 !word.contains('-'),
135 "ADJECTIVES entry contains a hyphen: {word:?}"
136 );
137 assert!(
138 word.chars().all(|c| c.is_lowercase()),
139 "ADJECTIVES entry is not all lowercase: {word:?}"
140 );
141 }
142 }
143
144 #[test]
145 fn test_nouns_are_valid() {
146 let mut seen = HashSet::default();
147 for &word in NOUNS {
148 assert!(seen.insert(word), "duplicate entry in NOUNS: {word:?}");
149 }
150
151 for window in NOUNS.windows(2) {
152 assert!(
153 window[0] < window[1],
154 "NOUNS is not sorted: {0:?} should come before {1:?}",
155 window[0],
156 window[1],
157 );
158 }
159
160 for &word in NOUNS {
161 assert!(
162 !word.contains('-'),
163 "NOUNS entry contains a hyphen: {word:?}"
164 );
165 assert!(
166 word.chars().all(|c| c.is_lowercase()),
167 "NOUNS entry is not all lowercase: {word:?}"
168 );
169 }
170 }
171}