|
| 1 | +<!-- |
| 2 | +<style> |
| 3 | +.markdown-body .highlight pre, .markdown-body pre { |
| 4 | + background-color: #0d1117; |
| 5 | +} |
| 6 | +.markdown-body .diff-deletion { |
| 7 | + color: #f85149; |
| 8 | + background-color: #3c1618; |
| 9 | +} |
| 10 | +.markdown-body .diff-addition { |
| 11 | + color: #56d364; |
| 12 | + background-color: #1b4721; |
| 13 | +} |
| 14 | +</style> |
| 15 | +--> |
| 16 | +Auto-generated by GitLoom Diff at Tuesday, November 12, 2024 at 6:14:36 PM GMT+8 |
| 17 | + |
| 18 | +# Changes in `src/sdk/GitLoomDiff.js` |
| 19 | + |
| 20 | +## File Statistics |
| 21 | +``` |
| 22 | + src/sdk/GitLoomDiff.js | 61 ++++++++++++++++++++++++++++++++++++++++++++++++-- |
| 23 | + 1 file changed, 59 insertions(+), 2 deletions(-) |
| 24 | +
|
| 25 | +``` |
| 26 | + |
| 27 | +## Changes |
| 28 | +```diff |
| 29 | +diff --git a/src/sdk/GitLoomDiff.js b/src/sdk/GitLoomDiff.js |
| 30 | +index 309c756..cbbe85d 100644 |
| 31 | +--- a/src/sdk/GitLoomDiff.js |
| 32 | ++++ b/src/sdk/GitLoomDiff.js |
| 33 | +@@ -2,12 +2,30 @@ const gitUtils = require('./utils/gitUtils'); |
| 34 | + const fsUtils = require('./utils/fsUtils'); |
| 35 | + const Config = require('./config/Config'); |
| 36 | + |
| 37 | ++/** |
| 38 | ++ * GitLoomDiff generates markdown-formatted git diffs with syntax highlighting and statistics. |
| 39 | ++ * Handles comparing git references (commits/branches/tags) and outputting formatted markdown files. |
| 40 | ++ */ |
| 41 | + class GitLoomDiff { |
| 42 | ++ /** |
| 43 | ++ * Creates a new GitLoomDiff instance with the provided configuration options. |
| 44 | ++ * @param {Object} options - Configuration options |
| 45 | ++ * @param {string} [options.outputDir='git-diffs'] - Directory to output the diff files |
| 46 | ++ * @param {string[]} [options.exclude=[]] - File patterns to exclude from the diff |
| 47 | ++ * @param {string} [options.format='diff'] - Diff format (diff, unified, side-by-side) |
| 48 | ++ * @param {boolean} [options.darkMode=true] - Whether to use dark mode theme |
| 49 | ++ */ |
| 50 | + constructor(options = {}) { |
| 51 | + this.config = new Config(options); |
| 52 | + } |
| 53 | + |
| 54 | +- |
| 55 | ++ /** |
| 56 | ++ * Runs the diff generation process. |
| 57 | ++ * @param {string} startRange - Starting git reference (commit/branch/tag) |
| 58 | ++ * @param {string} endRange - Ending git reference to compare against |
| 59 | ++ * @returns {Promise<void>} |
| 60 | ++ * @throws {Error} If git validation fails or diff generation encounters an error |
| 61 | ++ */ |
| 62 | + async run(startRange, endRange) { |
| 63 | + const { default: ora } = await import("ora"); |
| 64 | + const spinner = ora("Generating markdown diffs...").start(); |
| 65 | +@@ -36,6 +54,15 @@ class GitLoomDiff { |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | ++ /** |
| 70 | ++ * Processes each changed file to generate diffs and update the index. |
| 71 | ++ * @param {string[]} changedFiles - List of files that have changes |
| 72 | ++ * @param {string} range - Git range to compare |
| 73 | ++ * @param {Object} spinner - Progress spinner instance |
| 74 | ++ * @param {string[]} index - Index content array to update |
| 75 | ++ * @returns {Promise<void>} |
| 76 | ++ * @private |
| 77 | ++ */ |
| 78 | + async #processFiles(changedFiles, range, spinner, index) { |
| 79 | + for (let i = 0; i < changedFiles.length; i++) { |
| 80 | + const file = changedFiles[i]; |
| 81 | +@@ -49,10 +76,25 @@ class GitLoomDiff { |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | ++ /** |
| 86 | ++ * Builds a git range string from start and end references. |
| 87 | ++ * @param {string} startRange - Starting reference |
| 88 | ++ * @param {string} endRange - Ending reference |
| 89 | ++ * @returns {string} Formatted git range or empty string if no range provided |
| 90 | ++ * @private |
| 91 | ++ */ |
| 92 | + #buildGitRange(startRange, endRange) { |
| 93 | + return startRange && endRange ? `${endRange}..${startRange}` : ""; |
| 94 | + } |
| 95 | + |
| 96 | ++ /** |
| 97 | ++ * Builds the content for the index markdown file. |
| 98 | ++ * @param {string} startRange - Starting reference |
| 99 | ++ * @param {string} endRange - Ending reference |
| 100 | ++ * @param {string} totalStats - Total change statistics |
| 101 | ++ * @returns {Promise<string[]>} Array of index content lines |
| 102 | ++ * @private |
| 103 | ++ */ |
| 104 | + async #buildIndexContent(startRange, endRange, totalStats) { |
| 105 | + const index = [ |
| 106 | + "# Git Diff Summary\n", |
| 107 | +@@ -71,12 +113,20 @@ class GitLoomDiff { |
| 108 | + return index; |
| 109 | + } |
| 110 | + |
| 111 | ++ /** |
| 112 | ++ * Generates the markdown content for a single file's diff. |
| 113 | ++ * @param {string} file - File path |
| 114 | ++ * @param {string} range - Git range to compare |
| 115 | ++ * @param {string} fileInfo - File change statistics |
| 116 | ++ * @returns {Promise<string>} Generated markdown content |
| 117 | ++ * @private |
| 118 | ++ */ |
| 119 | + async #generateFileContent(file, range, fileInfo) { |
| 120 | + const diffOutput = await gitUtils.getFileDiff(file, range, this.config.diffFormat); |
| 121 | + |
| 122 | + return [ |
| 123 | + this.config.getCssStyle(), |
| 124 | +- `generated at ${new Date().toLocaleString()} (${Intl.DateTimeFormat().resolvedOptions().timeZone})`, |
| 125 | ++ `Auto-generated by GitLoom Diff at ${new Date().toLocaleString('en-US', { dateStyle: 'full', timeStyle: 'long', timeZone: Intl.DateTimeFormat().resolvedOptions().timeZone })}`, |
| 126 | + "", |
| 127 | + `# Changes in \`${file}\``, |
| 128 | + "", |
| 129 | +@@ -94,6 +144,13 @@ class GitLoomDiff { |
| 130 | + ].join("\n"); |
| 131 | + } |
| 132 | + |
| 133 | ++ /** |
| 134 | ++ * Updates the index content with a link to a processed file. |
| 135 | ++ * @param {string[]} index - Index content array to update |
| 136 | ++ * @param {string} file - File path |
| 137 | ++ * @param {string} fileInfo - File change statistics |
| 138 | ++ * @private |
| 139 | ++ */ |
| 140 | + #updateIndex(index, file, fileInfo) { |
| 141 | + const stats = fileInfo.match(/(\d+) insertion.+(\d+) deletion/)?.[0] || "No changes"; |
| 142 | + index.push(`- [${file}](./${file}.md) - ${stats}`); |
| 143 | + |
| 144 | +``` |
| 145 | + |
0 commit comments