screw-up 1.5.0 → 1.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,11 +1,11 @@
1
1
  /*!
2
2
  * name: screw-up
3
- * version: 1.5.0
3
+ * version: 1.7.0
4
4
  * description: Simply package metadata inserter on Vite plugin
5
5
  * author: Kouji Matsui (@kekyo@mi.kekyo.net)
6
6
  * license: MIT
7
7
  * repository.url: https://github.com/kekyo/screw-up.git
8
- * git.commit.hash: 72fd5b88656bc400ba3fed500895826208e23217
8
+ * git.commit.hash: 0865d5a2d4d1e89be8dae4b6abeba1393a767707
9
9
  */
10
10
  "use strict";
11
11
  const fs = require("fs");
@@ -1669,6 +1669,195 @@ function requireDayjs_min() {
1669
1669
  }
1670
1670
  var dayjs_minExports = requireDayjs_min();
1671
1671
  const dayjs = /* @__PURE__ */ getDefaultExportFromCjs(dayjs_minExports);
1672
+ const parsePackedRefs = async (packedRefsPath) => {
1673
+ try {
1674
+ const content = await fs$1.readFile(packedRefsPath, "utf-8");
1675
+ const lines = content.split("\n");
1676
+ const tags = [];
1677
+ for (const line2 of lines) {
1678
+ if (line2.startsWith("#") || !line2.trim()) continue;
1679
+ const match = line2.match(/^[0-9a-f]{40}\s+refs\/tags\/(.+)$/);
1680
+ if (match) {
1681
+ const tagName = match[1];
1682
+ if (!tagName.endsWith("^{}")) {
1683
+ tags.push(tagName);
1684
+ }
1685
+ }
1686
+ }
1687
+ return tags;
1688
+ } catch (error) {
1689
+ if (error.code === "ENOENT") {
1690
+ return [];
1691
+ }
1692
+ throw error;
1693
+ }
1694
+ };
1695
+ const readLooseTags = async (refsTagsPath) => {
1696
+ try {
1697
+ const entries = await fs$1.readdir(refsTagsPath, { withFileTypes: true });
1698
+ const tags = [];
1699
+ for (const entry of entries) {
1700
+ if (entry.isFile()) {
1701
+ tags.push(entry.name);
1702
+ }
1703
+ }
1704
+ return tags;
1705
+ } catch (error) {
1706
+ if (error.code === "ENOENT") {
1707
+ return [];
1708
+ }
1709
+ throw error;
1710
+ }
1711
+ };
1712
+ const listTagsFast = async (repoPath) => {
1713
+ const gitDir = path.join(repoPath, ".git");
1714
+ const gitStat = await fs$1.stat(gitDir).catch(() => null);
1715
+ let actualGitDir = gitDir;
1716
+ if (gitStat == null ? void 0 : gitStat.isFile()) {
1717
+ const content = await fs$1.readFile(gitDir, "utf-8");
1718
+ const match = content.match(/^gitdir:\s*(.+)$/m);
1719
+ if (match) {
1720
+ actualGitDir = path.isAbsolute(match[1]) ? match[1] : path.join(repoPath, match[1]);
1721
+ }
1722
+ }
1723
+ const [packedTags, looseTags] = await Promise.all([
1724
+ parsePackedRefs(path.join(actualGitDir, "packed-refs")),
1725
+ readLooseTags(path.join(actualGitDir, "refs", "tags"))
1726
+ ]);
1727
+ const allTags = /* @__PURE__ */ new Set([...packedTags, ...looseTags]);
1728
+ return Array.from(allTags).sort();
1729
+ };
1730
+ const resolveTagsBatchWithCommit = async (repoPath, tagNames, logger) => {
1731
+ const startTime = Date.now();
1732
+ const gitDir = path.join(repoPath, ".git");
1733
+ const result = /* @__PURE__ */ new Map();
1734
+ const gitStat = await fs$1.stat(gitDir).catch(() => null);
1735
+ let actualGitDir = gitDir;
1736
+ if (gitStat == null ? void 0 : gitStat.isFile()) {
1737
+ const content = await fs$1.readFile(gitDir, "utf-8");
1738
+ const match = content.match(/^gitdir:\s*(.+)$/m);
1739
+ if (match) {
1740
+ actualGitDir = path.isAbsolute(match[1]) ? match[1] : path.join(repoPath, match[1]);
1741
+ }
1742
+ }
1743
+ const tagSet = new Set(tagNames);
1744
+ const packedRefsStart = Date.now();
1745
+ try {
1746
+ const content = await fs$1.readFile(
1747
+ path.join(actualGitDir, "packed-refs"),
1748
+ "utf-8"
1749
+ );
1750
+ const lines = content.split("\n");
1751
+ for (let i = 0; i < lines.length; i++) {
1752
+ const line2 = lines[i];
1753
+ if (line2.startsWith("#") || !line2.trim()) continue;
1754
+ const match = line2.match(/^([0-9a-f]{40})\s+refs\/tags\/(.+)$/);
1755
+ if (match && tagSet.has(match[2])) {
1756
+ const tagName = match[2];
1757
+ const oid = match[1];
1758
+ let commitOid = oid;
1759
+ if (i + 1 < lines.length && lines[i + 1].startsWith("^")) {
1760
+ commitOid = lines[i + 1].substring(1, 41);
1761
+ }
1762
+ result.set(tagName, { oid, commitOid });
1763
+ }
1764
+ }
1765
+ } catch (error) {
1766
+ if (error.code !== "ENOENT") {
1767
+ throw error;
1768
+ }
1769
+ }
1770
+ logger.debug(
1771
+ `[fast-tags] read packed-refs: ${Date.now() - packedRefsStart}ms`
1772
+ );
1773
+ const remainingTags = tagNames.filter((tag) => !result.has(tag));
1774
+ if (remainingTags.length > 0) {
1775
+ const looseRefsStart = Date.now();
1776
+ await Promise.all(
1777
+ remainingTags.map(async (tagName) => {
1778
+ const looseRefPath = path.join(actualGitDir, "refs", "tags", tagName);
1779
+ try {
1780
+ const hash = await fs$1.readFile(looseRefPath, "utf-8");
1781
+ const oid = hash.trim();
1782
+ let commitOid = oid;
1783
+ try {
1784
+ const { execSync } = require("child_process");
1785
+ const objectType = execSync(
1786
+ `git -C "${repoPath}" cat-file -t ${oid}`,
1787
+ { encoding: "utf-8" }
1788
+ ).trim();
1789
+ if (objectType === "tag") {
1790
+ const tagContent = execSync(
1791
+ `git -C "${repoPath}" cat-file -p ${oid}`,
1792
+ { encoding: "utf-8" }
1793
+ );
1794
+ const objectMatch = tagContent.match(/^object ([0-9a-f]{40})$/m);
1795
+ if (objectMatch) {
1796
+ commitOid = objectMatch[1];
1797
+ }
1798
+ }
1799
+ } catch (error) {
1800
+ logger.debug(
1801
+ `[fast-tags] Could not determine object type for ${tagName}: ${error}`
1802
+ );
1803
+ }
1804
+ result.set(tagName, { oid, commitOid });
1805
+ } catch (error) {
1806
+ if (error.code !== "ENOENT") {
1807
+ throw error;
1808
+ }
1809
+ }
1810
+ })
1811
+ );
1812
+ logger.debug(
1813
+ `[fast-tags] read loose refs: ${Date.now() - looseRefsStart}ms`
1814
+ );
1815
+ }
1816
+ const totalTime = Date.now() - startTime;
1817
+ logger.debug(`[fast-tags] resolveTagsBatchWithCommit total: ${totalTime}ms`);
1818
+ logger.debug(`[fast-tags] Resolved ${result.size}/${tagNames.length} tags`);
1819
+ return result;
1820
+ };
1821
+ const buildCompleteTagCache = async (repoPath, parseVersion2, logger) => {
1822
+ const totalStart = Date.now();
1823
+ const cache = /* @__PURE__ */ new Map();
1824
+ const listStart = Date.now();
1825
+ const tags = await listTagsFast(repoPath);
1826
+ logger.debug(`[git-ops] listTagsFast: ${Date.now() - listStart}ms`);
1827
+ logger.debug(`[git-ops] Found ${tags.length} tags`);
1828
+ const resolveStart = Date.now();
1829
+ const tagData = await resolveTagsBatchWithCommit(repoPath, tags, logger);
1830
+ logger.debug(
1831
+ `[git-ops] resolveTagsBatchWithCommit: ${Date.now() - resolveStart}ms`
1832
+ );
1833
+ const buildStart = Date.now();
1834
+ for (const tagName of tags) {
1835
+ const data = tagData.get(tagName);
1836
+ if (!data) continue;
1837
+ const { commitOid } = data;
1838
+ const version2 = parseVersion2(tagName);
1839
+ const tagInfo = {
1840
+ name: tagName,
1841
+ hash: commitOid,
1842
+ version: version2
1843
+ };
1844
+ if (!cache.has(commitOid)) {
1845
+ cache.set(commitOid, []);
1846
+ }
1847
+ cache.get(commitOid).push(tagInfo);
1848
+ }
1849
+ logger.debug(`[git-ops] build cache map: ${Date.now() - buildStart}ms`);
1850
+ const sortStart = Date.now();
1851
+ for (const tags2 of cache.values()) {
1852
+ tags2.sort((a, b) => a.name.localeCompare(b.name));
1853
+ }
1854
+ logger.debug(`[git-ops] sort tags: ${Date.now() - sortStart}ms`);
1855
+ logger.debug(
1856
+ `[git-ops] buildCompleteTagCache total: ${Date.now() - totalStart}ms`
1857
+ );
1858
+ logger.debug(`[git-ops] Built cache with ${cache.size} unique commits`);
1859
+ return cache;
1860
+ };
1672
1861
  const parseVersionComponent = (value) => {
1673
1862
  const num = parseInt(value, 10);
1674
1863
  return num < 0 || num > 65535 ? void 0 : num;
@@ -1794,120 +1983,8 @@ const getCurrentCommit = async (repositoryPath) => {
1794
1983
  return void 0;
1795
1984
  }
1796
1985
  };
