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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
//! Version detection from Git tags for changelog generation.
//!
//! **What**: Provides functionality to detect and parse version tags from Git repositories,
//! supporting both monorepo (per-package tags) and single-package (root tags) scenarios.
//!
//! **How**: This module parses Git tags according to configurable formats, extracts version
//! information, and identifies the previous version for changelog generation. It supports
//! custom tag formats with placeholders for package names and versions.
//!
//! **Why**: To automatically detect version boundaries for changelog generation, enabling
//! the system to determine which commits belong to which version without manual intervention.
//!
//! # Tag Format Support
//!
//! This module supports two types of tag formats:
//!
//! ## Monorepo Tags
//!
//! Format: `{name}@{version}` (configurable via `version_tag_format`)
//! - Example: `@myorg/utils@1.2.3`
//! - Example: `pkg-core@2.0.0`
//!
//! ## Root Tags
//!
//! Format: `v{version}` (configurable via `root_tag_format`)
//! - Example: `v1.2.3`
//! - Example: `1.0.0` (when format is `{version}`)
//!
//! # Examples
//!
//! ```rust,ignore
//! use sublime_pkg_tools::changelog::version_detection::{VersionTag, parse_version_tag};
//!
//! // Parse monorepo tag
//! let tag = parse_version_tag("@myorg/utils@1.2.3", Some("@myorg/utils"), "{name}@{version}");
//! assert!(tag.is_some());
//! let tag = tag.unwrap();
//! assert_eq!(tag.version().to_string(), "1.2.3");
//! assert_eq!(tag.package_name(), Some("@myorg/utils"));
//!
//! // Parse root tag
//! let tag = parse_version_tag("v1.2.3", None, "v{version}");
//! assert!(tag.is_some());
//! let tag = tag.unwrap();
//! assert_eq!(tag.version().to_string(), "1.2.3");
//! assert_eq!(tag.package_name(), None);
//! ```
use crate;
use crateVersion;
use Regex;
use Ordering;
/// Represents a parsed version tag from Git.
///
/// A version tag contains a version number and optionally a package name
/// (for monorepo scenarios). This structure allows comparing tags and
/// determining version ordering.
///
/// # Examples
///
/// ```rust,ignore
/// use sublime_pkg_tools::changelog::version_detection::VersionTag;
/// use sublime_pkg_tools::types::Version;
///
/// let tag = VersionTag::new(
/// "mypackage@1.0.0".to_string(),
/// Version::parse("1.0.0").unwrap(),
/// Some("mypackage".to_string()),
/// );
///
/// assert_eq!(tag.tag_name(), "mypackage@1.0.0");
/// assert_eq!(tag.version().to_string(), "1.0.0");
/// assert_eq!(tag.package_name(), Some("mypackage"));
/// ```
/// Parses a Git tag string into a `VersionTag`.
///
/// This function attempts to parse a tag according to the provided format template.
/// It supports two types of formats:
/// - Monorepo format with `{name}` and `{version}` placeholders
/// - Root format with `{version}` placeholder only
///
/// # Arguments
///
/// * `tag` - The Git tag string to parse
/// * `expected_package` - The expected package name for monorepo tags, or None for root tags
/// * `format` - The tag format template with placeholders
///
/// # Returns
///
/// Returns `Some(VersionTag)` if the tag matches the format and contains a valid version,
/// otherwise returns `None`.
///
/// # Examples
///
/// ```rust,ignore
/// use sublime_pkg_tools::changelog::version_detection::parse_version_tag;
///
/// // Parse monorepo tag
/// let tag = parse_version_tag("@myorg/utils@1.2.3", Some("@myorg/utils"), "{name}@{version}");
/// assert!(tag.is_some());
///
/// // Parse root tag
/// let tag = parse_version_tag("v1.2.3", None, "v{version}");
/// assert!(tag.is_some());
///
/// // Invalid tag
/// let tag = parse_version_tag("invalid", None, "v{version}");
/// assert!(tag.is_none());
/// ```
pub
/// Builds a regex pattern from a tag format template.
///
/// Converts placeholders to named capture groups:
/// - `{name}` -> `(?P<name>.+?)`
/// - `{version}` -> `(?P<version>\d+\.\d+\.\d+(?:-[a-zA-Z0-9.-]+)?(?:\+[a-zA-Z0-9.-]+)?)`
///
/// # Arguments
///
/// * `format` - The format template string
///
/// # Returns
///
/// Returns `Some(String)` containing the regex pattern, or `None` if the format is invalid.
///
/// # Examples
///
/// ```rust,ignore
/// use sublime_pkg_tools::changelog::version_detection::build_tag_regex;
///
/// let pattern = build_tag_regex("v{version}");
/// assert!(pattern.is_some());
///
/// let pattern = build_tag_regex("{name}@{version}");
/// assert!(pattern.is_some());
/// ```
pub
/// Finds all version tags in a list of Git tags.
///
/// Filters and parses tags according to the provided format, optionally filtering
/// by package name for monorepo scenarios.
///
/// # Arguments
///
/// * `tags` - List of Git tag strings
/// * `package_name` - Optional package name to filter monorepo tags
/// * `format` - The tag format template
///
/// # Returns
///
/// Returns a vector of `VersionTag` instances, sorted by version (newest first).
///
/// # Examples
///
/// ```rust,ignore
/// use sublime_pkg_tools::changelog::version_detection::find_version_tags;
///
/// let tags = vec!["v1.0.0", "v1.1.0", "v2.0.0", "other-tag"];
/// let version_tags = find_version_tags(&tags, None, "v{version}");
/// assert_eq!(version_tags.len(), 3);
/// assert_eq!(version_tags[0].version().to_string(), "2.0.0"); // Sorted newest first
/// ```
pub
/// Finds the previous version tag before a given version.
///
/// Searches through a list of tags to find the most recent version that is
/// less than the current version. This is used to determine the commit range
/// for changelog generation.
///
/// # Arguments
///
/// * `tags` - List of Git tag strings
/// * `current_version` - The current version to compare against
/// * `package_name` - Optional package name for monorepo filtering
/// * `format` - The tag format template
///
/// # Returns
///
/// Returns `Ok(Some(VersionTag))` if a previous version is found,
/// `Ok(None)` if this is the first version,
/// or an error if the current version is invalid.
///
/// # Errors
///
/// Returns an error if:
/// - The current version string cannot be parsed
///
/// # Examples
///
/// ```rust,ignore
/// use sublime_pkg_tools::changelog::version_detection::find_previous_version;
///
/// let tags = vec!["v1.0.0".to_string(), "v1.1.0".to_string(), "v2.0.0".to_string()];
/// let previous = find_previous_version(&tags, "2.0.0", None, "v{version}").unwrap();
/// assert!(previous.is_some());
/// assert_eq!(previous.unwrap().version().to_string(), "1.1.0");
///
/// // First version
/// let previous = find_previous_version(&tags, "1.0.0", None, "v{version}").unwrap();
/// assert!(previous.is_none());
/// ```
pub
/// Detects if a tag format is for monorepo (contains `{name}` placeholder).
///
/// # Arguments
///
/// * `format` - The tag format template
///
/// # Returns
///
/// Returns `true` if the format contains a `{name}` placeholder, `false` otherwise.
///
/// # Examples
///
/// ```rust,ignore
/// use sublime_pkg_tools::changelog::version_detection::is_monorepo_format;
///
/// assert!(is_monorepo_format("{name}@{version}"));
/// assert!(!is_monorepo_format("v{version}"));
/// ```
pub
/// Formats a version tag string from a package name and version.
///
/// This is the inverse of `parse_version_tag`, creating a tag string from
/// components according to the format template.
///
/// # Arguments
///
/// * `package_name` - Optional package name for monorepo tags
/// * `version` - The version string
/// * `format` - The tag format template
///
/// # Returns
///
/// Returns the formatted tag string.
///
/// # Examples
///
/// ```rust,ignore
/// use sublime_pkg_tools::changelog::version_detection::format_version_tag;
///
/// let tag = format_version_tag(Some("mypackage"), "1.0.0", "{name}@{version}");
/// assert_eq!(tag, "mypackage@1.0.0");
///
/// let tag = format_version_tag(None, "1.0.0", "v{version}");
/// assert_eq!(tag, "v1.0.0");
/// ```
pub