Newer
Older
dub_jkp / source / dub / dependency.d
@Mathias Lang Mathias Lang on 5 Feb 2024 37 KB Add opEquals/opCmp to PackageName
  1. /**
  2. Dependency specification functionality.
  3.  
  4. Copyright: © 2012-2013 Matthias Dondorff, © 2012-2016 Sönke Ludwig
  5. License: Subject to the terms of the MIT license, as written in the included LICENSE.txt file.
  6. Authors: Matthias Dondorff, Sönke Ludwig
  7. */
  8. module dub.dependency;
  9.  
  10. import dub.internal.vibecompat.data.json;
  11. import dub.internal.vibecompat.inet.path;
  12. import dub.semver;
  13.  
  14. import dub.internal.dyaml.stdsumtype;
  15.  
  16. import std.algorithm;
  17. import std.array;
  18. import std.exception;
  19. import std.string;
  20.  
  21. /// Represents a fully-qualified package name
  22. public struct PackageName
  23. {
  24. /// The underlying full name of the package
  25. private string fullName;
  26. /// Where the separator lies, if any
  27. private size_t separator;
  28.  
  29. /// Creates a new instance of this struct
  30. public this(string fn) @safe pure
  31. {
  32. this.fullName = fn;
  33. if (auto idx = fn.indexOf(':'))
  34. this.separator = idx > 0 ? idx : fn.length;
  35. else // We were given `:foo`
  36. assert(0, "Argument to PackageName constructor needs to be " ~
  37. "a fully qualified string");
  38. }
  39.  
  40. /// Private constructor to have nothrow / @nogc
  41. private this(string fn, size_t sep) @safe pure nothrow @nogc
  42. {
  43. this.fullName = fn;
  44. this.separator = sep;
  45. }
  46.  
  47. /// The base package name in which the subpackages may live
  48. public PackageName main () const return @safe pure nothrow @nogc
  49. {
  50. return PackageName(this.fullName[0 .. this.separator], this.separator);
  51. }
  52.  
  53. /// The subpackage name, or an empty string if there isn't
  54. public string sub () const return @safe pure nothrow @nogc
  55. {
  56. // Return `null` instead of an empty string so that
  57. // it can be used in a boolean context, e.g.
  58. // `if (name.sub)` would be true with empty string
  59. return this.separator < this.fullName.length
  60. ? this.fullName[this.separator + 1 .. $]
  61. : null;
  62. }
  63.  
  64. /// Human readable representation
  65. public string toString () const return scope @safe pure nothrow @nogc
  66. {
  67. return this.fullName;
  68. }
  69.  
  70. ///
  71. public int opCmp (in PackageName other) const scope @safe pure nothrow @nogc
  72. {
  73. import core.internal.string : dstrcmp;
  74. return dstrcmp(this.toString(), other.toString());
  75. }
  76.  
  77. ///
  78. public bool opEquals (in PackageName other) const scope @safe pure nothrow @nogc
  79. {
  80. return this.toString() == other.toString();
  81. }
  82. }
  83.  
  84. /** Encapsulates the name of a package along with its dependency specification.
  85. */
  86. struct PackageDependency {
  87. /// Backward compatibility
  88. deprecated("Use the constructor that accepts a `PackageName` as first argument")
  89. this(string n, Dependency s = Dependency.init) @safe pure
  90. {
  91. this.name = PackageName(n);
  92. this.spec = s;
  93. }
  94.  
  95. // Remove once deprecated overload is gone
  96. this(PackageName n, Dependency s = Dependency.init) @safe pure nothrow @nogc
  97. {
  98. this.name = n;
  99. this.spec = s;
  100. }
  101.  
  102. /// Name of the referenced package.
  103. PackageName name;
  104.  
  105. /// Dependency specification used to select a particular version of the package.
  106. Dependency spec;
  107. }
  108.  
  109. /**
  110. Represents a dependency specification.
  111.  
  112. A dependency specification either represents a specific version or version
  113. range, or a path to a package. In addition to that it has `optional` and
  114. `default_` flags to control how non-mandatory dependencies are handled. The
  115. package name is notably not part of the dependency specification.
  116. */
  117. struct Dependency {
  118. /// We currently support 3 'types'
  119. private alias Value = SumType!(VersionRange, NativePath, Repository);
  120.  
  121. /// Used by `toString`
  122. private static immutable string[] BooleanOptions = [ "optional", "default" ];
  123.  
  124. // Shortcut to create >=0.0.0
  125. private enum ANY_IDENT = "*";
  126.  
  127. private Value m_value;
  128. private bool m_optional;
  129. private bool m_default;
  130.  
  131. /// A Dependency, which matches every valid version.
  132. static @property Dependency any() @safe { return Dependency(VersionRange.Any); }
  133.  
  134. /// An invalid dependency (with no possible version matches).
  135. static @property Dependency invalid() @safe
  136. {
  137. return Dependency(VersionRange.Invalid);
  138. }
  139.  
  140. /** Constructs a new dependency specification that matches a specific
  141. path.
  142. */
  143. this(NativePath path) @safe
  144. {
  145. this.m_value = path;
  146. }
  147.  
  148. /** Constructs a new dependency specification that matches a specific
  149. Git reference.
  150. */
  151. this(Repository repository) @safe
  152. {
  153. this.m_value = repository;
  154. }
  155.  
  156. /** Constructs a new dependency specification from a string
  157.  
  158. See the `versionSpec` property for a description of the accepted
  159. contents of that string.
  160. */
  161. this(string spec) @safe
  162. {
  163. this(VersionRange.fromString(spec));
  164. }
  165.  
  166. /** Constructs a new dependency specification that matches a specific
  167. version.
  168. */
  169. this(const Version ver) @safe
  170. {
  171. this(VersionRange(ver, ver));
  172. }
  173.  
  174. /// Construct a version from a range of possible values
  175. this (VersionRange rng) @safe
  176. {
  177. this.m_value = rng;
  178. }
  179.  
  180. deprecated("Instantiate the `Repository` struct with the string directly")
  181. this(Repository repository, string spec) @safe
  182. {
  183. assert(repository.m_ref is null);
  184. repository.m_ref = spec;
  185. this(repository);
  186. }
  187.  
  188. /// If set, overrides any version based dependency selection.
  189. deprecated("Construct a new `Dependency` object instead")
  190. @property void path(NativePath value) @trusted
  191. {
  192. this.m_value = value;
  193. }
  194. /// ditto
  195. @property NativePath path() const @safe
  196. {
  197. return this.m_value.match!(
  198. (const NativePath p) => p,
  199. ( any ) => NativePath.init,
  200. );
  201. }
  202.  
  203. /// If set, overrides any version based dependency selection.
  204. deprecated("Construct a new `Dependency` object instead")
  205. @property void repository(Repository value) @trusted
  206. {
  207. this.m_value = value;
  208. }
  209. /// ditto
  210. @property Repository repository() const @safe
  211. {
  212. return this.m_value.match!(
  213. (const Repository p) => p,
  214. ( any ) => Repository.init,
  215. );
  216. }
  217.  
  218. /// Determines if the dependency is required or optional.
  219. @property bool optional() const scope @safe pure nothrow @nogc
  220. {
  221. return m_optional;
  222. }
  223. /// ditto
  224. @property void optional(bool optional) scope @safe pure nothrow @nogc
  225. {
  226. m_optional = optional;
  227. }
  228.  
  229. /// Determines if an optional dependency should be chosen by default.
  230. @property bool default_() const scope @safe pure nothrow @nogc
  231. {
  232. return m_default;
  233. }
  234. /// ditto
  235. @property void default_(bool value) scope @safe pure nothrow @nogc
  236. {
  237. m_default = value;
  238. }
  239.  
  240. /// Returns true $(I iff) the version range only matches a specific version.
  241. @property bool isExactVersion() const scope @safe
  242. {
  243. return this.m_value.match!(
  244. (NativePath v) => false,
  245. (Repository v) => false,
  246. (VersionRange v) => v.isExactVersion(),
  247. );
  248. }
  249.  
  250. /// Returns the exact version matched by the version range.
  251. @property Version version_() const @safe {
  252. auto range = this.m_value.match!(
  253. // Can be simplified to `=> assert(0)` once we drop support for v2.096
  254. (NativePath p) { int dummy; if (dummy) return VersionRange.init; assert(0); },
  255. (Repository r) { int dummy; if (dummy) return VersionRange.init; assert(0); },
  256. (VersionRange v) => v,
  257. );
  258. enforce(range.isExactVersion(),
  259. "Dependency "~range.toString()~" is no exact version.");
  260. return range.m_versA;
  261. }
  262.  
  263. /// Sets/gets the matching version range as a specification string.
  264. deprecated("Create a new `Dependency` instead and provide a `VersionRange`")
  265. @property void versionSpec(string ves) @trusted
  266. {
  267. this.m_value = VersionRange.fromString(ves);
  268. }
  269.  
  270. /// ditto
  271. deprecated("Use `Dependency.visit` and match `VersionRange`instead")
  272. @property string versionSpec() const @safe {
  273. return this.m_value.match!(
  274. (const NativePath p) => ANY_IDENT,
  275. (const Repository r) => r.m_ref,
  276. (const VersionRange p) => p.toString(),
  277. );
  278. }
  279.  
  280. /** Returns a modified dependency that gets mapped to a given path.
  281.  
  282. This function will return an unmodified `Dependency` if it is not path
  283. based. Otherwise, the given `path` will be prefixed to the existing
  284. path.
  285. */
  286. Dependency mapToPath(NativePath path) const @trusted {
  287. // NOTE Path is @system in vibe.d 0.7.x and in the compatibility layer
  288. return this.m_value.match!(
  289. (NativePath v) {
  290. if (v.empty || v.absolute) return this;
  291. auto ret = Dependency(path ~ v);
  292. ret.m_default = m_default;
  293. ret.m_optional = m_optional;
  294. return ret;
  295. },
  296. (Repository v) => this,
  297. (VersionRange v) => this,
  298. );
  299. }
  300.  
  301. /** Returns a human-readable string representation of the dependency
  302. specification.
  303. */
  304. string toString() const scope @trusted {
  305. // Trusted because `SumType.match` doesn't seem to support `scope`
  306.  
  307. string Stringifier (T, string pre = null) (const T v)
  308. {
  309. const bool extra = this.optional || this.default_;
  310. return format("%s%s%s%-(%s, %)%s",
  311. pre, v,
  312. extra ? " (" : "",
  313. BooleanOptions[!this.optional .. 1 + this.default_],
  314. extra ? ")" : "");
  315. }
  316.  
  317. return this.m_value.match!(
  318. Stringifier!Repository,
  319. Stringifier!(NativePath, "@"),
  320. Stringifier!VersionRange
  321. );
  322. }
  323.  
  324. /** Returns a JSON representation of the dependency specification.
  325.  
  326. Simple specifications will be represented as a single specification
  327. string (`versionSpec`), while more complex specifications will be
  328. represented as a JSON object with optional "version", "path", "optional"
  329. and "default" fields.
  330.  
  331. Params:
  332. selections = We are serializing `dub.selections.json`, don't write out
  333. `optional` and `default`.
  334. */
  335. Json toJson(bool selections = false) const @safe
  336. {
  337. // NOTE Path and Json is @system in vibe.d 0.7.x and in the compatibility layer
  338. static void initJson(ref Json j, bool opt, bool def, bool s = selections)
  339. {
  340. j = Json.emptyObject;
  341. if (!s && opt) j["optional"] = true;
  342. if (!s && def) j["default"] = true;
  343. }
  344.  
  345. Json json;
  346. this.m_value.match!(
  347. (const NativePath v) @trusted {
  348. initJson(json, optional, default_);
  349. json["path"] = v.toString();
  350. },
  351.  
  352. (const Repository v) @trusted {
  353. initJson(json, optional, default_);
  354. json["repository"] = v.toString();
  355. json["version"] = v.m_ref;
  356. },
  357.  
  358. (const VersionRange v) @trusted {
  359. if (!selections && (optional || default_))
  360. {
  361. initJson(json, optional, default_);
  362. json["version"] = v.toString();
  363. }
  364. else
  365. json = Json(v.toString());
  366. },
  367. );
  368. return json;
  369. }
  370.  
  371. @trusted unittest {
  372. Dependency d = Dependency("==1.0.0");
  373. assert(d.toJson() == Json("1.0.0"), "Failed: " ~ d.toJson().toPrettyString());
  374. d = fromJson((fromJson(d.toJson())).toJson());
  375. assert(d == Dependency("1.0.0"));
  376. assert(d.toJson() == Json("1.0.0"), "Failed: " ~ d.toJson().toPrettyString());
  377. }
  378.  
  379. @trusted unittest {
  380. Dependency dependency = Dependency(Repository("git+http://localhost", "1.0.0"));
  381. Json expected = Json([
  382. "repository": Json("git+http://localhost"),
  383. "version": Json("1.0.0")
  384. ]);
  385. assert(dependency.toJson() == expected, "Failed: " ~ dependency.toJson().toPrettyString());
  386. }
  387.  
  388. @trusted unittest {
  389. Dependency d = Dependency(NativePath("dir"));
  390. Json expected = Json([ "path": Json("dir") ]);
  391. assert(d.toJson() == expected, "Failed: " ~ d.toJson().toPrettyString());
  392. }
  393.  
  394. /** Constructs a new `Dependency` from its JSON representation.
  395.  
  396. See `toJson` for a description of the JSON format.
  397. */
  398. static Dependency fromJson(Json verspec)
  399. @trusted { // NOTE Path and Json is @system in vibe.d 0.7.x and in the compatibility layer
  400. Dependency dep;
  401. if( verspec.type == Json.Type.object ){
  402. if( auto pp = "path" in verspec ) {
  403. dep = Dependency(NativePath(verspec["path"].get!string));
  404. } else if (auto repository = "repository" in verspec) {
  405. enforce("version" in verspec, "No version field specified!");
  406. enforce(repository.length > 0, "No repository field specified!");
  407.  
  408. dep = Dependency(Repository(
  409. repository.get!string, verspec["version"].get!string));
  410. } else {
  411. enforce("version" in verspec, "No version field specified!");
  412. auto ver = verspec["version"].get!string;
  413. // Using the string to be able to specify a range of versions.
  414. dep = Dependency(ver);
  415. }
  416.  
  417. if (auto po = "optional" in verspec) dep.optional = po.get!bool;
  418. if (auto po = "default" in verspec) dep.default_ = po.get!bool;
  419. } else {
  420. // canonical "package-id": "version"
  421. dep = Dependency(verspec.get!string);
  422. }
  423. return dep;
  424. }
  425.  
  426. @trusted unittest {
  427. assert(fromJson(parseJsonString("\">=1.0.0 <2.0.0\"")) == Dependency(">=1.0.0 <2.0.0"));
  428. Dependency parsed = fromJson(parseJsonString(`
  429. {
  430. "version": "2.0.0",
  431. "optional": true,
  432. "default": true,
  433. "path": "path/to/package"
  434. }
  435. `));
  436. Dependency d = NativePath("path/to/package"); // supposed to ignore the version spec
  437. d.optional = true;
  438. d.default_ = true;
  439. assert(d == parsed);
  440. }
  441.  
  442. /** Compares dependency specifications.
  443.  
  444. These methods are suitable for equality comparisons, as well as for
  445. using `Dependency` as a key in hash or tree maps.
  446. */
  447. bool opEquals(in Dependency o) const scope @safe {
  448. if (o.m_optional != this.m_optional) return false;
  449. if (o.m_default != this.m_default) return false;
  450. return this.m_value == o.m_value;
  451. }
  452.  
  453. /// ditto
  454. int opCmp(in Dependency o) const @safe {
  455. alias ResultMatch = match!(
  456. (VersionRange r1, VersionRange r2) => r1.opCmp(r2),
  457. (_1, _2) => 0,
  458. );
  459. if (auto result = ResultMatch(this.m_value, o.m_value))
  460. return result;
  461. if (m_optional != o.m_optional) return m_optional ? -1 : 1;
  462. return 0;
  463. }
  464.  
  465. /** Determines if this dependency specification is valid.
  466.  
  467. A specification is valid if it can match at least one version.
  468. */
  469. bool valid() const @safe {
  470. return this.m_value.match!(
  471. (NativePath v) => true,
  472. (Repository v) => true,
  473. (VersionRange v) => v.isValid(),
  474. );
  475. }
  476.  
  477. /** Determines if this dependency specification matches arbitrary versions.
  478.  
  479. This is true in particular for the `any` constant.
  480. */
  481. deprecated("Use `VersionRange.matchesAny` directly")
  482. bool matchesAny() const scope @safe {
  483. return this.m_value.match!(
  484. (NativePath v) => true,
  485. (Repository v) => true,
  486. (VersionRange v) => v.matchesAny(),
  487. );
  488. }
  489.  
  490. /** Tests if the specification matches a specific version.
  491. */
  492. bool matches(string vers, VersionMatchMode mode = VersionMatchMode.standard) const @safe
  493. {
  494. return matches(Version(vers), mode);
  495. }
  496. /// ditto
  497. bool matches(in Version v, VersionMatchMode mode = VersionMatchMode.standard) const @safe {
  498. return this.m_value.match!(
  499. (NativePath i) => true,
  500. (Repository i) => true,
  501. (VersionRange i) => i.matchesAny() || i.matches(v, mode),
  502. );
  503. }
  504.  
  505. /** Merges two dependency specifications.
  506.  
  507. The result is a specification that matches the intersection of the set
  508. of versions matched by the individual specifications. Note that this
  509. result can be invalid (i.e. not match any version).
  510. */
  511. Dependency merge(ref const(Dependency) o) const @trusted {
  512. alias Merger = match!(
  513. (const NativePath a, const NativePath b) => a == b ? this : invalid,
  514. (const NativePath a, any ) => o,
  515. ( any , const NativePath b) => this,
  516.  
  517. (const Repository a, const Repository b) => a.m_ref == b.m_ref ? this : invalid,
  518. (const Repository a, any ) => this,
  519. ( any , const Repository b) => o,
  520.  
  521. (const VersionRange a, const VersionRange b) {
  522. if (a.matchesAny()) return o;
  523. if (b.matchesAny()) return this;
  524.  
  525. VersionRange copy = a;
  526. copy.merge(b);
  527. if (!copy.isValid()) return invalid;
  528. return Dependency(copy);
  529. }
  530. );
  531.  
  532. Dependency ret = Merger(this.m_value, o.m_value);
  533. ret.m_optional = m_optional && o.m_optional;
  534. return ret;
  535. }
  536. }
  537.  
  538. /// Allow direct access to the underlying dependency
  539. public auto visit (Handlers...) (const auto ref Dependency dep)
  540. {
  541. return dep.m_value.match!(Handlers);
  542. }
  543.  
  544. //// Ditto
  545. public auto visit (Handlers...) (auto ref Dependency dep)
  546. {
  547. return dep.m_value.match!(Handlers);
  548. }
  549.  
  550.  
  551. unittest {
  552. Dependency a = Dependency(">=1.1.0"), b = Dependency(">=1.3.0");
  553. assert (a.merge(b).valid() && a.merge(b).toString() == ">=1.3.0", a.merge(b).toString());
  554.  
  555. assertThrown(Dependency("<=2.0.0 >=1.0.0"));
  556. assertThrown(Dependency(">=2.0.0 <=1.0.0"));
  557.  
  558. a = Dependency(">=1.0.0 <=5.0.0"); b = Dependency(">=2.0.0");
  559. assert (a.merge(b).valid() && a.merge(b).toString() == ">=2.0.0 <=5.0.0", a.merge(b).toString());
  560.  
  561. assertThrown(a = Dependency(">1.0.0 ==5.0.0"), "Construction is invalid");
  562.  
  563. a = Dependency(">1.0.0"); b = Dependency("<2.0.0");
  564. assert (a.merge(b).valid(), a.merge(b).toString());
  565. assert (a.merge(b).toString() == ">1.0.0 <2.0.0", a.merge(b).toString());
  566.  
  567. a = Dependency(">2.0.0"); b = Dependency("<1.0.0");
  568. assert (!(a.merge(b)).valid(), a.merge(b).toString());
  569.  
  570. a = Dependency(">=2.0.0"); b = Dependency("<=1.0.0");
  571. assert (!(a.merge(b)).valid(), a.merge(b).toString());
  572.  
  573. a = Dependency("==2.0.0"); b = Dependency("==1.0.0");
  574. assert (!(a.merge(b)).valid(), a.merge(b).toString());
  575.  
  576. a = Dependency("1.0.0"); b = Dependency("==1.0.0");
  577. assert (a == b);
  578.  
  579. a = Dependency("<=2.0.0"); b = Dependency("==1.0.0");
  580. Dependency m = a.merge(b);
  581. assert (m.valid(), m.toString());
  582. assert (m.matches(Version("1.0.0")));
  583. assert (!m.matches(Version("1.1.0")));
  584. assert (!m.matches(Version("0.0.1")));
  585.  
  586.  
  587. // branches / head revisions
  588. a = Dependency(Version.masterBranch);
  589. assert(a.valid());
  590. assert(a.matches(Version.masterBranch));
  591. b = Dependency(Version.masterBranch);
  592. m = a.merge(b);
  593. assert(m.matches(Version.masterBranch));
  594.  
  595. //assertThrown(a = Dependency(Version.MASTER_STRING ~ " <=1.0.0"), "Construction invalid");
  596. assertThrown(a = Dependency(">=1.0.0 " ~ Version.masterBranch.toString()), "Construction invalid");
  597.  
  598. immutable string branch1 = Version.branchPrefix ~ "Branch1";
  599. immutable string branch2 = Version.branchPrefix ~ "Branch2";
  600.  
  601. //assertThrown(a = Dependency(branch1 ~ " " ~ branch2), "Error: '" ~ branch1 ~ " " ~ branch2 ~ "' succeeded");
  602. //assertThrown(a = Dependency(Version.MASTER_STRING ~ " " ~ branch1), "Error: '" ~ Version.MASTER_STRING ~ " " ~ branch1 ~ "' succeeded");
  603.  
  604. a = Dependency(branch1);
  605. b = Dependency(branch2);
  606. assert(!a.merge(b).valid, "Shouldn't be able to merge to different branches");
  607. b = a.merge(a);
  608. assert(b.valid, "Should be able to merge the same branches. (?)");
  609. assert(a == b);
  610.  
  611. a = Dependency(branch1);
  612. assert(a.matches(branch1), "Dependency(branch1) does not match 'branch1'");
  613. assert(a.matches(Version(branch1)), "Dependency(branch1) does not match Version('branch1')");
  614. assert(!a.matches(Version.masterBranch), "Dependency(branch1) matches Version.masterBranch");
  615. assert(!a.matches(branch2), "Dependency(branch1) matches 'branch2'");
  616. assert(!a.matches(Version("1.0.0")), "Dependency(branch1) matches '1.0.0'");
  617. a = Dependency(">=1.0.0");
  618. assert(!a.matches(Version(branch1)), "Dependency(1.0.0) matches 'branch1'");
  619.  
  620. // Testing optional dependencies.
  621. a = Dependency(">=1.0.0");
  622. assert(!a.optional, "Default is not optional.");
  623. b = a;
  624. assert(!a.merge(b).optional, "Merging two not optional dependencies wrong.");
  625. a.optional = true;
  626. assert(!a.merge(b).optional, "Merging optional with not optional wrong.");
  627. b.optional = true;
  628. assert(a.merge(b).optional, "Merging two optional dependencies wrong.");
  629.  
  630. // SemVer's sub identifiers.
  631. a = Dependency(">=1.0.0-beta");
  632. assert(!a.matches(Version("1.0.0-alpha")), "Failed: match 1.0.0-alpha with >=1.0.0-beta");
  633. assert(a.matches(Version("1.0.0-beta")), "Failed: match 1.0.0-beta with >=1.0.0-beta");
  634. assert(a.matches(Version("1.0.0")), "Failed: match 1.0.0 with >=1.0.0-beta");
  635. assert(a.matches(Version("1.0.0-rc")), "Failed: match 1.0.0-rc with >=1.0.0-beta");
  636.  
  637. // Approximate versions.
  638. a = Dependency("~>3.0");
  639. b = Dependency(">=3.0.0 <4.0.0-0");
  640. assert(a == b, "Testing failed: " ~ a.toString());
  641. assert(a.matches(Version("3.1.146")), "Failed: Match 3.1.146 with ~>0.1.2");
  642. assert(!a.matches(Version("0.2.0")), "Failed: Match 0.2.0 with ~>0.1.2");
  643. assert(!a.matches(Version("4.0.0-beta.1")));
  644. a = Dependency("~>3.0.0");
  645. assert(a == Dependency(">=3.0.0 <3.1.0-0"), "Testing failed: " ~ a.toString());
  646. a = Dependency("~>3.5");
  647. assert(a == Dependency(">=3.5.0 <4.0.0-0"), "Testing failed: " ~ a.toString());
  648. a = Dependency("~>3.5.0");
  649. assert(a == Dependency(">=3.5.0 <3.6.0-0"), "Testing failed: " ~ a.toString());
  650. assert(!Dependency("~>3.0.0").matches(Version("3.1.0-beta")));
  651.  
  652. a = Dependency("^0.1.2");
  653. assert(a == Dependency(">=0.1.2 <0.1.3-0"));
  654. a = Dependency("^1.2.3");
  655. assert(a == Dependency(">=1.2.3 <2.0.0-0"), "Testing failed: " ~ a.toString());
  656. a = Dependency("^1.2");
  657. assert(a == Dependency(">=1.2.0 <2.0.0-0"), "Testing failed: " ~ a.toString());
  658.  
  659. a = Dependency("~>0.1.1");
  660. b = Dependency("==0.1.0");
  661. assert(!a.merge(b).valid);
  662. b = Dependency("==0.1.9999");
  663. assert(a.merge(b).valid);
  664. b = Dependency("==0.2.0");
  665. assert(!a.merge(b).valid);
  666. b = Dependency("==0.2.0-beta.1");
  667. assert(!a.merge(b).valid);
  668.  
  669. a = Dependency("~>1.0.1-beta");
  670. b = Dependency(">=1.0.1-beta <1.1.0-0");
  671. assert(a == b, "Testing failed: " ~ a.toString());
  672. assert(a.matches(Version("1.0.1-beta")));
  673. assert(a.matches(Version("1.0.1-beta.6")));
  674.  
  675. a = Dependency("~d2test");
  676. assert(!a.optional);
  677. assert(a.valid);
  678. assert(a.version_ == Version("~d2test"));
  679.  
  680. a = Dependency("==~d2test");
  681. assert(!a.optional);
  682. assert(a.valid);
  683. assert(a.version_ == Version("~d2test"));
  684.  
  685. a = Dependency.any;
  686. assert(!a.optional);
  687. assert(a.valid);
  688. assertThrown(a.version_);
  689. assert(a.matches(Version.masterBranch));
  690. assert(a.matches(Version("1.0.0")));
  691. assert(a.matches(Version("0.0.1-pre")));
  692. b = Dependency(">=1.0.1");
  693. assert(b == a.merge(b));
  694. assert(b == b.merge(a));
  695. b = Dependency(Version.masterBranch);
  696. assert(a.merge(b) == b);
  697. assert(b.merge(a) == b);
  698.  
  699. a.optional = true;
  700. assert(a.matches(Version.masterBranch));
  701. assert(a.matches(Version("1.0.0")));
  702. assert(a.matches(Version("0.0.1-pre")));
  703. b = Dependency(">=1.0.1");
  704. assert(b == a.merge(b));
  705. assert(b == b.merge(a));
  706. b = Dependency(Version.masterBranch);
  707. assert(a.merge(b) == b);
  708. assert(b.merge(a) == b);
  709.  
  710. assert(Dependency("1.0.0").matches(Version("1.0.0+foo")));
  711. assert(Dependency("1.0.0").matches(Version("1.0.0+foo"), VersionMatchMode.standard));
  712. assert(!Dependency("1.0.0").matches(Version("1.0.0+foo"), VersionMatchMode.strict));
  713. assert(Dependency("1.0.0+foo").matches(Version("1.0.0+foo"), VersionMatchMode.strict));
  714. assert(Dependency("~>1.0.0+foo").matches(Version("1.0.0+foo"), VersionMatchMode.strict));
  715. assert(Dependency("~>1.0.0").matches(Version("1.0.0+foo"), VersionMatchMode.strict));
  716. }
  717.  
  718. unittest {
  719. assert(VersionRange.fromString("~>1.0.4").toString() == "~>1.0.4");
  720. assert(VersionRange.fromString("~>1.4").toString() == "~>1.4");
  721. assert(VersionRange.fromString("~>2").toString() == "~>2");
  722. assert(VersionRange.fromString("~>1.0.4+1.2.3").toString() == "~>1.0.4");
  723. assert(VersionRange.fromString("^0.1.2").toString() == "^0.1.2");
  724. assert(VersionRange.fromString("^1.2.3").toString() == "^1.2.3");
  725. assert(VersionRange.fromString("^1.2").toString() == "~>1.2"); // equivalent; prefer ~>
  726. }
  727.  
  728. /**
  729. Represents an SCM repository.
  730. */
  731. struct Repository
  732. {
  733. private string m_remote;
  734. private string m_ref;
  735.  
  736. private Kind m_kind;
  737.  
  738. enum Kind
  739. {
  740. git,
  741. }
  742.  
  743. /**
  744. Params:
  745. remote = Repository remote.
  746. ref_ = Reference to use (SHA1, tag, branch name...)
  747. */
  748. this(string remote, string ref_)
  749. {
  750. enforce(remote.startsWith("git+"), "Unsupported repository type (supports: git+URL)");
  751.  
  752. m_remote = remote["git+".length .. $];
  753. m_kind = Kind.git;
  754. m_ref = ref_;
  755. assert(m_remote.length);
  756. assert(m_ref.length);
  757. }
  758.  
  759. /// Ditto
  760. deprecated("Use the constructor accepting a second parameter named `ref_`")
  761. this(string remote)
  762. {
  763. enforce(remote.startsWith("git+"), "Unsupported repository type (supports: git+URL)");
  764.  
  765. m_remote = remote["git+".length .. $];
  766. m_kind = Kind.git;
  767. assert(m_remote.length);
  768. }
  769.  
  770. string toString() const nothrow pure @safe
  771. {
  772. if (empty) return null;
  773. string kindRepresentation;
  774.  
  775. final switch (kind)
  776. {
  777. case Kind.git:
  778. kindRepresentation = "git";
  779. }
  780. return kindRepresentation~"+"~remote;
  781. }
  782.  
  783. /**
  784. Returns:
  785. Repository URL or path.
  786. */
  787. @property string remote() const @nogc nothrow pure @safe
  788. in { assert(m_remote !is null); }
  789. do
  790. {
  791. return m_remote;
  792. }
  793.  
  794. /**
  795. Returns:
  796. The reference (commit hash, branch name, tag) we are targeting
  797. */
  798. @property string ref_() const @nogc nothrow pure @safe
  799. in { assert(m_remote !is null); }
  800. in { assert(m_ref !is null); }
  801. do
  802. {
  803. return m_ref;
  804. }
  805.  
  806. /**
  807. Returns:
  808. Repository type.
  809. */
  810. @property Kind kind() const @nogc nothrow pure @safe
  811. {
  812. return m_kind;
  813. }
  814.  
  815. /**
  816. Returns:
  817. Whether the repository was initialized with an URL or path.
  818. */
  819. @property bool empty() const @nogc nothrow pure @safe
  820. {
  821. return m_remote.empty;
  822. }
  823. }
  824.  
  825.  
  826. /**
  827. Represents a version in semantic version format, or a branch identifier.
  828.  
  829. This can either have the form "~master", where "master" is a branch name,
  830. or the form "major.update.bugfix-prerelease+buildmetadata" (see the
  831. Semantic Versioning Specification v2.0.0 at http://semver.org/).
  832. */
  833. struct Version {
  834. private {
  835. static immutable MAX_VERS = "99999.0.0";
  836. static immutable masterString = "~master";
  837. enum branchPrefix = '~';
  838. string m_version;
  839. }
  840.  
  841. static immutable Version minRelease = Version("0.0.0");
  842. static immutable Version maxRelease = Version(MAX_VERS);
  843. static immutable Version masterBranch = Version(masterString);
  844.  
  845. /** Constructs a new `Version` from its string representation.
  846. */
  847. this(string vers) @safe pure
  848. {
  849. enforce(vers.length > 1, "Version strings must not be empty.");
  850. if (vers[0] != branchPrefix)
  851. enforce(vers.isValidVersion(), "Invalid SemVer format: " ~ vers);
  852. m_version = vers;
  853. }
  854.  
  855. /** Constructs a new `Version` from its string representation.
  856.  
  857. This method is equivalent to calling the constructor and is used as an
  858. endpoint for the serialization framework.
  859. */
  860. static Version fromString(string vers) @safe pure { return Version(vers); }
  861.  
  862. bool opEquals(in Version oth) const scope @safe pure
  863. {
  864. return opCmp(oth) == 0;
  865. }
  866.  
  867. /// Tests if this represents a branch instead of a version.
  868. @property bool isBranch() const scope @safe pure nothrow @nogc
  869. {
  870. return m_version.length > 0 && m_version[0] == branchPrefix;
  871. }
  872.  
  873. /// Tests if this represents the master branch "~master".
  874. @property bool isMaster() const scope @safe pure nothrow @nogc
  875. {
  876. return m_version == masterString;
  877. }
  878.  
  879. /** Tests if this represents a pre-release version.
  880.  
  881. Note that branches are always considered pre-release versions.
  882. */
  883. @property bool isPreRelease() const scope @safe pure nothrow @nogc
  884. {
  885. if (isBranch) return true;
  886. return isPreReleaseVersion(m_version);
  887. }
  888.  
  889. /** Tests two versions for equality, according to the selected match mode.
  890. */
  891. bool matches(in Version other, VersionMatchMode mode = VersionMatchMode.standard)
  892. const scope @safe pure
  893. {
  894. if (mode == VersionMatchMode.strict)
  895. return this.toString() == other.toString();
  896. return this == other;
  897. }
  898.  
  899. /** Compares two versions/branches for precedence.
  900.  
  901. Versions generally have precedence over branches and the master branch
  902. has precedence over other branches. Apart from that, versions are
  903. compared using SemVer semantics, while branches are compared
  904. lexicographically.
  905. */
  906. int opCmp(in Version other) const scope @safe pure
  907. {
  908. if (isBranch || other.isBranch) {
  909. if(m_version == other.m_version) return 0;
  910. if (!isBranch) return 1;
  911. else if (!other.isBranch) return -1;
  912. if (isMaster) return 1;
  913. else if (other.isMaster) return -1;
  914. return this.m_version < other.m_version ? -1 : 1;
  915. }
  916.  
  917. return compareVersions(m_version, other.m_version);
  918. }
  919.  
  920. /// Returns the string representation of the version/branch.
  921. string toString() const return scope @safe pure nothrow @nogc
  922. {
  923. return m_version;
  924. }
  925. }
  926.  
  927. /**
  928. * A range of versions that are acceptable
  929. *
  930. * While not directly described in SemVer v2.0.0, a common set
  931. * of range operators have appeared among package managers.
  932. * We mostly NPM's: https://semver.npmjs.com/
  933. *
  934. * Hence the acceptable forms for this string are as follows:
  935. *
  936. * $(UL
  937. * $(LI `"1.0.0"` - a single version in SemVer format)
  938. * $(LI `"==1.0.0"` - alternative single version notation)
  939. * $(LI `">1.0.0"` - version range with a single bound)
  940. * $(LI `">1.0.0 <2.0.0"` - version range with two bounds)
  941. * $(LI `"~>1.0.0"` - a fuzzy version range)
  942. * $(LI `"~>1.0"` - a fuzzy version range with partial version)
  943. * $(LI `"^1.0.0"` - semver compatible version range (same version if 0.x.y, ==major >=minor.patch if x.y.z))
  944. * $(LI `"^1.0"` - same as ^1.0.0)
  945. * $(LI `"~master"` - a branch name)
  946. * $(LI `"*"` - match any version (see also `VersionRange.Any`))
  947. * )
  948. *
  949. * Apart from "$(LT)" and "$(GT)", "$(GT)=" and "$(LT)=" are also valid
  950. * comparators.
  951. */
  952. public struct VersionRange
  953. {
  954. private Version m_versA;
  955. private Version m_versB;
  956. private bool m_inclusiveA = true; // A comparison > (true) or >= (false)
  957. private bool m_inclusiveB = true; // B comparison < (true) or <= (false)
  958.  
  959. /// Matches any version
  960. public static immutable Any = VersionRange(Version.minRelease, Version.maxRelease);
  961. /// Doesn't match any version
  962. public static immutable Invalid = VersionRange(Version.maxRelease, Version.minRelease);
  963.  
  964. ///
  965. public int opCmp (in VersionRange o) const scope @safe
  966. {
  967. if (m_inclusiveA != o.m_inclusiveA) return m_inclusiveA < o.m_inclusiveA ? -1 : 1;
  968. if (m_inclusiveB != o.m_inclusiveB) return m_inclusiveB < o.m_inclusiveB ? -1 : 1;
  969. if (m_versA != o.m_versA) return m_versA < o.m_versA ? -1 : 1;
  970. if (m_versB != o.m_versB) return m_versB < o.m_versB ? -1 : 1;
  971. return 0;
  972. }
  973.  
  974. public bool matches (in Version v, VersionMatchMode mode = VersionMatchMode.standard)
  975. const scope @safe
  976. {
  977. if (m_versA.isBranch) {
  978. enforce(this.isExactVersion());
  979. return m_versA == v;
  980. }
  981.  
  982. if (v.isBranch)
  983. return m_versA == v;
  984.  
  985. if (m_versA == m_versB)
  986. return this.m_versA.matches(v, mode);
  987.  
  988. return doCmp(m_inclusiveA, m_versA, v) &&
  989. doCmp(m_inclusiveB, v, m_versB);
  990. }
  991.  
  992. /// Modify in place
  993. public void merge (const VersionRange o) @safe
  994. {
  995. int acmp = m_versA.opCmp(o.m_versA);
  996. int bcmp = m_versB.opCmp(o.m_versB);
  997.  
  998. this.m_inclusiveA = !m_inclusiveA && acmp >= 0 ? false : o.m_inclusiveA;
  999. this.m_versA = acmp > 0 ? m_versA : o.m_versA;
  1000. this.m_inclusiveB = !m_inclusiveB && bcmp <= 0 ? false : o.m_inclusiveB;
  1001. this.m_versB = bcmp < 0 ? m_versB : o.m_versB;
  1002. }
  1003.  
  1004. /// Returns true $(I iff) the version range only matches a specific version.
  1005. @property bool isExactVersion() const scope @safe
  1006. {
  1007. return this.m_versA == this.m_versB;
  1008. }
  1009.  
  1010. /// Determines if this dependency specification matches arbitrary versions.
  1011. /// This is true in particular for the `any` constant.
  1012. public bool matchesAny() const scope @safe
  1013. {
  1014. return this.m_inclusiveA && this.m_inclusiveB
  1015. && this.m_versA == Version.minRelease
  1016. && this.m_versB == Version.maxRelease;
  1017. }
  1018.  
  1019. unittest {
  1020. assert(VersionRange.fromString("*").matchesAny);
  1021. assert(!VersionRange.fromString(">0.0.0").matchesAny);
  1022. assert(!VersionRange.fromString(">=1.0.0").matchesAny);
  1023. assert(!VersionRange.fromString("<1.0.0").matchesAny);
  1024. }
  1025.  
  1026. public static VersionRange fromString (string ves) @safe
  1027. {
  1028. static import std.string;
  1029.  
  1030. enforce(ves.length > 0);
  1031.  
  1032. if (ves == Dependency.ANY_IDENT) {
  1033. // Any version is good.
  1034. ves = ">=0.0.0";
  1035. }
  1036.  
  1037. if (ves.startsWith("~>")) {
  1038. // Shortcut: "~>x.y.z" variant. Last non-zero number will indicate
  1039. // the base for this so something like this: ">=x.y.z <x.(y+1).z"
  1040. ves = ves[2..$];
  1041. return VersionRange(
  1042. Version(expandVersion(ves)), Version(bumpVersion(ves) ~ "-0"),
  1043. true, false);
  1044. }
  1045.  
  1046. if (ves.startsWith("^")) {
  1047. // Shortcut: "^x.y.z" variant. "Semver compatible" - no breaking changes.
  1048. // if 0.x.y, ==0.x.y
  1049. // if x.y.z, >=x.y.z <(x+1).0.0-0
  1050. // ^x.y is equivalent to ^x.y.0.
  1051. ves = ves[1..$].expandVersion;
  1052. return VersionRange(
  1053. Version(ves), Version(bumpIncompatibleVersion(ves) ~ "-0"),
  1054. true, false);
  1055. }
  1056.  
  1057. if (ves[0] == Version.branchPrefix) {
  1058. auto ver = Version(ves);
  1059. return VersionRange(ver, ver, true, true);
  1060. }
  1061.  
  1062. if (std.string.indexOf("><=", ves[0]) == -1) {
  1063. auto ver = Version(ves);
  1064. return VersionRange(ver, ver, true, true);
  1065. }
  1066.  
  1067. auto cmpa = skipComp(ves);
  1068. size_t idx2 = std.string.indexOf(ves, " ");
  1069. if (idx2 == -1) {
  1070. if (cmpa == "<=" || cmpa == "<")
  1071. return VersionRange(Version.minRelease, Version(ves), true, (cmpa == "<="));
  1072.  
  1073. if (cmpa == ">=" || cmpa == ">")
  1074. return VersionRange(Version(ves), Version.maxRelease, (cmpa == ">="), true);
  1075.  
  1076. // Converts "==" to ">=a&&<=a", which makes merging easier
  1077. return VersionRange(Version(ves), Version(ves), true, true);
  1078. }
  1079.  
  1080. enforce(cmpa == ">" || cmpa == ">=",
  1081. "First comparison operator expected to be either > or >=, not " ~ cmpa);
  1082. assert(ves[idx2] == ' ');
  1083. VersionRange ret;
  1084. ret.m_versA = Version(ves[0..idx2]);
  1085. ret.m_inclusiveA = cmpa == ">=";
  1086. string v2 = ves[idx2+1..$];
  1087. auto cmpb = skipComp(v2);
  1088. enforce(cmpb == "<" || cmpb == "<=",
  1089. "Second comparison operator expected to be either < or <=, not " ~ cmpb);
  1090. ret.m_versB = Version(v2);
  1091. ret.m_inclusiveB = cmpb == "<=";
  1092.  
  1093. enforce(!ret.m_versA.isBranch && !ret.m_versB.isBranch,
  1094. format("Cannot compare branches: %s", ves));
  1095. enforce(ret.m_versA <= ret.m_versB,
  1096. "First version must not be greater than the second one.");
  1097.  
  1098. return ret;
  1099. }
  1100.  
  1101. /// Returns a string representation of this range
  1102. string toString() const @safe {
  1103. static import std.string;
  1104.  
  1105. string r;
  1106.  
  1107. if (this == Invalid) return "no";
  1108. if (this.isExactVersion() && m_inclusiveA && m_inclusiveB) {
  1109. // Special "==" case
  1110. if (m_versA == Version.masterBranch) return "~master";
  1111. else return m_versA.toString();
  1112. }
  1113.  
  1114. // "~>", "^" case
  1115. if (m_inclusiveA && !m_inclusiveB && !m_versA.isBranch) {
  1116. auto vs = m_versA.toString();
  1117. auto i1 = std.string.indexOf(vs, '-'), i2 = std.string.indexOf(vs, '+');
  1118. auto i12 = i1 >= 0 ? i2 >= 0 ? i1 < i2 ? i1 : i2 : i1 : i2;
  1119. auto va = i12 >= 0 ? vs[0 .. i12] : vs;
  1120. auto parts = va.splitter('.').array;
  1121. assert(parts.length == 3, "Version string with a digit group count != 3: "~va);
  1122.  
  1123. foreach (i; 0 .. 3) {
  1124. auto vp = parts[0 .. i+1].join(".");
  1125. auto ve = Version(expandVersion(vp));
  1126. auto veb = Version(bumpVersion(vp) ~ "-0");
  1127. if (ve == m_versA && veb == m_versB) return "~>" ~ vp;
  1128.  
  1129. auto veb2 = Version(bumpIncompatibleVersion(expandVersion(vp)) ~ "-0");
  1130. if (ve == m_versA && veb2 == m_versB) return "^" ~ vp;
  1131. }
  1132. }
  1133.  
  1134. if (m_versA != Version.minRelease) r = (m_inclusiveA ? ">=" : ">") ~ m_versA.toString();
  1135. if (m_versB != Version.maxRelease) r ~= (r.length==0 ? "" : " ") ~ (m_inclusiveB ? "<=" : "<") ~ m_versB.toString();
  1136. if (this.matchesAny()) r = ">=0.0.0";
  1137. return r;
  1138. }
  1139.  
  1140. public bool isValid() const @safe {
  1141. return m_versA <= m_versB && doCmp(m_inclusiveA && m_inclusiveB, m_versA, m_versB);
  1142. }
  1143.  
  1144. private static bool doCmp(bool inclusive, in Version a, in Version b)
  1145. @safe
  1146. {
  1147. return inclusive ? a <= b : a < b;
  1148. }
  1149.  
  1150. private static bool isDigit(char ch) @safe { return ch >= '0' && ch <= '9'; }
  1151. private static string skipComp(ref string c) @safe {
  1152. size_t idx = 0;
  1153. while (idx < c.length && !isDigit(c[idx]) && c[idx] != Version.branchPrefix) idx++;
  1154. enforce(idx < c.length, "Expected version number in version spec: "~c);
  1155. string cmp = idx==c.length-1||idx==0? ">=" : c[0..idx];
  1156. c = c[idx..$];
  1157. switch(cmp) {
  1158. default: enforce(false, "No/Unknown comparison specified: '"~cmp~"'"); return ">=";
  1159. case ">=": goto case; case ">": goto case;
  1160. case "<=": goto case; case "<": goto case;
  1161. case "==": return cmp;
  1162. }
  1163. }
  1164. }
  1165.  
  1166. enum VersionMatchMode {
  1167. standard, /// Match according to SemVer rules
  1168. strict /// Also include build metadata suffix in the comparison
  1169. }
  1170.  
  1171. unittest {
  1172. Version a, b;
  1173.  
  1174. assertNotThrown(a = Version("1.0.0"), "Constructing Version('1.0.0') failed");
  1175. assert(!a.isBranch, "Error: '1.0.0' treated as branch");
  1176. assert(a == a, "a == a failed");
  1177.  
  1178. assertNotThrown(a = Version(Version.masterString), "Constructing Version("~Version.masterString~"') failed");
  1179. assert(a.isBranch, "Error: '"~Version.masterString~"' treated as branch");
  1180. assert(a.isMaster);
  1181. assert(a == Version.masterBranch, "Constructed master version != default master version.");
  1182.  
  1183. assertNotThrown(a = Version("~BRANCH"), "Construction of branch Version failed.");
  1184. assert(a.isBranch, "Error: '~BRANCH' not treated as branch'");
  1185. assert(!a.isMaster);
  1186. assert(a == a, "a == a with branch failed");
  1187.  
  1188. // opCmp
  1189. a = Version("1.0.0");
  1190. b = Version("1.0.0");
  1191. assert(a == b, "a == b with a:'1.0.0', b:'1.0.0' failed");
  1192. b = Version("2.0.0");
  1193. assert(a != b, "a != b with a:'1.0.0', b:'2.0.0' failed");
  1194.  
  1195. a = Version.masterBranch;
  1196. b = Version("~BRANCH");
  1197. assert(a != b, "a != b with a:MASTER, b:'~branch' failed");
  1198. assert(a > b);
  1199. assert(a < Version("0.0.0"));
  1200. assert(b < Version("0.0.0"));
  1201. assert(a > Version("~Z"));
  1202. assert(b < Version("~Z"));
  1203.  
  1204. // SemVer 2.0.0-rc.2
  1205. a = Version("2.0.0-rc.2");
  1206. b = Version("2.0.0-rc.3");
  1207. assert(a < b, "Failed: 2.0.0-rc.2 < 2.0.0-rc.3");
  1208.  
  1209. a = Version("2.0.0-rc.2+build-metadata");
  1210. b = Version("2.0.0+build-metadata");
  1211. assert(a < b, "Failed: "~a.toString()~"<"~b.toString());
  1212.  
  1213. // 1.0.0-alpha < 1.0.0-alpha.1 < 1.0.0-beta.2 < 1.0.0-beta.11 < 1.0.0-rc.1 < 1.0.0
  1214. Version[] versions;
  1215. versions ~= Version("1.0.0-alpha");
  1216. versions ~= Version("1.0.0-alpha.1");
  1217. versions ~= Version("1.0.0-beta.2");
  1218. versions ~= Version("1.0.0-beta.11");
  1219. versions ~= Version("1.0.0-rc.1");
  1220. versions ~= Version("1.0.0");
  1221. for(int i=1; i<versions.length; ++i)
  1222. for(int j=i-1; j>=0; --j)
  1223. assert(versions[j] < versions[i], "Failed: " ~ versions[j].toString() ~ "<" ~ versions[i].toString());
  1224.  
  1225. assert(Version("1.0.0+a") == Version("1.0.0+b"));
  1226.  
  1227. assert(Version("1.0.0").matches(Version("1.0.0+foo")));
  1228. assert(Version("1.0.0").matches(Version("1.0.0+foo"), VersionMatchMode.standard));
  1229. assert(!Version("1.0.0").matches(Version("1.0.0+foo"), VersionMatchMode.strict));
  1230. assert(Version("1.0.0+foo").matches(Version("1.0.0+foo"), VersionMatchMode.strict));
  1231. }
  1232.  
  1233. /// Determines whether the given string is a Git hash.
  1234. bool isGitHash(string hash) @nogc nothrow pure @safe
  1235. {
  1236. import std.ascii : isHexDigit;
  1237. import std.utf : byCodeUnit;
  1238.  
  1239. return hash.length >= 7 && hash.length <= 40 && hash.byCodeUnit.all!isHexDigit;
  1240. }
  1241.  
  1242. @nogc nothrow pure @safe unittest {
  1243. assert(isGitHash("73535568b79a0b124bc1653002637a830ce0fcb8"));
  1244. assert(!isGitHash("735"));
  1245. assert(!isGitHash("73535568b79a0b124bc1-53002637a830ce0fcb8"));
  1246. assert(!isGitHash("73535568b79a0b124bg1"));
  1247. }