1797
- const buildTagCache = async (repositoryPath) => {
1798
- const cache = {
1799
- commitToTags: /* @__PURE__ */ new Map(),
1800
- initialized: true
1801
- };
1802
- try {
1803
- const tags = await git__namespace.listTags({ fs: fs__namespace, dir: repositoryPath });
1804
- await Promise.all(
1805
- tags.map(async (tagName) => {
1806
- try {
1807
- const tagOid = await git__namespace.resolveRef({
1808
- fs: fs__namespace,
1809
- dir: repositoryPath,
1810
- ref: `refs/tags/${tagName}`
1811
- });
1812
- const version2 = parseVersion(tagName);
1813
- const tagInfo = {
1814
- name: tagName,
1815
- hash: tagOid,
1816
- // This will be updated for annotated tags
1817
- version: version2 && isValidVersion(version2) ? version2 : void 0
1818
- };
1819
- if (!cache.commitToTags.has(tagOid)) {
1820
- cache.commitToTags.set(tagOid, []);
1821
- }
1822
- cache.commitToTags.get(tagOid).push({ ...tagInfo, hash: tagOid });
1823
- try {
1824
- const tagObject = await git__namespace.readTag({
1825
- fs: fs__namespace,
1826
- dir: repositoryPath,
1827
- oid: tagOid
1828
- });
1829
- if (tagObject && tagObject.tag.object) {
1830
- const commitOid = tagObject.tag.object;
1831
- const lightweightTags = cache.commitToTags.get(tagOid);
1832
- if (lightweightTags) {
1833
- const index = lightweightTags.findIndex(
1834
- (t) => t.name === tagName
1835
- );
1836
- if (index >= 0) {
1837
- lightweightTags.splice(index, 1);
1838
- if (lightweightTags.length === 0) {
1839
- cache.commitToTags.delete(tagOid);
1840
- }
1841
- }
1842
- }
1843
- if (!cache.commitToTags.has(commitOid)) {
1844
- cache.commitToTags.set(commitOid, []);
1845
- }
1846
- cache.commitToTags.get(commitOid).push({ ...tagInfo, hash: commitOid });
1847
- }
1848
- } catch (e) {
1849
- }
1850
- } catch (e) {
1851
- }
1852
- })
1853
- );
1854
- } catch (e) {
1855
- }
1856
- return cache;
1857
- };
1858
- const getRelatedTagsFromCache = (cache, commitHash) => {
1859
- if (!cache.initialized) {
1860
- return [];
1861
- }
1862
- return cache.commitToTags.get(commitHash) || [];
1863
- };
1864
- const getRelatedTagsForVersioning = async (repositoryPath, commitHash) => {
1865
- try {
1866
- const tags = await git__namespace.listTags({ fs: fs__namespace, dir: repositoryPath });
1867
- const tagInfos = [];
1868
- for (const tagName of tags) {
1869
- try {
1870
- const tagOid = await git__namespace.resolveRef({
1871
- fs: fs__namespace,
1872
- dir: repositoryPath,
1873
- ref: `refs/tags/${tagName}`
1874
- });
1875
- if (tagOid === commitHash) {
1876
- const version2 = parseVersion(tagName);
1877
- if (version2 && isValidVersion(version2)) {
1878
- tagInfos.push({
1879
- name: tagName,
1880
- hash: commitHash,
1881
- version: version2
1882
- });
1883
- }
1884
- } else {
1885
- try {
1886
- const tagObject = await git__namespace.readTag({
1887
- fs: fs__namespace,
1888
- dir: repositoryPath,
1889
- oid: tagOid
1890
- });
1891
- if (tagObject && tagObject.tag.object === commitHash) {
1892
- const version2 = parseVersion(tagName);
1893
- if (version2 && isValidVersion(version2)) {
1894
- tagInfos.push({
1895
- name: tagName,
1896
- hash: commitHash,
1897
- version: version2
1898
- });
1899
- }
1900
- }
1901
- } catch (e) {
1902
- }
1903
- }
1904
- } catch (e) {
1905
- }
1906
- }
1907
- return tagInfos;
1908
- } catch (e) {
1909
- return [];
1910
- }
1986
+ const getRelatedTagsFromMap = (commitToTags, commitHash) => {
1987
+ return commitToTags.get(commitHash) || [];
1911
1988
  };
1912
1989
  const getRelatedBranches = async (repositoryPath, commitHash) => {
1913
1990
  try {
@@ -1949,7 +2026,7 @@ const getModifiedFiles = async (repositoryPath) => {
1949
2026
  const formatModifiedFile = (modifiedFile) => {
1950
2027
  return `'${modifiedFile[0]}':${modifiedFile[1]}:${modifiedFile[2]}:${modifiedFile[3]}`;
1951
2028
  };
1952
- const lookupVersionLabelRecursive = async (cwd, commit, reachedCommits, tagCache) => {
2029
+ const lookupVersionLabelRecursive = async (cwd, commit, reachedCommits, commitToTags) => {
1953
2030
  const scheduledStack = [];
1954
2031
  let version2 = { major: 0, minor: 0, build: 1, original: "0.0.1" };
1955
2032
  let currentCommit = commit;
@@ -1958,7 +2035,7 @@ const lookupVersionLabelRecursive = async (cwd, commit, reachedCommits, tagCache
1958
2035
  version2 = reachedCommits.get(currentCommit.hash);
1959
2036
  break;
1960
2037
  }
1961
- const relatedTags = tagCache ? getRelatedTagsFromCache(tagCache, currentCommit.hash) : await getRelatedTagsForVersioning(cwd, currentCommit.hash);
2038
+ const relatedTags = getRelatedTagsFromMap(commitToTags, currentCommit.hash);
1962
2039
  const versionCandidates = relatedTags.filter((tag) => tag.version && isValidVersion(tag.version)).filter((tag) => tag.version.minor !== void 0).sort((a, b) => compareVersions(a.version, b.version));
1963
2040
  if (versionCandidates.length >= 1) {
1964
2041
  version2 = versionCandidates[0].version;
@@ -1994,7 +2071,7 @@ const lookupVersionLabelRecursive = async (cwd, commit, reachedCommits, tagCache
1994
2071
  cwd,
1995
2072
  parents[index],
1996
2073
  reachedCommits,
1997
- tagCache
2074
+ commitToTags
1998
2075
  );
1999
2076
  if (alternateParentVersion && compareVersions(alternateParentVersion, version2) < 0) {
2000
2077
  version2 = alternateParentVersion;
@@ -2007,28 +2084,44 @@ const lookupVersionLabelRecursive = async (cwd, commit, reachedCommits, tagCache
2007
2084
  return version2;
2008
2085
  };
2009
2086
  const getGitMetadata = async (repositoryPath, checkWorkingDirectoryStatus, logger) => {
2087
+ const startTime = Date.now();
2010
2088
  const metadata = {};
2011
2089
  let gitRootPath;
2012
2090
  try {
2013
2091
  gitRootPath = await git__namespace.findRoot({ fs: fs__namespace, filepath: repositoryPath });
2014
2092
  } catch (e) {
2093
+ logger.debug(
2094
+ `[screw-up] Total getGitMetadata: ${Date.now() - startTime}ms`
2095
+ );
2015
2096
  return metadata;
2016
2097
  }
2017
2098
  try {
2018
2099
  const currentCommit = await getCurrentCommit(gitRootPath);
2019
2100
  if (!currentCommit) {
2101
+ logger.debug(
2102
+ `[screw-up] Total getGitMetadata: ${Date.now() - startTime}ms`
2103
+ );
2020
2104
  return metadata;
2021
2105
  }
2022
- const tagCache = await buildTagCache(gitRootPath);
2106
+ const buildStart = Date.now();
2107
+ const commitToTags = await buildCompleteTagCache(
2108
+ gitRootPath,
2109
+ (tagName) => {
2110
+ const version22 = parseVersion(tagName);
2111
+ return version22 && isValidVersion(version22) ? version22 : void 0;
2112
+ },
2113
+ logger
2114
+ );
2023
2115
  logger.debug(
2024
- `Built tag cache with ${tagCache.commitToTags.size} commit entries`
2116
+ `[screw-up] buildCompleteTagCache: ${Date.now() - buildStart}ms`
2025
2117
  );
2118
+ logger.debug(`Built tag map with ${commitToTags.size} commits`);
2026
2119
  const reachedCommits = /* @__PURE__ */ new Map();
2027
2120
  let version2 = await lookupVersionLabelRecursive(
2028
2121
  gitRootPath,
2029
2122
  currentCommit,
2030
2123
  reachedCommits,
2031
- tagCache
2124
+ commitToTags
2032
2125
  );
2033
2126
  const gitMetadata = { tags: [], branches: [] };
2034
2127
  metadata.git = gitMetadata;
@@ -2053,7 +2146,7 @@ const getGitMetadata = async (repositoryPath, checkWorkingDirectoryStatus, logge
2053
2146
  date: dayjs(currentCommit.date).format("YYYY-MM-DDTHH:mm:ssZ[Z]"),
2054
2147
  message: currentCommit.message
2055
2148
  };
2056
- const relatedTags = getRelatedTagsFromCache(tagCache, currentCommit.hash);
2149
+ const relatedTags = getRelatedTagsFromMap(commitToTags, currentCommit.hash);
2057
2150
  gitMetadata.tags = relatedTags.map((tag) => tag.name);
2058
2151
  const relatedBranches = await getRelatedBranches(
2059
2152
  gitRootPath,
@@ -2063,6 +2156,7 @@ const getGitMetadata = async (repositoryPath, checkWorkingDirectoryStatus, logge
2063
2156
  } catch (error) {
2064
2157
  logger.warn(`Failed to extract git metadata: ${error}`);
2065
2158
  }
2159
+ logger.debug(`[screw-up] Total getGitMetadata: ${Date.now() - startTime}ms`);
2066
2160
  return metadata;
2067
2161
  };
2068
2162
  const getFetchGitMetadata = (targetDir, checkWorkingDirectoryStatus, logger) => {
@@ -2079,11 +2173,11 @@ const getFetchGitMetadata = (targetDir, checkWorkingDirectoryStatus, logger) =>
2079
2173
  };
2080
2174
  };
2081
2175
  const name = "screw-up";
2082
- const version = "1.5.0";
2176
+ const version = "1.7.0";
2083
2177
  const author = "Kouji Matsui (@kekyo@mi.kekyo.net)";
2084
2178
  const license = "MIT";
2085
2179
  const repository_url = "https://github.com/kekyo/screw-up.git";
2086
- const git_commit_hash = "72fd5b88656bc400ba3fed500895826208e23217";
2180
+ const git_commit_hash = "0865d5a2d4d1e89be8dae4b6abeba1393a767707";
2087
2181
  const packageMetadata = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
2088
2182
  __proto__: null,
2089
2183
  author,
@@ -2104,4 +2198,4 @@ exports.replacePeerDependenciesWildcards = replacePeerDependenciesWildcards;
2104
2198
  exports.resolvePackageMetadata = resolvePackageMetadata;
2105
2199
  exports.resolveRawPackageJsonObject = resolveRawPackageJsonObject;
2106
2200
  exports.version = version;
2107
- //# sourceMappingURL=packageMetadata-CbqTWPYD.cjs.map
2201
+ //# sourceMappingURL=packageMetadata-B3Z9TVh6.cjs.map